Răsfoiți Sursa

js -> eberon transition

Vladislav Folts 10 ani în urmă
părinte
comite
505260a894

BIN
bin/compiled.zip


+ 1 - 0
build.py

@@ -131,6 +131,7 @@ def recompile(bin):
                'ContextIdentdef.ob', 'ContextLoop.ob', 'ContextModule.ob', 'ContextProcedure.ob', 
                'ContextVar.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
                'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
+               'OberonContext.ob', 'OberonContextType.ob', 'OberonContextVar.ob',
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']
     
     result = os.path.join(root, 'bin.recompile')

+ 0 - 10
src/context.js

@@ -1,10 +0,0 @@
-"use strict";
-
-var ContextHierarchy = require("js/ContextHierarchy.js");
-var Class = require("rtl.js").Class;
-
-var ChainedContext = ContextHierarchy.Node;
-ChainedContext.extend = Class.extend;
-ChainedContext.prototype.init = ContextHierarchy.Node;
-
-exports.Chained = ChainedContext;

+ 31 - 27
src/eberon/eberon_context.js

@@ -4,7 +4,6 @@ var Cast = require("js/Cast.js");
 var Class = require("rtl.js").Class;
 var Code = require("js/Code.js");
 var CodeGenerator = require("js/CodeGenerator.js");
-var Context = require("context.js");
 var ContextCase = require("js/ContextCase.js");
 var ContextConst = require("js/ContextConst.js");
 var ContextDesignator = require("js/ContextDesignator.js");
@@ -55,9 +54,13 @@ function MethodOrProcMsg(id, type){
     this.type = type;
 }
 
