Vladislav Folts пре 9 година
родитељ
комит
dd6e77f9ba

+ 3 - 16
src/eberon/EberonContextInPlace.ob

@@ -2,8 +2,8 @@ MODULE EberonContextInPlace;
 IMPORT
     CodeGenerator, 
     ContextExpression, ContextHierarchy, 
-    EberonContextDesignator, EberonRecord,
-    Errors, Expression, LanguageContext, Symbols, Types;
+    EberonContextDesignator, EberonContextLoop,
+    EberonRecord, Errors, Expression, LanguageContext, Symbols, Types;
 TYPE
     VariableInit* = RECORD(ContextExpression.ExpressionHandler)
         PROCEDURE onParsed();
@@ -13,15 +13,7 @@ TYPE
         symbol: Symbols.PSymbol;
     END;
 
-    For = RECORD(ContextHierarchy.Node)
-        PROCEDURE handleInPlaceInit(symbol: Symbols.PSymbol; code: STRING);
-    END;
-    PFor = POINTER TO For;
-
     VariableInitFor* = RECORD(VariableInit)
-        PROCEDURE VariableInitFor(cx: PFor);
-
-        parentFor: PFor;
     END;
 
 PROCEDURE VariableInit.codeGenerator(): CodeGenerator.PIGenerator;
@@ -83,14 +75,9 @@ BEGIN
     RETURN result;
 END;
 
-PROCEDURE VariableInitFor.VariableInitFor(cx: PFor)
-    | SUPER(cx),
-      parentFor(cx);
-END;
-
 PROCEDURE VariableInitFor.onParsed();
 BEGIN
-    SELF.parentFor.handleInPlaceInit(SELF.symbol, SELF.code);
+    SELF.parent()^(EberonContextLoop.For).handleInPlaceInit(SELF.symbol, SELF.code);
 END;
 
 END EberonContextInPlace.

+ 93 - 2
src/eberon/EberonContextLoop.ob

@@ -4,9 +4,25 @@ IMPORT
     ContextLoop, ContextExpression, ContextHierarchy,
     Errors, Expression,
     EberonContextDesignator, 
-    EberonMap, EberonScope, EberonString,
-    Scope, Symbols, Types, Variable;
+    EberonMap, EberonOperatorScopes, EberonScope, EberonString,
+    Object, Scope, Symbols, Types, Variable;
 TYPE
+    While* = RECORD(ContextLoop.While)
+        PROCEDURE While(parent: ContextHierarchy.PNode);
+
+        scopes: EberonOperatorScopes.Type;
+    END;
+
+    Repeat* = RECORD(ContextLoop.Repeat)
+        PROCEDURE Repeat(parent: ContextHierarchy.PNode);
+    END;
+
+    For* = RECORD(ContextLoop.For)
+        PROCEDURE For(parent: ContextHierarchy.PNode);
+
+        PROCEDURE handleInPlaceInit*(symbol: Symbols.PSymbol; code: STRING);
+    END;
+
     ForEachVariable = RECORD(Variable.TypedVariable)
     END;
 
@@ -18,6 +34,81 @@ TYPE
         scopeWasCreated: BOOLEAN;
     END;
 
