Browse Source

js -> eberon transition

Vladislav Folts 10 years ago
parent
commit
cbea3481de

BIN
bin/compiled.zip


+ 5 - 37
src/context.js

@@ -3,6 +3,7 @@
 var Cast = require("js/Cast.js");
 var Code = require("js/Code.js");
 var CodeGenerator = require("js/CodeGenerator.js");
+var ConstValue = require("js/ConstValue.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var Errors = require("js/Errors.js");
 var Module = require("js/Module.js");
@@ -870,39 +871,6 @@ exports.MulOperator = ChainedContext.extend({
     }
 });
 
-exports.Term = ChainedContext.extend({
-    init: function TermContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.attributes = {};
-        this.__operator = undefined;
-        this.__expression = undefined;
-    },
-    type: function(){
-        return this.__expression ? this.__expression.type()
-                                 : this.attributes.designator.type();
-    },
-    handleOperator: function(o){this.__operator = o;},
-    endParse: function(){
-        var e = this.__expression;
-        if (!e){
-            var d = this.attributes.designator;
-            var value;
-            var info = d.info();
-            if (info instanceof Type.Const)
-                value = Type.constValue(info);
-            e = Code.makeExpression(d.code(), d.type(), d, value);
-        }
-        this.parent().handleTerm(e);
-    },
-    handleExpression: function(e){
-        ContextHierarchy.promoteExpressionType(this.root(), this.__expression, e);
-        if (this.__operator)
-            e = this.__expression ? this.__operator(this.__expression, e)
-                                  : this.__operator(e);
-        this.__expression = e;
-    }
-});
-
 function designatorAsExpression(d){
     var info = d.info();
     if (info instanceof Type.ProcedureId){
@@ -916,7 +884,7 @@ function designatorAsExpression(d){
 
     var value;
     if (info instanceof Type.Const)
-        value = Type.constValue(info);
+        value = info.value;
     return Code.makeExpression(d.code(), d.type(), d, value);
 }
 
@@ -946,7 +914,7 @@ exports.Set = ChainedContext.extend({
     endParse: function(){
         var parent = this.parent();
         if (!this.__expr.length)
-            parent.handleConst(basicTypes.set, Code.makeSetConst(this.__value), this.__value.toString());
+            parent.handleConst(basicTypes.set, new ConstValue.Set(this.__value), this.__value.toString());
         else{
             var code = this.root().language().rtl.makeSet(this.__expr);
             if (this.__value)
@@ -1213,7 +1181,7 @@ exports.CaseRange = ChainedContext.extend({
             if (!Type.stringAsChar(type, {set: function(v){value = v;}}))
                 throw new Errors.Error("single-character string expected");
             type = basicTypes.ch;
-            value = Code.makeIntConst(value);
+            value = new ConstValue.Int(value);
         }
         this.handleLabel(type, value);
     },
@@ -1226,7 +1194,7 @@ exports.CaseRange = ChainedContext.extend({
         if (type instanceof Type.String)
             this.handleConst(type, undefined);
         else
-            this.handleLabel(type, Type.constValue(s.info()));
+            this.handleLabel(type, s.info().value);
     },
     endParse: function(){this.parent().handleRange(this.__from, this.__to);}
 });

+ 2 - 2
src/eberon/EberonDynamicArray.ob

@@ -1,5 +1,5 @@
 MODULE EberonDynamicArray;
-IMPORT Cast, Code, Context, EberonArray, EberonOperator, EberonRecord, EberonTypes, Errors, LanguageContext, Procedure, Types;
+IMPORT Cast, Code, ConstValue, Context, EberonArray, EberonOperator, EberonRecord, EberonTypes, Errors, LanguageContext, Procedure, Types;
 CONST
     methodNameAdd = "add";
     methodNameClear = "clear";
@@ -184,7 +184,7 @@ BEGIN
     argCode <- Procedure.makeArgumentsCode(cx);
     arg <- Procedure.checkSingleArgument(args, SELF, cx.types, argCode);
     value <- arg.constValue();
-    IF (value # NIL) & (value^ IS Code.IntConst) THEN
+    IF (value # NIL) & (value^ IS ConstValue.Int) THEN
         Code.checkIndex(value.value);
     END;
     RETURN Code.makeSimpleExpression("(" + argCode.result() + ", 1)", NIL)

+ 22 - 22
src/eberon/EberonOperator.ob

@@ -1,44 +1,44 @@
 MODULE EberonOperator;
-IMPORT Cast, Code, CodePrecedence, EberonMap, LanguageContext, OberonRtl, Operator, Types;
+IMPORT Cast, Code, CodePrecedence, ConstValue, EberonMap, LanguageContext, OberonRtl, Operator, Types;
 TYPE
     CastOpRecord = RECORD(Cast.CastOpRecord)
     END;
 VAR
     castOperations*: Cast.Operations;
 
-PROCEDURE opAddStr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeStringConst(left^(Code.StringConst).value
-                              + right^(Code.StringConst).value)
+PROCEDURE opAddStr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.String(left^(ConstValue.String).value
+                               + right^(ConstValue.String).value)
 END opAddStr;
 
-PROCEDURE opEqualStr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.StringConst).value
-                                 = right^(Code.StringConst).value))
+PROCEDURE opEqualStr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.String).value
+                                  = right^(ConstValue.String).value))
 END opEqualStr;
 
-PROCEDURE opNotEqualStr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.StringConst).value
-                                 # right^(Code.StringConst).value))
+PROCEDURE opNotEqualStr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.String).value
+                                  # right^(ConstValue.String).value))
 END opNotEqualStr;
 