-var ProcOrMethodId = Context.Chained.extend({
+var ChainedContext = ContextHierarchy.Node;
+ChainedContext.extend = Class.extend;
+ChainedContext.prototype.init = ContextHierarchy.Node;
+
+var ProcOrMethodId = ChainedContext.extend({
     init: function EberonContext$ProcOrMethodId(parent){
-        Context.Chained.prototype.init.call(this, parent);
+        ChainedContext.prototype.init.call(this, parent);
         this.__maybeTypeId = undefined;
         this.__type = undefined;
     },
@@ -84,9 +87,9 @@ var ProcOrMethodId = Context.Chained.extend({
     }
 });
 
-var MethodHeading = Context.Chained.extend({
+var MethodHeading = ChainedContext.extend({
     init: function EberonContext$MethodHeading(parent){
-        Context.Chained.prototype.init.call(this, parent);
+        ChainedContext.prototype.init.call(this, parent);
         this.__id = undefined;
         this.__type = undefined;
     },
@@ -283,9 +286,9 @@ var Designator = Class.extend.call(ContextDesignator.Type, {
         return ContextDesignator.Type.prototype.doMakeDerefVar.call(this, info);
     },
     handleMessage: function(msg){
-        if (msg == ContextDesignator.beginCallMsg())
+        if (msg instanceof ContextDesignator.BeginCallMsg)
             return this.__beginCall();
-        if (msg == ContextDesignator.endCallMsg())
+        if (msg instanceof ContextDesignator.EndCallMsg)
             return this.__endCall();
         if (msg instanceof OperatorNewMsg){
             var e = msg.expression;
@@ -343,9 +346,9 @@ var Designator = Class.extend.call(ContextDesignator.Type, {
     }
 });
 
-var OperatorNew = Context.Chained.extend({
+var OperatorNew = ChainedContext.extend({
     init: function EberonContext$OperatorNew(parent){
-        Context.Chained.prototype.init.call(this, parent);
+        ChainedContext.prototype.init.call(this, parent);
         this.__info = undefined;
         this.__call = undefined;
     },
@@ -367,26 +370,26 @@ var OperatorNew = Context.Chained.extend({
         this.__call.handleArgument(e);
     },
     handleMessage: function(msg){
-        if (msg == ContextDesignator.beginCallMsg()){
+        if (msg instanceof ContextDesignator.BeginCallMsg){
             this.__call = makeContextCall(
                 this,
                 function(cx){ return EberonConstructor.makeConstructorCall(this.__info, cx, true); }.bind(this)
                 );
             return;
         }
-        if (msg == ContextDesignator.endCallMsg())
+        if (msg instanceof ContextDesignator.EndCallMsg)
             return;
 
-        return Context.Chained.prototype.handleMessage.call(this, msg);
+        return ChainedContext.prototype.handleMessage.call(this, msg);
     },
     endParse: function(){
         this.handleMessage(new OperatorNewMsg(this.__call.end()));
     }
 });
 
-var InPlaceVariableInit = Context.Chained.extend({
+var InPlaceVariableInit = ChainedContext.extend({
     init: function EberonContext$InPlaceVariableInit(context){
-        Context.Chained.prototype.init.call(this, context);
+        ChainedContext.prototype.init.call(this, context);
         this.__id = undefined;
         this._symbol = undefined;
         this._code = undefined;
@@ -447,9 +450,10 @@ var InPlaceVariableInitFor = InPlaceVariableInit.extend({
     }
 });
 
-var ExpressionProcedureCall = Context.Chained.extend({
+var ExpressionProcedureCall = ChainedContext.extend({
     init: function EberonContext$init(context){
-        Context.Chained.prototype.init.call(this, context);
+        ChainedContext.prototype.init.call(this, context);
+        this.attributes = {};
     },
     endParse: function(){
         var parent = this.parent();
@@ -466,9 +470,9 @@ var ExpressionProcedureCall = Context.Chained.extend({
     }
 });
 
-var AssignmentOrProcedureCall = Context.Chained.extend({
+var AssignmentOrProcedureCall = ChainedContext.extend({
     init: function EberonContext$init(context){
-        Context.Chained.prototype.init.call(this, context);
+        ChainedContext.prototype.init.call(this, context);
         this.attributes = {};
         this.__right = undefined;
     },
@@ -619,9 +623,9 @@ function InitFieldMsg(id){
     this.id = id;
 }
 
-var BaseInit = Context.Chained.extend({
+var BaseInit = ChainedContext.extend({
     init: function EberonContext$BaseInit(parent){
-        Context.Chained.prototype.init.call(this, parent);
+        ChainedContext.prototype.init.call(this, parent);
         this.__type = undefined;
         this.__initCall = undefined;
         this.__initField = undefined;
@@ -633,9 +637,9 @@ var BaseInit = Context.Chained.extend({
     },
     codeGenerator: function(){return CodeGenerator.nullGenerator();},
     handleMessage: function(msg){
-        if (msg == ContextDesignator.beginCallMsg())
+        if (msg instanceof ContextDesignator.BeginCallMsg)
             return;
-        if (msg == ContextDesignator.endCallMsg()){
+        if (msg instanceof ContextDesignator.EndCallMsg){
             var e = this.__initCall.end();
             if (this.__initField)
                 this.type().setFieldInitializationCode(this.__initField, e.code());
@@ -643,7 +647,7 @@ var BaseInit = Context.Chained.extend({
                 this.type().setBaseConstructorCallCode(e.code());
             return;
         }
-        return Context.Chained.prototype.handleMessage.call(this, msg);
+        return ChainedContext.prototype.handleMessage.call(this, msg);
     },
     handleIdent: function(id){
         this.__initField = id;
@@ -1237,9 +1241,9 @@ var ArrayDimensions = Class.extend.call(ContextType.ArrayDimensions, {
     }
 });
 
-var MapDecl = Context.Chained.extend({
+var MapDecl = ChainedContext.extend({
     init: function EberonContext$MapDecl(context){
-        Context.Chained.prototype.init.call(this, context);
+        ChainedContext.prototype.init.call(this, context);
         this.__type = undefined;
     },
     handleQIdent: function(q){
@@ -1258,9 +1262,9 @@ var MapDecl = Context.Chained.extend({
     }
 });
 
-var ForEach = Context.Chained.extend({
+var ForEach = ChainedContext.extend({
     init: function EberonContext$MapDecl(context){
-        Context.Chained.prototype.init.call(this, context);
+        ChainedContext.prototype.init.call(this, context);
         this.__valueId = undefined;
         this.__keyId = undefined;
         this.__scopeWasCreated = false;

+ 7 - 2
src/ob/ContextDesignator.ob

@@ -47,9 +47,14 @@ TYPE
 
         expressionHandler: ContextExpression.PExpressionHandler;
     END;
+
+    BeginCallMsg* = RECORD(ContextHierarchy.Message)
+    END;
+    EndCallMsg* = RECORD(ContextHierarchy.Message)
+    END;
 VAR
-    beginCallMsg*: ContextHierarchy.Message;
-    endCallMsg*: ContextHierarchy.Message;
+    beginCallMsg: BeginCallMsg;
+    endCallMsg: EndCallMsg;
 
 PROCEDURE Index.Index(length: INTEGER; type: Types.PType; info: Types.PId; code, lval, asProperty: STRING)
     | length(length),

+ 3 - 1
src/ob/ContextHierarchy.ob

@@ -34,7 +34,7 @@ TYPE
         PROCEDURE endParse*(): BOOLEAN;
 
         mParent: PNode;
-        attributes*: Attributes;
+        attributes*: POINTER TO Attributes;
     END;
 
     Root* = RECORD(Node)
@@ -65,6 +65,8 @@ PROCEDURE Node.Node(parent: PNode)
 BEGIN
     IF parent # NIL THEN
         SELF.attributes := parent.attributes;
+    ELSE
+        NEW(SELF.attributes);
     END;
 END;
 

+ 2 - 2
src/ob/ContextType.ob

@@ -51,12 +51,12 @@ TYPE
         id: Context.PIdentdefInfo;
         symbol: Symbols.PSymbol;
     END;
-    PDeclaration = POINTER TO Declaration;
+    PDeclaration* = POINTER TO Declaration;
 
     RecordTypeFactory = PROCEDURE(name, cons: STRING; scope: ScopeBase.PType): R.PType; 
 
     Record* = RECORD(ContextHierarchy.Node)
-        PROCEDURE Record(parent: PDeclaration; factory: RecordTypeFactory);
+        PROCEDURE Record*(parent: PDeclaration; factory: RecordTypeFactory);
 
         PROCEDURE addField(field: Context.PIdentdefInfo; type: Types.PStorageType);
         PROCEDURE setBaseType(type: Types.PType);

+ 2 - 2
src/ob/ContextVar.ob

@@ -5,10 +5,10 @@ IMPORT
 TYPE
     Declaration* = RECORD(ContextType.DeclarationAndIdentHandle)
         PROCEDURE doInitCode(): STRING;
-        PROCEDURE doCheckExport(name: STRING);
+        PROCEDURE doCheckExport*(name: STRING);
 
         idents: ARRAY * OF Context.PIdentdefInfo;
-        type: Types.PStorageType;
+        type-: Types.PStorageType;
     END;
 
 PROCEDURE Declaration.handleIdentdef(id: Context.PIdentdefInfo);

+ 123 - 0
src/oberon/OberonContext.ob

@@ -0,0 +1,123 @@
+MODULE OberonContext;
+IMPORT
+    CodeGenerator, 
+    ContextDesignator, ContextExpression, ContextHierarchy, ContextProcedure, 
+    Expression, Module, Object, Operator, Procedure, Types;
+TYPE
+    ProcedureCall = RECORD(ContextExpression.ExpressionHandler)
+        PROCEDURE ProcedureCall(parent: ContextHierarchy.PNode);
+
+        PROCEDURE do(): Procedure.PCallGenerator;
+        PROCEDURE callExpression(): Expression.PType;
+
+        code: CodeGenerator.PIGenerator;
+        type: Types.PType;
+        id: STRING;
+        call: Procedure.PCallGenerator;
+        cachedCallExpression: Expression.PType;
+    END;
+
+    StatementProcedureCall* = RECORD(ProcedureCall)
+    END;
+
+    ExpressionProcedureCall* = RECORD(ProcedureCall)
+        hasActualParameters: BOOLEAN;
+    END;
+
+    Assignment* = RECORD(ContextExpression.ExpressionHandler)
+    END;
+
+PROCEDURE ProcedureCall.ProcedureCall(parent: ContextHierarchy.PNode)
+    | SUPER(parent),
+      code(CodeGenerator.makeSimpleGenerator());
+BEGIN
+    NEW(SELF.attributes);
+END;
+
+PROCEDURE ProcedureCall.do(): Procedure.PCallGenerator;
+BEGIN
+    IF SELF.call = NIL THEN
+        d <- SELF.attributes.designator;
+        SELF.type := d.type();
+        SELF.id := d.code();
+        SELF.call := ContextProcedure.makeCall(SELF(POINTER), SELF.type, d.info());
+        SELF.cachedCallExpression := NIL;
+    END;
+    RETURN SELF.call;
+END;
+
+PROCEDURE ProcedureCall.codeGenerator(): CodeGenerator.PIGenerator;
+    RETURN SELF.code;
+END;
+
+PROCEDURE ProcedureCall.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
+VAR
+    result: Object.PType;
+BEGIN
+    IF ~(msg IS ContextDesignator.BeginCallMsg)
+        & ~(msg IS ContextDesignator.EndCallMsg) THEN
+        result := SUPER(msg);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE ProcedureCall.handleExpression(e: Expression.PType);
+BEGIN
+    SELF.do().handleArgument(e);
+END;
+
+PROCEDURE ProcedureCall.callExpression(): Expression.PType;
+BEGIN
+    IF SELF.cachedCallExpression = NIL THEN
+        e <- SELF.do().end();
+        SELF.cachedCallExpression := NEW Expression.Type(SELF.id + e.code(), e.type(), NIL, e.constValue(), e.maxPrecedence());
+    END;
+    RETURN SELF.cachedCallExpression;
+END;
+
+PROCEDURE StatementProcedureCall.endParse(): BOOLEAN;
+BEGIN
+    e <- SELF.callExpression();
+    Module.assertProcStatementResult(e.type());
+    SELF.parent().codeGenerator().write(e.code());
+    RETURN TRUE;
+END;
+
+PROCEDURE ExpressionProcedureCall.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
+VAR
+    result: Object.PType;
+BEGIN
+    IF msg IS ContextDesignator.BeginCallMsg THEN
+        SELF.hasActualParameters := TRUE;
+    ELSE
+        result := SUPER(msg);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE ExpressionProcedureCall.endParse(): BOOLEAN;
+VAR
+    e: Expression.PType;
+BEGIN
+    IF SELF.hasActualParameters THEN
+        e := SELF.callExpression();
+    ELSE
+        e := ContextExpression.designatorAsExpression(SELF.attributes.designator);
+    END;
+    SELF.parent()^(ContextExpression.ExpressionHandler).handleExpression(e);
+    RETURN TRUE;
+END;
+
+PROCEDURE Assignment.codeGenerator(): CodeGenerator.PIGenerator;
+    RETURN CodeGenerator.nullGenerator;
+END;
+
+PROCEDURE Assignment.handleExpression(e: Expression.PType);
+BEGIN
+    d <- SELF.attributes.designator;
+    left <- Expression.make(d.code(), d.type(), d, NIL);
+    SELF.parent().codeGenerator().write(
+            Operator.assign(left, e, ContextHierarchy.makeLanguageContext(SELF(POINTER))));
+END;
+
+END OberonContext.

+ 17 - 0
src/oberon/OberonContextType.ob

@@ -0,0 +1,17 @@
+MODULE OberonContextType;
+IMPORT
+    ContextType, R := Record, ScopeBase;
+TYPE
+    Record* = RECORD(ContextType.Record)
+        PROCEDURE Record*(parent: ContextType.PDeclaration);
+    END;
+
+PROCEDURE recordTypeFactory(name, cons: STRING; scope: ScopeBase.PType): R.PType;
+    RETURN NEW R.Type(name, cons, scope);
+END;
+
+PROCEDURE Record.Record(parent: ContextType.PDeclaration)
+    | SUPER(parent, recordTypeFactory);
+END;
+
+END OberonContextType.

+ 16 - 0
src/oberon/OberonContextVar.ob

@@ -0,0 +1,16 @@
+MODULE OberonContextVar;
+IMPORT
+    ContextVar, Errors, Types;
+TYPE
+    Declaration* = RECORD(ContextVar.Declaration)
+    END;
+
+PROCEDURE Declaration.doCheckExport(id: STRING);
+BEGIN
+    type <- SELF.type;
+    IF (type IS Types.PRecord) OR (type IS Types.PArray) THEN
+        Errors.raise("variable '" + id + "' cannot be exported: only scalar variables can be exported");
+    END;
+END;
+
+END OberonContextVar.

+ 0 - 122
src/oberon/oberon_context.js

@@ -1,122 +0,0 @@
-"use strict";
-
-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");
-var ContextVar = require("js/ContextVar.js");
-var Errors = require("js/Errors.js");
-var Expression = require("js/Expression.js");
-var Module = require("js/Module.js");
-var op = require("js/Operator.js");
-var Record = require("js/Record.js");
-var Type = require("js/Types.js");
-
-var RecordDecl = Class.extend.call(ContextType.Record, {
-    init: function OberonContext$RecordDecl(context){
-        ContextType.Record.call(this, context, function(name, cons, scope){return new Record.Type(name, cons, scope); });
-    }
-});
-
-var VariableDeclaration = Class.extend.call(ContextVar.Declaration, {
-    init: function(context){
-        ContextVar.Declaration.call(this, context);
-    },
-    doCheckExport: function(id){
-        var type = this.type;
-        if (type instanceof Type.Record || type instanceof Type.Array)
-            throw new Errors.Error("variable '" + id + "' cannot be exported: only scalar variables can be exported");
-    }
-});
-
-var ProcedureCall = Context.Chained.extend({
-    init: function ProcedureCallContext(context){
-        Context.Chained.prototype.init.call(this, context);
-        this.attributes = {};
-        this.__type = undefined;
-        this.__id = undefined;
-        this.__procCall = undefined;
-        this.__code = CodeGenerator.makeSimpleGenerator();
-    },
-    procCall: function(){
-        if (!this.__procCall){
-            var d = this.attributes.designator;
-            this.__type = d.type();
-            this.__id = d.code();
-            this.__procCall = ContextProcedure.makeCall(this, this.__type, d.info());
-            this.__callExpression = undefined;
-        }
-        return this.__procCall;
-    },
-    codeGenerator: function(){return this.__code;},
-    type: function(){return this.__type;},
-    handleMessage: function(msg){
-        if (msg == ContextDesignator.beginCallMsg() || msg == ContextDesignator.endCallMsg())
-            return undefined;
-        return Context.Chained.prototype.handleMessage.call(this, msg);
-    },
-    handleExpression: function(e){
-        this.procCall().handleArgument(e);
-    },
-    callExpression: function(){
-        if (!this.__callExpression){
-            var e = this.procCall().end();
-            this.__callExpression = new Expression.Type(this.__id + e.code(), e.type(), undefined, e.constValue(), e.maxPrecedence());
-        }
-        return this.__callExpression;
-    }
-});
-
-var StatementProcedureCall = ProcedureCall.extend({
-    init: function StatementProcedureCallContext(context){
-        ProcedureCall.prototype.init.call(this, context);
-    },
-    endParse: function(){
-        var e = this.callExpression();
-        Module.assertProcStatementResult(e.type());
-        this.parent().codeGenerator().write(e.code());
-    }
-});
-
-var ExpressionProcedureCall = ProcedureCall.extend({
-    init: function ExpressionProcedureCall(context){
-        ProcedureCall.prototype.init.call(this, context);
-        this.__hasActualParameters = false;
-    },
-    handleMessage: function(msg){
-        if (msg == ContextDesignator.beginCallMsg()){
-            this.__hasActualParameters = true;
-            return;
-        }
-        return ProcedureCall.prototype.handleMessage.call(this, msg);
-    },
-    endParse: function(){
-        var e = this.__hasActualParameters 
-              ? this.callExpression()
-              : ContextExpression.designatorAsExpression(this.attributes.designator); 
-        this.parent().handleExpression(e);
-    }
-});
-
-var Assignment = Context.Chained.extend({
-    init: function AssignmentContext(context){
-        Context.Chained.prototype.init.call(this, context);
-        this.attributes = {};
-    },
-    codeGenerator: function(){return CodeGenerator.nullGenerator();},
-    handleExpression: function(e){
-        var d = this.attributes.designator;
-        var left = Expression.make(d.code(), d.type(), d);
-        this.parent().codeGenerator().write(op.assign(left, e, ContextHierarchy.makeLanguageContext(this)));
-    }
-});
-
-exports.Assignment = Assignment;
-exports.ExpressionProcedureCall = ExpressionProcedureCall;
-exports.RecordDecl = RecordDecl;
-exports.StatementProcedureCall = StatementProcedureCall;
-exports.VariableDeclaration = VariableDeclaration;

+ 8 - 6
src/oberon/oberon_grammar.js

@@ -13,7 +13,9 @@ var ContextModule = require("js/ContextModule.js");
 var ContextProcedure = require("js/ContextProcedure.js");
 var ContextType = require("js/ContextType.js");
 var Grammar = require("grammar.js");
-var ObContext = require("oberon/oberon_context.js");
+var OberonContext = require("js/OberonContext.js");
+var OberonContextType = require("js/OberonContextType.js");
+var OberonContextVar = require("js/OberonContextVar.js");
 var ObRtl = require("js/OberonRtl.js");
 var ObRtlCode = require("rtl.js");
 var Operator = require("js/Operator.js");
@@ -44,9 +46,9 @@ function makeProcedureHeading(ident, identdef, formalParameters){
 
 function makeAssignmentOrProcedureCall(designator, actualParameters, assignment){
     return or(context(and(designator, assignment), 
-                      ObContext.Assignment),
+                      OberonContext.Assignment),
               context(and(designator, optional(actualParameters)), 
-                      ObContext.StatementProcedureCall)
+                      OberonContext.StatementProcedureCall)
               );
 }
 
@@ -57,7 +59,7 @@ function makeIdentdef(ident){
 function makeDesignator(ident, qualident, selector, actualParameters){
     var designator = context(and(qualident, repeat(selector)), ContextDesignator.Type);
     return { 
-        factor: context(and(designator, optional(actualParameters)), ObContext.ExpressionProcedureCall),
+        factor: context(and(designator, optional(actualParameters)), OberonContext.ExpressionProcedureCall),
         assignmentOrProcedureCall: function(assignment){
             return makeAssignmentOrProcedureCall(designator, actualParameters, assignment);
         }
@@ -116,8 +118,8 @@ exports.language = {
         {
             constDeclaration:   ContextConst.Type, 
             typeDeclaration:    ContextType.Declaration,
-            recordDecl:         ObContext.RecordDecl,
-            variableDeclaration: ObContext.VariableDeclaration,
+            recordDecl:         OberonContextType.Record,
+            variableDeclaration: OberonContextVar.Declaration,
             ArrayDecl:          ContextType.Array,
             Factor:             ContextExpression.Factor,
             FormalParameters:   ContextProcedure.FormalParameters,