on master help sign in
javascript: match on types
commitc79e46cf780aecddcf6cda68013711f43b07382d authorraichoo <raichoo@googlemail.com> dateTue 15 Jan 2013, 19:18 (14 years ago) parent2729962
1 file changed+21−2
src/IRTS/CodegenJavaScript.hs+21−2
src/IRTS/CodegenJavaScript.hs+21−2
@@ -46,7 +46,10 @@ codegenJavaScript definitions filename outputType =
4646idrRuntime :: String
4747idrRuntime =
4848 createModule Nothing idrNamespace $ concat
49− [ "__IDR__.IntType = { type: 'IntType' };"
49+ [ "__IDR__.Type = function(type) { this.type = type; };"
50+ , "__IDR__.Int = new __IDR__.Type('Int');"
51+ , "__IDR__.Char = new __IDR__.Type('Char');"
52+ , "__IDR__.String = new __IDR__.Type('String');"
5053
5154 , "__IDR__.Tailcall = function(f) { this.f = f };"
5255
@@ -152,7 +155,9 @@ translateConstant (BI i) = show i
152155translateConstant (Fl f) = show f
153156translateConstant (Ch c) = show c
154157translateConstant (Str s) = show s
155−translateConstant IType = "__IDR__.IntType"
158+translateConstant IType = "__IDR__.Int"
159+translateConstant ChType = "__IDR__.Char"
160+translateConstant StrType = "__IDR__.String"
156161translateConstant c =
157162 "(function(){throw 'Unimplemented Const: " ++ show c ++ "';})()"
158163
@@ -354,6 +359,12 @@ translateCase :: String -> String -> SAlt -> String
354359translateCase modname _ (SDefaultCase e) =
355360 createIfBlock "true" (translateExpression modname e)
356361
362+translateCase modname var (SConstCase ChType e) =
363+ translateTypeMatch modname var "Char" e
364+
365+translateCase modname var (SConstCase StrType e) =
366+ translateTypeMatch modname var "String" e
367+
357368translateCase modname var (SConstCase cst e) =
358369 let cond = var ++ " == " ++ translateConstant cst in
359370 createIfBlock cond (translateExpression modname e)
@@ -370,6 +381,14 @@ translateCase modname var (SConCase a i name vars e) =
370381 cond = intercalate " && " [isCon, isI] in
371382 createIfBlock cond $ f (translateExpression modname e)
372383
384+translateTypeMatch :: String -> String -> String -> SExp -> String
385+translateTypeMatch modname var ty exp =
386+ let e = translateExpression modname exp in
387+ createIfBlock (var
388+ ++ " instanceof __IDR__.Type && "
389+ ++ var ++ ".type == '"++ ty ++"'") e
390+
391+
373392createIfBlock cond e =
374393 "if (" ++ cond ++") {\n"
375394 ++ "return " ++ e