on master help sign in
javascript: get more out of google closure
commitfa526e4d360edd4180294f0f524b50fbd11cc009 authorraichoo <raichoo@googlemail.com> dateThu 4 Jul 2013, 23:03 (13 years ago) parentb753590
4 files changed+40−54
js/Runtime-browser.js+1−1 js/Runtime-common.js+16−20 js/Runtime-node.js+1−1 src/IRTS/CodegenJavaScript.hs+22−32
js/Runtime-browser.js+1−1
@@ -1,3 +1,3 @@
1−__IDRRT__.print = function(s) {
1+var __IDRRT__print = function(s) {
22 console.log(s);
33};
js/Runtime-common.js+16−20
@@ -1,36 +1,34 @@
1−var __IDRRT__ = {};
2−
31/** @constructor */
4−__IDRRT__.Type = function(type) {
2+var __IDRRT__Type = function(type) {
53 this.type = type;
64};
75
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__.Ptr = new __IDRRT__.Type('Pointer');
14−__IDRRT__.Forgot = new __IDRRT__.Type('Forgot');
6+var __IDRRT__Int = new __IDRRT__Type('Int');
7+var __IDRRT__Char = new __IDRRT__Type('Char');
8+var __IDRRT__String = new __IDRRT__Type('String');
9+var __IDRRT__Integer = new __IDRRT__Type('Integer');
10+var __IDRRT__Float = new __IDRRT__Type('Float');
11+var __IDRRT__Ptr = new __IDRRT__Type('Pointer');
12+var __IDRRT__Forgot = new __IDRRT__Type('Forgot');
1513
1614
1715/** @constructor */
18−__IDRRT__.Tailcall = function(f) { this.f = f };
19−__IDRRT__.ffiWrap = function(fid) {
16+var __IDRRT__Tailcall = function(f) { this.f = f };
17+var __IDRRT__ffiWrap = function(fid) {
2018 return function(arg) {
21− return __IDRRT__.tailcall(function(){
22− return __IDR__.APPLY0(fid, arg);
19+ return __IDRRT__tailcall(function(){
20+ return __IDR__APPLY0(fid, arg);
2321 });
2422 };
2523};
2624
2725/** @constructor */
28−__IDRRT__.Con = function(i,vars) {
26+var __IDRRT__Con = function(i,vars) {
2927 this.i = i;
3028 this.vars = vars;
3129};
3230
33−__IDRRT__.tailcall = function(f) {
31+var __IDRRT__tailcall = function(f) {
3432 var __f = f;
3533 var ret;
3634 while (__f) {
@@ -38,7 +36,7 @@ __IDRRT__.tailcall = function(f) {
3836 __f = null;
3937 ret = f();
4038
41− if (ret instanceof __IDRRT__.Tailcall) {
39+ if (ret instanceof __IDRRT__Tailcall) {
4240 __f = ret.f;
4341 } else {
4442 return ret;
@@ -50,7 +48,7 @@ __IDRRT__.tailcall = function(f) {
5048 BigInteger Javascript code taken from:
5149 https://github.com/peterolson
5250*/
53−__IDRRT__.bigInt = (function () {
51+var __IDRRT__bigInt = (function () {
5452 var base = 10000000, logBase = 7;
5553 var sign = {
5654 positive: false,
@@ -391,5 +389,3 @@ __IDRRT__.bigInt = (function () {
391389 fnReturn.minusOne = MINUS_ONE;
392390 return fnReturn;
393391})();
394−
395−var __IDR__ = {};
js/Runtime-node.js+1−1
@@ -1,4 +1,4 @@
1−__IDRRT__.print = (function() {
1+var __IDRRT__print = (function() {
22 var util = require('util');
33 return function(s) {
44 util.print(s);
src/IRTS/CodegenJavaScript.hs+22−32
@@ -41,7 +41,6 @@ codegenJavaScript target definitions filename outputType = do
4141 writeFile filename ( header
4242 ++ idrRuntime
4343 ++ tgtRuntime
44− ++ modules
4544 ++ functions
4645 ++ mainLoop)
4746
@@ -56,19 +55,10 @@ codegenJavaScript target definitions filename outputType = do
5655
5756 mainLoop :: String
5857 mainLoop = intercalate "\n" [ "\nfunction main() {"
59− , createTailcall "__IDR__.runMain0()"
58+ , createTailcall "__IDR__runMain0()"
6059 , "}\n\nmain();\n"
6160 ]
6261
63− modules =
64− concatMap allocMod mods
65− where
66− allocMod m = intercalate "." m ++ " = {};\n"
67− sortMods a b = compare (length a) (length b)
68−
69− mods =
70− drop 1 $ sortBy sortMods $ nub $ concatMap (inits . fst) def
71−
7262translateIdentifier :: String -> String
7363translateIdentifier =
7464 replaceReserved . concatMap replaceBadChars
@@ -139,16 +129,16 @@ translateName (MN i name) = translateIdentifier name ++ show i
139129
140130translateConstant :: Const -> String
141131translateConstant (I i) = show i
142−translateConstant (BI i) = "__IDRRT__.bigInt('" ++ show i ++ "')"
132+translateConstant (BI i) = "__IDRRT__bigInt('" ++ show i ++ "')"
143133translateConstant (Fl f) = show f
144134translateConstant (Ch c) = show c
145135translateConstant (Str s) = show s
146−translateConstant (AType (ATInt ITNative)) = "__IDRRT__.Int"
147−translateConstant StrType = "__IDRRT__.String"
148−translateConstant (AType (ATInt ITBig)) = "__IDRRT__.Integer"
149−translateConstant (AType ATFloat) = "__IDRRT__.Float"
150−translateConstant PtrType = "__IDRRT__.Ptr"
151−translateConstant Forgot = "__IDRRT__.Forgot"
136+translateConstant (AType (ATInt ITNative)) = "__IDRRT__Int"
137+translateConstant StrType = "__IDRRT__String"
138+translateConstant (AType (ATInt ITBig)) = "__IDRRT__Integer"
139+translateConstant (AType ATFloat) = "__IDRRT__Float"
140+translateConstant PtrType = "__IDRRT__Ptr"
141+translateConstant Forgot = "__IDRRT__Forgot"
152142translateConstant c =
153143 "(function(){throw 'Unimplemented Const: " ++ show c ++ "';})()"
154144
@@ -159,7 +149,7 @@ translateParameterlist =
159149
160150translateDeclaration :: ([String], SDecl) -> String
161151translateDeclaration (path, SFun name params stackSize body) =
162− intercalate "." (path ++ [translateName name])
152+ "var " ++ concat (path ++ [translateName name])
163153 ++ " = function("
164154 ++ intercalate "," p
165155 ++ "){\n"
@@ -205,12 +195,13 @@ translateExpression (SApp False name vars) =
205195 createTailcall $ translateFunctionCall name vars
206196
207197translateExpression (SApp True name vars) =
208− "new __IDRRT__.Tailcall("
198+ "new __IDRRT__Tailcall("
209199 ++ "function(){\n"
210200 ++ "return " ++ translateFunctionCall name vars
211201 ++ ";\n})"
212202
213203translateExpression (SOp op vars)
204+ | LNoOp <- op = translateVariableName (last vars) ++ "/* NOOP */"
214205 | (LPlus _) <- op
215206 , (lhs:rhs:_) <- vars = translateBinaryOp "+" lhs rhs
216207 | (LMinus _) <- op
@@ -298,13 +289,13 @@ translateExpression (SOp op vars)
298289 | (LIntStr ITNative) <- op
299290 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
300291 | (LSExt ITNative ITBig) <- op
301− , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
292+ , (arg:_) <- vars = "__IDRRT__bigInt(" ++ translateVariableName arg ++ ")"
302293 | (LTrunc ITBig ITNative) <- op
303294 , (arg:_) <- vars = translateVariableName arg ++ ".valueOf()"
304295 | (LIntStr ITBig) <- op
305296 , (arg:_) <- vars = translateVariableName arg ++ ".toString()"
306297 | (LStrInt ITBig) <- op
307− , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
298+ , (arg:_) <- vars = "__IDRRT__bigInt(" ++ translateVariableName arg ++ ")"
308299 | LFloatStr <- op
309300 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
310301 | LStrFloat <- op
@@ -367,7 +358,7 @@ translateExpression (SError msg) =
367358 "(function(){throw \'" ++ msg ++ "\';})()"
368359
369360translateExpression (SForeign _ _ "putStr" [(FString, var)]) =
370− "__IDRRT__.print(" ++ translateVariableName var ++ ")"
361+ "__IDRRT__print(" ++ translateVariableName var ++ ")"
371362
372363translateExpression (SForeign _ _ fun args)
373364 | "[]=" `isSuffixOf` fun
@@ -423,8 +414,8 @@ translateExpression (SForeign _ _ fun args)
423414 '(' : intercalate "," (map generateWrapper as) ++ ")"
424415
425416 generateWrapper (ffunc, name)
426− | FFunction <- ffunc = "__IDRRT__.ffiWrap(" ++ translateVariableName name ++ ")"
427− | FFunctionIO <- ffunc = "__IDRRT__.ffiWrap(" ++ translateVariableName name ++ ")"
417+ | FFunction <- ffunc = "__IDRRT__ffiWrap(" ++ translateVariableName name ++ ")"
418+ | FFunctionIO <- ffunc = "__IDRRT__ffiWrap(" ++ translateVariableName name ++ ")"
428419
429420 generateWrapper (_, name) =
430421 translateVariableName name
@@ -444,7 +435,7 @@ translateExpression (SCase var cases) =
444435 ++ ")"
445436
446437translateExpression (SCon i name vars) =
447− concat [ "new __IDRRT__.Con("
438+ concat [ "new __IDRRT__Con("
448439 , show i
449440 , ",["
450441 , intercalate "," $ map translateVariableName vars
@@ -480,7 +471,7 @@ translateCase var (SConstCase ty e)
480471 matchHelper tyName = translateTypeMatch var tyName e
481472
482473translateCase var (SConstCase cst@(BI _) e) =
483− let cond = "__IDRRT__.bigInt(" ++ var ++ ").equals(" ++ translateConstant cst ++ ")" in
474+ let cond = "__IDRRT__bigInt(" ++ var ++ ").equals(" ++ translateConstant cst ++ ")" in
484475 createIfBlock cond (translateExpression e)
485476
486477translateCase var (SConstCase cst e) =
@@ -488,7 +479,7 @@ translateCase var (SConstCase cst e) =
488479 createIfBlock cond (translateExpression e)
489480
490481translateCase var (SConCase a i name vars e) =
491− let isCon = var ++ " instanceof __IDRRT__.Con"
482+ let isCon = var ++ " instanceof __IDRRT__Con"
492483 isI = show i ++ " == " ++ var ++ ".i"
493484 params = intercalate "," $ map (("__var_" ++) . show) [a..(a+length vars)]
494485 args = ".apply(this," ++ var ++ ".vars)"
@@ -503,7 +494,7 @@ translateTypeMatch :: String -> String -> SExp -> String
503494translateTypeMatch var ty exp =
504495 let e = translateExpression exp in
505496 createIfBlock (var
506− ++ " instanceof __IDRRT__.Type && "
497+ ++ " instanceof __IDRRT__Type && "
507498 ++ var ++ ".type == '"++ ty ++"'") e
508499
509500
@@ -513,11 +504,10 @@ createIfBlock cond e =
513504 ++ ";\n}"
514505
515506createTailcall call =
516− "__IDRRT__.tailcall(function(){return " ++ call ++ "})"
507+ "__IDRRT__tailcall(function(){return " ++ call ++ "})"
517508
518509translateFunctionCall name vars =
519− concat (intersperse "." $ translateNamespace name)
520− ++ "."
510+ concat (translateNamespace name)
521511 ++ translateName name
522512 ++ "("
523513 ++ intercalate "," (map translateVariableName vars)