on master help sign in
javascript: move runtime into own namespace
ca5660draichoo
js/Runtime.js+17−13
@@ -1,25 +1,27 @@
1+var __IDRRT__ = {};
2+
1/** @constructor */
3/** @constructor */
2−__IDR__.Type = function(type) {
4+__IDRRT__.Type = function(type) {
3 this.type = type;
5 this.type = type;
4};
6};
5
7
6−__IDR__.Int = new __IDR__.Type('Int');
8+__IDRRT__.Int = new __IDRRT__.Type('Int');
7−__IDR__.Char = new __IDR__.Type('Char');
9+__IDRRT__.Char = new __IDRRT__.Type('Char');
8−__IDR__.String = new __IDR__.Type('String');
10+__IDRRT__.String = new __IDRRT__.Type('String');
9−__IDR__.Integer = new __IDR__.Type('Integer');
11+__IDRRT__.Integer = new __IDRRT__.Type('Integer');
10−__IDR__.Float = new __IDR__.Type('Float');
12+__IDRRT__.Float = new __IDRRT__.Type('Float');
11−__IDR__.Forgot = new __IDR__.Type('Forgot');
13+__IDRRT__.Forgot = new __IDRRT__.Type('Forgot');
12
14
13/** @constructor */
15/** @constructor */
14−__IDR__.Tailcall = function(f) { this.f = f };
16+__IDRRT__.Tailcall = function(f) { this.f = f };
15
17
16/** @constructor */
18/** @constructor */
17−__IDR__.Con = function(i,vars) {
19+__IDRRT__.Con = function(i,vars) {
18 this.i = i;
20 this.i = i;
19 this.vars = vars;
21 this.vars = vars;
20};
22};
21
23
22−__IDR__.tailcall = function(f) {
24+__IDRRT__.tailcall = function(f) {
23 var __f = f;
25 var __f = f;
24 var ret;
26 var ret;
25 while (__f) {
27 while (__f) {
@@ -27,7 +29,7 @@ __IDR__.tailcall = function(f) {
27 __f = null;
29 __f = null;
28 ret = f();
30 ret = f();
29
31
30− if (ret instanceof __IDR__.Tailcall) {
32+ if (ret instanceof __IDRRT__.Tailcall) {
31 __f = ret.f;
33 __f = ret.f;
32 } else {
34 } else {
33 return ret;
35 return ret;
@@ -35,7 +37,7 @@ __IDR__.tailcall = function(f) {
35 }
37 }
36};
38};
37
39
38−__IDR__.print = function(s){
40+__IDRRT__.print = function(s){
39 var m = s.match(/(.*)\n$/);
41 var m = s.match(/(.*)\n$/);
40 console.log(m ? m[1] : s);
42 console.log(m ? m[1] : s);
41};
43};
@@ -44,7 +46,7 @@ __IDR__.print = function(s){
44 BigInteger Javascript code taken from:
46 BigInteger Javascript code taken from:
45 https://github.com/peterolson
47 https://github.com/peterolson
46*/
48*/
47−__IDR__.bigInt = (function () {
49+__IDRRT__.bigInt = (function () {
48 var base = 10000000, logBase = 7;
50 var base = 10000000, logBase = 7;
49 var sign = {
51 var sign = {
50 positive: false,
52 positive: false,
@@ -385,3 +387,5 @@ __IDR__.bigInt = (function () {
385 fnReturn.minusOne = MINUS_ONE;
387 fnReturn.minusOne = MINUS_ONE;
386 return fnReturn;
388 return fnReturn;
387})();
389})();
390+
391+var __IDR__ = {};
src/IRTS/CodegenJavaScript.hs+16−16
@@ -28,7 +28,7 @@ codegenJavaScript
28codegenJavaScript definitions filename outputType = do
28codegenJavaScript definitions filename outputType = do
29 path <- getDataDir
29 path <- getDataDir
30 idrRuntime <- readFile (path ++ "/js/Runtime.js")
30 idrRuntime <- readFile (path ++ "/js/Runtime.js")
31− writeFile filename (modules ++ idrRuntime ++ functions ++ mainLoop)
31+ writeFile filename (idrRuntime ++ modules ++ functions ++ mainLoop)
32 where
32 where
33 def = map (first translateNamespace) definitions
33 def = map (first translateNamespace) definitions
34
34
@@ -97,16 +97,16 @@ translateName (MN i name) = translateIdentifier name ++ show i
97
97
98translateConstant :: Const -> String
98translateConstant :: Const -> String
99translateConstant (I i) = show i
99translateConstant (I i) = show i
100−translateConstant (BI i) = "__IDR__.bigInt('" ++ show i ++ "')"
100+translateConstant (BI i) = "__IDRRT__.bigInt('" ++ show i ++ "')"
101translateConstant (Fl f) = show f
101translateConstant (Fl f) = show f
102translateConstant (Ch c) = show c
102translateConstant (Ch c) = show c
103translateConstant (Str s) = show s
103translateConstant (Str s) = show s
104−translateConstant IType = "__IDR__.Int"
104+translateConstant IType = "__IDRRT__.Int"
105−translateConstant ChType = "__IDR__.Char"
105+translateConstant ChType = "__IDRRT__.Char"
106−translateConstant StrType = "__IDR__.String"
106+translateConstant StrType = "__IDRRT__.String"
107−translateConstant BIType = "__IDR__.Integer"
107+translateConstant BIType = "__IDRRT__.Integer"
108−translateConstant FlType = "__IDR__.Float"
108+translateConstant FlType = "__IDRRT__.Float"
109−translateConstant Forgot = "__IDR__.Forgot"
109+translateConstant Forgot = "__IDRRT__.Forgot"
110translateConstant c =
110translateConstant c =
111 "(function(){throw 'Unimplemented Const: " ++ show c ++ "';})()"
111 "(function(){throw 'Unimplemented Const: " ++ show c ++ "';})()"
112
112
@@ -163,7 +163,7 @@ translateExpression (SApp False name vars) =
163 createTailcall $ translateFunctionCall name vars
163 createTailcall $ translateFunctionCall name vars
164
164
165translateExpression (SApp True name vars) =
165translateExpression (SApp True name vars) =
166− "new __IDR__.Tailcall("
166+ "new __IDRRT__.Tailcall("
167 ++ "function(){\n"
167 ++ "function(){\n"
168 ++ "return " ++ translateFunctionCall name vars
168 ++ "return " ++ translateFunctionCall name vars
169 ++ ";\n});"
169 ++ ";\n});"
@@ -256,13 +256,13 @@ translateExpression (SOp op vars)
256 | LIntStr <- op
256 | LIntStr <- op
257 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
257 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
258 | LIntBig <- op
258 | LIntBig <- op
259− , (arg:_) <- vars = "__IDR__.bigInt(" ++ translateVariableName arg ++ ")"
259+ , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
260 | LBigInt <- op
260 | LBigInt <- op
261 , (arg:_) <- vars = translateVariableName arg ++ ".valueOf()"
261 , (arg:_) <- vars = translateVariableName arg ++ ".valueOf()"
262 | LBigStr <- op
262 | LBigStr <- op
263 , (arg:_) <- vars = translateVariableName arg ++ ".toString()"
263 , (arg:_) <- vars = translateVariableName arg ++ ".toString()"
264 | LStrBig <- op
264 | LStrBig <- op
265− , (arg:_) <- vars = "__IDR__.bigInt(" ++ translateVariableName arg ++ ")"
265+ , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
266 | LFloatStr <- op
266 | LFloatStr <- op
267 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
267 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
268 | LStrFloat <- op
268 | LStrFloat <- op
@@ -325,7 +325,7 @@ translateExpression (SError msg) =
325 "(function(){throw \'" ++ msg ++ "\';})();"
325 "(function(){throw \'" ++ msg ++ "\';})();"
326
326
327translateExpression (SForeign _ _ "putStr" [(FString, var)]) =
327translateExpression (SForeign _ _ "putStr" [(FString, var)]) =
328− "__IDR__.print(" ++ translateVariableName var ++ ");"
328+ "__IDRRT__.print(" ++ translateVariableName var ++ ");"
329
329
330translateExpression (SForeign _ _ fun args) =
330translateExpression (SForeign _ _ fun args) =
331 fun
331 fun
@@ -348,7 +348,7 @@ translateExpression (SCase var cases) =
348 ++ ")"
348 ++ ")"
349
349
350translateExpression (SCon i name vars) =
350translateExpression (SCon i name vars) =
351− concat [ "new __IDR__.Con("
351+ concat [ "new __IDRRT__.Con("
352 , show i
352 , show i
353 , ",["
353 , ",["
354 , intercalate "," $ map translateVariableName vars
354 , intercalate "," $ map translateVariableName vars
@@ -391,7 +391,7 @@ translateCase var (SConstCase cst e) =
391 createIfBlock cond (translateExpression e)
391 createIfBlock cond (translateExpression e)
392
392
393translateCase var (SConCase a i name vars e) =
393translateCase var (SConCase a i name vars e) =
394− let isCon = var ++ " instanceof __IDR__.Con"
394+ let isCon = var ++ " instanceof __IDRRT__.Con"
395 isI = show i ++ " == " ++ var ++ ".i"
395 isI = show i ++ " == " ++ var ++ ".i"
396 params = intercalate "," $ map (("__var_" ++) . show) [a..(a+length vars)]
396 params = intercalate "," $ map (("__var_" ++) . show) [a..(a+length vars)]
397 args = ".apply(this," ++ var ++ ".vars)"
397 args = ".apply(this," ++ var ++ ".vars)"
@@ -406,7 +406,7 @@ translateTypeMatch :: String -> String -> SExp -> String
406translateTypeMatch var ty exp =
406translateTypeMatch var ty exp =
407 let e = translateExpression exp in
407 let e = translateExpression exp in
408 createIfBlock (var
408 createIfBlock (var
409− ++ " instanceof __IDR__.Type && "
409+ ++ " instanceof __IDRRT__.Type && "
410 ++ var ++ ".type == '"++ ty ++"'") e
410 ++ var ++ ".type == '"++ ty ++"'") e
411
411
412
412
@@ -416,7 +416,7 @@ createIfBlock cond e =
416 ++ ";\n}"
416 ++ ";\n}"
417
417
418createTailcall call =
418createTailcall call =
419− "__IDR__.tailcall(function(){return " ++ call ++ "})"
419+ "__IDRRT__.tailcall(function(){return " ++ call ++ "})"
420
420
421translateFunctionCall name vars =
421translateFunctionCall name vars =
422 concat (intersperse "." $ translateNamespace name)
422 concat (intersperse "." $ translateNamespace name)