-PROCEDURE opLessStr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.StringConst).value
-                               < right^(Code.StringConst).value))
+PROCEDURE opLessStr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.String).value
+                                < right^(ConstValue.String).value))
 END opLessStr;
 
-PROCEDURE opGreaterStr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.StringConst).value
-                               > right^(Code.StringConst).value))
+PROCEDURE opGreaterStr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.String).value
+                                > right^(ConstValue.String).value))
 END opGreaterStr;
 
-PROCEDURE opLessEqualStr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.StringConst).value
-                              <= right^(Code.StringConst).value))
+PROCEDURE opLessEqualStr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.String).value
+                               <= right^(ConstValue.String).value))
 END opLessEqualStr;
 
-PROCEDURE opGraterEqualStr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.StringConst).value
-                              >= right^(Code.StringConst).value))
+PROCEDURE opGraterEqualStr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.String).value
+                               >= right^(ConstValue.String).value))
 END opGraterEqualStr;
 
 PROCEDURE addStr*(left, right: Code.PExpression): Code.PExpression;

+ 3 - 3
src/eberon/eberon_context.js

@@ -926,9 +926,9 @@ function BeginTypePromotionOrMsg(){
     this.result = undefined;
 }
 
-var Term = Context.Term.extend({
+var Term = Class.extend.call(ContextExpression.Term, {
     init: function EberonContext$Term(context){
-        Context.Term.prototype.init.call(this, context);
+        ContextExpression.Term.call(this, context);
         this.__typePromotion = undefined;
         this.__currentPromotion = undefined;
         this.__andHandled = false;
@@ -947,7 +947,7 @@ var Term = Context.Term.extend({
                 msg.result = cp.makeOr();
             return;
         }
-        return Context.Term.prototype.handleMessage.call(this, msg);
+        return ContextExpression.Term.prototype.handleMessage.call(this, msg);
     },
     handleLogicalAnd: function(){
         if (this.__typePromotion)

+ 9 - 56
src/ob/Code.ob

@@ -1,6 +1,7 @@
 MODULE Code;
 IMPORT 
     CodeGenerator,
+    ConstValue,
     Errors,
     Object, 
     Stream, 
@@ -14,9 +15,9 @@ TYPE
     Designator* = RECORD
         PROCEDURE Designator*(code: STRING; lval: STRING; type: Types.PType; info: Types.PId; scope: ScopeBase.PType);
 
-        PROCEDURE code(): STRING;
+        PROCEDURE code*(): STRING;
         PROCEDURE lval(): STRING;
-        PROCEDURE type(): Types.PType;
+        PROCEDURE type*(): Types.PType;
         PROCEDURE info*(): Types.PId;
         PROCEDURE scope(): ScopeBase.PType;
 
@@ -29,46 +30,26 @@ TYPE
 
     PDesignator* = POINTER TO Designator;
 
-    Const* = RECORD
-    END;
-    PConst* = POINTER TO Const;
-
-    IntConst* = RECORD (Const)
-        value*: INTEGER
-    END;
-
-    RealConst* = RECORD (Const)
-        value*: REAL
-    END;
-
-    SetConst* = RECORD (Const)
-        value*: SET
-    END;
-
-    StringConst* = RECORD (Const)
-        value*: STRING
-    END;
-
     Expression* = RECORD(Object.Type)
         PROCEDURE Expression*(
             code: STRING; 
             type: Types.PType; 
             designator: PDesignator; 
-            constValue: PConst; 
+            constValue: ConstValue.PType; 
             maxPrecedence: INTEGER);
 
         PROCEDURE code*(): STRING;
         PROCEDURE lval*(): STRING;
         PROCEDURE type*(): Types.PType;
         PROCEDURE designator*(): PDesignator;
-        PROCEDURE constValue*(): PConst;
+        PROCEDURE constValue*(): ConstValue.PType;
         PROCEDURE maxPrecedence*(): INTEGER;
         PROCEDURE isTerm*(): BOOLEAN;
 
         mCode: STRING;
         mType: Types.PType;
         mDesignator: PDesignator;
-        mConstValue: PConst;
+        mConstValue: ConstValue.PType;
         mMaxPrecedence: INTEGER
     END;
 
@@ -112,7 +93,7 @@ PROCEDURE Expression.designator(): PDesignator;
     RETURN SELF.mDesignator
 END Expression.designator;
 
-PROCEDURE Expression.constValue(): PConst;
+PROCEDURE Expression.constValue(): ConstValue.PType;
     RETURN SELF.mConstValue
 END Expression.constValue;
 
@@ -124,39 +105,11 @@ PROCEDURE Expression.isTerm(): BOOLEAN;
     RETURN (SELF.mDesignator = NIL) & (SELF.mMaxPrecedence = Precedence.none)
 END Expression.isTerm;
 
-PROCEDURE makeIntConst*(n: INTEGER): PConst;
-BEGIN
-    result <- NEW IntConst();
-    result.value := n;
-    RETURN result
-END;
-
-PROCEDURE makeRealConst*(n: REAL): PConst;
-BEGIN
-    result <- NEW RealConst();
-    result.value := n;
-    RETURN result
-END;
-
-PROCEDURE makeSetConst*(s: SET): PConst;
-BEGIN
-    result <- NEW SetConst();
-    result.value := s;
-    RETURN result
-END;
-
-PROCEDURE makeStringConst*(s: STRING): PConst;
-BEGIN
-    result <- NEW StringConst();
-    result.value := s;
-    RETURN result
-END;
-
 PROCEDURE Expression.Expression(
     code: STRING; 
     type: Types.PType; 
     designator: PDesignator; 
-    constValue: PConst; 
+    constValue: ConstValue.PType; 
     maxPrecedence: INTEGER)
   | mCode(code),
     mType(type),
@@ -169,7 +122,7 @@ PROCEDURE makeExpression*(
     code: STRING; 
     type: Types.PType; 
     designator: PDesignator; 
-    constValue: PConst)
+    constValue: ConstValue.PType)
     : PExpression;
     RETURN NEW Expression(code, type, designator, constValue, Precedence.none)
 END makeExpression;

+ 47 - 0
src/ob/ConstValue.ob

@@ -0,0 +1,47 @@
+MODULE ConstValue;
+TYPE
+    Type* = RECORD
+    END;
+    PType* = POINTER TO Type;
+
+    Int* = RECORD (Type)
+        PROCEDURE Int*(n: INTEGER);
+
+        value-: INTEGER;
+    END;
+
+    Real* = RECORD (Type)
+        PROCEDURE Real*(r: REAL);
+
+        value-: REAL;
+    END;
+
+    Set* = RECORD (Type)
+        PROCEDURE Set*(s: SET);
+
+        value-: SET
+    END;
+
+    String* = RECORD (Type)
+        PROCEDURE String*(s: STRING);
+
+        value-: STRING
+    END;
+
+PROCEDURE Int.Int(n: INTEGER)
+    | value(n);
+END;
+
+PROCEDURE Real.Real(r: REAL)
+    | value(r);
+END;
+
+PROCEDURE Set.Set(s: SET)
+    | value(s);
+END;
+
+PROCEDURE String.String(s: STRING)
+    | value(s);
+END;
+
+END ConstValue.

+ 89 - 18
src/ob/ContextExpression.ob

@@ -1,22 +1,42 @@
 MODULE ContextExpression;
 IMPORT 
-    Chars, Code, ContextHierarchy, Operator, String, Types;
+    Chars, Code, ConstValue, ContextHierarchy, Operator, String, Types;
 TYPE
+    ExpressionHandler = RECORD(ContextHierarchy.Node)
+        PROCEDURE handleExpression(e: Code.PExpression);
+        PROCEDURE endParse();
+
+        expression: Code.PExpression;
+    END;
+
+    SimpleExpression = RECORD(ContextHierarchy.Node)
+        PROCEDURE handleTerm(e: Code.PExpression);
+    END;
+    PSimpleExpression = POINTER TO SimpleExpression;
+
     PFactor = POINTER TO Factor;
 
-    Factor* = RECORD(ContextHierarchy.Node)
+    Factor* = RECORD(ExpressionHandler)
         PROCEDURE Factor(parent: PFactor);
 
-        PROCEDURE handleConst(type: Types.PType; value: Code.PConst; code: STRING);
+        PROCEDURE handleConst(type: Types.PType; value: ConstValue.PType; code: STRING);
         PROCEDURE handleLiteral(s: STRING);
         PROCEDURE handleLogicalNot();
-        PROCEDURE handleExpression(e: Code.PExpression);
-
-        PROCEDURE endParse();
 
         factorParent: PFactor;
         logicalNot: BOOLEAN;
-        e: Code.PExpression;
+    END;
+
+    BinaryOperator = PROCEDURE(l, r: Code.PExpression): Code.PExpression;
+
+    Term* = RECORD(ExpressionHandler)
+        PROCEDURE Term(parent: PSimpleExpression);
+
+        PROCEDURE type(): Types.PType;
+        PROCEDURE handleOperator(op: BinaryOperator);
+
+        simpleExpression: PSimpleExpression;
+        operator: BinaryOperator;
     END;
 
     Const = RECORD(ContextHierarchy.Node)
@@ -42,9 +62,9 @@ PROCEDURE Factor.Factor(parent: PFactor)
       factorParent(parent);
 END;
 
-PROCEDURE Factor.handleConst(type: Types.PType; value: Code.PConst; code: STRING);
+PROCEDURE Factor.handleConst(type: Types.PType; value: ConstValue.PType; code: STRING);
 BEGIN
-    SELF.e := Code.makeExpression(code, type, NIL, value);
+    SELF.expression := Code.makeExpression(code, type, NIL, value);
 END;
 
 PROCEDURE Factor.handleLiteral(s: STRING);
@@ -52,9 +72,9 @@ BEGIN
     IF s = "NIL" THEN
         SELF.handleConst(Types.nil, NIL, "null");
     ELSIF s = "TRUE" THEN
-        SELF.handleConst(Types.basic.bool, Code.makeIntConst(1), "true");
+        SELF.handleConst(Types.basic.bool, NEW ConstValue.Int(1), "true");
     ELSIF s = "FALSE" THEN
-        SELF.handleConst(Types.basic.bool, Code.makeIntConst(0), "false");
+        SELF.handleConst(Types.basic.bool, NEW ConstValue.Int(0), "false");
     ELSIF s = "~" THEN
         SELF.handleLogicalNot();
     END;
@@ -67,16 +87,67 @@ END;
 
 PROCEDURE Factor.handleExpression(e: Code.PExpression);
 BEGIN
-    SELF.e := e;
+    SELF.expression := e;
 END;
 
 PROCEDURE Factor.endParse();
 BEGIN
     IF SELF.logicalNot THEN
-        ContextHierarchy.checkTypeMatch(SELF.e.type(), Types.basic.bool);
-        SELF.e := Operator.not(SELF.e);
+        ContextHierarchy.checkTypeMatch(SELF.expression.type(), Types.basic.bool);
+        SELF.expression := Operator.not(SELF.expression);
+    END;
+    SELF.factorParent.handleExpression(SELF.expression);
+END;
+
+PROCEDURE Term.Term(parent: PSimpleExpression)
+    | SUPER(parent),
+      simpleExpression(parent);
+END;
+
+
+PROCEDURE Term.type(): Types.PType;
+VAR
+    result: Types.PType;
+BEGIN
+    IF SELF.expression # NIL THEN
+        result := SELF.expression.type();
+    ELSE
+        result := SELF.attributes.designator.type();
+    END;
+    RETURN result;
+END;
+
+PROCEDURE Term.handleOperator(op: BinaryOperator);
+BEGIN
+    SELF.operator := op;
+END;
+
+PROCEDURE Term.handleExpression(e: Code.PExpression);
+BEGIN
+    ContextHierarchy.promoteExpressionType(SELF.root()^, SELF.expression, e);
+    IF SELF.operator = NIL THEN
+        SELF.expression := e;
+    ELSIF SELF.expression # NIL THEN
+        SELF.expression := SELF.operator(SELF.expression, e);
+    ELSE
+        SELF.expression := SELF.operator(e, NIL);
+    END;
+END;
+
+PROCEDURE Term.endParse();
+VAR
+    const: ConstValue.PType;
+BEGIN
+    e <- SELF.expression;
+    IF e = NIL THEN
+        d <- SELF.attributes.designator;
+        info <- d.info();
+        IF info IS Types.PConst THEN
+            const := info.value;
+        END;
+        e := Code.makeExpression(d.code(), d.type(), d, const);
     END;
-    SELF.factorParent.handleExpression(SELF.e);
+    SELF.simpleExpression.handleTerm(e);
 END;
 
 PROCEDURE Const.Const(factor: PFactor)
@@ -88,7 +159,7 @@ PROCEDURE Integer.handleInt(n: INTEGER);
 BEGIN
     SELF.factor.handleConst(
         Types.basic.integer, 
-        Code.makeIntConst(n), 
+        NEW ConstValue.Int(n), 
         String.fromInt(n));
 END;
 
@@ -96,7 +167,7 @@ PROCEDURE Real.handleReal(r: REAL);
 BEGIN
     SELF.factor.handleConst(
         Types.basic.real, 
-        Code.makeRealConst(r), 
+        NEW ConstValue.Real(r), 
         String.fromReal(r));
 END;
 
@@ -138,7 +209,7 @@ PROCEDURE Str.handleStr(s: STRING);
 BEGIN
     SELF.factor.handleConst(
         NEW Types.String(s), 
-        Code.makeStringConst(s), 
+        NEW ConstValue.String(s), 
         escapeString(s));
 END;
 

+ 2 - 1
src/ob/ContextHierarchy.ob

@@ -17,12 +17,13 @@ TYPE
     END;
 
     Attributes* = RECORD
+        designator*: Code.PDesignator;
     END;
 
     Node* = RECORD
         PROCEDURE Node*(parent: PNode);
 
-        PROCEDURE root(): PRoot;
+        PROCEDURE root*(): PRoot;
         PROCEDURE parent*(): PNode;
         PROCEDURE handleMessage(VAR msg: Message): PMessageResult;
         PROCEDURE codeGenerator(): CodeGenerator.PIGenerator;

+ 123 - 122
src/ob/Operator.ob

@@ -1,7 +1,8 @@
 MODULE Operator;
 IMPORT 
     Cast, 
-    Code, 
+    Code,
+    ConstValue, 
     Errors, 
     LanguageContext, 
     OberonRtl, 
@@ -14,10 +15,10 @@ CONST
 TYPE
     BinaryProc* = PROCEDURE(left, right: Code.PExpression): Code.PExpression;
 
-    BinaryOp = PROCEDURE(left, right: Code.PConst): Code.PConst;
+    BinaryOp = PROCEDURE(left, right: ConstValue.PType): ConstValue.PType;
     CodePredicate = PROCEDURE(left, right: STRING; rtl: OberonRtl.PType): STRING;
 
-    UnaryOp = PROCEDURE(value: Code.PConst): Code.PConst;
+    UnaryOp = PROCEDURE(value: ConstValue.PType): ConstValue.PType;
 
     CodeMaker = RECORD
         PROCEDURE make(left, right: STRING; rtl: OberonRtl.PType): STRING
@@ -56,7 +57,7 @@ PROCEDURE binary(
     ): Code.PExpression;
 VAR
     result: Code.PExpression;
-    leftValue, rightValue, resultValue: Code.PConst;
+    leftValue, rightValue, resultValue: ConstValue.PType;
     leftCode, rightCode, resultCode: STRING;
     resultType: Types.PType;
     resultPrecedence: INTEGER;
@@ -215,7 +216,7 @@ END binaryPred;
 
 PROCEDURE unary(e: Code.PExpression; op: UnaryOp; code: STRING): Code.PExpression;
 VAR
-    value: Code.PConst;
+    value: ConstValue.PType;
 BEGIN
     value := e.constValue();
     IF value # NIL THEN
@@ -241,194 +242,194 @@ BEGIN
     RETURN resultExpression.code()
 END castToStr;
 
-PROCEDURE opAddReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeRealConst(left^(Code.RealConst).value 
-                            + right^(Code.RealConst).value)
+PROCEDURE opAddReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Real(left^(ConstValue.Real).value 
+                             + right^(ConstValue.Real).value)
 END opAddReal;
 
-PROCEDURE opAddInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(left^(Code.IntConst).value 
-                           + right^(Code.IntConst).value)
+PROCEDURE opAddInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(left^(ConstValue.Int).value 
+                           + right^(ConstValue.Int).value)
 END opAddInt;
 
-PROCEDURE opSubReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeRealConst(left^(Code.RealConst).value 
-                            - right^(Code.RealConst).value)
+PROCEDURE opSubReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Real(left^(ConstValue.Real).value 
+                            - right^(ConstValue.Real).value)
 END opSubReal;
 
-PROCEDURE opSubInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(left^(Code.IntConst).value 
-                           - right^(Code.IntConst).value)
+PROCEDURE opSubInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(left^(ConstValue.Int).value 
+                            - right^(ConstValue.Int).value)
 END opSubInt;
 
-PROCEDURE opMulReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeRealConst(left^(Code.RealConst).value 
-                            * right^(Code.RealConst).value)
+PROCEDURE opMulReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Real(left^(ConstValue.Real).value 
+                             * right^(ConstValue.Real).value)
 END opMulReal;
 
-PROCEDURE opMulInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(left^(Code.IntConst).value 
-                           * right^(Code.IntConst).value)
+PROCEDURE opMulInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(left^(ConstValue.Int).value 
+                           * right^(ConstValue.Int).value)
 END opMulInt;
 
-PROCEDURE opDivReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeRealConst(left^(Code.RealConst).value 
-                            / right^(Code.RealConst).value)
+PROCEDURE opDivReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Real(left^(ConstValue.Real).value 
+                             / right^(ConstValue.Real).value)
 END opDivReal;
 
-PROCEDURE opDivInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(left^(Code.IntConst).value 
-                         DIV right^(Code.IntConst).value)
+PROCEDURE opDivInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(left^(ConstValue.Int).value 
+                          DIV right^(ConstValue.Int).value)
 END opDivInt;
 
-PROCEDURE opMod(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(left^(Code.IntConst).value 
-                         MOD right^(Code.IntConst).value)
+PROCEDURE opMod(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(left^(ConstValue.Int).value 
+                          MOD right^(ConstValue.Int).value)
 END opMod;
 
-PROCEDURE opSetUnion(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeSetConst(left^(Code.SetConst).value 
-                           + right^(Code.SetConst).value)
+PROCEDURE opSetUnion(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Set(left^(ConstValue.Set).value 
+                            + right^(ConstValue.Set).value)
 END opSetUnion;
 
-PROCEDURE opSetDiff(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeSetConst(left^(Code.SetConst).value 
-                           - right^(Code.SetConst).value)
+PROCEDURE opSetDiff(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Set(left^(ConstValue.Set).value 
+                            - right^(ConstValue.Set).value)
 END opSetDiff;
 
-PROCEDURE opSetIntersection(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeSetConst(left^(Code.SetConst).value 
-                           * right^(Code.SetConst).value)
+PROCEDURE opSetIntersection(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Set(left^(ConstValue.Set).value 
+                            * right^(ConstValue.Set).value)
 END opSetIntersection;
 
-PROCEDURE opSetSymmetricDiff(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeSetConst(left^(Code.SetConst).value 
-                           / right^(Code.SetConst).value)
+PROCEDURE opSetSymmetricDiff(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Set(left^(ConstValue.Set).value 
+                            / right^(ConstValue.Set).value)
 END opSetSymmetricDiff;
 
-PROCEDURE opSetInclL(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.SetConst).value 
-                              <= right^(Code.SetConst).value))
+PROCEDURE opSetInclL(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Set).value 
+                               <= right^(ConstValue.Set).value))
 END opSetInclL;
 
-PROCEDURE opSetInclR(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.SetConst).value 
-                              >= right^(Code.SetConst).value))
+PROCEDURE opSetInclR(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Set).value 
+                               >= right^(ConstValue.Set).value))
 END opSetInclR;
 
-PROCEDURE opOr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD((left^(Code.IntConst).value # 0)
-                              OR (right^(Code.IntConst).value # 0)))
+PROCEDURE opOr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD((left^(ConstValue.Int).value # 0)
+                               OR (right^(ConstValue.Int).value # 0)))
 END opOr;
 
-PROCEDURE opAnd(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD((left^(Code.IntConst).value # 0)
-                               & (right^(Code.IntConst).value # 0)))
+PROCEDURE opAnd(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD((left^(ConstValue.Int).value # 0)
+                                & (right^(ConstValue.Int).value # 0)))
 END opAnd;
 
-PROCEDURE opEqualInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.IntConst).value
-                               = right^(Code.IntConst).value))
+PROCEDURE opEqualInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Int).value
+                                = right^(ConstValue.Int).value))
 END opEqualInt;
 
-PROCEDURE opEqualReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.RealConst).value
-                               = right^(Code.RealConst).value))
+PROCEDURE opEqualReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Real).value
+                                = right^(ConstValue.Real).value))
 END opEqualReal;
 
-PROCEDURE opEqualSet(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.SetConst).value
-                               = right^(Code.SetConst).value))
+PROCEDURE opEqualSet(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Set).value
+                                = right^(ConstValue.Set).value))
 END opEqualSet;
 
-PROCEDURE opNotEqualInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.IntConst).value
-                               # right^(Code.IntConst).value))
+PROCEDURE opNotEqualInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Int).value
+                                # right^(ConstValue.Int).value))
 END opNotEqualInt;
 
-PROCEDURE opNotEqualReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.RealConst).value
-                               # right^(Code.RealConst).value))
+PROCEDURE opNotEqualReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Real).value
+                                # right^(ConstValue.Real).value))
 END opNotEqualReal;
 
-PROCEDURE opNotEqualSet(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.SetConst).value
-                               # right^(Code.SetConst).value))
+PROCEDURE opNotEqualSet(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Set).value
+                                # right^(ConstValue.Set).value))
 END opNotEqualSet;
 
-PROCEDURE opLessInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.IntConst).value
-                               < right^(Code.IntConst).value))
+PROCEDURE opLessInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Int).value
+                                < right^(ConstValue.Int).value))
 END opLessInt;
 
