Quellcode durchsuchen

js -> eberon transition

Vladislav Folts vor 10 Jahren
Ursprung
Commit
d711586723
9 geänderte Dateien mit 128 neuen und 91 gelöschten Zeilen
  1. BIN
      bin/compiled.zip
  2. 1 1
      build.py
  3. 0 65
      src/context.js
  4. 8 7
      src/eberon/eberon_context.js
  5. 2 1
      src/grammar.js
  6. 38 0
      src/ob/ContextConst.ob
  7. 75 14
      src/ob/ContextType.ob
  8. 1 1
      src/ob/Types.ob
  9. 3 2
      src/oberon/oberon_grammar.js

BIN
bin/compiled.zip


+ 1 - 1
build.py

@@ -127,7 +127,7 @@ def run_tests(bin, unit_test=None, code_test=None):
 def recompile(bin):
     print('recompile oberon sources using "%s"...' % bin)
     compiler = os.path.join(root, 'src', 'oc_nodejs.js')
-    sources = ['ContextDesignator', 'ContextType.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
+    sources = ['ContextConst.ob', 'ContextDesignator', 'ContextType.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
                'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']
     

+ 0 - 65
src/context.js

@@ -36,16 +36,6 @@ var ChainedContext = ContextHierarchy.Node;
 ChainedContext.extend = Class.extend;
 ChainedContext.prototype.init = ContextHierarchy.Node;
 
-exports.BaseType = ChainedContext.extend({
-    init: function BaseTypeContext(context){
-        ChainedContext.prototype.init.call(this, context);
-    },
-    handleQIdent: function(q){
-        var s = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
-        this.parent().setBaseType(ContextExpression.unwrapType(s.symbol().info()));
-    }
-});
-
 exports.QualifiedIdentificatorModule = ChainedContext.extend({
     init: function QualifiedIdentificatorModule(context){
         ChainedContext.prototype.init.call(this, context);
@@ -655,31 +645,6 @@ exports.CheckAssignment = ChainedContext.extend({
     }
 });
 
-exports.ConstDecl = ChainedContext.extend({
-    init: function ConstDeclContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__id = undefined;
-        this.__type = undefined;
-        this.__value = undefined;
-    },
-    handleIdentdef: function(id){
-        this.__id = id;
-        this.codeGenerator().write("var " + id.id() + " = ");
-    },
-    handleExpression: function(e){
-        var value = e.constValue();
-        if (!value)
-            throw new Errors.Error("constant expression expected");
-        this.__type = e.type();
-        this.__value = value;
-    },
-    endParse: function(){
-        var c = new Type.Const(this.__type, this.__value);
-        this.root().currentScope().addSymbol(new Symbol.Symbol(this.__id.id(), c), this.__id.exported());
-        this.codeGenerator().write(";\n");
-    }
-});
-
 exports.VariableDeclaration = HandleSymbolAsType.extend({
     init: function Context$VariableDeclaration(context){
         HandleSymbolAsType.prototype.init.call(this, context);
@@ -754,36 +719,6 @@ exports.ActualParameters = ChainedContext.extend({
     }
 });
 
-exports.TypeDeclaration = ChainedContext.extend({
-    init: function TypeDeclarationContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__id = undefined;
-        this.__symbol = undefined;
-    },
-    handleIdentdef: function(id){
-        var typeId = new TypeId.Lazy();
-        var symbol = new Symbol.Symbol(id.id(), typeId);
-        var scope = this.root().currentScope();
-        scope.addSymbol(symbol, id.exported());
-        if (!id.exported())
-            scope.addFinalizer(function(){Record.stripTypeId(typeId);});
-        this.__id = id;
-        this.__symbol = symbol;
-    },
-    setType: function(type){
-        TypeId.define(this.__symbol.info(), type);
-        Scope.resolve(this.root().currentScope(), this.__symbol);
-    },
-    typeName: function(){return this.__id.id();},
-    id: function(){return this.__id;},
-    genTypeName: function(){return this.__id.id();},
-    isAnonymousDeclaration: function(){return false;},
-    type: function(){return this.parent().type();},
-    exportField: function(name){
-        ContextType.checkIfFieldCanBeExported(name, [this.__id], "record");
-    }
-});
-
 exports.TypeSection = ChainedContext.extend({
     init: function TypeSection(context){
         ChainedContext.prototype.init.call(this, context);

+ 8 - 7
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 ContextConst = require("js/ContextConst.js");
 var ContextDesignator = require("js/ContextDesignator.js");
 var ContextExpression = require("js/ContextExpression.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
@@ -504,13 +505,13 @@ function checkOrdinaryExport(id, hint){
         throw new Errors.Error(hint + " cannot be exported as read-only using '-' mark (did you mean '*'?)");
 }
 
-var ConstDecl = Context.ConstDecl.extend({
+var ConstDecl = Class.extend.call(ContextConst.Type, {
     init: function EberonContext$ConstDecl(context){
-        Context.ConstDecl.prototype.init.call(this, context);
+        ContextConst.Type.call(this, context);
     },
     handleIdentdef: function(id){
         checkOrdinaryExport(id, "constant");
-        Context.ConstDecl.prototype.handleIdentdef.call(this, id);
+        ContextConst.Type.prototype.handleIdentdef.call(this, id);
     }
 });
 
@@ -530,13 +531,13 @@ var VariableDeclaration = Context.VariableDeclaration.extend({
     }
 });
 
-var TypeDeclaration = Context.TypeDeclaration.extend({
+var TypeDeclaration = Class.extend.call(ContextType.Declaration, {
     init: function EberonContext$TypeDeclaration(context){
-        Context.TypeDeclaration.prototype.init.call(this, context);
+        ContextType.Declaration.call(this, context);
     },
     handleIdentdef: function(id){
         checkOrdinaryExport(id, "type");
-        Context.TypeDeclaration.prototype.handleIdentdef.call(this, id);
+        ContextType.Declaration.prototype.handleIdentdef.call(this, id);
     }
 });
 
@@ -551,7 +552,7 @@ var RecordDecl = Class.extend.call(ContextType.Record, {
             var id = msg.id.id();
             if (Type.typeName(boundType) == id){
                 if (msg.id.exported()){
-                    var typeId = this.parent().id();
+                    var typeId = this.parent().id;
                     if (!typeId.exported())
                         throw new Errors.Error("constructor '" + id + "' cannot be exported because record itslef is not exported");
                 }

+ 2 - 1
src/grammar.js

@@ -3,6 +3,7 @@
 var Context = require("context.js");
 var ContextDesignator = require("js/ContextDesignator.js");
 var ContextExpression = require("js/ContextExpression.js");
+var ContextType = require("js/ContextType.js");
 var Lexer = require("js/Lexer.js");
 var Parser = require("parser.js");
 var Class = require("rtl.js").Class;
@@ -168,7 +169,7 @@ var arrayType = and("ARRAY",
                     context(and(makeArrayDimensions(constExpression), "OF", type), 
                             contexts.ArrayDecl));
 
-var baseType = context(qualident, Context.BaseType);
+var baseType = context(qualident, ContextType.RecordBase);
 var recordType = and("RECORD", context(and(optional(and("(", baseType, ")")), optional(fieldListSequence)
                                      , "END"), contexts.recordDecl));
 

+ 38 - 0
src/ob/ContextConst.ob

@@ -0,0 +1,38 @@
+MODULE ContextConst;
+IMPORT
+    Chars, ConstValue, Context, ContextExpression, 
+    Errors, Expression, Symbols, Types;
+TYPE
+    Type* = RECORD(ContextExpression.ExpressionHandler)
+        PROCEDURE handleIdentdef(id: Context.PIdentdefInfo);
+
+        id: Context.PIdentdefInfo;
+        type: Types.PType;
+        value: ConstValue.PType;
+    END;
+
+PROCEDURE Type.handleIdentdef(id: Context.PIdentdefInfo);
+BEGIN
+    SELF.id := id;
+    SELF.codeGenerator().write("var " + id.id() + " = ");
+END;
+
+PROCEDURE Type.handleExpression(e: Expression.PType);
+BEGIN
+    value <- e.constValue();
+    IF value = NIL THEN
+        Errors.raise("constant expression expected");
+    END;
+    SELF.type := e.type();
+    SELF.value := value;
+END;
+
+PROCEDURE Type.endParse(): BOOLEAN;
+BEGIN
+    c <- NEW Types.Const(SELF.type, SELF.value);
+    SELF.root().currentScope().addSymbol(NEW Symbols.Symbol(SELF.id.id(), c), SELF.id.exported());
+    SELF.codeGenerator().write(";" + Chars.ln);
+    RETURN TRUE;
+END;
+
+END ContextConst.

+ 75 - 14
src/ob/ContextType.ob

@@ -1,7 +1,8 @@
 MODULE ContextType;
 IMPORT
     Chars, CodeGenerator, ConstValue, Context, ContextExpression, ContextHierarchy, 
-    Errors, Expression, R := Record, ScopeBase, String, Types;
+    Errors, Expression, Object, R := Record, 
+    Scope, ScopeBase, String, Symbols, TypeId, Types;
 TYPE
     HandleSymbolAsType* = RECORD(ContextHierarchy.Node)
         PROCEDURE handleQIdent(q: ContextHierarchy.QIdent);
@@ -34,9 +35,16 @@ TYPE
         dimensions: ARRAY * OF INTEGER;
     END;
 
-    Declaration = RECORD(HandleSymbolAsType)
+    DeclarationHandle = RECORD(HandleSymbolAsType)
         PROCEDURE isAnonymousDeclaration(): BOOLEAN;
         PROCEDURE exportField(name: STRING);
+        PROCEDURE handleIdentdef(id: Context.PIdentdefInfo);
+        PROCEDURE typeName(): STRING;
+    END;
+
+    Declaration* = RECORD(DeclarationHandle)
+        id: Context.PIdentdefInfo;
+        symbol: Symbols.PSymbol;
     END;
     PDeclaration = POINTER TO Declaration;
 
@@ -60,10 +68,11 @@ TYPE
     END;
     PRecord = POINTER TO Record;
 
-    FieldList* = RECORD(Declaration)
-        PROCEDURE handleIdentdef(id: Context.PIdentdefInfo);
-        PROCEDURE typeName(): STRING;
+    RecordBase* = RECORD(ContextHierarchy.Node)
+        PROCEDURE handleQIdent(q: ContextHierarchy.QIdent);
+    END;
 
+    FieldList* = RECORD(Declaration)
         idents: ARRAY * OF Context.PIdentdefInfo;
         type: Types.PStorageType;
     END;
@@ -208,6 +217,63 @@ BEGIN
     RETURN result;
 END;
 
+PROCEDURE stripTypeId(closure: Object.PType);
+BEGIN
+    typeId <- closure(TypeId.PType);
+    R.stripTypeId(typeId^);
+END;
+
+PROCEDURE checkIfFieldCanBeExported*(name: STRING; idents: ARRAY OF Context.PIdentdefInfo; hint: STRING);
+BEGIN
+    FOR i <- 0 TO LEN(idents) - 1 DO
+        id <- idents[i];
+        IF ~id.exported() THEN
+            Errors.raise(
+                "field '" + name + "' can be exported only if " + hint + " '" +
+                id.id() + "' itself is exported too");
+        END;
+    END;
+END;
+
+PROCEDURE Declaration.handleIdentdef(id: Context.PIdentdefInfo);
+BEGIN
+    typeId <- NEW TypeId.Lazy();
+    symbol <- NEW Symbols.Symbol(id.id(), typeId);
+    scope <- SELF.root().currentScope();
+    scope.addSymbol(symbol, id.exported());
+    IF ~id.exported() THEN
+        scope.addFinalizer(stripTypeId, typeId);
+    END;
+    SELF.id := id;
+    SELF.symbol := symbol;
+END;
+
+PROCEDURE Declaration.setType(type: Types.PStorageType);
+BEGIN
+    TypeId.define(SELF.symbol.info()^(TypeId.Lazy), type);
+    Scope.resolve(SELF.root().currentScope()^, SELF.symbol);
+END;
+
+PROCEDURE Declaration.isAnonymousDeclaration(): BOOLEAN;
+    RETURN FALSE;
+END;
+
+PROCEDURE Declaration.exportField(name: STRING);
+VAR
+    idents: ARRAY 1 OF Context.PIdentdefInfo;
+BEGIN
+    idents[0] := SELF.id;
+    checkIfFieldCanBeExported(name, idents, "record");
+END;
+
+PROCEDURE Declaration.typeName(): STRING;
+    RETURN SELF.id.id();
+END;
+
+PROCEDURE Declaration.genTypeName(): STRING;
+    RETURN SELF.typeName();
+END;
+
 PROCEDURE Record.Record(parent: PDeclaration; factory: RecordTypeFactory)
     | SUPER(parent),
       declaration(parent);
@@ -317,16 +383,11 @@ BEGIN
     RETURN TRUE;
 END;
 
-PROCEDURE checkIfFieldCanBeExported*(name: STRING; idents: ARRAY OF Context.PIdentdefInfo; hint: STRING);
+PROCEDURE RecordBase.handleQIdent(q: ContextHierarchy.QIdent);
 BEGIN
-    FOR i <- 0 TO LEN(idents) - 1 DO
-        id <- idents[i];
-        IF ~id.exported() THEN
-            Errors.raise(
-                "field '" + name + "' can be exported only if " + hint + " '" +
-                id.id() + "' itself is exported too");
-        END;
-    END;
+    s <- ContextHierarchy.getQIdSymbolAndScope(SELF.root()^, q);
+    base <- ContextExpression.unwrapType(s.symbol().info());
+    SELF.parent()^(Record).setBaseType(base);
 END;
 
 PROCEDURE FieldList.isAnonymousDeclaration(): BOOLEAN;

+ 1 - 1
src/ob/Types.ob

@@ -2,7 +2,7 @@ MODULE Types;
 IMPORT
     Chars, ConstValue, Context, Errors, OberonRtl, Object, Str := String;
 TYPE
-    Id* = RECORD
+    Id* = RECORD(Object.Type)
         PROCEDURE idType*(): STRING
     END;
 

+ 3 - 2
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 ContextConst = require("js/ContextConst.js");
 var ContextDesignator = require("js/ContextDesignator.js");
 var ContextExpression = require("js/ContextExpression.js");
 var ContextType = require("js/ContextType.js");
@@ -108,8 +109,8 @@ exports.language = {
         makeFormalResult,
         makeReturn,
         {
-            constDeclaration:   Context.ConstDecl, 
-            typeDeclaration:    Context.TypeDeclaration,
+            constDeclaration:   ContextConst.Type, 
+            typeDeclaration:    ContextType.Declaration,
             recordDecl:         ObContext.RecordDecl,
             variableDeclaration: ObContext.VariableDeclaration,
             ArrayDecl:          ContextType.Array,