Quellcode durchsuchen

js -> eberon transition

Vladislav Folts vor 10 Jahren
Ursprung
Commit
74f12fd45a

BIN
bin/compiled.zip


+ 1 - 1
build.py

@@ -130,7 +130,7 @@ def recompile(bin):
     sources = ['ContextAssignment.ob', 'ContextCase.ob', 'ContextConst.ob', 
                'ContextIdentdef.ob', 'ContextLoop.ob', 'ContextModule.ob', 'ContextProcedure.ob', 
                'ContextVar.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
-               'EberonContextExpression.ob', 'EberonContextType.ob', 'EberonOperator.ob', 'EberonScope.ob',
+               'EberonContextExpression.ob', 'EberonContextIdentdef.ob', 'EberonContextType.ob', 'EberonScope.ob',
                'OberonContext.ob', 'OberonContextType.ob', 'OberonContextVar.ob',
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']
     

+ 1 - 1
src/eberon/EberonContext.ob

@@ -3,7 +3,7 @@ IMPORT
     Context, Errors;
 TYPE
     IdentdefInfo* = RECORD(Context.IdentdefInfo)
-        PROCEDURE IdentdefInfo(id: STRING; exported: BOOLEAN; ro: BOOLEAN);
+        PROCEDURE IdentdefInfo*(id: STRING; exported: BOOLEAN; ro: BOOLEAN);
 
         PROCEDURE isReadOnly*(): BOOLEAN;
         

+ 21 - 0
src/eberon/EberonContextIdentdef.ob

@@ -0,0 +1,21 @@
+MODULE EberonContextIdentdef;
+IMPORT
+    Context, ContextIdentdef, EberonContext;
+TYPE
+    Type* = RECORD(ContextIdentdef.Type)
+        ro: BOOLEAN;
+    END;
+
+PROCEDURE Type.handleLiteral(s: STRING);
+BEGIN
+    IF s = "-" THEN
+        SELF.ro := TRUE;
+    END;  
+    SUPER(s);
+END;
+
+PROCEDURE Type.doMakeIdendef(): Context.PIdentdefInfo;
+    RETURN NEW EberonContext.IdentdefInfo(SELF.id, SELF.export, SELF.ro);
+END;
+
+END EberonContextIdentdef.

+ 65 - 5
src/eberon/EberonContextProcedure.ob

@@ -1,7 +1,9 @@
 MODULE EberonContextProcedure;
 IMPORT
-    Chars, Context, ContextExpression, ContextHierarchy, ContextProcedure, ContextType, 
-    EberonConstructor, EberonContext, EberonContextDesignator, EberonRecord, EberonTypePromotion, EberonTypes,
+    Chars, CodeGenerator,
+    Context, ContextDesignator, ContextExpression, ContextHierarchy, ContextProcedure, ContextType, 
+    EberonConstructor, EberonContext, EberonContextDesignator, 
+    EberonRecord, EberonTypePromotion, EberonTypes,
     Errors, Expression, LanguageContext, Object, Procedure, Types;
 TYPE
     ProcOrMethodDeclaration* = RECORD(ContextProcedure.Declaration)
@@ -23,12 +25,18 @@ TYPE
         type: EberonRecord.PRecord;
     END;
 
-    GetConstructorBoundTypeMsg* = RECORD(ContextHierarchy.Message)
+    BaseInit* = RECORD(ContextExpression.ExpressionHandler)
+        initCall: Procedure.PCallGenerator;
+        initField: STRING;
+        typeOnDemand: EberonRecord.PRecord;
     END;
-    GetConstructorSuperMsg* = RECORD(ContextHierarchy.Message)
+
+    GetConstructorBoundTypeMsg = RECORD(ContextHierarchy.Message)
+    END;
+    GetConstructorSuperMsg = RECORD(ContextHierarchy.Message)
     END;
 
-    InitFieldMsg* = RECORD(ContextHierarchy.Message)
+    InitFieldMsg = RECORD(ContextHierarchy.Message)
         PROCEDURE InitFieldMsg(id: STRING);
 
         id: STRING;
@@ -294,6 +302,58 @@ BEGIN
     void <- SELF.handleMessage(NEW MethodOrProcMsg(id, SELF.type)^);
 END;
 
+PROCEDURE baseInitType(VAR b: BaseInit): EberonRecord.PRecord;
+BEGIN
+    IF b.typeOnDemand = NIL THEN
+        b.typeOnDemand := b.handleMessage(NEW GetConstructorBoundTypeMsg()^)(EberonRecord.PRecord);
+    END;
+    RETURN b.typeOnDemand;
+END;
+
+PROCEDURE BaseInit.codeGenerator(): CodeGenerator.PIGenerator;
+    RETURN CodeGenerator.nullGenerator;
+END;
+
+PROCEDURE BaseInit.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
+VAR
+    result: Object.PType;
+BEGIN
+    IF msg IS ContextDesignator.BeginCallMsg THEN
+    ELSIF msg IS ContextDesignator.EndCallMsg THEN
+        e <- SELF.initCall.end();
+        IF LEN(SELF.initField) # 0 THEN
+            baseInitType(SELF).setFieldInitializationCode(SELF.initField, e.code());
+        ELSE
+            baseInitType(SELF).setBaseConstructorCallCode(e.code());
+        END;
+    ELSE
+        result := SUPER(msg);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE BaseInit.handleIdent(id: STRING);
+BEGIN
+    SELF.initField := id;
+    SELF.initCall := SELF.handleMessage(NEW InitFieldMsg(id)^)(Procedure.PCallGenerator);
+END;
+
+PROCEDURE BaseInit.handleExpression(e: Expression.PType);
+BEGIN
+    SELF.initCall.handleArgument(e);
+END;
+
+PROCEDURE BaseInit.handleLiteral(s: STRING);
+BEGIN
+    IF s = "SUPER" THEN
+        ms <- SELF.handleMessage(NEW GetConstructorSuperMsg()^);
+        SELF.initCall := EberonConstructor.makeBaseConstructorCall(
+            baseInitType(SELF).base(EberonRecord.PRecord), 
+            ContextHierarchy.makeLanguageContext(SELF(POINTER))
+            );
+    END;
+END;
+
 PROCEDURE InitFieldMsg.InitFieldMsg(id: STRING)
     | id(id);
 END;

+ 44 - 1
src/eberon/EberonContextType.ob

@@ -2,9 +2,11 @@ MODULE EberonContextType;
 IMPORT
     Chars,
     Context, ContextHierarchy, ContextProcedure, ContextType, 
-    EberonContext, EberonRecord, EberonTypes,
+    EberonContext, EberonDynamicArray, EberonRecord, EberonTypes,
     Errors,
     Object, Procedure, R := Record, ScopeBase, Types;
+CONST
+    dynamicArrayLength = -1;
 TYPE
     Declaration* = RECORD(ContextType.Declaration)
     END;
@@ -14,6 +16,12 @@ TYPE
         PROCEDURE Record*(parent: PDeclaration);
     END;
 
+    Array* = RECORD(ContextType.Array)
+    END;
+
+    ArrayDimensions* = RECORD(ContextType.ArrayDimensions)
+    END;
+
     MethodHeading* = RECORD(ContextType.DeclarationAndIdentHandle)
         id: EberonContext.PIdentdefInfo;
         type: Procedure.PType;
@@ -105,6 +113,41 @@ BEGIN
     RETURN result;
 END;
 
+PROCEDURE Array.doMakeInit(type: Types.PStorageType; dimensions: STRING; length: INTEGER): STRING;
+VAR
+    result: STRING;
+BEGIN
+    IF length = dynamicArrayLength THEN
+        result := "[]";
+    ELSIF (type IS EberonRecord.PRecord) & (EberonRecord.hasParameterizedConstructor(type^)) THEN
+        Errors.raise("cannot use '" + type.description() + "' as an element of static array because it has constructor with parameters");
+    ELSE
+        result := SUPER(type, dimensions, length);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE Array.doMakeType(elementsType: Types.PStorageType; init: STRING; length: INTEGER): Types.PStorageType;
+VAR
+    result: Types.PStorageType;
+BEGIN
+    IF length = dynamicArrayLength THEN
+        result := NEW EberonDynamicArray.DynamicArray(elementsType);
+    ELSE
+        result := SUPER(elementsType, init, length);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE ArrayDimensions.handleLiteral(s: STRING);
+BEGIN
+    IF s = "*" THEN
+        SELF.doAddDimension(dynamicArrayLength);
+    ELSE
+        SUPER(s);
+    END;
+END;
+
 PROCEDURE MethodHeading.handleIdentdef(id: Context.PIdentdefInfo);
 BEGIN
     SELF.id := id(EberonContext.PIdentdefInfo);

+ 2 - 2
src/eberon/EberonRecord.ob

@@ -23,8 +23,8 @@ TYPE
         PROCEDURE defineConstructor*(type: Procedure.PType);
         PROCEDURE defineMethod*(methodId: Context.PIdentdefInfo; type: EberonTypes.PMethodType);
         PROCEDURE requireNewOnly*();
-        PROCEDURE setBaseConstructorCallCode(code: STRING);
-        PROCEDURE setFieldInitializationCode(field: STRING; code: STRING);
+        PROCEDURE setBaseConstructorCallCode*(code: STRING);
+        PROCEDURE setFieldInitializationCode*(field: STRING; code: STRING);
         PROCEDURE setRecordInitializationCode*(baseConstructorCallCode: STRING);
 
         customConstructor-: Procedure.PType;

+ 0 - 101
src/eberon/eberon_context.js

@@ -65,21 +65,6 @@ var ForEachVariable = Class.extend.call(EberonContextDesignator.TypeNarrowVariab
     idType: function(){return "FOR variable";}
 });
 
