2
0
Vladislav Folts 10 жил өмнө
parent
commit
78f9cc4181

BIN
bin/compiled.zip


+ 1 - 43
src/context.js

@@ -882,9 +882,6 @@ exports.Term = ChainedContext.extend({
                                  : this.attributes.designator.type();
     },
     handleOperator: function(o){this.__operator = o;},
-    handleFactor: function(e){
-        this.handleExpression(e);
-    },
     endParse: function(){
         var e = this.__expression;
         if (!e){
@@ -906,45 +903,6 @@ exports.Term = ChainedContext.extend({
     }
 });
 
-exports.Factor = ChainedContext.extend({
-    init: function FactorContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__logicalNot = false;
-        this.__factor = undefined;
-    },
-    type: function(){return this.parent().type();},
-    handleLiteral: function(s){
-        if (s == "NIL")
-            this.handleConst(nilType, undefined, "null");
-        else if (s == "TRUE")
-            this.handleConst(basicTypes.bool, Code.makeIntConst(1), "true");
-        else if (s == "FALSE")
-            this.handleConst(basicTypes.bool, Code.makeIntConst(0), "false");
-        else if (s == "~")
-            this.handleLogicalNot();
-    },
-    handleConst: function(type, value, code){
-        this.__factor = Code.makeExpression(
-            code, type, undefined, value);
-    },
-    handleFactor: function(e){
-        this.__factor = e;
-    },
-    handleExpression: function(e){
-        this.__factor = e;
-    },
-    handleLogicalNot: function(){
-        this.__logicalNot = true;
-    },
-    endParse: function(){
-        if (this.__logicalNot){
-            ContextHierarchy.checkTypeMatch(this.__factor.type(), basicTypes.bool);
-            this.__factor = op.not(this.__factor);
-        }
-        this.parent().handleFactor(this.__factor);
-    }
-});
-
 function designatorAsExpression(d){
     var info = d.info();
     if (info instanceof Type.ProcedureId){
@@ -994,7 +952,7 @@ exports.Set = ChainedContext.extend({
             if (this.__value)
                 code += " | " + this.__value;
             var e = Code.makeExpression(code, basicTypes.set);
-            parent.handleFactor(e);
+            parent.handleExpression(e);
         }
     }
 });

+ 4 - 3
src/eberon/eberon_context.js

@@ -5,6 +5,7 @@ var Class = require("rtl.js").Class;
 var Code = require("js/Code.js");
 var CodeGenerator = require("js/CodeGenerator.js");
 var Context = require("context.js");
+var ContextExpression = require("js/ContextExpression.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var EberonConstructor= require("js/EberonConstructor.js");
 var EberonContext= require("js/EberonContext.js");
@@ -815,12 +816,12 @@ var ProcOrMethodDecl = Context.ProcDecl.extend({
     }
 });
 
-var Factor = Context.Factor.extend({
+var Factor = Class.extend.call(ContextExpression.Factor, {
     init: function EberonContext$Factor(context){
-        Context.Factor.prototype.init.call(this, context);
+        ContextExpression.Factor.call(this, context);
     },
     handleLogicalNot: function(){
-        Context.Factor.prototype.handleLogicalNot.call(this);
+        ContextExpression.Factor.prototype.handleLogicalNot.call(this);
         var p = this.getCurrentPromotion();
         if (p)
             p.invert();

+ 56 - 2
src/ob/ContextExpression.ob

@@ -1,11 +1,23 @@
 MODULE ContextExpression;
 IMPORT 
-    Chars, Code, ContextHierarchy, String, Types;
+    Chars, Code, ContextHierarchy, Operator, String, Types;
 TYPE
+    PFactor = POINTER TO Factor;
+
     Factor* = RECORD(ContextHierarchy.Node)
+        PROCEDURE Factor(parent: PFactor);
+
         PROCEDURE handleConst(type: Types.PType; value: Code.PConst; code: STRING);
+        PROCEDURE handleLiteral(s: STRING);
+        PROCEDURE handleLogicalNot();
+        PROCEDURE handleExpression(e: Code.PExpression);
+
+        PROCEDURE endParse();
+
+        factorParent: PFactor;
+        logicalNot: BOOLEAN;
+        e: Code.PExpression;
     END;
-    PFactor = POINTER TO Factor;
 
     Const = RECORD(ContextHierarchy.Node)
         PROCEDURE Const(factor: PFactor);
@@ -25,6 +37,48 @@ TYPE
         PROCEDURE handleStr*(s: STRING);
     END;
 
+PROCEDURE Factor.Factor(parent: PFactor)
+    | SUPER(parent),
+      factorParent(parent);
+END;
+
+PROCEDURE Factor.handleConst(type: Types.PType; value: Code.PConst; code: STRING);
+BEGIN
+    SELF.e := Code.makeExpression(code, type, NIL, value);
+END;
+
+PROCEDURE Factor.handleLiteral(s: STRING);
+BEGIN
+    IF s = "NIL" THEN
+        SELF.handleConst(Types.nil, NIL, "null");
+    ELSIF s = "TRUE" THEN
+        SELF.handleConst(Types.basic.bool, Code.makeIntConst(1), "true");
+    ELSIF s = "FALSE" THEN
+        SELF.handleConst(Types.basic.bool, Code.makeIntConst(0), "false");
+    ELSIF s = "~" THEN
+        SELF.handleLogicalNot();
+    END;
+END;
+
+PROCEDURE Factor.handleLogicalNot();
+BEGIN
+    SELF.logicalNot := TRUE;
+END;
+
+PROCEDURE Factor.handleExpression(e: Code.PExpression);
+BEGIN
+    SELF.e := e;
+END;
+
+PROCEDURE Factor.endParse();
+BEGIN
+    IF SELF.logicalNot THEN
+        ContextHierarchy.checkTypeMatch(SELF.e.type(), Types.basic.bool);
+        SELF.e := Operator.not(SELF.e);
+    END;
+    SELF.factorParent.handleExpression(SELF.e);
+END;
+
 PROCEDURE Const.Const(factor: PFactor)
     | SUPER(factor),
       factor(factor);

+ 1 - 1
src/ob/ContextHierarchy.ob

@@ -23,7 +23,7 @@ TYPE
         PROCEDURE Node*(parent: PNode);
 
         PROCEDURE root(): PRoot;
-        PROCEDURE parent(): PNode;
+        PROCEDURE parent*(): PNode;
         PROCEDURE handleMessage(VAR msg: Message): PMessageResult;
         PROCEDURE codeGenerator(): CodeGenerator.PIGenerator;
         PROCEDURE qualifyScope(scope: Scope.PType): STRING;

+ 1 - 1
src/ob/Operator.ob

@@ -678,7 +678,7 @@ PROCEDURE eqGreaterStr*(left, right: Code.PExpression; cx: LanguageContext.PType
     RETURN strCmp(" >= ", left, right, cx)
 END eqGreaterStr;
 
-PROCEDURE not*(x: Code.PExpression; cx: LanguageContext.PType): Code.PExpression;
+PROCEDURE not*(x: Code.PExpression): Code.PExpression;
     RETURN unary(x, opNot, "!")
 END not;
 

+ 1 - 1
src/oberon/oberon_context.js

@@ -89,7 +89,7 @@ var ExpressionProcedureCall = ProcedureCall.extend({
         var e = this.__hasActualParameters 
               ? this.callExpression()
               : Context.designatorAsExpression(this.attributes.designator); 
-        this.parent().handleFactor(e);
+        this.parent().handleExpression(e);
     }
 });
 

+ 2 - 1
src/oberon/oberon_grammar.js

@@ -3,6 +3,7 @@
 var Cast = require("js/Cast.js");
 var CodeGenerator = require("js/CodeGenerator.js");
 var Context = require("context.js");
+var ContextExpression = require("js/ContextExpression.js");
 var Grammar = require("grammar.js");
 var ObContext = require("oberon/oberon_context.js");
 var ObRtl = require("js/OberonRtl.js");
@@ -109,7 +110,7 @@ exports.language = {
             recordDecl:         ObContext.RecordDecl,
             variableDeclaration: ObContext.VariableDeclaration,
             ArrayDecl:          Context.ArrayDecl,
-            Factor:             Context.Factor,
+            Factor:             ContextExpression.Factor,
             FormalParameters:   Context.FormalParameters,
             FormalType:         Context.FormalType,
             Term:               Context.Term,