-PROCEDURE opLessReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.RealConst).value
-                               < right^(Code.RealConst).value))
+PROCEDURE opLessReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Real).value
+                                < right^(ConstValue.Real).value))
 END opLessReal;
 
-PROCEDURE opGreaterInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.IntConst).value
-                               > right^(Code.IntConst).value))
+PROCEDURE opGreaterInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Int).value
+                                > right^(ConstValue.Int).value))
 END opGreaterInt;
 
-PROCEDURE opGreaterReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.RealConst).value
-                               > right^(Code.RealConst).value))
+PROCEDURE opGreaterReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Real).value
+                                > right^(ConstValue.Real).value))
 END opGreaterReal;
 
-PROCEDURE opEqLessInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.IntConst).value
-                              <= right^(Code.IntConst).value))
+PROCEDURE opEqLessInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Int).value
+                               <= right^(ConstValue.Int).value))
 END opEqLessInt;
 
-PROCEDURE opEqLessReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.RealConst).value
-                              <= right^(Code.RealConst).value))
+PROCEDURE opEqLessReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Real).value
+                               <= right^(ConstValue.Real).value))
 END opEqLessReal;
 
-PROCEDURE opEqGreaterInt(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.IntConst).value
-                              >= right^(Code.IntConst).value))
+PROCEDURE opEqGreaterInt(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Int).value
+                               >= right^(ConstValue.Int).value))
 END opEqGreaterInt;
 