-var Identdef = Class.extend.call(ContextIdentdef.Type, {
-    init: function(parent){
-        ContextIdentdef.Type.call(this, parent);
-        this.__ro = false;
-    },
-    handleLiteral: function(l){
-        if (l == "-")
-            this.__ro = true;  
-        ContextIdentdef.Type.prototype.handleLiteral.call(this, l);
-    },
-    doMakeIdendef: function(){
-        return new EberonContext.IdentdefInfo(this.id, this.export$, this.__ro);
-    }
-});
-
 function makeContextCall(context, call){
     return call(ContextHierarchy.makeLanguageContext(context));
     }
@@ -267,54 +252,6 @@ var VariableDeclaration = Class.extend.call(ContextVar.Declaration, {
     }
 });
 
-var BaseInit = ChainedContext.extend({
-    init: function EberonContext$BaseInit(parent){
-        ChainedContext.prototype.init.call(this, parent);
-        this.__type = undefined;
-        this.__initCall = undefined;
-        this.__initField = undefined;
-    },
-    type: function(){
-        if (!this.__type)
-            this.__type = this.handleMessage(new EberonContextProcedure.GetConstructorBoundTypeMsg());
-        return this.__type;
-    },
-    codeGenerator: function(){return CodeGenerator.nullGenerator();},
-    handleMessage: function(msg){
-        if (msg instanceof ContextDesignator.BeginCallMsg)
-            return;
-        if (msg instanceof ContextDesignator.EndCallMsg){
-            var e = this.__initCall.end();
-            if (this.__initField)
-                this.type().setFieldInitializationCode(this.__initField, e.code());
-            else
-                this.type().setBaseConstructorCallCode(e.code());
-            return;
-        }
-        return ChainedContext.prototype.handleMessage.call(this, msg);
-    },
-    handleIdent: function(id){
-        this.__initField = id;
-        this.__initCall = this.handleMessage(new EberonContextProcedure.InitFieldMsg(id));
-    },
-    handleExpression: function(e){
-        this.__initCall.handleArgument(e);
-    },
-    handleLiteral: function(s){
-        if (s == "SUPER"){
-            var ms = this.handleMessage(new EberonContextProcedure.GetConstructorSuperMsg());
-            this.__initCall = makeContextCall(
-                this,
-                function(cx){ 
-                    return EberonConstructor.makeBaseConstructorCall(
-                        this.type().base, 
-                        cx);
-                    }.bind(this)
-                );
-        }
-    }
-});
-
 var OperatorScopes = Class.extend({
     init: function EberonContext$OperatorScopes(context){
         this.__context = context;
@@ -473,20 +410,6 @@ var For = Class.extend.call(ContextLoop.For, {
     }
 });
 
-var dynamicArrayLength = -1;
-
-var ArrayDimensions = Class.extend.call(ContextType.ArrayDimensions, {
-    init: function EberonContext$ArrayDimensions(context){
-        ContextType.ArrayDimensions.call(this, context);
-    },
-    handleLiteral: function(s){
-        if ( s == "*" )
-            this.doAddDimension(dynamicArrayLength);
-        else
-            ContextType.ArrayDimensions.prototype.handleLiteral.call(this, s);
-    }
-});
-
 var MapDecl = ChainedContext.extend({
     init: function EberonContext$MapDecl(context){
         ChainedContext.prototype.init.call(this, context);
@@ -560,26 +483,6 @@ var ForEach = ChainedContext.extend({
     }
 });
 
-var ArrayDecl = Class.extend.call(ContextType.Array, {
-    init: function EberonContext$ArrayDecl(context){
-        ContextType.Array.call(this, context);
-    },
-    doMakeInit: function(type, dimensions, length){
-        if (length == dynamicArrayLength)
-            return '[]';
-
-        if (type instanceof EberonRecord.Record && EberonRecord.hasParameterizedConstructor(type))
-            throw new Errors.Error("cannot use '" + Type.typeName(type) + "' as an element of static array because it has constructor with parameters");
-
-        return ContextType.Array.prototype.doMakeInit.call(this, type, dimensions, length);
-    },
-    doMakeType: function(elementsType, init, length){
-        return length == dynamicArrayLength
-            ? new EberonDynamicArray.DynamicArray(elementsType)
-            : ContextType.Array.prototype.doMakeType.call(this, elementsType, init, length);
-    }
-});
-
 function assertArgumentIsNotNonVarDynamicArray(msg){
     if (msg instanceof ContextProcedure.AddArgumentMsg){
         var arg = msg.arg;
@@ -662,9 +565,6 @@ var ModuleDeclaration = Class.extend.call(ContextModule.Declaration, {
     }
 });
 
-exports.ArrayDecl = ArrayDecl;
-exports.ArrayDimensions = ArrayDimensions;
-exports.BaseInit = BaseInit;
 exports.CaseLabel = CaseLabel;
 exports.ConstDecl = ConstDecl;
 exports.ExpressionProcedureCall = ExpressionProcedureCall;
@@ -673,7 +573,6 @@ exports.ForEach = ForEach;
 exports.FormalParameters = FormalParameters;
 exports.FormalParametersProcDecl = FormalParametersProcDecl;
 exports.FormalType = FormalType;
-exports.Identdef = Identdef;
 exports.If = If;
 exports.ModuleDeclaration = ModuleDeclaration;
 exports.AssignmentOrProcedureCall = AssignmentOrProcedureCall;

+ 5 - 4
src/eberon/eberon_grammar.js

@@ -7,6 +7,7 @@ var ContextType = require("js/ContextType.js");
 var EbContext = require("eberon/eberon_context.js");
 var EberonContextDesignator = require("js/EberonContextDesignator.js");
 var EberonContextExpression = require("js/EberonContextExpression.js");
+var EberonContextIdentdef = require("js/EberonContextIdentdef.js");
 var EberonContextProcedure = require("js/EberonContextProcedure.js");
 var EberonContextType = require("js/EberonContextType.js");
 var Grammar = require("grammar.js");
@@ -55,7 +56,7 @@ function makeAssignmentOrProcedureCall(ident, designator, assignment, expression
 }
 
 function makeIdentdef(ident){
-    return context(and(ident, optional(or("*", "-"))), EbContext.Identdef);
+    return context(and(ident, optional(or("*", "-"))), EberonContextIdentdef.Type);
 }
 
 function makeDesignator(ident, qualident, selector, actualParameters){
@@ -106,7 +107,7 @@ function makeForInit(ident, expression, assignment){
 function makeArrayDimensions(constExpression){
     var oneDimension = or("*", constExpression);
     return context(and(oneDimension, repeat(and(",", oneDimension))), 
-                   EbContext.ArrayDimensions);
+                   EberonContextType.ArrayDimensions);
 }
 
 function makeFormalArray(){
@@ -119,7 +120,7 @@ function makeFormalResult(base, ident, actualParameters){
     return or(base, 
               context(and("|", or(and("SUPER", actualParameters, followingFields),
                                   and(initField, followingFields))), 
-                      EbContext.BaseInit));
+                      EberonContextProcedure.BaseInit));
 }
 
 function makeReturn(base){
@@ -146,7 +147,7 @@ exports.language = {
             typeDeclaration:    EberonContextType.Declaration,
             recordDecl:         EberonContextType.Record,
             variableDeclaration: EbContext.VariableDeclaration,
-            ArrayDecl:          EbContext.ArrayDecl,
+            ArrayDecl:          EberonContextType.Array,
             Factor:             EberonContextExpression.Factor,
             FormalParameters:   EbContext.FormalParameters,
             FormalType:         EbContext.FormalType,

+ 4 - 4
src/ob/ContextIdentdef.ob

@@ -4,12 +4,12 @@ IMPORT
     Module, Record, TypeId;
 TYPE
     Type* = RECORD(ContextHierarchy.Node)
-        PROCEDURE Type(parent: ContextType.PDeclarationAndIdentHandle);
-        PROCEDURE doMakeIdendef(): Context.PIdentdefInfo;
+        PROCEDURE Type*(parent: ContextType.PDeclarationAndIdentHandle);
+        PROCEDURE doMakeIdendef*(): Context.PIdentdefInfo;
 
         parentDecl: ContextType.PDeclarationAndIdentHandle;
-        id: STRING;
-        export: BOOLEAN;
+        id-: STRING;
+        export-: BOOLEAN;
     END;
 
     Qualified* = RECORD(ContextHierarchy.Node)

+ 5 - 5
src/ob/ContextType.ob

@@ -18,11 +18,11 @@ TYPE
     END;
 
     Array* = RECORD(HandleSymbolAsType)
-        PROCEDURE Array(parent: PHandleSymbolAsType);
+        PROCEDURE Array*(parent: PHandleSymbolAsType);
 
         PROCEDURE isAnonymousDeclaration(): BOOLEAN;
-        PROCEDURE doMakeInit(type: Types.PStorageType; dimensions: STRING; length: INTEGER): STRING;
-        PROCEDURE doMakeType(elementsType: Types.PType; init: STRING; length: INTEGER): Types.PStorageType;
+        PROCEDURE doMakeInit*(type: Types.PStorageType; dimensions: STRING; length: INTEGER): STRING;
+        PROCEDURE doMakeType*(elementsType: Types.PStorageType; init: STRING; length: INTEGER): Types.PStorageType;
 
         handleType: PHandleSymbolAsType;
         dimensions: POINTER TO ArrayDimensions;
@@ -30,7 +30,7 @@ TYPE
     END;
 
     ArrayDimensions* = RECORD(ContextExpression.ExpressionHandler) 
-        PROCEDURE doAddDimension(size: INTEGER);
+        PROCEDURE doAddDimension*(size: INTEGER);
 
         dimensions: ARRAY * OF INTEGER;
     END;
@@ -182,7 +182,7 @@ BEGIN
     RETURN result;
 END;
 
-PROCEDURE Array.doMakeType(elementsType: Types.PType; init: STRING; length: INTEGER): Types.PStorageType;
+PROCEDURE Array.doMakeType(elementsType: Types.PStorageType; init: STRING; length: INTEGER): Types.PStorageType;
     RETURN SELF.root().language().types.makeStaticArray(elementsType, init, length);
 END;