浏览代码

js -> eberon transition

Vladislav Folts 10 年之前
父节点
当前提交
ca4daefd4f
共有 10 个文件被更改,包括 105 次插入77 次删除
  1. 二进制
      bin/compiled.zip
  2. 1 1
      build.py
  3. 0 61
      src/context.js
  4. 4 3
      src/eberon/eberon_context.js
  5. 1 1
      src/ob/ContextHierarchy.ob
  6. 80 0
      src/ob/ContextModule.ob
  7. 9 0
      src/ob/LanguageContext.ob
  8. 8 6
      src/ob/Scope.ob
  9. 0 4
      src/ob/Types.ob
  10. 2 1
      src/oberon/oberon_grammar.js

二进制
bin/compiled.zip


+ 1 - 1
build.py

@@ -128,7 +128,7 @@ def recompile(bin):
     print('recompile oberon sources using "%s"...' % bin)
     print('recompile oberon sources using "%s"...' % bin)
     compiler = os.path.join(root, 'src', 'oc_nodejs.js')
     compiler = os.path.join(root, 'src', 'oc_nodejs.js')
     sources = ['ContextAssignment.ob', 'ContextCase.ob', 'ContextConst.ob', 
     sources = ['ContextAssignment.ob', 'ContextCase.ob', 'ContextConst.ob', 
-               'ContextIdentdef', 'ContextLoop', 'ContextProcedure.ob', 
+               'ContextIdentdef.ob', 'ContextLoop.ob', 'ContextModule.ob', 'ContextProcedure.ob', 
                'ContextVar.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
                'ContextVar.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
                'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
                'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']

+ 0 - 61
src/context.js

@@ -38,67 +38,6 @@ var ChainedContext = ContextHierarchy.Node;
 ChainedContext.extend = Class.extend;
 ChainedContext.extend = Class.extend;
 ChainedContext.prototype.init = ContextHierarchy.Node;
 ChainedContext.prototype.init = ContextHierarchy.Node;
 
 
-exports.ModuleDeclaration = ChainedContext.extend({
-    init: function ModuleDeclarationContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__name = undefined;
-        this.__imports = {};
-        this.__moduleScope = undefined;
-        this.__moduleGen = undefined;
-        this.__stdSymbols = this.root().language().stdSymbols;
-    },
-    handleIdent: function(id){
-        if (this.__name === undefined ) {
-            this.__name = id;
-            this.__moduleScope = new Scope.Module(id, this.__stdSymbols);
-            this.root().pushScope(this.__moduleScope);
-        }
-        else if (id === this.__name){
-            var scope = this.root().currentScope();
-            scope.close();
-            var exports = scope.exports;
-            Scope.defineExports(Scope.moduleSymbol(scope).info(), exports);
-            this.codeGenerator().write(this.__moduleGen.epilog(exports));
-        }
-        else
-            throw new Errors.Error("original module name '" + this.__name + "' expected, got '" + id + "'" );
-    },
-    findModule: function(name){
-        if (name == this.__name)
-            throw new Errors.Error("module '" + this.__name + "' cannot import itself");
-        return this.parent().findModule(name);
-    },
-    handleImport: function(modules){
-        var scope = this.root().currentScope();
-        var moduleAliases = {};
-        for(var i = 0; i < modules.length; ++i){
-            var s = modules[i];
-            var name = Type.moduleName(s.info());
-            this.__imports[name] = s;
-            scope.addSymbol(s);
-            moduleAliases[name] = s.id();
-        }
-        this.__moduleGen = this.root().language().moduleGenerator(
-                this.__name,
-                moduleAliases);
-        this.codeGenerator().write(this.__moduleGen.prolog());
-    },
-    handleLiteral: function(){},
-    qualifyScope: function(scope){
-        if (scope != this.__moduleScope && scope instanceof Scope.Module){
-            var id = Scope.moduleSymbol(scope).id();
-            
-            // implicitly imported module, e.g.: record.pointerToRecordFromAnotherModule.field
-            // should not be used in code generation, 
-            // just return non-empty value to indicate this is not current module
-            if (!(id in this.__imports))
-                return "module '" + id + "' is not imported";
-            return this.__imports[id].id() + ".";
-        }
-        return "";
-    }
-});
-
 var ModuleImport = ChainedContext.extend({
 var ModuleImport = ChainedContext.extend({
     init: function ModuleImport(context){
     init: function ModuleImport(context){
         ChainedContext.prototype.init.call(this, context);
         ChainedContext.prototype.init.call(this, context);

+ 4 - 3
src/eberon/eberon_context.js

@@ -12,6 +12,7 @@ var ContextExpression = require("js/ContextExpression.js");
 var ContextIdentdef = require("js/ContextIdentdef.js");
 var ContextIdentdef = require("js/ContextIdentdef.js");
 var ContextIf = require("js/ContextIf.js");
 var ContextIf = require("js/ContextIf.js");
 var ContextLoop = require("js/ContextLoop.js");
 var ContextLoop = require("js/ContextLoop.js");
+var ContextModule = require("js/ContextModule.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var ContextProcedure = require("js/ContextProcedure.js");
 var ContextProcedure = require("js/ContextProcedure.js");
 var ContextType = require("js/ContextType.js");
 var ContextType = require("js/ContextType.js");
@@ -1400,14 +1401,14 @@ var FormalParametersProcDecl = Class.extend.call(ContextProcedure.FormalParamete
     }
     }
 });
 });
 
 
-var ModuleDeclaration = Context.ModuleDeclaration.extend({
+var ModuleDeclaration = Class.extend.call(ContextModule.Declaration, {
     init: function EberonContext$ModuleDeclaration(context){
     init: function EberonContext$ModuleDeclaration(context){
-        Context.ModuleDeclaration.prototype.init.call(this, context);
+        ContextModule.Declaration.call(this, context);
     },
     },
     handleMessage: function(msg){
     handleMessage: function(msg){
         if (handleTypePromotionMadeInSeparateStatement(msg))
         if (handleTypePromotionMadeInSeparateStatement(msg))
             return;
             return;
-        return Context.ModuleDeclaration.prototype.handleMessage.call(this, msg);
+        return ContextModule.Declaration.prototype.handleMessage.call(this, msg);
     }
     }
 });
 });
 
 

+ 1 - 1
src/ob/ContextHierarchy.ob

@@ -43,7 +43,7 @@ TYPE
         PROCEDURE language*(): LanguageContext.PLanguage;
         PROCEDURE language*(): LanguageContext.PLanguage;
 
 
         PROCEDURE findSymbol*(ident: STRING): Symbols.PFoundSymbol;
         PROCEDURE findSymbol*(ident: STRING): Symbols.PFoundSymbol;
-        PROCEDURE findModule(name: STRING): Types.PModule;
+        PROCEDURE findModule*(name: STRING): Types.PModule;
 
 
         PROCEDURE currentScope*(): Scope.PType;
         PROCEDURE currentScope*(): Scope.PType;
         PROCEDURE pushScope*(scope: Scope.PType);
         PROCEDURE pushScope*(scope: Scope.PType);

+ 80 - 0
src/ob/ContextModule.ob

@@ -0,0 +1,80 @@
+MODULE ContextModule;
+IMPORT
+    ContextHierarchy, Errors, LanguageContext, Scope, ScopeBase, Symbols,
+    Types;
+TYPE
+    Declaration* = RECORD(ContextHierarchy.Node)
+        PROCEDURE findModule(name: STRING): Types.PModule;
+        PROCEDURE handleImport(modules: ARRAY OF Symbols.PSymbol);
+
+        name: STRING;
+        imports: MAP OF Symbols.PSymbol;
+        moduleScope: Scope.PModule;
+        moduleGen: LanguageContext.PModuleGenerator;
+    END;
+
+PROCEDURE Declaration.handleIdent(id: STRING);
+BEGIN
+    IF LEN(SELF.name) = 0 THEN
+        SELF.name := id;
+        root <- SELF.root();
+        SELF.moduleScope := NEW Scope.Module(id, root.language().stdSymbols);
+        root.pushScope(SELF.moduleScope);
+    ELSIF id = SELF.name THEN
+        scope <- SELF.moduleScope;
+        scope.close();
+        Scope.defineExports(scope^);
+        SELF.codeGenerator().write(SELF.moduleGen.epilog(scope.exports));
+    ELSE
+        Errors.raise("original module name '" + SELF.name + "' expected, got '" + id + "'" );
+    END;
+END;
+
+PROCEDURE Declaration.findModule(name: STRING): Types.PModule;
+BEGIN
+    IF name = SELF.name THEN
+        Errors.raise("module '" + SELF.name + "' cannot import itself");
+    END;
+    RETURN SELF.root().findModule(name);
+END;
+
+PROCEDURE Declaration.handleImport(modules: ARRAY OF Symbols.PSymbol);
+VAR
+    moduleAliases: MAP OF STRING;
+BEGIN
+    root <- SELF.root();
+    scope <- root.currentScope();
+    FOR i <- 0 TO LEN(modules) - 1 DO
+        s <- modules[i];
+        name <- s.info()(Types.PModule).name;
+        SELF.imports[name] := s;
+        scope.addSymbol(s, FALSE);
+        moduleAliases[name] := s.id();
+    END;
+    SELF.moduleGen := root.language().moduleGenerator(
+            SELF.name,
+            moduleAliases);
+    SELF.codeGenerator().write(SELF.moduleGen.prolog());
+END;
+
+PROCEDURE Declaration.qualifyScope(scope: ScopeBase.PType): STRING;
+VAR
+    result: STRING;
+BEGIN
+    IF (scope # SELF.moduleScope) & (scope IS Scope.PModule) THEN
+        id <- scope.symbol.id();
+        
+        (* implicitly imported module, e.g.: record.pointerToRecordFromAnotherModule.field
+           should not be used in code generation, 
+           just return non-empty value to indicate this is not current module
+        *)
+        IF ~(id IN SELF.imports) THEN
+            result := "module '" + id + "' is not imported";
+        ELSE
+            result := SELF.imports[id].id() + ".";
+        END;
+    END;
+    RETURN result;
+END;
+
+END ContextModule.

+ 9 - 0
src/ob/LanguageContext.ob

@@ -20,8 +20,17 @@ TYPE
     END;
     END;
     PTypes* = POINTER TO Types;
     PTypes* = POINTER TO Types;
 
 
+    ModuleGenerator* = RECORD
+        PROCEDURE prolog*(): STRING;
+        PROCEDURE epilog*(exports: Symbols.Map): STRING;
+    END;
+    PModuleGenerator* = POINTER TO ModuleGenerator;
+
+    Imports = MAP OF STRING;
+
     Language* = RECORD
     Language* = RECORD
         moduleResolver-: PROCEDURE(name: STRING): T.PModule;
         moduleResolver-: PROCEDURE(name: STRING): T.PModule;
+        PROCEDURE moduleGenerator*(name: STRING; imports: Imports): PModuleGenerator;
         rtl-: OberonRtl.PType;
         rtl-: OberonRtl.PType;
         codeGenerator-: CodeGenerator.PIGenerator;
         codeGenerator-: CodeGenerator.PIGenerator;
         types-: PTypes;
         types-: PTypes;

+ 8 - 6
src/ob/Scope.ob

@@ -43,15 +43,16 @@ TYPE
     CompiledModule = RECORD(M.Type)
     CompiledModule = RECORD(M.Type)
         exports: Symbols.Map;
         exports: Symbols.Map;
     END;
     END;
+    PCompiledModule = POINTER TO CompiledModule;
 
 
     Module* = RECORD(Type)
     Module* = RECORD(Type)
         PROCEDURE Module*(name: STRING; stdSymbols: Symbols.Map);
         PROCEDURE Module*(name: STRING; stdSymbols: Symbols.Map);
 
 
-        symbol: Symbols.PSymbol;
-        exports: Symbols.Map;
+        symbol-: Symbols.PSymbol;
+        exports-: Symbols.Map;
         tempVarCounter: INTEGER;
         tempVarCounter: INTEGER;
     END;
     END;
-    PModule = POINTER TO Module;
+    PModule* = POINTER TO Module;
 
 
 PROCEDURE addSymbolForType*(t: Types.PBasicType; VAR result: Symbols.Map);
 PROCEDURE addSymbolForType*(t: Types.PBasicType; VAR result: Symbols.Map);
 BEGIN
 BEGIN
@@ -213,15 +214,16 @@ PROCEDURE makeProcedure*(stdSymbols: Symbols.Map): PType;
     RETURN NEW Procedure(stdSymbols);
     RETURN NEW Procedure(stdSymbols);
 END;
 END;
 
 
-PROCEDURE defineExports*(VAR m: CompiledModule; exports: Symbols.Map);
+PROCEDURE defineExports*(m: Module);
 BEGIN
 BEGIN
-    FOR id, k IN exports DO
+    cm <- m.symbol.info()(PCompiledModule);
+    FOR id, k IN m.exports DO
         symbol <- k;
         symbol <- k;
         info <- symbol.info();
         info <- symbol.info();
         IF info IS Types.PVariable THEN
         IF info IS Types.PVariable THEN
             symbol := NEW Symbols.Symbol(id, Variable.makeExportedVariable(info^));
             symbol := NEW Symbols.Symbol(id, Variable.makeExportedVariable(info^));
         END;
         END;
-        m.exports[id] := symbol;
+        cm.exports[id] := symbol;
     END;
     END;
 END;
 END;
 
 

+ 0 - 4
src/ob/Types.ob

@@ -227,10 +227,6 @@ PROCEDURE isString*(t: PType): BOOLEAN;
            OR (t^ IS String)
            OR (t^ IS String)
 END isString;
 END isString;
 
 
-PROCEDURE moduleName*(m: Module): STRING;
-    RETURN m.name
-END moduleName;
-
 PROCEDURE BasicType.BasicType(name: STRING; initializer: STRING)
 PROCEDURE BasicType.BasicType(name: STRING; initializer: STRING)
     | SUPER(name),
     | SUPER(name),
       mInitializer(initializer);
       mInitializer(initializer);

+ 2 - 1
src/oberon/oberon_grammar.js

@@ -10,6 +10,7 @@ var ContextExpression = require("js/ContextExpression.js");
 var ContextIdentdef = require("js/ContextIdentdef.js");
 var ContextIdentdef = require("js/ContextIdentdef.js");
 var ContextIf = require("js/ContextIf.js");
 var ContextIf = require("js/ContextIf.js");
 var ContextLoop = require("js/ContextLoop.js");
 var ContextLoop = require("js/ContextLoop.js");
+var ContextModule = require("js/ContextModule.js");
 var ContextProcedure = require("js/ContextProcedure.js");
 var ContextProcedure = require("js/ContextProcedure.js");
 var ContextType = require("js/ContextType.js");
 var ContextType = require("js/ContextType.js");
 var Grammar = require("grammar.js");
 var Grammar = require("grammar.js");
@@ -132,7 +133,7 @@ exports.language = {
             If:                 ContextIf.Type,
             If:                 ContextIf.Type,
             CaseLabel:          ContextCase.Label,
             CaseLabel:          ContextCase.Label,
             Repeat:             ContextLoop.Repeat,
             Repeat:             ContextLoop.Repeat,
-            ModuleDeclaration:  Context.ModuleDeclaration
+            ModuleDeclaration:  ContextModule.Declaration
         },
         },
         Grammar.reservedWords
         Grammar.reservedWords
         ),
         ),