on master help sign in
javascript: move runtime into own namespace
commitca5660dbe66af9288c98244ad674ce5283be4efc authorraichoo <raichoo@googlemail.com> dateFri 25 Jan 2013, 12:38 (14 years ago) parentc48896f
2 files changed+33−29
js/Runtime.js+17−13 src/IRTS/CodegenJavaScript.hs+16−16
js/Runtime.js+17−13
@@ -1,25 +1,27 @@
1+var __IDRRT__ = {};
2+
13/** @constructor */
2−__IDR__.Type = function(type) {
4+__IDRRT__.Type = function(type) {
35 this.type = type;
46};
57
6−__IDR__.Int = new __IDR__.Type('Int');
7−__IDR__.Char = new __IDR__.Type('Char');
8−__IDR__.String = new __IDR__.Type('String');
9−__IDR__.Integer = new __IDR__.Type('Integer');
10−__IDR__.Float = new __IDR__.Type('Float');
11−__IDR__.Forgot = new __IDR__.Type('Forgot');
8+__IDRRT__.Int = new __IDRRT__.Type('Int');
9+__IDRRT__.Char = new __IDRRT__.Type('Char');
10+__IDRRT__.String = new __IDRRT__.Type('String');
11+__IDRRT__.Integer = new __IDRRT__.Type('Integer');
12+__IDRRT__.Float = new __IDRRT__.Type('Float');
13+__IDRRT__.Forgot = new __IDRRT__.Type('Forgot');
1214
1315/** @constructor */
14−__IDR__.Tailcall = function(f) { this.f = f };
16+__IDRRT__.Tailcall = function(f) { this.f = f };
1517
1618/** @constructor */
17−__IDR__.Con = function(i,vars) {
19+__IDRRT__.Con = function(i,vars) {
1820 this.i = i;
1921 this.vars = vars;
2022};
2123
22−__IDR__.tailcall = function(f) {
24+__IDRRT__.tailcall = function(f) {
2325 var __f = f;
2426 var ret;
2527 while (__f) {
@@ -27,7 +29,7 @@ __IDR__.tailcall = function(f) {
2729 __f = null;
2830 ret = f();
2931
30− if (ret instanceof __IDR__.Tailcall) {
32+ if (ret instanceof __IDRRT__.Tailcall) {
3133 __f = ret.f;
3234 } else {
3335 return ret;
@@ -35,7 +37,7 @@ __IDR__.tailcall = function(f) {
3537 }
3638};
3739
38−__IDR__.print = function(s){
40+__IDRRT__.print = function(s){
3941 var m = s.match(/(.*)\n$/);
4042 console.log(m ? m[1] : s);
4143};
@@ -44,7 +46,7 @@ __IDR__.print = function(s){
4446 BigInteger Javascript code taken from:
4547 https://github.com/peterolson
4648*/
47−__IDR__.bigInt = (function () {
49+__IDRRT__.bigInt = (function () {
4850 var base = 10000000, logBase = 7;
4951 var sign = {
5052 positive: false,
@@ -385,3 +387,5 @@ __IDR__.bigInt = (function () {
385387 fnReturn.minusOne = MINUS_ONE;
386388 return fnReturn;
387389})();
390+
391+var __IDR__ = {};
src/IRTS/CodegenJavaScript.hs+16−16
@@ -28,7 +28,7 @@ codegenJavaScript
2828codegenJavaScript definitions filename outputType = do
2929 path <- getDataDir
3030 idrRuntime <- readFile (path ++ "/js/Runtime.js")
31− writeFile filename (modules ++ idrRuntime ++ functions ++ mainLoop)
31+ writeFile filename (idrRuntime ++ modules ++ functions ++ mainLoop)
3232 where
3333 def = map (first translateNamespace) definitions
3434
@@ -97,16 +97,16 @@ translateName (MN i name) = translateIdentifier name ++ show i
9797
9898translateConstant :: Const -> String
9999translateConstant (I i) = show i
100−translateConstant (BI i) = "__IDR__.bigInt('" ++ show i ++ "')"
100+translateConstant (BI i) = "__IDRRT__.bigInt('" ++ show i ++ "')"
101101translateConstant (Fl f) = show f
102102translateConstant (Ch c) = show c
103103translateConstant (Str s) = show s
104−translateConstant IType = "__IDR__.Int"
105−translateConstant ChType = "__IDR__.Char"
106−translateConstant StrType = "__IDR__.String"
107−translateConstant BIType = "__IDR__.Integer"
108−translateConstant FlType = "__IDR__.Float"
109−translateConstant Forgot = "__IDR__.Forgot"
104+translateConstant IType = "__IDRRT__.Int"
105+translateConstant ChType = "__IDRRT__.Char"
106+translateConstant StrType = "__IDRRT__.String"
107+translateConstant BIType = "__IDRRT__.Integer"
108+translateConstant FlType = "__IDRRT__.Float"
109+translateConstant Forgot = "__IDRRT__.Forgot"
110110translateConstant c =
111111 "(function(){throw 'Unimplemented Const: " ++ show c ++ "';})()"
112112
@@ -163,7 +163,7 @@ translateExpression (SApp False name vars) =
163163 createTailcall $ translateFunctionCall name vars
164164
165165translateExpression (SApp True name vars) =
166− "new __IDR__.Tailcall("
166+ "new __IDRRT__.Tailcall("
167167 ++ "function(){\n"
168168 ++ "return " ++ translateFunctionCall name vars
169169 ++ ";\n});"
@@ -256,13 +256,13 @@ translateExpression (SOp op vars)
256256 | LIntStr <- op
257257 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
258258 | LIntBig <- op
259− , (arg:_) <- vars = "__IDR__.bigInt(" ++ translateVariableName arg ++ ")"
259+ , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
260260 | LBigInt <- op
261261 , (arg:_) <- vars = translateVariableName arg ++ ".valueOf()"
262262 | LBigStr <- op
263263 , (arg:_) <- vars = translateVariableName arg ++ ".toString()"
264264 | LStrBig <- op
265− , (arg:_) <- vars = "__IDR__.bigInt(" ++ translateVariableName arg ++ ")"
265+ , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
266266 | LFloatStr <- op
267267 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
268268 | LStrFloat <- op
@@ -325,7 +325,7 @@ translateExpression (SError msg) =
325325 "(function(){throw \'" ++ msg ++ "\';})();"
326326
327327translateExpression (SForeign _ _ "putStr" [(FString, var)]) =
328− "__IDR__.print(" ++ translateVariableName var ++ ");"
328+ "__IDRRT__.print(" ++ translateVariableName var ++ ");"
329329
330330translateExpression (SForeign _ _ fun args) =
331331 fun
@@ -348,7 +348,7 @@ translateExpression (SCase var cases) =
348348 ++ ")"
349349
350350translateExpression (SCon i name vars) =
351− concat [ "new __IDR__.Con("
351+ concat [ "new __IDRRT__.Con("
352352 , show i
353353 , ",["
354354 , intercalate "," $ map translateVariableName vars
@@ -391,7 +391,7 @@ translateCase var (SConstCase cst e) =
391391 createIfBlock cond (translateExpression e)
392392
393393translateCase var (SConCase a i name vars e) =
394− let isCon = var ++ " instanceof __IDR__.Con"
394+ let isCon = var ++ " instanceof __IDRRT__.Con"
395395 isI = show i ++ " == " ++ var ++ ".i"
396396 params = intercalate "," $ map (("__var_" ++) . show) [a..(a+length vars)]
397397 args = ".apply(this," ++ var ++ ".vars)"
@@ -406,7 +406,7 @@ translateTypeMatch :: String -> String -> SExp -> String
406406translateTypeMatch var ty exp =
407407 let e = translateExpression exp in
408408 createIfBlock (var
409− ++ " instanceof __IDR__.Type && "
409+ ++ " instanceof __IDRRT__.Type && "
410410 ++ var ++ ".type == '"++ ty ++"'") e
411411
412412
@@ -416,7 +416,7 @@ createIfBlock cond e =
416416 ++ ";\n}"
417417
418418createTailcall call =
419− "__IDR__.tailcall(function(){return " ++ call ++ "})"
419+ "__IDRRT__.tailcall(function(){return " ++ call ++ "})"
420420
421421translateFunctionCall name vars =
422422 concat (intersperse "." $ translateNamespace name)