Forráskód Böngészése

js -> eberon transition + bug fixes

Vladislav Folts 10 éve
szülő
commit
d7d4943529

BIN
bin/compiled.zip


+ 0 - 65
src/context.js

@@ -442,71 +442,6 @@ exports.ArrayDimensions = ChainedContext.extend({
     }
     }
 });
 });
 
 
-exports.Set = ChainedContext.extend({
-    init: function SetContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__value = 0;
-        this.__expr = "";
-    },
-    handleElement: function(from, fromValue, to, toValue){
-        if (fromValue && (!to || toValue)){
-            if (to)
-                for(var i = fromValue.value; i <= toValue.value; ++i)
-                    this.__value |= 1 << i;
-            else
-                this.__value |= 1 << fromValue.value;
-        }
-        else{
-            if (this.__expr.length)
-                this.__expr += ", ";
-            if (to)
-                this.__expr += "[" + from + ", " + to + "]";
-            else
-                this.__expr += from;
-        }
-    },
-    endParse: function(){
-        var parent = this.parent();
-        if (!this.__expr.length)
-            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)
-                code += " | " + this.__value;
-            var e = Expression.make(code, basicTypes.set);
-            parent.handleExpression(e);
-        }
-    }
-});
-
-exports.SetElement = ChainedContext.extend({
-    init: function SetElementContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__from = undefined;
-        this.__fromValue = undefined;
-        this.__to = undefined;
-        this.__toValue = undefined;
-        this.__expr = CodeGenerator.makeSimpleGenerator();
-    },
-    codeGenerator: function(){return this.__expr;},
-    handleExpression: function(e){
-        var value = e.constValue();
-        if (!this.__from)
-            {
-            this.__from = this.__expr.result();
-            this.__fromValue = value;
-            this.__expr = CodeGenerator.makeSimpleGenerator();
-            }
-        else{
-            this.__to = this.__expr.result();
-            this.__toValue = value;
-        }
-    },
-    endParse: function(){
-        this.parent().handleElement(this.__from, this.__fromValue, this.__to, this.__toValue);
-    }
-});
-
 function handleIfExpression(e){
 function handleIfExpression(e){
     var type = e.type();
     var type = e.type();
     if (type !== basicTypes.bool)
     if (type !== basicTypes.bool)

+ 2 - 2
src/grammar.js

@@ -98,8 +98,8 @@ var expression = context(and(simpleExpression, optional(and(relation, simpleExpr
                        , contexts.Expression);
                        , contexts.Expression);
 var constExpression = expression;
 var constExpression = expression;
 
 
-var element = context(and(expression, optional(and("..", expression))), Context.SetElement);
-var set = and("{", context(optional(and(element, repeat(and(",", element)))), Context.Set)
+var element = context(and(expression, optional(and("..", expression))), ContextExpression.SetElement);
+var set = and("{", context(optional(and(element, repeat(and(",", element)))), ContextExpression.Set)
             , "}");
             , "}");
 
 
 var expList = and(expression, repeat(and(",", expression)));
 var expList = and(expression, repeat(and(",", expression)));

+ 1 - 0
src/ob/ConstValue.ob

@@ -9,6 +9,7 @@ TYPE
 
 
         value-: INTEGER;
         value-: INTEGER;
     END;
     END;
+    PInt* = POINTER TO Int;
 
 
     Real* = RECORD (Type)
     Real* = RECORD (Type)
         PROCEDURE Real*(r: REAL);
         PROCEDURE Real*(r: REAL);

+ 81 - 0
src/ob/ContextExpression.ob

@@ -103,6 +103,23 @@ TYPE
         PROCEDURE handleStr*(s: STRING);
         PROCEDURE handleStr*(s: STRING);
     END;
     END;
 
 
+    PSet = POINTER TO Set;
+
+    SetElement* = RECORD(ExpressionHandler)
+        PROCEDURE SetElement*(parent: PSet);
+
+        from, to: STRING;
+        fromValue, toValue: ConstValue.PInt;
+        code: CodeGenerator.PIGenerator;
+    END;
+
+    Set* = RECORD(ContextHierarchy.Node)
+        PROCEDURE handleElement(s: SetElement);
+
+        value: SET;
+        expression: STRING;
+    END;
+
     OpTypeCheck = RECORD
     OpTypeCheck = RECORD
         PROCEDURE expect(): STRING;
         PROCEDURE expect(): STRING;
         PROCEDURE check(t: Types.PType): BOOLEAN;
         PROCEDURE check(t: Types.PType): BOOLEAN;
@@ -908,6 +925,70 @@ BEGIN
         escapeString(s));
         escapeString(s));
 END;
 END;
 
 
+PROCEDURE SetElement.SetElement(parent: PSet)
+    | SUPER(parent),
+      code(CodeGenerator.makeSimpleGenerator());
+END;
+
+PROCEDURE SetElement.codeGenerator(): CodeGenerator.PIGenerator;
+    RETURN SELF.code;
+END;
+
+PROCEDURE SetElement.handleExpression(e: Expression.PType);
+BEGIN
+    value <- e.constValue()(ConstValue.PInt);
+    IF LEN(SELF.from) = 0 THEN
+        SELF.from := SELF.code.result();
+        SELF.fromValue := value;
+        SELF.code := CodeGenerator.makeSimpleGenerator();
+    ELSE
+        SELF.to := SELF.code.result();
+        SELF.toValue := value;
+    END;
+END;
+
+PROCEDURE SetElement.endParse();
+BEGIN
+    SELF.parent()^(Set).handleElement(SELF);
+END;
+
+PROCEDURE Set.handleElement(s: SetElement);
+BEGIN
+    IF (s.fromValue # NIL) & ((LEN(s.to) = 0) OR (s.toValue # NIL)) THEN
+        IF LEN(s.to) # 0 THEN
+            FOR i <- s.fromValue.value TO s.toValue.value DO
+                INCL(SELF.value, i);
+            END;
+        ELSE
+            INCL(SELF.value, s.fromValue.value);
+        END;
+    ELSE
+        IF LEN(SELF.expression) # 0 THEN
+            SELF.expression := SELF.expression + ", ";
+        END;
+        IF LEN(s.to) # 0 THEN
+            SELF.expression := SELF.expression + "[" + s.from + ", " + s.to + "]";
+        ELSE
+            SELF.expression := SELF.expression + s.from;
+        END;
+    END;
+END;
+
+PROCEDURE Set.endParse();
+BEGIN
+    parent <- SELF.parent()(PFactor);
+    IF LEN(SELF.expression) = 0 THEN
+        parent.handleConst(Types.basic.set, NEW ConstValue.Set(SELF.value), String.fromInt(ORD(SELF.value)));
+    ELSE
+        code <- SELF.root().language().rtl().makeSet(SELF.expression);
+        IF SELF.value # {} THEN
+            code := code + " | " + String.fromInt(ORD(SELF.value));
+        END;
+        e <- Expression.makeSimple(code, Types.basic.set);
+        parent.handleExpression(e);
+    END;
+END;
+
 PROCEDURE IntOpTypeCheck.expect(): STRING;
 PROCEDURE IntOpTypeCheck.expect(): STRING;
     RETURN Types.intsDescription();
     RETURN Types.intsDescription();
 END;
 END;

+ 1 - 0
src/ob/OberonRtl.ob

@@ -5,6 +5,7 @@ TYPE
         clone*: PROCEDURE(from, type, constructor: STRING): STRING;
         clone*: PROCEDURE(from, type, constructor: STRING): STRING;
         strCmp*: PROCEDURE(s1, s2: STRING): STRING;
         strCmp*: PROCEDURE(s1, s2: STRING): STRING;
         assignArrayFromString*: PROCEDURE(s1, s2: STRING): STRING;
         assignArrayFromString*: PROCEDURE(s1, s2: STRING): STRING;
+        makeSet*: PROCEDURE(repr: STRING): STRING;
         setInclL*: PROCEDURE(l, r: STRING): STRING;
         setInclL*: PROCEDURE(l, r: STRING): STRING;
         setInclR*: PROCEDURE(l, r: STRING): STRING;
         setInclR*: PROCEDURE(l, r: STRING): STRING;
         assertId*: PROCEDURE(): STRING;
         assertId*: PROCEDURE(): STRING;

+ 1 - 1
src/rtl.js

@@ -137,7 +137,7 @@ var methods = {
             if (b instanceof Array){
             if (b instanceof Array){
                 var from = b[0];
                 var from = b[0];
                 var to = b[1];
                 var to = b[1];
-                if (from < to)
+                if (to < from)
                     throw new Error("invalid SET diapason: " + from + ".." + to);
                     throw new Error("invalid SET diapason: " + from + ".." + to);
                 for(var bi = from; bi <= to; ++bi)
                 for(var bi = from; bi <= to; ++bi)
                     setBit(bi);
                     setBit(bi);

+ 10 - 2
test/input/run/set.ob

@@ -1,7 +1,15 @@
 MODULE test;
 MODULE test;
-VAR s1, s2: SET;
+VAR 
+    s1, s2: SET;
+    i0, i5, i8: INTEGER;
 BEGIN
 BEGIN
     s1 := {0..5};
     s1 := {0..5};
     s2 := {0..8};
     s2 := {0..8};
-    ASSERT(s1 <= s2)
+    ASSERT(s1 <= s2);
+
+    i0 := 0;
+    i5 := 5;
+    i8 := 8;
+    ASSERT(s1 = {i0..i5});
+    ASSERT(s2 = {i0..i8});
 END test.
 END test.