Ver código fonte

support aliases in IMPORT

Vladislav Folts 12 anos atrás
pai
commit
7322c2703a

+ 33 - 7
src/context.js

@@ -1627,25 +1627,48 @@ exports.ModuleDeclaration = ChainedContext.extend({
     init: function ModuleDeclarationContext(context){
         ChainedContext.prototype.init.bind(this)(context);
         this.__name = undefined;
+        this.__imports = [];
     },
     setIdent: function(id){
-        var gen = this.codeGenerator();
         if (this.__name === undefined ) {
             this.__name = id;
             this.parent().pushScope(new Scope.Module(id));
-            gen.write("var " + id + " = function " + "(){\n");
         }
         else if (id === this.__name){
             var scope = this.parent().currentScope();
             var exports = scope.exports();
             scope.module().info().defineExports(exports);
+            var gen = this.codeGenerator();
             genExports(exports, gen);
-            gen.write("}();\n");
+            gen.write("}(");
+
+            var modules = this.__imports;
+            for(var i = 0; i < modules.length; ++i){
+                var s = modules[i];
+                if (i)
+                    gen.write(", ");
+                gen.write(s.info().id());
+            }
+            gen.write(");\n");
         }
         else
             throw new Errors.Error("original module name '" + this.__name + "' expected, got '" + id + "'" );
     },
-    findModule: function(name){return this.parent().findModule(name);}
+    findModule: function(name){return this.parent().findModule(name);},
+    handleImport: function(modules){
+        var gen = this.codeGenerator();
+        gen.write("var " + this.__name + " = function " + "(");
+        var scope = this.currentScope();
+        for(var i = 0; i < modules.length; ++i){
+            var s = modules[i];
+            scope.addSymbol(s);
+            if (i)
+                gen.write(", ");
+            gen.write(s.id());
+        }
+        gen.write("){\n");
+        this.__imports = modules;
+    }
 });
 
 var ModuleImport = ChainedContext.extend({
@@ -1665,8 +1688,10 @@ var ModuleImport = ChainedContext.extend({
             this.__handleImport();
     },
     endParse: function(){
-        this.__handleImport();
+        if (this.__currentModule)
+            this.__handleImport();
 
+        var modules = [];
         var unresolved  = [];
         for(var alias in this.__import){
             var moduleName = this.__import[alias];
@@ -1674,10 +1699,12 @@ var ModuleImport = ChainedContext.extend({
             if (!module)
                 unresolved.push(moduleName);
             else
-                this.currentScope().addSymbol(new Symbol.Symbol(alias, module));
+                modules.push(new Symbol.Symbol(alias, module));
         }
         if (unresolved.length)
             throw new Errors.Error("module(s) not found: " + unresolved.join(", "));
+        
+        this.parent().handleImport(modules);
     },
     __handleImport: function(){
         var alias = this.__currentAlias;
@@ -1735,7 +1762,6 @@ exports.Context = Class.extend({
     rtl: function(){return this.__rtl;},
     findModule: function(name){
         if (name == "JS"){
-            this.rtl().supportJS();
             return new Module.JS();
         }
         return this.__moduleResolver ? this.__moduleResolver(name) : undefined;

+ 5 - 6
src/grammar.js

@@ -178,13 +178,12 @@ var procedureBody = and(declarationSequence
                       , required("END", "END expected (PROCEDURE)"));
 
 var imprt = and(ident, optional(and(":=", ident)));
-var importList = context(and("IMPORT", imprt, repeat(and(",", imprt))),
-                         Context.ModuleImport);
+var importList = and("IMPORT", imprt, repeat(and(",", imprt)));
 var modul = context(and("MODULE", ident, ";",
-                         optional(and(importList, ";")),
-                         declarationSequence,
-                         optional(and("BEGIN", statementSequence)),
-                         required("END", "END expected (MODULE)"), ident, point),
+                        context(optional(and(importList, ";")), Context.ModuleImport),
+                        declarationSequence,
+                        optional(and("BEGIN", statementSequence)),
+                        required("END", "END expected (MODULE)"), ident, point),
                      Context.ModuleDeclaration);
 
 exports.declarationSequence = declarationSequence;

+ 2 - 1
src/module.js

@@ -17,9 +17,10 @@ var AnyType = Type.Basic.extend({
 var any = new AnyType();
 
 var JSModule = Type.Module.extend({
-	init: function(){
+	init: function Module$JSModule(){
 		Type.Module.prototype.init.call(this);
 	},
+	id: function(){return "this";},
 	findSymbol: function(id){
 		return new Symbol.Found(new Symbol.Symbol(id, any));
 	}

+ 1 - 5
src/rtl.js

@@ -80,7 +80,7 @@ var impl = {
     },
     strToArray: function(s){
         var result = new Array(s.length);
-        for(i = 0; i < s.length; ++i)
+        for(var i = 0; i < s.length; ++i)
             result[i] = s.charCodeAt(i);
         return result;
     },
@@ -106,13 +106,11 @@ exports.Class = Class;
 exports.RTL = Class.extend({
     init: function RTL(){
         this.__entries = {};
-        this.__supportJS = false;
         for(var fName in impl){
             this[fName] = this.__makeOnDemand(fName);
             this[fName + "Id"] = this.__makeIdOnDemand(fName);
         }
     },
-    supportJS: function(){this.__supportJS = true;},
     baseClass: function(){
         if (!this.__entries["extend"])
             this.__entries.extend = Class.extend;
@@ -133,8 +131,6 @@ exports.RTL = Class.extend({
         else
             result = "";
         
-        if (this.__supportJS)
-            result += "var JS = function(){return this;}();\n";
         return result;
     },
     __makeIdOnDemand: function(name){

+ 5 - 3
src/scope.js

@@ -67,10 +67,12 @@ var ProcedureScope = Scope.extend({
 });
 
 var CompiledModule = Type.Module.extend({
-    init: function Module(){
+    init: function Scope$CompiledModule(id){
         Type.Module.prototype.init.call(this);
+        this.__id = id;
         this.__exports = undefined;
     },
+    id: function(){return this.__id;},
     defineExports: function(exports){
         this.__exports = exports;
     },  
@@ -80,11 +82,11 @@ var CompiledModule = Type.Module.extend({
 });
 
 var Module = Scope.extend({
-    init: function Module(name){
+    init: function Scope$Module(name){
         Scope.prototype.init.call(this, "module");
         this.__name = name;
         this.__exports = {};
-        this.__symbol = new Symbol.Symbol(name, new CompiledModule());
+        this.__symbol = new Symbol.Symbol(name, new CompiledModule(name));
         this.addSymbol(this.__symbol);
     },
     module: function(){return this.__symbol;},

+ 1 - 1
src/type.js

@@ -172,7 +172,7 @@ exports.Procedure = BasicType.extend({
 });
 
 var Module = Id.extend({
-    init: function Module(){
+    init: function Type$Module(){
         Id.prototype.init.call(this);
     }
 });

+ 2 - 3
test/expected/js_module.js

@@ -1,4 +1,3 @@
-var JS = function(){return this;}();
-var m = function (){
+var m = function (JS){
 JS.console.info("test");
-}();
+}(this);

+ 2 - 3
test/expected/man_or_boy.js

@@ -11,8 +11,7 @@ var RTL$ = {
         return result;
     }
 };
-var JS = function(){return this;}();
-var test = function (){
+var test = function (JS){
 var State = RTL$.extend({
 	init: function State(){
 		this.f = null;
@@ -78,4 +77,4 @@ function B(s/*PState*/){
 }
 pB = B;
 JS.alert(call(makeState(A, 10, makeEmptyState(F1), makeEmptyState(Fn1), makeEmptyState(Fn1), makeEmptyState(F1), makeEmptyState(F0))));
-}();
+}(this);

+ 5 - 2
test/expected/modules.js

@@ -6,6 +6,9 @@ return {
 	p: p
 }
 }();
-var m2 = function (){
+var m2 = function (m1){
 m1.p();
-}();
+}(m1);
+var m3 = function (m1, m2){
+m2.p();
+}(m2, m1);

+ 2 - 2
test/expected/string.js

@@ -26,7 +26,7 @@ var RTL$ = {
     },
     strToArray: function (s){
         var result = new Array(s.length);
-        for(i = 0; i < s.length; ++i)
+        for(var i = 0; i < s.length; ++i)
             result[i] = s.charCodeAt(i);
         return result;
     },
@@ -56,4 +56,4 @@ p1(RTL$.strToArray(s2));
 p2(34);
 RTL$.assert(ch1 == 34);
 RTL$.assert(34 == ch1);
-}();
+}();

+ 7 - 1
test/input/modules.ob

@@ -9,4 +9,10 @@ MODULE m2;
 IMPORT m1;
 BEGIN
 	m1.p();
-END m2.
+END m2.
+
+MODULE m3;
+IMPORT m1 := m2, m2 := m1;
+BEGIN
+    m2.p();
+END m3.

+ 31 - 24
test/test_compile.js

@@ -1,3 +1,5 @@
+"use strict";
+
 var oc = require("oc");
 var fs = require("fs");
 var path = require("path");
@@ -64,30 +66,35 @@ function makeTests(test, dirs){
     return tests;
 }
 
-if (process.argv.length > 2){
-    var tests = {};
-    var name = process.argv[2];
-    tests[name] = function(){run(name);};
-    Test.run(tests);
-    return;
-}
+function main(){
+    if (process.argv.length > 2){
+        var tests = {};
+        var name = process.argv[2];
+        tests[name] = function(){run(name);};
+        Test.run(tests);
+        return;
+    }
+
+    var okDirs = {input: "input", output: "output", expected: "expected"};
+    var errDirs = {};
+    var runDirs = {};
+    var p;
+    for(p in okDirs)
+        errDirs[p] = okDirs[p] + "/errors";
+    for(p in okDirs)
+        runDirs[p] = okDirs[p] + "/run";
 
-var okDirs = {input: "input", output: "output", expected: "expected"};
-var errDirs = {};
-var runDirs = {};
-for(var p in okDirs)
-    errDirs[p] = okDirs[p] + "/errors";
-for(var p in okDirs)
-    runDirs[p] = okDirs[p] + "/run";
+    if (!fs.existsSync(okDirs.output))
+        fs.mkdirSync(okDirs.output);
+    if (!fs.existsSync(errDirs.output))
+        fs.mkdirSync(errDirs.output);
+    if (!fs.existsSync(runDirs.output))
+        fs.mkdirSync(runDirs.output);
 
-if (!fs.existsSync(okDirs.output))
-    fs.mkdirSync(okDirs.output);
-if (!fs.existsSync(errDirs.output))
-    fs.mkdirSync(errDirs.output);
-if (!fs.existsSync(runDirs.output))
-    fs.mkdirSync(runDirs.output);
+    Test.run({"expect OK": makeTests(expectOk, okDirs),
+              "expect compile error": makeTests(expectError, errDirs),
+              "run": makeTests(run, runDirs)}
+            );
+}
 
-Test.run({"expect OK": makeTests(expectOk, okDirs),
-          "expect compile error": makeTests(expectError, errDirs),
-          "run": makeTests(run, runDirs)}
-        );
+main();