Przeglądaj źródła

JS -> Eberon transition

Vladislav Folts 10 lat temu
rodzic
commit
e0db8392e6

BIN
bin/compiled.zip


+ 2 - 1
build.py

@@ -127,7 +127,8 @@ 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 = ['EberonSymbols.ob', 'EberonCast.ob', 'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
+    sources = ['ContextHierarchy.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
+               'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']
     
     result = os.path.join(root, 'bin.recompile')

+ 4 - 46
src/context.js

@@ -17,11 +17,11 @@ var Type = require("js/Types.js");
 var basicTypes = Type.basic();
 var nullCodeGenerator = CodeGenerator.nullGenerator();
 var nilType = Type.nil();
-
+/*
 function log(s){
     console.info(s);
 }
-
+*/
 function getSymbolAndScope(context, id){
     var s = context.findSymbol(id);
     if (!s)
@@ -1914,11 +1914,12 @@ exports.ModuleDeclaration = ChainedContext.extend({
             scope.addSymbol(s);
             moduleAliases[name] = s.id();
         }
-        this.__moduleGen = this.parent().makeModuleGenerator(
+        this.__moduleGen = this.parent().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();
@@ -1987,49 +1988,6 @@ var ModuleImport = ChainedContext.extend({
 });
 exports.ModuleImport = ModuleImport;
 
-exports.Context = Class.extend({
-    init: function Context(language){
-        this.__language = language;
-        this.__scopes = [];
-        this.__gen = 0;
-    },
-    language: function(){return this.__language;},
-    genTypeName: function(){
-        ++this.__gen;
-        return "anonymous$" + this.__gen;
-    },
-    findSymbol: function(ident){
-        for(var i = this.__scopes.length; i--;){
-            var scope = this.__scopes[i];
-            var s = scope.findSymbol(ident);
-
-            if (s)
-                return s;
-        }
-        return undefined;
-    },
-    currentScope: function(){
-        return this.__scopes[this.__scopes.length - 1];
-    },
-    pushScope: function(scope){this.__scopes.push(scope);},
-    popScope: function(){
-        var scope = this.__scopes.pop();
-        scope.close();
-    },
-    handleExpression: function(){},
-    handleLiteral: function(){},
-    codeGenerator: function(){return this.__language.codeGenerator;},
-    makeModuleGenerator: function(name, imports){
-        return this.__language.moduleGenerator(name, imports);
-    },
-    findModule: function(name){
-        if (name == "JS"){
-            return Module.makeJS();
-        }
-        return this.__language.moduleResolver ? this.__language.moduleResolver(name) : undefined;
-    }
-});
-
 function makeProcCall(context, type, info){
     assertProcType(type, info);
     var l = context.language();

+ 2 - 2
src/nodejs.js

@@ -2,7 +2,7 @@
 
 var Class = require("rtl.js").Class;
 var Code = require("js/Code.js");
-var Context = require("context.js");
+var ContextHierarchy = require("js/ContextHierarchy.js");
 var oc = require("oc.js");
 var makeRTL = require("rtl_code.js").makeRTL;
 var Type = require("js/Types.js");
@@ -81,7 +81,7 @@ function compile(sources, language, handleErrors, includeDirs, outDir, importDir
                 return fs.readFileSync(readPath, "utf8");
             },
             language.grammar,
-            function(moduleResolver){return new Context.Context(
+            function(moduleResolver){return new ContextHierarchy.Root(
                 { codeGenerator: language.codeGenerator.make(),
                   moduleGenerator: moduleCode,
                   rtl: rtl,

+ 1 - 1
src/ob/Code.ob

@@ -86,7 +86,7 @@ TYPE
         imports: StringsMap;
     END;
 
-    PModuleGenerator = POINTER TO ModuleGenerator;
+    PModuleGenerator* = POINTER TO ModuleGenerator;
 
 PROCEDURE Expression.code(): STRING;
     RETURN SELF.mCode

+ 1 - 1
src/ob/CodeGenerator.ob

@@ -11,7 +11,7 @@ TYPE
         PROCEDURE result*(): STRING;
     END;
 
-    PIGenerator = POINTER TO IGenerator;
+    PIGenerator* = POINTER TO IGenerator;
 
     NullGenerator = RECORD(IGenerator)
     END;

+ 1 - 1
src/ob/Context.ob

@@ -9,7 +9,7 @@ TYPE
         isLexem*:       PROCEDURE(): BOOLEAN;
         qualifyScope*:  PROCEDURE(scope: ScopeBase.PType): STRING;
         
-        rtl*: OberonRtl.PType
+        rtl*: OberonRtl.PType;
     END;
     PType* = POINTER TO Type;
 

+ 111 - 0
src/ob/ContextHierarchy.ob

@@ -0,0 +1,111 @@
+MODULE ContextHierarchy;
+IMPORT CodeGenerator, Module, Scope, Symbols, String;
+TYPE
+    PRoot = POINTER TO Root;
+    PNode = POINTER TO Node;
+
+    Node* = RECORD
+        PROCEDURE root(): PRoot;
+        PROCEDURE parent(): PNode;
+
+        mParent: PNode;
+    END;
+
+    Language = RECORD
+        moduleResolver: PROCEDURE(name: STRING): Module.PType;
+        codeGenerator: CodeGenerator.PIGenerator;
+    END;
+    PLanguage = POINTER TO Language;
+
+    Root* = RECORD(Node)
+        PROCEDURE Root(language: PLanguage);
+
+        PROCEDURE language(): PLanguage;
+
+        PROCEDURE genTypeName(): STRING;
+        PROCEDURE findSymbol(ident: STRING): Symbols.PFoundSymbol;
+        PROCEDURE findModule(name: STRING): Module.PType;
+
+        PROCEDURE currentScope(): Scope.PType;
+        PROCEDURE pushScope(scope: Scope.PType);
+        PROCEDURE popScope();
+
+        PROCEDURE codeGenerator(): CodeGenerator.PIGenerator;
+
+        mLanguage: PLanguage;
+        scopes: ARRAY * OF Scope.PType;
+        gen: INTEGER;
+    END;
+
+PROCEDURE Node.root(): PRoot;
+    RETURN SELF.mParent.root();
+END;
+
+PROCEDURE Node.parent(): PNode;
+    RETURN SELF.mParent;
+END;
+
+PROCEDURE Root.Root(language: PLanguage)
+    | mLanguage(language);
+END;
+
+PROCEDURE Root.language(): PLanguage;
+    RETURN SELF.mLanguage;
+END;
+
+PROCEDURE Root.genTypeName(): STRING;
+BEGIN
+    INC(SELF.gen);
+    RETURN "anonymous$" + String.fromInt(SELF.gen);
+END;
+
+PROCEDURE Root.findSymbol(ident: STRING): Symbols.PFoundSymbol;
+VAR
+    result: Symbols.PFoundSymbol;
+BEGIN
+    i <- LEN(SELF.scopes);
+    WHILE (i # 0) & (result = NIL) DO
+        DEC(i);
+        scope <- SELF.scopes[i];
+        result := scope.findSymbol(ident);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE Root.findModule(name: STRING): Module.PType;
+VAR
+    result: Module.PType;
+BEGIN
+    IF name = "JS" THEN
+        result := Module.makeJS();
+    ELSIF SELF.mLanguage.moduleResolver # NIL THEN
+        result := SELF.mLanguage.moduleResolver(name);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE Root.currentScope(): Scope.PType;
+    RETURN SELF.scopes[LEN(SELF.scopes) - 1];
+END;
+
+PROCEDURE Root.pushScope(scope: Scope.PType);
+BEGIN
+    SELF.scopes.add(scope);
+END;
+
+PROCEDURE Root.popScope();
+BEGIN
+    i <- LEN(SELF.scopes) - 1;
+    SELF.scopes[i].close();
+    SELF.scopes.remove(i);
+END;
+
+PROCEDURE Root.codeGenerator(): CodeGenerator.PIGenerator;
+    RETURN SELF.mLanguage.codeGenerator;
+END;
+
+PROCEDURE Root.root(): PRoot;
+    RETURN SELF(POINTER);
+END;
+
+END ContextHierarchy.

+ 1 - 1
src/ob/Scope.ob

@@ -22,7 +22,7 @@ TYPE
 
         PROCEDURE addSymbol*(s: Symbols.PSymbol; exported: BOOLEAN);
         PROCEDURE findSymbol*(id: STRING): Symbols.PFoundSymbol;
-        PROCEDURE close();
+        PROCEDURE close*();
         PROCEDURE generateTempVar*(pattern: STRING): STRING;
 
         stdSymbols: Symbols.Map;

+ 2 - 2
src/oc.js

@@ -2,7 +2,7 @@
 
 var Class = require("rtl.js").Class;
 var Code = require("js/Code.js");
-var Context = require("context.js");
+var ContextHierarchy = require("js/ContextHierarchy.js");
 var Errors = require("js/Errors.js");
 var Lexer = require("js/Lexer.js");
 var makeRTL = require("rtl_code.js").makeRTL;
@@ -128,7 +128,7 @@ function compile(text, language, handleErrors){
     var resolver = makeResolver(
             language.grammar,
             function(moduleResolver){
-                return new Context.Context(
+                return new ContextHierarchy.Root(
                     { codeGenerator: language.codeGenerator.make(),
                       moduleGenerator: moduleCode,
                       rtl: rtl,

+ 6 - 4
test/test_unit_common.js

@@ -2,7 +2,7 @@
 
 var Class = require("rtl.js").Class;
 var Code = require("js/Code.js");
-var Context = require("context.js");
+var ContextHierarchy = require("js/ContextHierarchy.js");
 var Errors = require("js/Errors.js");
 var oc = require("oc.js");
 var makeRTL = require("rtl_code.js").makeRTL;
@@ -26,9 +26,9 @@ var TestModuleGenerator = Class.extend({
     epilog: function(){return undefined;}
 });
 
-var TestContext = Context.Context.extend({
+var TestContext = Class.extend.call(ContextHierarchy.Root, {
     init: function TestContext(language){
-        Context.Context.prototype.init.call(
+        ContextHierarchy.Root.call(
                 this,
                 { codeGenerator: language.codeGenerator.nil,
                   moduleGenerator: function(){return new TestModuleGenerator();},
@@ -39,7 +39,9 @@ var TestContext = Context.Context.extend({
         this.pushScope(new Scope.Module("test", language.stdSymbols));
     },
     qualifyScope: function(){return "";},
-    handleMessage: function(){}
+    handleMessage: function(){},
+    handleExpression: function(){},
+    handleLiteral: function(){}
 });
 
 function makeContext(language){return new TestContext(language);}