on master help sign in
javascript: eval constructors during compiletime
64ce1b8raichoo
src/IRTS/CodegenJavaScript.hs+47
@@ -1060,6 +1060,52 @@ extractLocalConstructors js =
1060 []
1060 []
1061
1061
1062
1062
1063+evalCons :: [JS] -> [JS]
1064+evalCons js =
1065+ map (expandProj . evalCons') js
1066+ where
1067+ cons :: [(String, JS)]
1068+ cons = concatMap getGlobalCons js
1069+
1070+ evalCons' :: JS -> JS
1071+ evalCons' js = transformJS match js
1072+ where
1073+ match :: JS -> JS
1074+ match (JSApp (JSIdent "__IDRRT__EVALTC") [arg])
1075+ | JSIdent name <- arg
1076+ , Just (JSNew _ [_, JSNull, _, _]) <- lookup name cons = arg
1077+
1078+ match js = transformJS match js
1079+
1080+
1081+ expandProj :: JS -> JS
1082+ expandProj js = transformJS match js
1083+ where
1084+ match :: JS -> JS
1085+ match (
1086+ JSIndex (
1087+ JSProj (JSIdent name) "vars"
1088+ ) (
1089+ JSNum (JSInt idx)
1090+ )
1091+ )
1092+ | Just (JSNew _ [_, _, _, JSArray args]) <- lookup name cons =
1093+ args !! idx
1094+
1095+ match js = transformJS match js
1096+
1097+
1098+getGlobalCons :: JS -> [(String, JS)]
1099+getGlobalCons js = foldJS match (++) [] js
1100+ where
1101+ match :: JS -> [(String, JS)]
1102+ match js
1103+ | (JSAlloc name (Just con@(JSNew "__IDRRT__Con" _))) <- js =
1104+ [(name, con)]
1105+ | otherwise =
1106+ []
1107+
1108+
1063codegenJavaScript
1109codegenJavaScript
1064 :: JSTarget
1110 :: JSTarget
1065 -> [(Name, SDecl)]
1111 -> [(Name, SDecl)]
@@ -1118,6 +1164,7 @@ codegenJavaScript target definitions filename outputType = do
1118 , map reduceContinuations
1164 , map reduceContinuations
1119 , extractLocalConstructors
1165 , extractLocalConstructors
1120 , unfoldLookupTable
1166 , unfoldLookupTable
1167+ , evalCons
1121 ]
1168 ]
1122
1169
1123 prelude :: [JS]
1170 prelude :: [JS]