Bläddra i källkod

Implement JS.do function to insert JavaScript code to generated code.

Vladislav Folts 12 år sedan
förälder
incheckning
3c0cfed82b
3 ändrade filer med 64 tillägg och 15 borttagningar
  1. 61 14
      src/module.js
  2. 1 0
      test/expected/js_module.js
  3. 2 1
      test/input/js_module.ob

+ 61 - 14
src/module.js

@@ -1,29 +1,76 @@
 "use strict";
 
+var Code = require("code.js");
+var Errors = require("errors.js");
 var Procedure = require("procedure.js");
 var Symbol = require("symbol.js");
 var Type = require("type.js");
 
 var AnyType = Type.Basic.extend({
-	init: function AnyType(){
-		Type.Basic.prototype.init.call(this, "ANY");
-	},
-	findSymbol: function(){return this;},
-	callGenerator: function(context, id){
-		return new Procedure.CallGenerator(context, id);
-	}
+    init: function AnyType(){
+        Type.Basic.prototype.init.call(this, "ANY");
+    },
+    findSymbol: function(){return this;},
+    callGenerator: function(context, id){
+        return new Procedure.CallGenerator(context, id);
+    }
 });
 
 var any = new AnyType();
 
+var doProcId = "do$";
+
+var doProcSymbol = (function(){
+    var description = "JS predefined procedure 'do'";
+
+    var DoProcCallGenerator = Procedure.CallGenerator.extend({
+        init: function DoProcCallGenerator(context, id, type){
+            Procedure.CallGenerator.prototype.init.call(this, context, id, type);
+            this.__code = undefined;
+        },
+        prolog: function(id){return "";},
+        checkArgument: function(pos, e){
+            var type = e.type();
+            if (!(type instanceof Type.String))
+                throw new Errors.Error(
+                    "string is expected as an argument of " + description);
+            
+            this.__code = type.value();
+            return Procedure.CallGenerator.prototype.checkArgument.call(this, pos, e);
+        },
+        epilog: function(){return "";},
+        writeArgumentCode: function(){},
+        callExpression: function(){
+            return new Code.Expression(this.__code);
+        }
+    });
+
+    var args = [new Procedure.Arg(undefined, false)];
+    var ProcType = Type.Procedure.extend({
+        init: function(){
+            Type.Procedure.prototype.init.call(this, doProcId);
+        },
+        description: function(){return description;},
+        args: function(){return args;},
+        result: function(){return undefined;},
+        callGenerator: function(context, id){
+            return new DoProcCallGenerator(context, id, this);
+        }
+        });
+
+    return new Symbol.Symbol(doProcId, new ProcType());
+})();
+
 var JSModule = Type.Module.extend({
-	init: function Module$JSModule(){
-		Type.Module.prototype.init.call(this);
-	},
-	name: function(){return "this";},
-	findSymbol: function(id){
-		return new Symbol.Found(new Symbol.Symbol(id, any));
-	}
+    init: function Module$JSModule(){
+        Type.Module.prototype.init.call(this);
+    },
+    name: function(){return "this";},
+    findSymbol: function(id){
+        return new Symbol.Found(
+            id == doProcId ? doProcSymbol
+                           : new Symbol.Symbol(id, any));
+    }
 });
 
 exports.AnyType = AnyType;

+ 1 - 0
test/expected/js_module.js

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

+ 2 - 1
test/input/js_module.ob

@@ -1,5 +1,6 @@
 MODULE m;
 IMPORT JS;
 BEGIN
-	JS.console.info("test")
+	JS.console.info("test");
+	JS.do("var x = 123; ++x");
 END m.