瀏覽代碼

fix export/import code generation

Vladislav Folts 12 年之前
父節點
當前提交
e6c39d2aaf
共有 4 個文件被更改,包括 67 次插入12 次删除
  1. 10 10
      src/context.js
  2. 2 2
      test/expected/export.js
  3. 38 0
      test/expected/modules.js
  4. 17 0
      test/input/modules.ob

+ 10 - 10
src/context.js

@@ -225,7 +225,10 @@ exports.QualifiedIdentificator = ChainedContext.extend({
     },
     endParse: function(){
         var s = getSymbolAndScope(this.__module ? this.__module : this, this.__id);
-        this.parent().handleSymbol(s, this.__code + this.__id);
+        var code = this.__code + this.__id;
+        if (this.__module && s.symbol().isVariable())
+            code += "()";
+        this.parent().handleSymbol(s, code);
     }
 });
 
@@ -275,17 +278,14 @@ exports.Designator = ChainedContext.extend({
     },
     setIdent: function(id){
         var t = this.__currentType;
-        if (t instanceof Type.Pointer){
+        if (t instanceof Type.Pointer)
             this.__handleDeref();
-            this.__denote(id);
-        }
-        else if (t instanceof Type.Record
-              || t instanceof Type.Module
-              || t instanceof Module.AnyType)
-            this.__denote(id);
-        else
+        else if (!(t instanceof Type.Record
+                || t instanceof Type.Module
+                || t instanceof Module.AnyType))
             throw new Errors.Error("cannot designate '" + t.description() + "'");
 
+        this.__denote(id);
         this.__scope = undefined;
     },
     codeGenerator: function(){return this.__code;},
@@ -1614,7 +1614,7 @@ function genExports(exports, gen){
     var result = "";
     for(var access in exports){
         var e = exports[access];
-        if (e.isVariable() && !(e.info().type() instanceof Type.Pointer))
+        if (e.isVariable())
             access = "function(){return " + access + ";}";
         result += "\t" + e.id() + ": " + access + "\n";
     }

+ 2 - 2
test/expected/export.js

@@ -44,9 +44,9 @@ function p1(){
 return {
 	ci: ci
 	T1: T1
-	pr1: pr1
+	pr1: function(){return pr1;}
 	p2: function(){return p2;}
 	vi: function(){return vi;}
 	p1: p1
 }
-}();
+}();

+ 38 - 0
test/expected/modules.js

@@ -1,13 +1,51 @@
+var RTL$ = {
+    extend: function extend(methods){
+        methods.__proto__ = this.prototype; // make instanceof work
+
+        // to see constructor name in diagnostic
+        var result = methods.init;
+        methods.constructor = result.prototype.constructor;
+
+        result.prototype = methods;
+        result.extend = extend;
+        return result;
+    },
+    makeRef: function (obj, prop){
+        return {set: function(v){ obj[prop] = v; },
+                get: function(){ return obj[prop]; }};
+    }
+};
 var m1 = function (){
+var ci = 123;
+var i = 0;
+var anonymous$1$base = RTL$.extend({
+	init: function anonymous$1$base(){
+		this.i = 0;
+	}
+});
+var pr = null;
 
 function p(){
 }
+pr = new anonymous$1$base();
 return {
+	ci: ci
+	i: function(){return i;}
+	pr: function(){return pr;}
 	p: p
 }
 }();
 var m2 = function (m1){
+
+function p(i/*INTEGER*/){
+}
+
+function ref(i/*VAR INTEGER*/){
+}
 m1.p();
+p(m1.i());
+p(m1.ci);
+ref(RTL$.makeRef(m1.pr(), "i"));
 }(m1);
 var m3 = function (m1, m2){
 m2.p();

+ 17 - 0
test/input/modules.ob

@@ -1,14 +1,31 @@
 MODULE m1;
+CONST
+    ci* = 123;
+VAR
+    i*: INTEGER;
+    pr*: POINTER TO RECORD i: INTEGER END;
 
 PROCEDURE p*();
 END p;
 
+BEGIN
+    NEW(pr);
 END m1.
 
 MODULE m2;
 IMPORT m1;
+
+PROCEDURE p(i: INTEGER);
+END p;
+
+PROCEDURE ref(VAR i: INTEGER);
+END ref;
+
 BEGIN
 	m1.p();
+    p(m1.i);
+    p(m1.ci);
+    ref(m1.pr.i);
 END m2.
 
 MODULE m3;