+PROCEDURE While.While(parent: ContextHierarchy.PNode)
+    | SUPER(parent),
+      scopes(parent);
+END;
+
+PROCEDURE While.handleLiteral(s: STRING);
+BEGIN
+    SUPER(s);
+    IF s = "DO" THEN
+        SELF.scopes.doThen();
+    ELSIF s = "ELSIF" THEN
+        SELF.scopes.alternate();
+    END;
+END;
+
+PROCEDURE While.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
+VAR
+    result: Object.PType;
+BEGIN
+    IF ~SELF.scopes.handleMessage(msg) THEN
+        result := SUPER(msg);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE While.endParse(): BOOLEAN;
+BEGIN
+    SELF.scopes.reset();
+    RETURN SUPER();
+END;
+
+PROCEDURE startLoopScope(cx: ContextHierarchy.Node);
+BEGIN
+    root <- cx.root();
+    scope <- EberonScope.makeOperator(
+        root.currentScope(),
+        root.language().stdSymbols);
+    root.pushScope(scope);
+END;
+
+PROCEDURE endLoopScope(cx: ContextHierarchy.Node);
+BEGIN
+    cx.root().popScope();
+END;
+
+PROCEDURE Repeat.Repeat(parent: ContextHierarchy.PNode)
+    | SUPER(parent);
+BEGIN
+    startLoopScope(SELF);
+END;
+
+PROCEDURE Repeat.endParse(): BOOLEAN;
+BEGIN
+    endLoopScope(SELF);
+    RETURN TRUE;
+END;
+
+PROCEDURE For.For(parent: ContextHierarchy.PNode)
+    | SUPER(parent);
+BEGIN
+    startLoopScope(SELF);
+END;
+
+PROCEDURE For.handleInPlaceInit(symbol: Symbols.PSymbol; code: STRING);
+BEGIN
+    SELF.doHandleInitCode(symbol.id(), "for (" + code);
+    SELF.doHandleInitExpression(symbol.info()(Types.PVariable).type());
+END;
+
+PROCEDURE For.endParse(): BOOLEAN;
+BEGIN
+    endLoopScope(SELF);
+    RETURN SUPER();
+END;
+
 PROCEDURE ForEachVariable.idType(): STRING;
     RETURN "FOR variable";
 END;

+ 5 - 5
src/eberon/EberonOperatorScopes.ob

@@ -6,12 +6,12 @@ IMPORT
     Scope;
 TYPE
     Type* = RECORD
-        PROCEDURE Type(cx: ContextHierarchy.PNode);
+        PROCEDURE Type*(cx: ContextHierarchy.PNode);
 
-        PROCEDURE handleMessage(VAR msg: ContextHierarchy.Message): BOOLEAN;
-        PROCEDURE doThen();
-        PROCEDURE alternate();
-        PROCEDURE reset();
+        PROCEDURE handleMessage*(VAR msg: ContextHierarchy.Message): BOOLEAN;
+        PROCEDURE doThen*();
+        PROCEDURE alternate*();
+        PROCEDURE reset*();
 
         context: ContextHierarchy.PNode;
         scope: Scope.PType;

+ 0 - 61
src/eberon/eberon_context.js

@@ -52,30 +52,6 @@ var ChainedContext = ContextHierarchy.Node;
 ChainedContext.extend = Class.extend;
 ChainedContext.prototype.init = ContextHierarchy.Node;
 
