ソースを参照

Merge pull request #28 from vladfolts/rel_v1.0

fixes from rel v1.0
vladfolts 11 年 前
コミット
559e80a9ed
5 ファイル変更74 行追加34 行削除
  1. 5 5
      src/context.js
  2. 1 1
      test/compile.cmd
  3. 49 12
      test/compile.js
  4. 16 14
      test/expected/var_parameter.js
  5. 3 2
      test/input/var_parameter.ob

+ 5 - 5
src/context.js

@@ -390,13 +390,13 @@ exports.Designator = ChainedContext.extend({
             new DesignatorInfo(code, refCode, this.__currentType, this.__info, this.__scope));
     },
     __makeRefCode: function(code){
+        if ((this.__currentType instanceof Type.Array)
+            || (this.__currentType instanceof Type.Record)
+            || (this.__info instanceof Type.VariableRef))
+            return code;
         if (this.__derefCode)
             return this.rtl().makeRef(this.__derefCode, this.__propCode);
-        if (!(this.__currentType instanceof Type.Array)
-            && !(this.__currentType instanceof Type.Record)
-            && !(this.__info instanceof Type.VariableRef))
-            return "{set: function($v){" + code + " = $v;}, get: function(){return " + code + ";}}";
-        return code;
+        return "{set: function($v){" + code + " = $v;}, get: function(){return " + code + ";}}";
     }
 });
 

+ 1 - 1
test/compile.cmd

@@ -1,2 +1,2 @@
 @SET NODE_PATH=.;%~dp0../src
-@"C:\Program Files\nodejs\node.exe" compile.js %*
+@"C:\Program Files\nodejs\node.exe" %~dp0\compile.js %*

+ 49 - 12
test/compile.js

@@ -2,25 +2,62 @@
 
 var oc = require("oc");
 var fs = require("fs");
-var path = require("path");
+var rtl = require("rtl.js");
+var Stream = require("stream.js").Stream;
+var Class = rtl.Class;
+
+var Compiler = Class.extend({
+    init: function Compiler(){
+        this.__rtl = new rtl.RTL();
+        this.__code = "";
+        this.__errors = "";
+        this.__modules = {};
+    },
+    addFile: function(file){
+        console.info("compiling '" + file + "...")
+        var text = fs.readFileSync(file, "utf8");
+        var stream = new Stream(text);
+        var module = oc.compileModule(
+              stream,
+              this.__rtl,
+              this.__resolveModule.bind(this),
+              function(e){this.__handleErrors(file, e);}.bind(this)
+              );
+        if (!module)
+            return undefined;
+
+        this.__code += module.code();
+
+        var symbol = module.symbol();
+        this.__modules[symbol.id()] = symbol.info();
+        return symbol.info();
+    },
+    code: function(){return this.__rtl.generate() + this.__code;},
+    errors: function(){return this.__errors;},
+    __resolveModule: function(name){
+        var compiled = this.__modules[name];
+        if (compiled)
+            return compiled;
+
+        var fileName = name + ".ob";
+        return this.addFile(fileName);
+    },
+    __handleErrors: function(file, e){
+        this.__errors += "File \"" + file + "\", " + e;
+    }
+});
 
 function compile(src){
-    var text = fs.readFileSync(src, "utf8");
-    var errors = "";
-    var result = oc.compile(text, function(e){errors += "File \"" + src + "\", " + e;});
+    var compiler = new Compiler();
+    compiler.addFile(src);
+    var errors = compiler.errors();
     if (errors){
         console.info(errors);
         return;
     }
 
-    var fileName = path.basename(src);
-    if (path.extname(fileName) != ".ob"){
-        console.info(result);
-        return;
-    }
-
-    fileName = fileName.substr(0, fileName.length - ".ob".length) + ".js";
-    fs.writeFileSync(fileName, result);
+    var fileName = "a.js";
+    fs.writeFileSync(fileName, compiler.code());
     console.info("compiled to '" + fileName + "' - OK!");
 
 }

+ 16 - 14
test/expected/var_parameter.js

@@ -1,18 +1,4 @@
 var RTL$ = {
-    extend: function extend(methods){
-        function Type(){
-            for(var m in methods)
-                this[m] = methods[m];
-        }
-        Type.prototype = this.prototype;
-
-        var result = methods.init;
-        result.prototype = new Type(); // inherit this.prototype
-        result.prototype.constructor = result; // to see constructor name in diagnostic
-        
-        result.extend = extend;
-        return result;
-    },
     makeArray: function (/*dimensions, initializer*/){
         var forward = Array.prototype.slice.call(arguments);
         var result = new Array(forward.shift());
@@ -31,6 +17,20 @@ var RTL$ = {
                 result[i] = this.makeArray.apply(this, forward);
         return result;
     },
+    extend: function extend(methods){
+        function Type(){
+            for(var m in methods)
+                this[m] = methods[m];
+        }
+        Type.prototype = this.prototype;
+
+        var result = methods.init;
+        result.prototype = new Type(); // inherit this.prototype
+        result.prototype.constructor = result; // to see constructor name in diagnostic
+        
+        result.extend = extend;
+        return result;
+    },
     makeRef: function (obj, prop){
         return {set: function(v){ obj[prop] = v; },
                 get: function(){ return obj[prop]; }};
@@ -40,6 +40,7 @@ var m = function (){
 var R = RTL$.extend({
 	init: function R(){
 		this.i = 0;
+		this.a = RTL$.makeArray(3, 0);
 		this.p = null;
 	}
 });
@@ -84,6 +85,7 @@ function p3(i/*VAR INTEGER*/, b/*VAR BOOLEAN*/){
 	p1(RTL$.makeRef(r.p, "i"), RTL$.makeRef(ar[j].p, "i"));
 	p2(ar[j].p.i, r.p.i == ar[j].p.i);
 	j = array(ai);
+	j = array(r.a);
 }
 p3({set: function($v){i = $v;}, get: function(){return i;}}, {set: function($v){b = $v;}, get: function(){return b;}});
 }();

+ 3 - 2
test/input/var_parameter.ob

@@ -1,6 +1,6 @@
 MODULE m;
 
-TYPE R = RECORD i: INTEGER; p: POINTER TO R END;
+TYPE R = RECORD i: INTEGER; a: ARRAY 3 OF INTEGER; p: POINTER TO R END;
 
 VAR 
 	i: INTEGER;
@@ -48,7 +48,8 @@ BEGIN
 	p1(r.p.i, ar[j].p.i);
 	p2(ar[j].p.i, r.p.i = ar[j].p.i);
 
-	j := array(ai)
+	j := array(ai);
+	j := array(r.a);
 END p3;
 
 BEGIN