-PROCEDURE opEqGreaterReal(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(left^(Code.RealConst).value
-                              >= right^(Code.RealConst).value))
+PROCEDURE opEqGreaterReal(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(left^(ConstValue.Real).value
+                               >= right^(ConstValue.Real).value))
 END opEqGreaterReal;
 
-PROCEDURE opNot(x: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ORD(~(x^(Code.IntConst).value # 0)))
+PROCEDURE opNot(x: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ORD(~(x^(ConstValue.Int).value # 0)))
 END opNot;
 
-PROCEDURE opNegateInt(x: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(-x^(Code.IntConst).value)
+PROCEDURE opNegateInt(x: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(-x^(ConstValue.Int).value)
 END opNegateInt;
 
-PROCEDURE opNegateReal(x: Code.PConst): Code.PConst;
-    RETURN Code.makeRealConst(-x^(Code.RealConst).value)
+PROCEDURE opNegateReal(x: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Real(-x^(ConstValue.Real).value)
 END opNegateReal;
 
-PROCEDURE opUnaryPlus(x: Code.PConst): Code.PConst;
+PROCEDURE opUnaryPlus(x: ConstValue.PType): ConstValue.PType;
     RETURN x
 END opUnaryPlus;
 
-PROCEDURE opSetComplement(x: Code.PConst): Code.PConst;
-    RETURN Code.makeSetConst(-x^(Code.SetConst).value)
+PROCEDURE opSetComplement(x: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Set(-x^(ConstValue.Set).value)
 END opSetComplement;
 
-PROCEDURE opLsl(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(LSL(left^(Code.IntConst).value,
-                                 right^(Code.IntConst).value))
+PROCEDURE opLsl(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(LSL(left^(ConstValue.Int).value,
+                                  right^(ConstValue.Int).value))
 END opLsl;
 
-PROCEDURE opAsr(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ASR(left^(Code.IntConst).value,
-                                 right^(Code.IntConst).value))
+PROCEDURE opAsr(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ASR(left^(ConstValue.Int).value,
+                                  right^(ConstValue.Int).value))
 END opAsr;
 
-PROCEDURE opRor(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(ROR(left^(Code.IntConst).value,
-                                 right^(Code.IntConst).value))
+PROCEDURE opRor(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(ROR(left^(ConstValue.Int).value,
+                                  right^(ConstValue.Int).value))
 END opRor;
 
 PROCEDURE codeSetInclL(left, right: STRING; rtl: OberonRtl.PType): STRING;
@@ -689,8 +690,8 @@ BEGIN
     overflowCheck <- TRUE;
     c <- x.constValue();
     IF c # NIL THEN
-        value <- -c^(Code.IntConst).value;
-        result := NEW Code.Expression(String.fromInt(value), Types.basic.integer, NIL, Code.makeIntConst(value), Precedence.unary);
+        value <- -c^(ConstValue.Int).value;
+        result := NEW Code.Expression(String.fromInt(value), Types.basic.integer, NIL, NEW ConstValue.Int(value), Precedence.unary);
     ELSE
         result := promoteToWideIfNeeded(unary(x, opNegateInt, "-"));
         result := NEW Code.Expression(result.code() + " | 0", result.type(), result.designator(), result.constValue(), Precedence.bitOr);
@@ -752,9 +753,9 @@ BEGIN
             Precedence.bitOr)
 END log2;
 
-PROCEDURE opCastToUint8(left, right: Code.PConst): Code.PConst;
-    RETURN Code.makeIntConst(left^(Code.IntConst).value 
-                           * right^(Code.IntConst).value)
+PROCEDURE opCastToUint8(left, right: ConstValue.PType): ConstValue.PType;
+    RETURN NEW ConstValue.Int(left^(ConstValue.Int).value 
+                            * right^(ConstValue.Int).value)
 END opCastToUint8;
 
 PROCEDURE CastToUint8.make(cx: LanguageContext.PType; e: Code.PExpression): Code.PExpression;
@@ -763,7 +764,7 @@ PROCEDURE CastToUint8.make(cx: LanguageContext.PType; e: Code.PExpression): Code
         Code.makeExpression("0xFF", 
                             Types.basic.integer, 
                             NIL, 
-                            Code.makeIntConst(0FFH)), 
+                            NEW ConstValue.Int(0FFH)), 
         opCastToUint8, 
         " & ", 
         Precedence.bitAnd)

+ 14 - 13
src/ob/Procedure.ob

@@ -3,6 +3,7 @@ IMPORT
     Cast,
     Code, 
     Context, 
+    ConstValue,
     Errors, 
     LanguageContext,
     OberonRtl,
@@ -362,15 +363,15 @@ PROCEDURE makeOdd(): Symbols.PSymbol;
     VAR
         arg: Code.PExpression;
         code: STRING;
-        constValue: Code.PConst;
+        constValue: ConstValue.PType;
     BEGIN
         arg := checkSingleArgument(args, SELF, cx.types, NIL);
         code := Code.adjustPrecedence(arg, Precedence.bitAnd);
         
         constValue := arg.constValue();
         IF constValue # NIL THEN
-            constValue := Code.makeIntConst(
-                ORD(ODD(constValue^(Code.IntConst).value)));
+            constValue := NEW ConstValue.Int(
+                ORD(ODD(constValue^(ConstValue.Int).value)));
         END;
 
         RETURN NEW Code.Expression(
@@ -417,7 +418,7 @@ PROCEDURE setBitImpl(name: STRING; bitOp: BinaryOpStr): Symbols.PSymbol;
     VAR
         x, y: Code.PExpression;
         yValue: INTEGER;
-        value: Code.PConst;
+        value: ConstValue.PType;
         valueCodeExp: Code.PExpression;
         valueCode: STRING;
         comment: STRING;
@@ -433,11 +434,11 @@ PROCEDURE setBitImpl(name: STRING; bitOp: BinaryOpStr): Symbols.PSymbol;
                     "1", 
                     Types.basic.integer,
                     NIL,
-                    Code.makeIntConst(1)), 
+                    NEW ConstValue.Int(1)), 
                 y);
             valueCode := valueCodeExp.code();
         ELSE
-            yValue := value^(Code.IntConst).value;
+            yValue := value^(ConstValue.Int).value;
             IF (yValue < 0) OR (yValue > 31) THEN
                 Errors.raise("value (0..31) expected as a second argument of " 
                              + SELF.name + ", got " + String.fromInt(yValue));
@@ -490,7 +491,7 @@ PROCEDURE incImpl(name: STRING; unary: STRING; incOp: BinaryOpStr; incRefOp: Ope
     VAR
         x, y: Code.PExpression;
         code: STRING;
-        value: Code.PConst;
+        value: ConstValue.PType;
         valueCode: STRING;
     BEGIN
         checkVariableArgumentsCount(1, 2, args);
@@ -512,7 +513,7 @@ PROCEDURE incImpl(name: STRING; unary: STRING; incOp: BinaryOpStr; incRefOp: Ope
             IF value = NIL THEN
                 valueCode := y.code();
             ELSE
-                valueCode := String.fromInt(value^(Code.IntConst).value);
+                valueCode := String.fromInt(value^(ConstValue.Int).value);
                 IF ~y.isTerm() THEN
                     valueCode := valueCode + "/*" + y.code() + "*/";
                 END;
@@ -605,12 +606,12 @@ PROCEDURE makeFlt(): Symbols.PSymbol;
     PROCEDURE CallImpl.make(args: ARRAY OF Code.PExpression; cx: LanguageContext.PType): Code.PExpression;
     VAR
         arg: Code.PExpression;
-        value: Code.PConst;
+        value: ConstValue.PType;
     BEGIN
         arg := checkSingleArgument(args, SELF, cx.types, NIL);
         value := arg.constValue();
         IF value # NIL THEN
-            value := Code.makeRealConst(FLT(value^(Code.IntConst).value));
+            value := NEW ConstValue.Real(FLT(value^(ConstValue.Int).value));
         END;
         RETURN NEW Code.Expression(
                 arg.code(), 
@@ -656,7 +657,7 @@ PROCEDURE makeOrd(): Symbols.PSymbol;
     VAR
         arg: Code.PExpression;
         argType: Types.PType;
-        value: Code.PConst;
+        value: ConstValue.PType;
         code: STRING;
         ch: CHAR;
         result: Code.PExpression;
@@ -666,7 +667,7 @@ PROCEDURE makeOrd(): Symbols.PSymbol;
         IF (argType = Types.basic.ch) OR (argType = Types.basic.set) THEN
             value := arg.constValue();
             IF (value # NIL) & (argType = Types.basic.set) THEN
-                value := Code.makeIntConst(ORD(value^(Code.SetConst).value));
+                value := NEW ConstValue.Int(ORD(value^(ConstValue.Set).value));
             END;
             result := Code.makeExpression(arg.code(), Types.basic.integer, NIL, value);
         ELSIF argType = Types.basic.bool THEN
@@ -684,7 +685,7 @@ PROCEDURE makeOrd(): Symbols.PSymbol;
                 String.fromInt(ORD(ch)), 
                 Types.basic.integer,
                 NIL,
-                Code.makeIntConst(ORD(ch)));
+                NEW ConstValue.Int(ORD(ch)));
         ELSE
             Errors.raise(
                 "ORD function expects CHAR or BOOLEAN or SET as an argument, got '"

+ 4 - 8
src/ob/Types.ob

@@ -1,6 +1,6 @@
 MODULE Types;
 IMPORT
-    Chars, Context, Errors, JS, OberonRtl, Object, ScopeBase, Str := String;
+    Chars, ConstValue, Context, Errors, OberonRtl, Object, ScopeBase, Str := String;
 TYPE
     Id* = RECORD(Object.Type)
         PROCEDURE idType*(): STRING
@@ -41,10 +41,10 @@ TYPE
     PLazyTypeId = POINTER TO LazyTypeId;
 
     Const* = RECORD(Id)
-        PROCEDURE Const*(type: PType; value: JS.var);
+        PROCEDURE Const*(type: PType; value: ConstValue.PType);
 
         type: PType;
-        value: JS.var
+        value-: ConstValue.PType;
     END;
 
     PConst* = POINTER TO Const;
@@ -397,10 +397,6 @@ PROCEDURE constType*(c: Const): PType;
     RETURN c.type
 END constType;
 
-PROCEDURE constValue*(c: Const): JS.var;
-    RETURN c.value
-END constValue;
-
 PROCEDURE Variable.idType(): STRING;
     RETURN "variable"
 END Variable.idType;
@@ -875,7 +871,7 @@ PROCEDURE Pointer.Pointer(name: STRING; base: PTypeId)
       base(base);
 END;
 
-PROCEDURE Const.Const(type: PType; value: JS.var)
+PROCEDURE Const.Const(type: PType; value: ConstValue.PType)
     | type(type),
       value(value);
 END;

+ 1 - 1
src/oberon/oberon_grammar.js

@@ -113,7 +113,7 @@ exports.language = {
             Factor:             ContextExpression.Factor,
             FormalParameters:   Context.FormalParameters,
             FormalType:         Context.FormalType,
-            Term:               Context.Term,
+            Term:               ContextExpression.Term,
             AddOperator:        Context.AddOperator,
             MulOperator:        Context.MulOperator,
             SimpleExpression:   Context.SimpleExpression,