-var While = Class.extend.call(ContextLoop.While, {
-    init: function EberonContext$While(context){
-        ContextLoop.While.call(this, context);
-        this.__scopes = new EberonOperatorScopes.Type(this);
-    },
-    handleLiteral: function(s){
-        ContextLoop.While.prototype.handleLiteral.call(this, s);
-        if (s == "DO")
-            this.__scopes.doThen();
-        else if (s == "ELSIF")
-            this.__scopes.alternate();
-    },
-    handleMessage: function(msg){
-        if (this.__scopes.handleMessage(msg))
-            return;
-
-        return ContextLoop.While.prototype.handleMessage.call(this, msg);
-    },
-    endParse: function(){
-        this.__scopes.reset();
-        ContextLoop.While.prototype.endParse.call(this);
-    }
-});
-
 var If = Class.extend.call(ContextIf.Type, {
     init: function EberonContext$If(context){
         ContextIf.Type.call(this, context);
@@ -119,40 +95,6 @@ var CaseLabel = Class.extend.call(ContextCase.Label, {
     }
 });
 
-var Repeat = Class.extend.call(ContextLoop.Repeat, {
-    init: function EberonContext$Repeat(context){
-        ContextLoop.Repeat.call(this, context);
-        var root = this.root();
-        var scope = EberonScope.makeOperator(
-            root.currentScope(),
-            root.language().stdSymbols);
-        root.pushScope(scope);
-    },
-    endParse: function(){
-        this.root().popScope();
-        //Context.Repeat.prototype.endParse.call(this);
-    }
-});
-
-var For = Class.extend.call(ContextLoop.For, {
-    init: function EberonContext$Repeat(context){
-        ContextLoop.For.call(this, context);
-        var root = this.root();
-        var scope = EberonScope.makeOperator(
-            root.currentScope(),
-            root.language().stdSymbols);
-        root.pushScope(scope);
-    },
-    handleInPlaceInit: function(symbol, code){
-        this.doHandleInitCode(symbol.id(), "for (" + code);
-        this.doHandleInitExpression(symbol.info().type());
-    },
-    endParse: function(){
-        this.root().popScope();
-        ContextLoop.For.prototype.endParse.call(this);
-    }
-});
-
 var MapDecl = ChainedContext.extend({
     init: function EberonContext$MapDecl(context){
         ChainedContext.prototype.init.call(this, context);
@@ -257,12 +199,9 @@ var ModuleDeclaration = Class.extend.call(ContextModule.Declaration, {
 });
 
 exports.CaseLabel = CaseLabel;
-exports.For = For;
 exports.FormalParameters = FormalParameters;
 exports.FormalParametersProcDecl = FormalParametersProcDecl;
 exports.FormalType = FormalType;
 exports.If = If;
 exports.ModuleDeclaration = ModuleDeclaration;
 exports.MapDecl = MapDecl;
-exports.Repeat = Repeat;
-exports.While = While;

+ 3 - 3
src/eberon/eberon_grammar.js

@@ -160,11 +160,11 @@ exports.language = {
             MulOperator:        EberonContextExpression.MulOperator,
             SimpleExpression:   EberonContextExpression.SimpleExpression, 
             Expression:         EberonContextExpression.ExpressionNode,
-            For:                EbContext.For,
-            While:              EbContext.While,
+            For:                EberonContextLoop.For,
+            While:              EberonContextLoop.While,
             If:                 EbContext.If,
             CaseLabel:          EbContext.CaseLabel,
-            Repeat:             EbContext.Repeat,
+            Repeat:             EberonContextLoop.Repeat,
             ModuleDeclaration:  EbContext.ModuleDeclaration
         },
         Grammar.reservedWords + " SELF SUPER MAP"

+ 5 - 5
src/ob/ContextLoop.ob

@@ -4,11 +4,11 @@ IMPORT
     Errors, Expression, Operator, String, Types;
 TYPE
     While* = RECORD(ContextExpression.ExpressionHandler)
-        PROCEDURE While(parent: ContextHierarchy.PNode);
+        PROCEDURE While*(parent: ContextHierarchy.PNode);
     END;
 
     Repeat* = RECORD(ContextHierarchy.Node)
-        PROCEDURE Repeat(parent: ContextHierarchy.PNode);
+        PROCEDURE Repeat*(parent: ContextHierarchy.PNode);
     END;
 
     Until* = RECORD(ContextExpression.ExpressionHandler)
@@ -16,10 +16,10 @@ TYPE
     END;
 
     For* = RECORD(ContextExpression.ExpressionHandler)
-        PROCEDURE For(parent: ContextHierarchy.PNode);
+        PROCEDURE For*(parent: ContextHierarchy.PNode);
 
-        PROCEDURE doHandleInitCode(id, code: STRING);
-        PROCEDURE doHandleInitExpression(type: Types.PType);
+        PROCEDURE doHandleInitCode*(id, code: STRING);
+        PROCEDURE doHandleInitExpression*(type: Types.PType);
 
         toExpr: CodeGenerator.PIGenerator;
         var: STRING;