瀏覽代碼

js -> eberon transition

Vladislav Folts 10 年之前
父節點
當前提交
3a0d3b1ebf
共有 8 個文件被更改,包括 120 次插入93 次删除
  1. 二進制
      bin/compiled.zip
  2. 21 69
      src/context.js
  3. 6 5
      src/eberon/eberon_context.js
  4. 87 15
      src/ob/ContextHierarchy.ob
  5. 3 1
      src/ob/LanguageContext.ob
  6. 1 1
      src/ob/Module.ob
  7. 1 1
      src/ob/Symbols.ob
  8. 1 1
      src/ob/Types.ob

二進制
bin/compiled.zip


+ 21 - 69
src/context.js

@@ -23,53 +23,6 @@ function log(s){
     console.info(s);
 }
 */
-function getSymbolAndScope(context, id){
-    var s = context.findSymbol(id);
-    if (!s)
-        throw new Errors.Error(
-            context instanceof Type.Module
-                ? "identifier '" + id + "' is not exported by module '" + Type.moduleName(context) + "'"
-                : "undeclared identifier: '" + id + "'");
-    return s;
-}
-
-function getQIdSymbolAndScope(context, q){
-    return getSymbolAndScope(q.module ? q.module : context.root(), q.id);
-}
-
-function getSymbol(context, id){
-    return getSymbolAndScope(context.root(), id).symbol();
-}
-
-function unwrapTypeId(type){
-    if (!(type instanceof Type.TypeId))
-        throw new Errors.Error("type name expected");
-    return type;
-}
-
-function unwrapType(type){
-    return unwrapTypeId(type).type();
-}
-
-function getTypeSymbol(context, id){
-    return unwrapType(getSymbol(context, id).info());
-}
-
-function throwTypeMismatch(from, to){
-    throw new Errors.Error("type mismatch: expected '" + to.description() +
-                           "', got '" + from.description() + "'");
-}
-
-function checkTypeMatch(from, to){
-    if (!Cast.areTypesMatch(from, to))
-        throwTypeMismatch(from, to);
-}
-
-function checkImplicitCast(types, from, to){
-    var op;
-    if (types.implicitCast(from, to, false, {set: function(v){op = v;}, get:function(){return op;}}))
-        throwTypeMismatch(from, to);
-}
 
 function promoteTypeInExpression(e, type){
     var fromType = e.type();
@@ -90,7 +43,7 @@ function promoteExpressionType(context, left, right){
     if (!rightType)
         return;
 
-    checkImplicitCast(context.root().language().types, rightType, leftType);
+    ContextHierarchy.checkImplicitCast(context.root(), rightType, leftType);
 }
 
 function checkTypeCast(fromInfo, fromType, toType, msg){
@@ -220,8 +173,8 @@ exports.BaseType = ChainedContext.extend({
         ChainedContext.prototype.init.call(this, context);
     },
     handleQIdent: function(q){
-        var s = getQIdSymbolAndScope(this, q);
-        this.parent().setBaseType(unwrapType(s.symbol().info()));
+        var s = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
+        this.parent().setBaseType(ContextHierarchy.unwrapType(s.symbol().info()));
     }
 });
 
@@ -311,7 +264,7 @@ exports.Designator = ChainedContext.extend({
         this.__propCode = undefined;
     },
     handleQIdent: function(q){
-        var found = getQIdSymbolAndScope(this, q);
+        var found = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
         this.__scope = found.scope();
         var s = found.symbol();
         var info = s.info();
@@ -475,8 +428,8 @@ var HandleSymbolAsType = ChainedContext.extend({
         ChainedContext.prototype.init.call(this, context);
     },
     handleQIdent: function(q){
-        var s = getQIdSymbolAndScope(this, q);
-        this.setType(unwrapType(s.symbol().info()));
+        var s = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
+        this.setType(ContextHierarchy.unwrapType(s.symbol().info()));
     }
 });
 
@@ -533,8 +486,8 @@ exports.FormalParameters = ChainedContext.extend({
         return ChainedContext.prototype.handleMessage.call(this, msg);
     },
     handleQIdent: function(q){
-        var s = getQIdSymbolAndScope(this, q);
-        var resultType = unwrapType(s.symbol().info());
+        var s = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
+        var resultType = ContextHierarchy.unwrapType(s.symbol().info());
         this._checkResultType(resultType);
         this.__result = resultType;
     },
@@ -701,12 +654,12 @@ exports.PointerDecl = ChainedContext.extend({
     handleQIdent: function(q){
         var id = q.id;
         var s = q.module
-              ? getQIdSymbolAndScope(this, q)
+              ? ContextHierarchy.getModuleSymbolAndScope(q.module, q.id)
               : this.root().findSymbol(id);
         
         var info = s ? s.symbol().info()
                      : this.parent().handleMessage(new ForwardTypeMsg(id));
-        var typeId = unwrapTypeId(info);
+        var typeId = ContextHierarchy.unwrapTypeId(info);
         this.__setTypeId(typeId);
     },
     __setTypeId: function(typeId){
@@ -917,7 +870,7 @@ var RelationOps = Class.extend({
         // special case for strings
         var isStrings = Type.isString(leftType) && Type.isString(rightType);
         if (!isStrings)
-            checkTypeMatch(rightType, leftType);
+            ContextHierarchy.checkTypeMatch(rightType, leftType);
 
         return leftType;
     }
@@ -929,12 +882,12 @@ function checkSetHasBit(leftType, rightType, context){
     if (!Type.isInt(leftType))
         throw new Errors.Error(
             Type.intsDescription() + " expected as an element of SET, got '" + Type.typeName(leftType) + "'");
-    checkImplicitCast(context.root().language().types, rightType, basicTypes.set);
+    ContextHierarchy.checkImplicitCast(context.root(), rightType, basicTypes.set);
 }
 
 function relationOp(leftType, rightType, literal, ops, context){
     var type = 
-          literal == "IS" ? unwrapType(rightType)
+          literal == "IS" ? ContextHierarchy.unwrapType(rightType)
         : literal == "IN" ? checkSetHasBit(leftType, rightType, context)
                           : ops.coalesceType(leftType, rightType);
     var o;
@@ -1122,7 +1075,7 @@ exports.Factor = ChainedContext.extend({
     },
     endParse: function(){
         if (this.__logicalNot){
-            checkTypeMatch(this.__factor.type(), basicTypes.bool);
+            ContextHierarchy.checkTypeMatch(this.__factor.type(), basicTypes.bool);
             this.__factor = op.not(this.__factor);
         }
         this.parent().handleFactor(this.__factor);
@@ -1251,7 +1204,7 @@ exports.SimpleExpression = ChainedContext.extend({
         if (type === undefined || this.__type === undefined)
             this.__type = type;
         else
-            checkImplicitCast(this.root().language().types, type, this.__type);
+            ContextHierarchy.checkImplicitCast(this.root(), type, this.__type);
     },
     handleBinaryOperator: function(o){this.__binaryOperator = o;},
     endParse: function(){
@@ -1450,7 +1403,7 @@ exports.CaseRange = ChainedContext.extend({
         this.handleLabel(type, value);
     },
     handleIdent: function(id){
-        var s = getSymbol(this.parent(), id);
+        var s = ContextHierarchy.getSymbol(this.root(), id);
         if (!s.isConst())
             throw new Errors.Error("'" + id + "' is not a constant");
         
@@ -1526,7 +1479,7 @@ exports.For = ChainedContext.extend({
         this.__by = undefined;
     },
     handleIdent: function(id){
-        var s = getSymbol(this.parent(), id);
+        var s = ContextHierarchy.getSymbol(this.root(), id);
         if (!s.isVariable())
             throw new Errors.Error("'" + s.id() + "' is not a variable");
         var type = s.info().type();
@@ -1866,7 +1819,9 @@ exports.TypeSection = ChainedContext.extend({
         if (msg instanceof ForwardTypeMsg){
             var scope = this.root().currentScope();
             Scope.addUnresolved(scope, msg.id);
-            var resolve = function(){return getSymbol(this, msg.id).info().type();}.bind(this);
+            var resolve = function(){
+                return ContextHierarchy.getSymbol(this.root(), msg.id).info().type();
+            }.bind(this);
 
             return new Type.ForwardTypeId(resolve);
         }
@@ -1885,7 +1840,7 @@ exports.TypeCast = ChainedContext.extend({
         this.__type = undefined;
     },
     handleQIdent: function(q){
-        var s = getQIdSymbolAndScope(this, q);
+        var s = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
         s = s.symbol();
         if (!s.isType())
             return; // this is not a type cast, may be procedure call
@@ -2030,10 +1985,7 @@ exports.endCallMsg = endCallMsg;
 exports.Chained = ChainedContext;
 exports.designatorAsExpression = designatorAsExpression;
 exports.endParametersMsg = endParametersMsg;
-exports.getSymbolAndScope = getSymbolAndScope;
-exports.getQIdSymbolAndScope = getQIdSymbolAndScope;
 exports.makeProcCall = makeProcCall;
-exports.unwrapType = unwrapType;
 exports.RelationOps = RelationOps;
 exports.HandleSymbolAsType = HandleSymbolAsType;
 exports.makeContext = makeContext;

+ 6 - 5
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 ContextHierarchy = require("js/ContextHierarchy.js");
 var EberonConstructor= require("js/EberonConstructor.js");
 var EberonContext= require("js/EberonContext.js");
 var EberonDynamicArray = require("js/EberonDynamicArray.js");
@@ -46,8 +47,8 @@ var ProcOrMethodId = Context.Chained.extend({
     },
     handleIdent: function(id){this.__maybeTypeId = id;},
     handleLiteral: function(s){
-        var ss = Context.getSymbolAndScope(this.root(), this.__maybeTypeId);
-        var type = Context.unwrapType(ss.symbol().info());
+        var ss = ContextHierarchy.getSymbolAndScope(this.root(), this.__maybeTypeId);
+        var type = ContextHierarchy.unwrapType(ss.symbol().info());
         if (!(type instanceof Type.Record))
             throw new Errors.Error(
                   "RECORD type expected in method declaration, got '"
@@ -331,7 +332,7 @@ var OperatorNew = Context.Chained.extend({
         this.__call = undefined;
     },
     handleQIdent: function(q){
-        var found = Context.getQIdSymbolAndScope(this, q);
+        var found = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
         var s = found.symbol();
         var info = s.info();
 
@@ -1221,8 +1222,8 @@ var MapDecl = Context.Chained.extend({
         this.__type = undefined;
     },
     handleQIdent: function(q){
-        var s = Context.getQIdSymbolAndScope(this, q);
-        var type = Context.unwrapType(s.symbol().info());
+        var s = ContextHierarchy.getQIdSymbolAndScope(this.root(), q);
+        var type = ContextHierarchy.unwrapType(s.symbol().info());
         this.setType(type);
     },
     // anonymous types can be used in map declaration

+ 87 - 15
src/ob/ContextHierarchy.ob

@@ -1,21 +1,21 @@
 MODULE ContextHierarchy;
-IMPORT CodeGenerator, Module, Scope, Symbols, String;
+IMPORT Cast, CodeGenerator, Errors, LanguageContext, Module, Scope, Symbols, String, Types;
 TYPE
     PRoot = POINTER TO Root;
     PNode = POINTER TO Node;
 
-    Language = RECORD
-        moduleResolver: PROCEDURE(name: STRING): Module.PType;
-        codeGenerator: CodeGenerator.PIGenerator;
-    END;
-    PLanguage = POINTER TO Language;
-
     Message = RECORD
     END;
 
     PMessageResult = POINTER TO RECORD
     END;
 
+    QIdent = RECORD
+        module: Module.PType;
+        id: STRING;
+        code: STRING;
+    END;
+
     Attributes = POINTER TO RECORD
     END;
 
@@ -35,18 +35,18 @@ TYPE
     END;
 
     Root* = RECORD(Node)
-        PROCEDURE Root(language: PLanguage);
+        PROCEDURE Root(language: LanguageContext.PType);
 
-        PROCEDURE language(): PLanguage;
+        PROCEDURE language(): LanguageContext.PType;
 
         PROCEDURE findSymbol(ident: STRING): Symbols.PFoundSymbol;
-        PROCEDURE findModule(name: STRING): Module.PType;
+        PROCEDURE findModule(name: STRING): Types.PModule;
 
         PROCEDURE currentScope(): Scope.PType;
         PROCEDURE pushScope(scope: Scope.PType);
         PROCEDURE popScope();
 
-        mLanguage: PLanguage;
+        mLanguage: LanguageContext.PType;
         scopes: ARRAY * OF Scope.PType;
         gen: INTEGER;
     END;
@@ -86,12 +86,12 @@ PROCEDURE Node.genTypeName(): STRING;
     RETURN SELF.mParent.genTypeName();
 END;
 
-PROCEDURE Root.Root(language: PLanguage)
+PROCEDURE Root.Root(language: LanguageContext.PType)
     | SUPER(NIL),
       mLanguage(language);
 END;
 
-PROCEDURE Root.language(): PLanguage;
+PROCEDURE Root.language(): LanguageContext.PType;
     RETURN SELF.mLanguage;
 END;
 
@@ -114,9 +114,9 @@ BEGIN
     RETURN result;
 END;
 
-PROCEDURE Root.findModule(name: STRING): Module.PType;
+PROCEDURE Root.findModule(name: STRING): Types.PModule;
 VAR
-    result: Module.PType;
+    result: Types.PModule;
 BEGIN
     IF name = "JS" THEN
         result := Module.makeJS();
@@ -150,4 +150,76 @@ PROCEDURE Root.root(): PRoot;
     RETURN SELF(POINTER);
 END;
 
+PROCEDURE getSymbolAndScope*(cx: Root; id: STRING): Symbols.PFoundSymbol;
+BEGIN
+    s <- cx.findSymbol(id);
+    IF s = NIL THEN
+        Errors.raise("undeclared identifier: '" + id + "'");
+    END;
+    RETURN s;
+END;
+
+PROCEDURE getModuleSymbolAndScope*(m: Module.Type; id: STRING): Symbols.PFoundSymbol;
+BEGIN
+    s <- m.findSymbol(id);
+    IF s = NIL THEN
+        Errors.raise("identifier '" + id + "' is not exported by module '" + m.name + "'");
+    END;
+    RETURN s;
+END;
+
+PROCEDURE getQIdSymbolAndScope*(cx: Root; q: QIdent): Symbols.PFoundSymbol;
+VAR 
+    result: Symbols.PFoundSymbol;
+BEGIN
+    IF q.module # NIL THEN
+        result := getModuleSymbolAndScope(q.module^, q.id);
+    ELSE
+        result := getSymbolAndScope(cx, q.id);
+    END;
+    RETURN result;
+END;
+
+PROCEDURE getSymbol*(cx: Root; id: STRING): Symbols.PSymbol;
+    RETURN getSymbolAndScope(cx, id).symbol();
+END;
+
+PROCEDURE unwrapTypeId*(id: Types.PId): Types.PTypeId;
+VAR
+    result: Types.PTypeId;
+BEGIN
+    IF ~(id IS Types.PTypeId) THEN
+        Errors.raise("type name expected");
+    ELSE
+        result := id;
+    END;
+    RETURN result;
+END;
+
+PROCEDURE unwrapType*(id: Types.PId): Types.PType;
+    RETURN unwrapTypeId(id).type();
+END;
+
+PROCEDURE throwTypeMismatch(from, to: Types.PType);
+BEGIN
+    Errors.raise("type mismatch: expected '" + to.description() 
+               + "', got '" + from.description() + "'");
+END;
+
+PROCEDURE checkTypeMatch*(from, to: Types.PType);
+BEGIN
+    IF ~Cast.areTypesMatch(from, to) THEN
+        throwTypeMismatch(from, to);
+    END;
+END;
+
+PROCEDURE checkImplicitCast*(cx: Root; from, to: Types.PType);
+VAR
+    op: LanguageContext.PCastOp;
+BEGIN
+    IF cx.language().types.implicitCast(from, to, FALSE, op) # Cast.errNo THEN
+        throwTypeMismatch(from, to);
+    END;
+END;
+
 END ContextHierarchy.

+ 3 - 1
src/ob/LanguageContext.ob

@@ -1,5 +1,5 @@
 MODULE LanguageContext;
-IMPORT Code, Context, T := Types;
+IMPORT Code, Context, CodeGenerator, T := Types;
 
 TYPE
     PType* = POINTER TO Type;
@@ -19,6 +19,8 @@ TYPE
     PTypes* = POINTER TO Types;
 
     Type* = RECORD(Context.Type)
+        moduleResolver*: PROCEDURE(name: STRING): T.PModule;
+        codeGenerator*: CodeGenerator.PIGenerator;
         types*: PTypes;
     END;
 

+ 1 - 1
src/ob/Module.ob

@@ -2,7 +2,7 @@ MODULE Module;
 IMPORT Code, Context, Errors, LanguageContext, Procedure, Symbols, Types;
 TYPE
     Type* = RECORD(Types.Module)
-        PROCEDURE findSymbol(id: STRING): Symbols.PFoundSymbol
+        PROCEDURE findSymbol*(id: STRING): Symbols.PFoundSymbol
     END;
     PType* = POINTER TO Type;
 

+ 1 - 1
src/ob/Symbols.ob

@@ -22,7 +22,7 @@ TYPE
     FoundSymbol* = RECORD
         PROCEDURE FoundSymbol*(s: PSymbol; scope: ScopeBase.PType);
 
-        PROCEDURE symbol(): PSymbol;
+        PROCEDURE symbol*(): PSymbol;
         PROCEDURE scope*(): ScopeBase.PType;
 
         mSymbol: PSymbol;

+ 1 - 1
src/ob/Types.ob

@@ -252,7 +252,7 @@ TYPE
     Module* = RECORD(Id)
         PROCEDURE Module*(name: STRING);
 
-        name: STRING
+        name-: STRING
     END;
 
     PModule* = POINTER TO Module;