on master help sign in
javascript: added print function
1b87e09raichoo
src/IRTS/CodegenJavaScript.hs+12−2
@@ -47,9 +47,12 @@ idrRuntime :: String
47idrRuntime =
47idrRuntime =
48 createModule Nothing idrNamespace $ concat
48 createModule Nothing idrNamespace $ concat
49 [ "__IDR__.IntType = { type: 'IntType' };"
49 [ "__IDR__.IntType = { type: 'IntType' };"
50+
50 , "__IDR__.Tailcall = function(f) { this.f = f };"
51 , "__IDR__.Tailcall = function(f) { this.f = f };"
52+
51 , "__IDR__.Con = function(i,name,vars)"
53 , "__IDR__.Con = function(i,name,vars)"
52 , "{this.i = i;this.name = name;this.vars = vars;};\n"
54 , "{this.i = i;this.name = name;this.vars = vars;};\n"
55+
53 , "__IDR__.tailcall = function(f){\n"
56 , "__IDR__.tailcall = function(f){\n"
54 ++ "var __f = f;\n"
57 ++ "var __f = f;\n"
55 ++ "while (__f) {\n"
58 ++ "while (__f) {\n"
@@ -62,7 +65,14 @@ idrRuntime =
62 ++ "return ret;"
65 ++ "return ret;"
63 ++ "\n}"
66 ++ "\n}"
64 ++ "\n}"
67 ++ "\n}"
65− ++ "\n}"
68+ ++ "\n};\n"
69+
70+ , "var newline_regex =/(.*)\\n$/;\n"
71+
72+ , "__IDR__.print = function(s){\n"
73+ ++ "var m = s.match(newline_regex);\n"
74+ ++ "console.log(m ? m[1] : s);"
75+ ++ "\n};\n"
66 ]
76 ]
67
77
68createModule :: Maybe String -> NamespaceName -> String -> String
78createModule :: Maybe String -> NamespaceName -> String -> String
@@ -281,7 +291,7 @@ translateExpression _ (SError msg) =
281 "(function(){throw \'" ++ msg ++ "\';})();"
291 "(function(){throw \'" ++ msg ++ "\';})();"
282
292
283translateExpression _ (SForeign _ _ "putStr" [(FString, var)]) =
293translateExpression _ (SForeign _ _ "putStr" [(FString, var)]) =
284− "console.log(" ++ translateVariableName var ++ ");"
294+ "__IDR__.print(" ++ translateVariableName var ++ ");"
285
295
286translateExpression _ (SForeign _ _ fun args) =
296translateExpression _ (SForeign _ _ fun args) =
287 fun
297 fun