1{-# LANGUAGE PatternGuards #-}
3module IRTS.CodegenJavaScript (codegenJavaScript) where
9import IRTS.CodegenCommon
17import qualified Data.Map as Map
21idrNamespace = "__IDR__"
28codegenJavaScript definitions filename outputType = do
30 idrRuntime <- readFile (path ++ "/js/Runtime.js")
31 writeFile filename (idrRuntime ++ modules ++ functions ++ mainLoop)
33 def = map (first translateNamespace) definitions
35 functions = concatMap translateDeclaration def
38 mainLoop = intercalate "\n" [ "\nfunction main() {"
39 , createTailcall "__IDR__.runMain0()"
44 concatMap allocMod mods
46 allocMod m = intercalate "." m ++ " = {};\n"
47 sortMods a b = compare (length a) (length b)
50 drop 1 $ sortBy sortMods $ nub $ concatMap (inits . fst) def
52translateIdentifier :: String -> String
54 concatMap replaceBadChars
55 where replaceBadChars :: Char -> String
56 replaceBadChars ' ' = "_"
57 replaceBadChars '_' = "__"
58 replaceBadChars '@' = "_at"
59 replaceBadChars '[' = "_OSB"
60 replaceBadChars ']' = "_CSB"
61 replaceBadChars '(' = "_OP"
62 replaceBadChars ')' = "_CP"
63 replaceBadChars '{' = "_OB"
64 replaceBadChars '}' = "_CB"
65 replaceBadChars '!' = "_bang"
66 replaceBadChars '#' = "_hash"
67 replaceBadChars '.' = "_dot"
68 replaceBadChars ',' = "_comma"
69 replaceBadChars ':' = "_colon"
70 replaceBadChars '+' = "_plus"
71 replaceBadChars '-' = "_minus"
72 replaceBadChars '*' = "_times"
73 replaceBadChars '<' = "_lt"
74 replaceBadChars '>' = "_gt"
75 replaceBadChars '=' = "_eq"
76 replaceBadChars '|' = "_pipe"
77 replaceBadChars '&' = "_amp"
78 replaceBadChars '/' = "_SL"
79 replaceBadChars '\\' = "_BSL"
80 replaceBadChars '%' = "_per"
81 replaceBadChars '?' = "_que"
82 replaceBadChars '~' = "_til"
83 replaceBadChars '\'' = "_apo"
85 | isDigit c = "_" ++ [c] ++ "_"
88translateNamespace :: Name -> [String]
89translateNamespace (UN _) = [idrNamespace]
90translateNamespace (NS _ ns) = idrNamespace : map translateIdentifier ns
91translateNamespace (MN _ _) = [idrNamespace]
93translateName :: Name -> String
94translateName (UN name) = translateIdentifier name
95translateName (NS name _) = translateName name
96translateName (MN i name) = translateIdentifier name ++ show i
98translateConstant :: Const -> String
99translateConstant (I i) = show i
100translateConstant (BI i) = "__IDRRT__.bigInt('" ++ show i ++ "')"
101translateConstant (Fl f) = show f
102translateConstant (Ch c) = show c
103translateConstant (Str s) = show s
104translateConstant IType = "__IDRRT__.Int"
105translateConstant ChType = "__IDRRT__.Char"
106translateConstant StrType = "__IDRRT__.String"
107translateConstant BIType = "__IDRRT__.Integer"
108translateConstant FlType = "__IDRRT__.Float"
109translateConstant Forgot = "__IDRRT__.Forgot"
111 "(function(){throw 'Unimplemented Const: " ++ show c ++ "';})()"
113translateParameterlist =
114 map translateParameter
115 where translateParameter (MN i name) = name ++ show i
116 translateParameter (UN name) = name
118translateDeclaration :: ([String], SDecl) -> String
119translateDeclaration (path, SFun name params stackSize body) =
120 intercalate "." (path ++ [translateName name])
124 ++ concatMap assignVar (zip [0..] p)
125 ++ concatMap allocVar [numP..(numP+stackSize-1)]
127 ++ translateExpression body
133 allocVar :: Int -> String
134 allocVar n = "var __var_" ++ show n ++ ";\n"
136 assignVar :: (Int, String) -> String
137 assignVar (n, s) = "var __var_" ++ show n ++ " = " ++ s ++ ";\n"
140 p = translateParameterlist params
142translateVariableName :: LVar -> String
143translateVariableName (Loc i) =
146translateExpression :: SExp -> String
147translateExpression (SLet name value body) =
149 ++ translateVariableName name
151 ++ translateExpression body
153 ++ translateExpression value
156translateExpression (SConst cst) =
157 translateConstant cst
159translateExpression (SV var) =
160 translateVariableName var
162translateExpression (SApp False name vars) =
163 createTailcall $ translateFunctionCall name vars
165translateExpression (SApp True name vars) =
166 "new __IDRRT__.Tailcall("
168 ++ "return " ++ translateFunctionCall name vars
171translateExpression (SOp op vars)
173 , (lhs:rhs:_) <- vars = translateBinaryOp "+" lhs rhs
175 , (lhs:rhs:_) <- vars = translateBinaryOp "-" lhs rhs
177 , (lhs:rhs:_) <- vars = translateBinaryOp "*" lhs rhs
179 , (lhs:rhs:_) <- vars = translateBinaryOp "/" lhs rhs
181 , (lhs:rhs:_) <- vars = translateBinaryOp "%" lhs rhs
183 , (lhs:rhs:_) <- vars = translateBinaryOp "==" lhs rhs
185 , (lhs:rhs:_) <- vars = translateBinaryOp "<" lhs rhs
187 , (lhs:rhs:_) <- vars = translateBinaryOp "<=" lhs rhs
189 , (lhs:rhs:_) <- vars = translateBinaryOp ">" lhs rhs
191 , (lhs:rhs:_) <- vars = translateBinaryOp ">=" lhs rhs
193 , (lhs:rhs:_) <- vars = translateBinaryOp "&" lhs rhs
195 , (lhs:rhs:_) <- vars = translateBinaryOp "|" lhs rhs
197 , (lhs:rhs:_) <- vars = translateBinaryOp "^" lhs rhs
199 , (lhs:rhs:_) <- vars = translateBinaryOp "<<" rhs lhs
201 , (lhs:rhs:_) <- vars = translateBinaryOp ">>" rhs lhs
203 , (arg:_) <- vars = '~' : translateVariableName arg
206 , (lhs:rhs:_) <- vars = translateBinaryOp ".add(" lhs rhs ++ ")"
208 , (lhs:rhs:_) <- vars = translateBinaryOp ".minus(" lhs rhs ++ ")"
210 , (lhs:rhs:_) <- vars = translateBinaryOp ".times(" lhs rhs ++ ")"
212 , (lhs:rhs:_) <- vars = translateBinaryOp ".divide(" lhs rhs ++ ")"
214 , (lhs:rhs:_) <- vars = translateBinaryOp ".mod(" lhs rhs ++ ")"
216 , (lhs:rhs:_) <- vars = translateBinaryOp ".equals(" lhs rhs ++ ")"
218 , (lhs:rhs:_) <- vars = translateBinaryOp ".lesser(" lhs rhs ++ ")"
220 , (lhs:rhs:_) <- vars = translateBinaryOp ".lesserOrEquals(" lhs rhs ++ ")"
222 , (lhs:rhs:_) <- vars = translateBinaryOp ".greater(" lhs rhs ++ ")"
224 , (lhs:rhs:_) <- vars = translateBinaryOp ".greaterOrEquals(" lhs rhs ++ ")"
227 , (lhs:rhs:_) <- vars = translateBinaryOp "+" lhs rhs
229 , (lhs:rhs:_) <- vars = translateBinaryOp "-" lhs rhs
231 , (lhs:rhs:_) <- vars = translateBinaryOp "*" lhs rhs
233 , (lhs:rhs:_) <- vars = translateBinaryOp "/" lhs rhs
235 , (lhs:rhs:_) <- vars = translateBinaryOp "==" lhs rhs
237 , (lhs:rhs:_) <- vars = translateBinaryOp "<" lhs rhs
239 , (lhs:rhs:_) <- vars = translateBinaryOp "<=" lhs rhs
241 , (lhs:rhs:_) <- vars = translateBinaryOp ">" lhs rhs
243 , (lhs:rhs:_) <- vars = translateBinaryOp ">=" lhs rhs
246 , (lhs:rhs:_) <- vars = translateBinaryOp "+" lhs rhs
248 , (lhs:rhs:_) <- vars = translateBinaryOp "==" lhs rhs
250 , (lhs:rhs:_) <- vars = translateBinaryOp "<" lhs rhs
252 , (arg:_) <- vars = translateVariableName arg ++ ".length"
255 , (arg:_) <- vars = "parseInt(" ++ translateVariableName arg ++ ")"
257 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
259 , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
261 , (arg:_) <- vars = translateVariableName arg ++ ".valueOf()"
263 , (arg:_) <- vars = translateVariableName arg ++ ".toString()"
265 , (arg:_) <- vars = "__IDRRT__.bigInt(" ++ translateVariableName arg ++ ")"
267 , (arg:_) <- vars = "String(" ++ translateVariableName arg ++ ")"
269 , (arg:_) <- vars = "parseFloat(" ++ translateVariableName arg ++ ")"
271 , (arg:_) <- vars = translateVariableName arg
273 , (arg:_) <- vars = translateVariableName arg
275 , (arg:_) <- vars = translateVariableName arg ++ ".charCodeAt(0)"
278 "String.fromCharCode(" ++ translateVariableName arg ++ ")"
281 , (arg:_) <- vars = "Math.exp(" ++ translateVariableName arg ++ ")"
283 , (arg:_) <- vars = "Math.log(" ++ translateVariableName arg ++ ")"
285 , (arg:_) <- vars = "Math.sin(" ++ translateVariableName arg ++ ")"
287 , (arg:_) <- vars = "Math.cos(" ++ translateVariableName arg ++ ")"
289 , (arg:_) <- vars = "Math.tan(" ++ translateVariableName arg ++ ")"
291 , (arg:_) <- vars = "Math.asin(" ++ translateVariableName arg ++ ")"
293 , (arg:_) <- vars = "Math.acos(" ++ translateVariableName arg ++ ")"
295 , (arg:_) <- vars = "Math.atan(" ++ translateVariableName arg ++ ")"
297 , (arg:_) <- vars = "Math.sqrt(" ++ translateVariableName arg ++ ")"
299 , (arg:_) <- vars = "Math.floor(" ++ translateVariableName arg ++ ")"
301 , (arg:_) <- vars = "Math.ceil(" ++ translateVariableName arg ++ ")"
304 , (lhs:rhs:_) <- vars = translateBinaryOp "+" lhs rhs
306 , (arg:_) <- vars = translateVariableName arg ++ "[0]"
308 , (arg:_) <- vars = let v = translateVariableName arg in
309 v ++ "split('').reverse().join('')"
311 , (lhs:rhs:_) <- vars = let l = translateVariableName lhs
312 r = translateVariableName rhs in
315 , (arg:_) <- vars = let v = translateVariableName arg in
316 v ++ ".substr(1," ++ v ++ ".length-1)"
318 translateBinaryOp :: String -> LVar -> LVar -> String
319 translateBinaryOp f lhs rhs =
320 translateVariableName lhs
322 ++ translateVariableName rhs
324translateExpression (SError msg) =
325 "(function(){throw \'" ++ msg ++ "\';})();"
327translateExpression (SForeign _ _ "putStr" [(FString, var)]) =
328 "__IDRRT__.print(" ++ translateVariableName var ++ ");"
330translateExpression (SForeign _ _ fun args) =
333 ++ intercalate "," (map (translateVariableName . snd) args)
336translateExpression (SChkCase var cases) =
338 ++ intercalate " else " (map (translateCase "e") cases)
340 ++ translateVariableName var
343translateExpression (SCase var cases) =
345 ++ intercalate " else " (map (translateCase "e") cases)
347 ++ translateVariableName var
350translateExpression (SCon i name vars) =
351 concat [ "new __IDRRT__.Con("
354 , intercalate "," $ map translateVariableName vars
358translateExpression (SUpdate var e) =
359 translateVariableName var ++ " = " ++ translateExpression e
361translateExpression (SProj var i) =
362 translateVariableName var ++ ".vars[" ++ show i ++"]"
364translateExpression SNothing = "null"
366translateExpression e =
367 "(function(){throw 'Not yet implemented: "
368 ++ filter (/= '\'') (show e)
371translateCase :: String -> SAlt -> String
372translateCase _ (SDefaultCase e) =
373 createIfBlock "true" (translateExpression e)
375translateCase var (SConstCase ty e)
376 | ChType <- ty = matchHelper "Char"
377 | StrType <- ty = matchHelper "String"
378 | IType <- ty = matchHelper "Int"
379 | BIType <- ty = matchHelper "Integer"
380 | FlType <- ty = matchHelper "Float"
381 | PtrType <- ty = matchHelper "Pointer"
382 | Forgot <- ty = matchHelper "Forgot"
384 matchHelper tyName = translateTypeMatch var tyName e
386translateCase var (SConstCase cst@(BI _) e) =
387 let cond = var ++ ".equals(" ++ translateConstant cst ++ ")" in
388 createIfBlock cond (translateExpression e)
390translateCase var (SConstCase cst e) =
391 let cond = var ++ " == " ++ translateConstant cst in
392 createIfBlock cond (translateExpression e)
394translateCase var (SConCase a i name vars e) =
395 let isCon = var ++ " instanceof __IDRRT__.Con"
396 isI = show i ++ " == " ++ var ++ ".i"
397 params = intercalate "," $ map (("__var_" ++) . show) [a..(a+length vars)]
398 args = ".apply(this," ++ var ++ ".vars)"
402 ++ "){\nreturn " ++ b ++ "\n})" ++ args
403 cond = intercalate " && " [isCon, isI] in
404 createIfBlock cond $ f (translateExpression e)
406translateTypeMatch :: String -> String -> SExp -> String
407translateTypeMatch var ty exp =
408 let e = translateExpression exp in
410 ++ " instanceof __IDRRT__.Type && "
411 ++ var ++ ".type == '"++ ty ++"'") e
414createIfBlock cond e =
415 "if (" ++ cond ++") {\n"
420 "__IDRRT__.tailcall(function(){return " ++ call ++ "})"
422translateFunctionCall name vars =
423 concat (intersperse "." $ translateNamespace name)
425 ++ translateName name
427 ++ intercalate "," (map translateVariableName vars)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list