2
0
Vladislav Folts 10 жил өмнө
parent
commit
32298d5962

BIN
bin/compiled.zip


+ 0 - 46
src/context.js

@@ -38,49 +38,6 @@ var ChainedContext = ContextHierarchy.Node;
 ChainedContext.extend = Class.extend;
 ChainedContext.prototype.init = ContextHierarchy.Node;
 
-var HandleSymbolAsType = ContextType.HandleSymbolAsType;
-HandleSymbolAsType.extend = Class.extend;
-HandleSymbolAsType.prototype.init = ContextType.HandleSymbolAsType;
-
-function beginCallMsg(){}
-function endCallMsg(){}
-
-exports.ActualParameters = ChainedContext.extend({
-    init: function ActualParametersContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.handleMessage(beginCallMsg);
-    },
-    handleExpression: function(e){
-        this.parent().handleExpression(e);
-    },
-    endParse: function(){
-        this.handleMessage(endCallMsg);
-    }
-});
-
-exports.TypeSection = ChainedContext.extend({
-    init: function TypeSection(context){
-        ChainedContext.prototype.init.call(this, context);
-    },
-    handleMessage: function(msg){
-        if (msg instanceof ContextType.ForwardTypeMsg){
-            var scope = this.root().currentScope();
-            Scope.addUnresolved(scope, msg.id);
-            var resolve = function(){
-                return ContextHierarchy.getSymbol(this.root(), msg.id).info().type();
-            }.bind(this);
-
-            return new TypeId.Forward(resolve);
-        }
-        return ChainedContext.prototype.handleMessage.call(this, msg);
-    },
-    endParse: function(){
-        var unresolved = Scope.unresolved(this.root().currentScope());
-        if (unresolved.length)
-            throw new Errors.Error("no declaration found for '" + unresolved.join("', '") + "'");
-    }
-});
-
 exports.ModuleDeclaration = ChainedContext.extend({
     init: function ModuleDeclarationContext(context){
         ChainedContext.prototype.init.call(this, context);
@@ -195,7 +152,4 @@ var ModuleImport = ChainedContext.extend({
 });
 exports.ModuleImport = ModuleImport;
 
-exports.beginCallMsg = beginCallMsg;
-exports.endCallMsg = endCallMsg;
 exports.Chained = ChainedContext;
-exports.HandleSymbolAsType = HandleSymbolAsType;

+ 8 - 8
src/eberon/eberon_context.js

@@ -282,9 +282,9 @@ var Designator = Class.extend.call(ContextDesignator.Type, {
         return ContextDesignator.Type.prototype.doMakeDerefVar.call(this, info);
     },
     handleMessage: function(msg){
-        if (msg == Context.beginCallMsg)
+        if (msg == ContextDesignator.beginCallMsg())
             return this.__beginCall();
-        if (msg == Context.endCallMsg)
+        if (msg == ContextDesignator.endCallMsg())
             return this.__endCall();
         if (msg instanceof OperatorNewMsg){
             var e = msg.expression;
@@ -366,14 +366,14 @@ var OperatorNew = Context.Chained.extend({
         this.__call.handleArgument(e);
     },
     handleMessage: function(msg){
-        if (msg == Context.beginCallMsg){
+        if (msg == ContextDesignator.beginCallMsg()){
             this.__call = makeContextCall(
                 this,
                 function(cx){ return EberonConstructor.makeConstructorCall(this.__info, cx, true); }.bind(this)
                 );
             return;
         }
-        if (msg == Context.endCallMsg)
+        if (msg == ContextDesignator.endCallMsg())
             return;
 
         return Context.Chained.prototype.handleMessage.call(this, msg);
@@ -632,9 +632,9 @@ var BaseInit = Context.Chained.extend({
     },
     codeGenerator: function(){return CodeGenerator.nullGenerator();},
     handleMessage: function(msg){
-        if (msg == Context.beginCallMsg)
+        if (msg == ContextDesignator.beginCallMsg())
             return;
-        if (msg == Context.endCallMsg){
+        if (msg == ContextDesignator.endCallMsg()){
             var e = this.__initCall.end();
             if (this.__initField)
                 this.type().setFieldInitializationCode(this.__initField, e.code());
@@ -1358,9 +1358,9 @@ var FormalParameters = Class.extend.call(ContextProcedure.FormalParameters, {
     }
 });
 
-var FormalType = Context.HandleSymbolAsType.extend({
+var FormalType = Class.extend.call(ContextType.HandleSymbolAsType, {
     init: function EberonContext$FormalType(context){
-        Context.HandleSymbolAsType.prototype.init.call(this, context);
+        ContextType.HandleSymbolAsType.call(this, context);
         this.__arrayDimensions = [];
         this.__dynamicDimension = false;
     },

+ 2 - 2
src/grammar.js

@@ -110,7 +110,7 @@ var set = and("{", context(optional(and(element, repeat(and(",", element)))), Co
             , "}");
 
 var expList = and(expression, repeat(and(",", expression)));
-var actualParameters = and("(", context(optional(expList), Context.ActualParameters), ")");
+var actualParameters = and("(", context(optional(expList), ContextDesignator.ActualParameters), ")");
 
 var assignment = and(context(or(":=", "="), ContextAssignment.Check),
                      required(expression, "expression expected"));
@@ -215,7 +215,7 @@ result.procedureDeclaration
     );
 result.declarationSequence
     = and(optional(and("CONST", repeat(and(constantDeclaration, required(";"))))),
-          optional(and("TYPE", context(repeat(and(typeDeclaration, required(";"))), Context.TypeSection))),
+          optional(and("TYPE", context(repeat(and(typeDeclaration, required(";"))), ContextType.Section))),
           optional(and("VAR", repeat(and(variableDeclaration, required(";"))))),
           repeat(and(result.procedureDeclaration, ";")));
 result.procedureBody

+ 27 - 0
src/ob/ContextDesignator.ob

@@ -42,6 +42,15 @@ TYPE
         type: Types.PType;
     END;
 
+    ActualParameters* = RECORD(ContextExpression.ExpressionHandler)
+        PROCEDURE ActualParameters(parent: ContextExpression.PExpressionHandler);
+
+        expressionHandler: ContextExpression.PExpressionHandler;
+    END;
+VAR
+    beginCallMsg*: ContextHierarchy.Message;
+    endCallMsg*: ContextHierarchy.Message;
+
 PROCEDURE Index.Index(length: INTEGER; type: Types.PType; info: Types.PId; code, lval, asProperty: STRING)
     | length(length),
       type(type),
@@ -277,4 +286,22 @@ BEGIN
     RETURN result;
 END;
 
+PROCEDURE ActualParameters.ActualParameters(parent: ContextExpression.PExpressionHandler)
+    | SUPER(parent)
+    , expressionHandler(parent);
+BEGIN
+    void <- SELF.handleMessage(beginCallMsg);
+END;
+
+PROCEDURE ActualParameters.handleExpression(e: Expression.PType);
+BEGIN
+    SELF.expressionHandler.handleExpression(e);
+END;
+
+PROCEDURE ActualParameters.endParse(): BOOLEAN;
+BEGIN
+    void <- SELF.handleMessage(endCallMsg);
+    RETURN TRUE;
+END;
+
 END ContextDesignator.

+ 1 - 1
src/ob/ContextExpression.ob

@@ -7,7 +7,7 @@ TYPE
     ExpressionHandler* = RECORD(ContextHierarchy.Node)
         PROCEDURE handleExpression*(e: Expression.PType);
     END;
-    PExpressionHandler = POINTER TO ExpressionHandler;
+    PExpressionHandler* = POINTER TO ExpressionHandler;
 
     BinaryOperator = PROCEDURE(l, r: Expression.PType): Expression.PType;
     BinaryOperatorCx = PROCEDURE(l, r: Expression.PType; cx: LanguageContext.PType): Expression.PType;

+ 1 - 1
src/ob/ContextHierarchy.ob

@@ -3,7 +3,7 @@ IMPORT
     CodeGenerator, Context, Designator, Errors, LanguageContext, Module, 
     OberonRtl, Object, Scope, ScopeBase, Symbols, String, Types;
 TYPE
-    PRoot = POINTER TO Root;
+    PRoot* = POINTER TO Root;
     PNode* = POINTER TO Node;
 
     Message* = RECORD

+ 44 - 0
src/ob/ContextType.ob

@@ -88,6 +88,17 @@ TYPE
         declParent: PDeclarationHandle;
     END;
 
+    Section* = RECORD(ContextHierarchy.Node)
+    END;
+
+    ResolveClosure = RECORD(Object.Type)
+        PROCEDURE ResolveClosure(root: ContextHierarchy.PRoot; id: STRING);
+
+        root: ContextHierarchy.PRoot;
+        id: STRING;
+    END;
+    PResolveClosure = POINTER TO ResolveClosure;
+
     ForwardTypeMsg* = RECORD(ContextHierarchy.Message)
         PROCEDURE ForwardTypeMsg(id: STRING);
 
@@ -500,6 +511,39 @@ BEGIN
     Errors.raise("cannot export anonymous RECORD field: '" + field + "'");
 END;
 
+PROCEDURE ResolveClosure.ResolveClosure(root: ContextHierarchy.PRoot; id: STRING)
+    | root(root),
+      id(id);
+END;
+
+PROCEDURE resolve(closure: Object.PType): Types.PStorageType;
+BEGIN
+    r <- closure(PResolveClosure);
+    info <- ContextHierarchy.getSymbol(r.root^, r.id).info();
+    RETURN info(TypeId.PType).type();
+END;
+
+PROCEDURE Section.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
+VAR
+    result: Object.PType;
+BEGIN
+    IF msg IS ForwardTypeMsg THEN
+        root <- SELF.root();
+        scope <- root.currentScope();
+        Scope.addUnresolved(scope^, msg.id);
+        result := NEW TypeId.Forward(resolve, NEW ResolveClosure(root, msg.id));
+    ELSE
+        result := SUPER(msg);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE Section.endParse(): BOOLEAN;
+BEGIN
+    Scope.checkAllResolved(SELF.root().currentScope()^);
+    RETURN TRUE;
+END;
+
 PROCEDURE ForwardTypeMsg.ForwardTypeMsg(id: STRING)
     | id(id);
 END;

+ 6 - 2
src/ob/Scope.ob

@@ -121,8 +121,12 @@ BEGIN
     END;
 END;
 
-PROCEDURE unresolved*(s: Type): Unresolved;
-    RETURN s.unresolved
+PROCEDURE checkAllResolved*(s: Type);
+BEGIN
+    IF LEN(s.unresolved) # 0 THEN
+        Errors.raise("no declaration found for '" 
+                     + String.join(s.unresolved, "', '") + "'");
+    END;
 END;
 
 PROCEDURE Type.close();

+ 9 - 7
src/ob/TypeId.ob

@@ -1,6 +1,6 @@
 MODULE TypeId;
 IMPORT
-    Types;
+    Types, Object;
 TYPE
     PType* = POINTER TO Type;
     Type* = RECORD(Types.Id)
@@ -13,12 +13,13 @@ TYPE
         mType: Types.PStorageType;
     END;
 
-    ResolveTypeCallback = PROCEDURE(): Types.PStorageType;
+    ResolveTypeCallback = PROCEDURE(closure: Object.PType): Types.PStorageType;
 
     Forward* = RECORD(Type)
-        PROCEDURE Forward(resolve: ResolveTypeCallback);
+        PROCEDURE Forward*(resolve: ResolveTypeCallback; closure: Object.PType);
 
-        resolve: ResolveTypeCallback
+        resolve: ResolveTypeCallback;
+        closure: Object.PType;
     END;
 
     PForward = POINTER TO Forward;
@@ -42,15 +43,16 @@ BEGIN
     SELF.mType := type;
 END;
 
-PROCEDURE Forward.Forward(resolve: ResolveTypeCallback)
+PROCEDURE Forward.Forward(resolve: ResolveTypeCallback; closure: Object.PType)
     | SUPER(NIL),
-      resolve(resolve);
+      resolve(resolve),
+      closure(closure);
 END;
 
 PROCEDURE Forward.type(): Types.PStorageType;
 BEGIN
     IF SELF.mType = NIL THEN
-        SELF.mType := SELF.resolve();
+        SELF.mType := SELF.resolve(SELF.closure);
     END;
     RETURN SELF.mType
 END;

+ 3 - 2
src/oberon/oberon_context.js

@@ -4,6 +4,7 @@ var Class = require("rtl.js").Class;
 var CodeGenerator = require("js/CodeGenerator.js");
 var Context = require("context.js");
 var ContextExpression = require("js/ContextExpression.js");
+var ContextDesignator = require("js/ContextDesignator.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var ContextProcedure = require("js/ContextProcedure.js");
 var ContextType = require("js/ContextType.js");
@@ -54,7 +55,7 @@ var ProcedureCall = Context.Chained.extend({
     codeGenerator: function(){return this.__code;},
     type: function(){return this.__type;},
     handleMessage: function(msg){
-        if (msg == Context.beginCallMsg || msg == Context.endCallMsg)
+        if (msg == ContextDesignator.beginCallMsg() || msg == ContextDesignator.endCallMsg())
             return undefined;
         return Context.Chained.prototype.handleMessage.call(this, msg);
     },
@@ -87,7 +88,7 @@ var ExpressionProcedureCall = ProcedureCall.extend({
         this.__hasActualParameters = false;
     },
     handleMessage: function(msg){
-        if (msg == Context.beginCallMsg){
+        if (msg == ContextDesignator.beginCallMsg()){
             this.__hasActualParameters = true;
             return;
         }