Преглед на файлове

js -> eberon transition

Vladislav Folts преди 10 години
родител
ревизия
b4d14851b4
променени са 10 файла, в които са добавени 119 реда и са изтрити 78 реда
  1. BIN
      bin/compiled.zip
  2. 3 1
      build.py
  3. 0 51
      src/context.js
  4. 7 6
      src/eberon/eberon_context.js
  5. 3 2
      src/grammar.js
  6. 20 0
      src/ob/ContextAssignment.ob
  7. 2 9
      src/ob/ContextProcedure.ob
  8. 4 5
      src/ob/ContextType.ob
  9. 75 0
      src/ob/ContextVar.ob
  10. 5 4
      src/oberon/oberon_context.js

BIN
bin/compiled.zip


+ 3 - 1
build.py

@@ -127,7 +127,9 @@ 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 = ['ContextCase.ob', 'ContextConst.ob', 'ContextIdentdef', 'ContextLoop', 'ContextProcedure.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
+    sources = ['ContextAssignment.ob', 'ContextCase.ob', 'ContextConst.ob', 
+               'ContextIdentdef', 'ContextLoop', 'ContextProcedure.ob', 
+               'ContextVar.ob', 'EberonSymbols.ob', 'EberonCast.ob', 
                'EberonConstructor.ob', 'EberonOperator.ob', 'EberonScope.ob',
                'OberonSymbols.ob', 'Lexer.ob', 'Module.ob']
     

+ 0 - 51
src/context.js

@@ -42,57 +42,6 @@ var HandleSymbolAsType = ContextType.HandleSymbolAsType;
 HandleSymbolAsType.extend = Class.extend;
 HandleSymbolAsType.prototype.init = ContextType.HandleSymbolAsType;
 
-exports.CheckAssignment = ChainedContext.extend({
-    init: function Context$CheckAssignment(context){
-        ChainedContext.prototype.init.call(this, context);
-    },
-    handleLiteral: function(s){
-        if (s == "=")
-            throw new Errors.Error("did you mean ':=' (statement expected, got expression)?");
-    }
-});
-
-exports.VariableDeclaration = HandleSymbolAsType.extend({
-    init: function Context$VariableDeclaration(context){
-        HandleSymbolAsType.prototype.init.call(this, context);
-        this.__idents = [];
-        this.__type = undefined;
-    },
-    handleIdentdef: function(id){this.__idents.push(id);},
-    exportField: function(name){
-        ContextType.checkIfFieldCanBeExported(name, this.__idents, "variable");
-    },
-    setType: function(type){this.__type = type;},
-    type: function(){return this.__type;},
-    typeName: function(){return "";},
-    isAnonymousDeclaration: function(){return true;},
-    checkExport: function(){},
-    handleMessage: function(msg){
-        if (msg instanceof ContextType.ForwardTypeMsg)
-            throw new Errors.Error("type '" + msg.id + "' was not declared");
-        return HandleSymbolAsType.prototype.handleMessage.call(this, msg);
-    },
-    _initCode: function(){
-        return this.__type.initializer(this);
-    },
-    endParse: function(){
-        var idents = this.__idents;
-        var gen = this.codeGenerator();
-        for(var i = 0; i < idents.length; ++i){
-            var id = idents[i];
-            var varName = id.id();
-            if (id.exported())
-                this.checkExport(varName);
-
-            var v = new Variable.DeclaredVariable(varName, this.__type);
-            this.root().currentScope().addSymbol(new Symbol.Symbol(varName, v), id.exported());
-            gen.write("var " + varName + " = " + this._initCode() + ";");
-        }
-
-        gen.write("\n");
-    }
-});
-
 function assertProcType(type, info){
     var unexpected;
     if ( !type )

+ 7 - 6
src/eberon/eberon_context.js

@@ -15,6 +15,7 @@ var ContextLoop = require("js/ContextLoop.js");
 var ContextHierarchy = require("js/ContextHierarchy.js");
 var ContextProcedure = require("js/ContextProcedure.js");
 var ContextType = require("js/ContextType.js");
+var ContextVar = require("js/ContextVar.js");
 var EberonConstructor= require("js/EberonConstructor.js");
 var EberonContext= require("js/EberonContext.js");
 var EberonDynamicArray = require("js/EberonDynamicArray.js");
@@ -520,19 +521,19 @@ var ConstDecl = Class.extend.call(ContextConst.Type, {
     }
 });
 
-var VariableDeclaration = Context.VariableDeclaration.extend({
+var VariableDeclaration = Class.extend.call(ContextVar.Declaration, {
     init: function EberonContext$VariableDeclaration(context){
-        Context.VariableDeclaration.prototype.init.call(this, context);
+        ContextVar.Declaration.call(this, context);
     },
     handleIdentdef: function(id){
         checkOrdinaryExport(id, "variable");
-        Context.VariableDeclaration.prototype.handleIdentdef.call(this, id);
+        ContextVar.Declaration.prototype.handleIdentdef.call(this, id);
     },
-    _initCode: function(){
-        var type = this.type();
+    doInitCode: function(){
+        var type = this.type;
         if (type instanceof EberonRecord.Record)
             EberonRecord.ensureCanBeInstantiated(this, type, EberonRecord.instantiateForVar);
-        return Context.VariableDeclaration.prototype._initCode.call(this);
+        return ContextVar.Declaration.prototype.doInitCode.call(this);
     }
 });
 

+ 3 - 2
src/grammar.js

@@ -1,6 +1,7 @@
 "use strict";
 
 var Context = require("context.js");
+var ContextAssignment = require("js/ContextAssignment.js");
 var ContextCase = require("js/ContextCase.js");
 var ContextDesignator = require("js/ContextDesignator.js");
 var ContextExpression = require("js/ContextExpression.js");
@@ -111,7 +112,7 @@ var set = and("{", context(optional(and(element, repeat(and(",", element)))), Co
 var expList = and(expression, repeat(and(",", expression)));
 var actualParameters = and("(", context(optional(expList), Context.ActualParameters), ")");
 
-var assignment = and(context(or(":=", "="), Context.CheckAssignment),
+var assignment = and(context(or(":=", "="), ContextAssignment.Check),
                      required(expression, "expression expected"));
 
 // break recursive declaration of statement
@@ -151,7 +152,7 @@ var forStatement = and("FOR",
                              , contexts.For));
 
 var statement = optional(
-    makeStatement(or( emit(designator.assignmentOrProcedureCall(assignment, expression), ContextProcedure.emitEndStatement),
+    makeStatement(or( emit(designator.assignmentOrProcedureCall(assignment, expression), ContextAssignment.emitEnd),
                       ifStatement,
                       caseStatement,
                       whileStatement,

+ 20 - 0
src/ob/ContextAssignment.ob

@@ -0,0 +1,20 @@
+MODULE ContextAssignment;
+IMPORT
+    Chars, ContextHierarchy, Errors;
+TYPE
+    Check* = RECORD(ContextHierarchy.Node)
+    END;
+
+PROCEDURE Check.handleLiteral(s: STRING);
+BEGIN
+    IF s = "=" THEN
+        Errors.raise("did you mean ':=' (statement expected, got expression)?");
+    END;
+END;
+
+PROCEDURE emitEnd*(cx: ContextHierarchy.Node);
+BEGIN
+    cx.codeGenerator().write(";" + Chars.ln);
+END;
+
+END ContextAssignment.

+ 2 - 9
src/ob/ContextProcedure.ob

@@ -8,8 +8,6 @@ TYPE
     Declaration* = RECORD(ContextType.DeclarationAndIdentHandle)
         PROCEDURE Declaration(parent: ContextHierarchy.PNode);
 
-        PROCEDURE typeName(): STRING;
-
         PROCEDURE doProlog(): STRING;
         PROCEDURE doEpilog(): STRING;
         PROCEDURE doBeginBody();
@@ -26,7 +24,7 @@ TYPE
     PDeclaration = POINTER TO Declaration;
 
     FormalParameters* = RECORD(ContextHierarchy.Node)
-        PROCEDURE FormalParameters(parent: PDeclaration);
+        PROCEDURE FormalParameters(parent: ContextType.PDeclarationAndIdentHandle);
 
         PROCEDURE handleQIdent(q: ContextHierarchy.QIdent);
         PROCEDURE doCheckResultType(type: Types.PStorageType);
@@ -178,7 +176,7 @@ BEGIN
     RETURN TRUE;
 END;
 
-PROCEDURE FormalParameters.FormalParameters(parent: PDeclaration)
+PROCEDURE FormalParameters.FormalParameters(parent: ContextType.PDeclarationAndIdentHandle)
     | SUPER(parent),
       type(NEW Procedure.Type(parent.typeName()));
 BEGIN
@@ -275,9 +273,4 @@ PROCEDURE AddArgumentMsg.AddArgumentMsg(name: STRING; arg: Types.PProcedureArgum
       arg(arg);
 END;
 
-PROCEDURE emitEndStatement*(cx: ContextHierarchy.Node);
-BEGIN
-    cx.codeGenerator().write(";" + Chars.ln);
-END;
-
 END ContextProcedure.

+ 4 - 5
src/ob/ContextType.ob

@@ -36,19 +36,18 @@ TYPE
     END;
 
     DeclarationHandle* = RECORD(HandleSymbolAsType)
-        PROCEDURE isAnonymousDeclaration(): BOOLEAN;
-        PROCEDURE exportField(name: STRING);
+        PROCEDURE isAnonymousDeclaration*(): BOOLEAN;
+        PROCEDURE exportField*(name: STRING);
     END;
     PDeclarationHandle* = POINTER TO DeclarationHandle;
 
     DeclarationAndIdentHandle* = RECORD(DeclarationHandle)
         PROCEDURE handleIdentdef*(id: Context.PIdentdefInfo);
+        PROCEDURE typeName*(): STRING;
     END;
     PDeclarationAndIdentHandle* = POINTER TO DeclarationAndIdentHandle;
 
     Declaration* = RECORD(DeclarationAndIdentHandle)
-        PROCEDURE typeName*(): STRING;
-
         id: Context.PIdentdefInfo;
         symbol: Symbols.PSymbol;
     END;
@@ -92,7 +91,7 @@ TYPE
     ForwardTypeMsg* = RECORD(ContextHierarchy.Message)
         PROCEDURE ForwardTypeMsg(id: STRING);
 
-        id: STRING;
+        id-: STRING;
     END;
 
 PROCEDURE HandleSymbolAsType.handleQIdent(q: ContextHierarchy.QIdent);

+ 75 - 0
src/ob/ContextVar.ob

@@ -0,0 +1,75 @@
+MODULE ContextVar;
+IMPORT
+    Chars, Context, ContextHierarchy, ContextType, Errors, 
+    Object, Symbols, Types, Variable;
+TYPE
+    Declaration* = RECORD(ContextType.DeclarationAndIdentHandle)
+        PROCEDURE doInitCode(): STRING;
+        PROCEDURE doCheckExport(name: STRING);
+
+        idents: ARRAY * OF Context.PIdentdefInfo;
+        type: Types.PStorageType;
+    END;
+
+PROCEDURE Declaration.handleIdentdef(id: Context.PIdentdefInfo);
+BEGIN
+    SELF.idents.add(id);
+END;
+
+PROCEDURE Declaration.typeName(): STRING;
+    RETURN "";
+END;
+
+PROCEDURE Declaration.setType(type: Types.PStorageType);
+BEGIN
+    SELF.type := type;
+END;
+
+PROCEDURE Declaration.exportField(name: STRING);
+BEGIN
+    ContextType.checkIfFieldCanBeExported(name, SELF.idents, "variable");
+END;
+
+PROCEDURE Declaration.isAnonymousDeclaration(): BOOLEAN;
+    RETURN TRUE;
+END;
+(*
+PROCEDURE Declaration.typeName(): STRING;
+    RETURN "";
+END;
+*)
+PROCEDURE Declaration.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
+BEGIN
+    IF msg IS ContextType.ForwardTypeMsg THEN
+        Errors.raise("type '" + msg.id + "' was not declared");
+    END;
+    RETURN SUPER(msg);
+END;
+
+PROCEDURE Declaration.doInitCode(): STRING;
+    RETURN SELF.type.initializer(SELF);
+END;
+
+PROCEDURE Declaration.doCheckExport(name: STRING);
+END;
+
+PROCEDURE Declaration.endParse(): BOOLEAN;
+BEGIN
+    gen <- SELF.codeGenerator();
+    FOR i <- 0 TO LEN(SELF.idents) - 1 DO
+        id <-SELF.idents[i];
+        varName <- id.id();
+        IF id.exported() THEN
+            SELF.doCheckExport(varName);
+        END;
+
+        v <- NEW Variable.DeclaredVariable(varName, SELF.type);
+        SELF.root().currentScope().addSymbol(NEW Symbols.Symbol(varName, v), id.exported());
+        gen.write("var " + varName + " = " + SELF.doInitCode() + ";");
+    END;
+
+    gen.write(Chars.ln);
+    RETURN TRUE;
+END;
+
+END ContextVar.

+ 5 - 4
src/oberon/oberon_context.js

@@ -5,6 +5,7 @@ var CodeGenerator = require("js/CodeGenerator.js");
 var Context = require("context.js");
 var ContextExpression = require("js/ContextExpression.js");
 var ContextType = require("js/ContextType.js");
+var ContextVar = require("js/ContextVar.js");
 var Errors = require("js/Errors.js");
 var Expression = require("js/Expression.js");
 var op = require("js/Operator.js");
@@ -17,12 +18,12 @@ var RecordDecl = Class.extend.call(ContextType.Record, {
     }
 });
 
-var VariableDeclaration = Context.VariableDeclaration.extend({
+var VariableDeclaration = Class.extend.call(ContextVar.Declaration, {
     init: function(context){
-        Context.VariableDeclaration.prototype.init.call(this, context);
+        ContextVar.Declaration.call(this, context);
     },
-    checkExport: function(id){
-        var type = this.type();
+    doCheckExport: function(id){
+        var type = this.type;
         if (type instanceof Type.Record || type instanceof Type.Array)
             throw new Errors.Error("variable '" + id + "' cannot be exported: only scalar variables can be exported");
     }