Bläddra i källkod

js -> eberon transition

Vladislav Folts 10 år sedan
förälder
incheckning
34dcc2de1d

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 = ['ContextConst.ob', 'ContextDesignator', 'ContextType.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
+    sources = ['ContextConst.ob', 'ContextIdentdef', 'ContextType.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
                'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']
     

+ 0 - 59
src/context.js

@@ -36,65 +36,6 @@ var ChainedContext = ContextHierarchy.Node;
 ChainedContext.extend = Class.extend;
 ChainedContext.prototype.init = ContextHierarchy.Node;
 
-exports.QualifiedIdentificatorModule = ChainedContext.extend({
-    init: function QualifiedIdentificatorModule(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__id = undefined;
-    },
-    handleIdent: function(id){
-        this.__id = id;
-    },
-    endParse: function(){
-        var found = this.root().findSymbol(this.__id);
-        if (!found)
-            return false;
-        var s = found.symbol();
-        if (!s || !s.isModule())
-            return false;
-        this.parent().handleModule(this.__id, s.info());
-    }
-});
-
-exports.QualifiedIdentificator = ChainedContext.extend({
-    init: function QualifiedIdentificator(context){
-        ChainedContext.prototype.init.call(this, context);
-        this.__module = undefined;
-        this.__id = undefined;
-        this.__code = "";
-    },
-    handleIdent: function(id){
-        this.__id = id;
-    },
-    handleModule: function(id, module){
-        this.__module = module;
-        this.__code = id + ".";
-    },
-    endParse: function(){
-        var code = this.__code ? this.__code + Record.mangleJSProperty(this.__id) : this.__id;
-        this.parent().handleQIdent({
-            module: this.__module,
-            id: this.__id,
-            code: code
-        });
-    }
-});
-
-exports.Identdef = ChainedContext.extend({
-    init: function IdentdefContext(context){
-        ChainedContext.prototype.init.call(this, context);
-        this._id = undefined;
-        this._export = false;
-    },
-    handleIdent: function(id){this._id = id;},
-    handleLiteral: function(){this._export = true;},
-    endParse: function(){
-        this.parent().handleIdentdef(this._makeIdendef());
-    },
-    _makeIdendef: function(){
-        return new ObContext.IdentdefInfo(this._id, this._export);
-    }
-});
-
 var HandleSymbolAsType = ContextType.HandleSymbolAsType;
 HandleSymbolAsType.extend = Class.extend;
 HandleSymbolAsType.prototype.init = ContextType.HandleSymbolAsType;

+ 6 - 5
src/eberon/eberon_context.js

@@ -8,6 +8,7 @@ var Context = require("context.js");
 var ContextConst = require("js/ContextConst.js");
 var ContextDesignator = require("js/ContextDesignator.js");
 var ContextExpression = require("js/ContextExpression.js");
+var ContextIdentdef = require("js/ContextIdentdef.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var ContextType = require("js/ContextType.js");
 var EberonConstructor= require("js/EberonConstructor.js");
@@ -178,18 +179,18 @@ var ForEachVariable = TypeNarrowVariable.extend({
     idType: function(){return "FOR variable";}
 });
 
-var Identdef = Context.Identdef.extend({
+var Identdef = Class.extend.call(ContextIdentdef.Type, {
     init: function(parent){
-        Context.Identdef.prototype.init.call(this, parent);
+        ContextIdentdef.Type.call(this, parent);
         this.__ro = false;
     },
     handleLiteral: function(l){
         if (l == "-")
             this.__ro = true;  
-        Context.Identdef.prototype.handleLiteral.call(this, l);
+        ContextIdentdef.Type.prototype.handleLiteral.call(this, l);
     },
-    _makeIdendef: function(){
-        return new EberonContext.IdentdefInfo(this._id, this._export, this.__ro);
+    doMakeIdendef: function(){
+        return new EberonContext.IdentdefInfo(this.id, this.export$, this.__ro);
     }
 });
 

+ 3 - 2
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 ContextIdentdef = require("js/ContextIdentdef.js");
 var ContextType = require("js/ContextType.js");
 var Lexer = require("js/Lexer.js");
 var Parser = require("parser.js");
@@ -44,8 +45,8 @@ var ident = function(stream, context){
     return Lexer.ident(stream, context, reservedWords);
 };
 
-var qualident = context(and(optional(context(and(ident, "."), Context.QualifiedIdentificatorModule)), ident),
-                        Context.QualifiedIdentificator);
+var qualident = context(and(optional(context(and(ident, "."), ContextIdentdef.QualifiedModule)), ident),
+                        ContextIdentdef.Qualified);
 var identdef = makeIdentdef(ident);
 
 var selector = or(and(point, ident)

+ 7 - 5
src/ob/ContextDesignator.ob

@@ -14,8 +14,12 @@ TYPE
     END;
     PIndex = POINTER TO Index;
 
-    Type* = RECORD(ContextHierarchy.Node)
-        PROCEDURE handleQIdent(q: ContextHierarchy.QIdent);
+    QIdentHandler* = RECORD(ContextHierarchy.Node)
+        PROCEDURE handleQIdent*(q: ContextHierarchy.QIdent);
+    END;
+    PQIdentHandler* = POINTER TO QIdentHandler;
+
+    Type* = RECORD(QIdentHandler)
         PROCEDURE handleExpression(e: Expression.PType);
         PROCEDURE handleTypeCast(type: Types.PType);
 
@@ -34,9 +38,7 @@ TYPE
         indexExpression: Expression.PType;
     END;
 
-    TypeCast* = RECORD(ContextHierarchy.Node)
-        PROCEDURE handleQIdent(q: ContextHierarchy.QIdent);
-
+    TypeCast* = RECORD(QIdentHandler)
         type: Types.PType;
     END;
 

+ 9 - 1
src/ob/ContextHierarchy.ob

@@ -13,6 +13,8 @@ TYPE
     END;
 
     QIdent* = RECORD
+        PROCEDURE QIdent*(module: Module.PType; id: STRING; code: STRING);
+
         module-: Module.PType;
         id-: STRING;
         code-: STRING;
@@ -43,7 +45,7 @@ TYPE
 
         PROCEDURE language*(): LanguageContext.PType;
 
-        PROCEDURE findSymbol(ident: STRING): Symbols.PFoundSymbol;
+        PROCEDURE findSymbol*(ident: STRING): Symbols.PFoundSymbol;
         PROCEDURE findModule(name: STRING): Types.PModule;
 
         PROCEDURE currentScope*(): Scope.PType;
@@ -55,6 +57,12 @@ TYPE
         gen: INTEGER;
     END;
 
+PROCEDURE QIdent.QIdent(module: Module.PType; id: STRING; code: STRING)
+    | module(module),
+      id(id),
+      code(code);
+END;
+
 PROCEDURE Node.Node(parent: PNode)
     | mParent(parent);
 BEGIN

+ 106 - 0
src/ob/ContextIdentdef.ob

@@ -0,0 +1,106 @@
+MODULE ContextIdentdef;
+IMPORT
+    Context, ContextDesignator, ContextHierarchy, ContextType, 
+    Module, Record, TypeId;
+TYPE
+    Type* = RECORD(ContextHierarchy.Node)
+        PROCEDURE Type(parent: ContextType.PDeclarationHandle);
+        PROCEDURE doMakeIdendef(): Context.PIdentdefInfo;
+
+        parentDecl: ContextType.PDeclarationHandle;
+        id: STRING;
+        export: BOOLEAN;
+    END;
+
+    Qualified* = RECORD(ContextHierarchy.Node)
+        PROCEDURE Qualified(parent: ContextDesignator.PQIdentHandler);
+        PROCEDURE handleModule(id: STRING; module: Module.PType);
+
+        qidentHandler: ContextDesignator.PQIdentHandler;
+        module: Module.PType;
+        id: STRING;
+        code: STRING;
+    END;
+
+    QualifiedModule* = RECORD(ContextHierarchy.Node)
+        id: STRING;
+    END;
+
+PROCEDURE Type.Type(parent: ContextType.PDeclarationHandle)
+    | SUPER(parent),
+      parentDecl(parent);
+END;
+
+PROCEDURE Type.handleIdent(id: STRING);
+BEGIN
+    SELF.id := id;
+END;
+
+PROCEDURE Type.handleLiteral(s: STRING);
+BEGIN
+    SELF.export := TRUE;
+END;
+
+PROCEDURE Type.doMakeIdendef(): Context.PIdentdefInfo;
+    RETURN NEW Context.IdentdefInfo(SELF.id, SELF.export);
+END;
+
+PROCEDURE Type.endParse(): BOOLEAN;
+BEGIN
+    SELF.parentDecl.handleIdentdef(SELF.doMakeIdendef());
+    RETURN TRUE;
+END;
+
+PROCEDURE Qualified.Qualified(parent: ContextDesignator.PQIdentHandler)
+    | SUPER(parent),
+      qidentHandler(parent);
+END;
+
+PROCEDURE Qualified.handleIdent(id: STRING);
+BEGIN
+    SELF.id := id;
+END;
+
+PROCEDURE Qualified.handleModule(id: STRING; module: Module.PType);
+BEGIN
+    SELF.module := module;
+    SELF.code := id + ".";
+END;
+
+PROCEDURE Qualified.endParse(): BOOLEAN;
+VAR
+    code: STRING;
+BEGIN
+    IF LEN(SELF.code) = 0 THEN
+        code := SELF.id;
+    ELSE
+        code := SELF.code + Record.mangleJSProperty(SELF.id);
+    END;
+    SELF.qidentHandler.handleQIdent(
+        ContextHierarchy.QIdent(SELF.module, SELF.id, code));
+    RETURN TRUE;
+END;
+
+PROCEDURE QualifiedModule.handleIdent(id: STRING);
+BEGIN
+    SELF.id := id;
+END;
+
+PROCEDURE QualifiedModule.endParse(): BOOLEAN;
+BEGIN
+    result <- FALSE;
+    found <- SELF.root().findSymbol(SELF.id);
+    IF found # NIL THEN
+        s <- found.symbol();
+        IF s # NIL THEN
+            info <- s.info();
+            IF info IS Module.PType THEN
+                SELF.parent()^(Qualified).handleModule(SELF.id, info);
+                result := TRUE;
+            END;
+        END;
+    END;
+    RETURN result;
+END;
+
+END ContextIdentdef.

+ 3 - 2
src/ob/ContextType.ob

@@ -35,12 +35,13 @@ TYPE
         dimensions: ARRAY * OF INTEGER;
     END;
 
-    DeclarationHandle = RECORD(HandleSymbolAsType)
+    DeclarationHandle* = RECORD(HandleSymbolAsType)
         PROCEDURE isAnonymousDeclaration(): BOOLEAN;
         PROCEDURE exportField(name: STRING);
-        PROCEDURE handleIdentdef(id: Context.PIdentdefInfo);
+        PROCEDURE handleIdentdef*(id: Context.PIdentdefInfo);
         PROCEDURE typeName(): STRING;
     END;
+    PDeclarationHandle* = POINTER TO DeclarationHandle;
 
     Declaration* = RECORD(DeclarationHandle)
         id: Context.PIdentdefInfo;

+ 2 - 3
src/ob/Scope.ob

@@ -1,6 +1,7 @@
 MODULE Scope;
 IMPORT 
     Errors, 
+    M := Module,
     Object, 
     Procedures := Procedure, 
     Record,
@@ -39,9 +40,7 @@ TYPE
         tempVarCounter: INTEGER;
     END;
 
-    CompiledModule = RECORD(Types.Module)
-        PROCEDURE findSymbol(id: STRING): Symbols.PFoundSymbol;
-
+    CompiledModule = RECORD(M.Type)
         exports: Symbols.Map;
     END;
 

+ 2 - 1
src/oberon/oberon_grammar.js

@@ -6,6 +6,7 @@ var Context = require("context.js");
 var ContextConst = require("js/ContextConst.js");
 var ContextDesignator = require("js/ContextDesignator.js");
 var ContextExpression = require("js/ContextExpression.js");
+var ContextIdentdef = require("js/ContextIdentdef.js");
 var ContextType = require("js/ContextType.js");
 var Grammar = require("grammar.js");
 var ObContext = require("oberon/oberon_context.js");
@@ -46,7 +47,7 @@ function makeAssignmentOrProcedureCall(designator, actualParameters, assignment)
 }
 
 function makeIdentdef(ident){
-    return context(and(ident, optional("*")), Context.Identdef);
+    return context(and(ident, optional("*")), ContextIdentdef.Type);
 }
 
 function makeDesignator(ident, qualident, selector, actualParameters){