@@ -75,6 +75,7 @@ data JS = JSRaw String
75 | JSIndex JS JS
75 | JSIndex JS JS
76 | JSCond [(JS, JS)]
76 | JSCond [(JS, JS)]
77 | JSTernary JS JS JS
77 | JSTernary JS JS JS
78 deriving Eq
79 deriving Eq
@@ -190,6 +191,9 @@ compileJS (JSTernary cond true false) =
190 f = compileJS false in
191 f = compileJS false in
191 "(" ++ c ++ ")?(" ++ t ++ "):(" ++ f ++ ")"
192 "(" ++ c ++ ")?(" ++ t ++ "):(" ++ f ++ ")"
194+compileJS (JSParens js) =
195+ "(" ++ compileJS js ++ ")"
194jsTailcall :: JS -> JS
198jsTailcall :: JS -> JS
195jsTailcall call =
199jsTailcall call =
@@ -326,6 +330,9 @@ jsSubst var new (JSAssign lhs rhs) =
326jsSubst var new (JSAlloc val (Just js)) =
330jsSubst var new (JSAlloc val (Just js)) =
327 JSAlloc val (Just (jsSubst var new js))
331 JSAlloc val (Just (jsSubst var new js))
333+jsSubst var new (JSParens js) =
334+ JSParens $ jsSubst var new js
329jsSubst _ _ js = js
336jsSubst _ _ js = js
@@ -378,6 +385,9 @@ removeAllocations (JSCond conds) =
378removeAllocations (JSTernary c t f) =
385removeAllocations (JSTernary c t f) =
379 JSTernary (removeAllocations c) (removeAllocations t) (removeAllocations f)
386 JSTernary (removeAllocations c) (removeAllocations t) (removeAllocations f)
388+removeAllocations (JSParens js) =
389+ JSParens $ removeAllocations js
381removeAllocations js = js
391removeAllocations js = js
@@ -497,6 +507,9 @@ inlineJS (JSCond cases) =
497inlineJS (JSOp op lhs rhs) =
507inlineJS (JSOp op lhs rhs) =
498 JSOp op (inlineJS lhs) (inlineJS rhs)
508 JSOp op (inlineJS lhs) (inlineJS rhs)
510+inlineJS (JSParens js) =
511+ JSParens $ inlineJS js
500inlineJS js = js
513inlineJS js = js
@@ -549,6 +562,8 @@ deadEvalApplyCases js =
550 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
563 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
565+ getTags (JSParens js) = getTags js
552 getTags js = []
567 getTags js = []
@@ -606,6 +621,8 @@ initConstructors js =
607 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
622 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
624+ getTags (JSParens js) = getTags js
609 getTags js = []
626 getTags js = []
@@ -656,6 +673,9 @@ initConstructors js =
656 replaceHelper tags f
673 replaceHelper tags f
676+ replaceHelper tags (JSParens js) =
677+ JSParens $ replaceHelper tags js
659 replaceHelper tags js = js
679 replaceHelper tags js = js
@@ -730,6 +750,9 @@ removeIDs js =
730 removeIDCall ids (JSArray fields) =
750 removeIDCall ids (JSArray fields) =
731 JSArray $ map (removeIDCall ids) fields
751 JSArray $ map (removeIDCall ids) fields
753+ removeIDCall ids (JSParens js) =
754+ JSParens $ removeIDCall ids js
733 removeIDCall _ js = js
756 removeIDCall _ js = js
@@ -816,13 +839,39 @@ inlineFunctions js =
816 , countInvokations name f
839 , countInvokations name f
842+ countInvokations name (JSParens js) =
843+ countInvokations name js
819 countInvokations _ _ = 0
845 countInvokations _ _ = 0
822reduceConstant :: JS -> JS
848reduceConstant :: JS -> JS
823−reduceConstant (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
850+ (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
824− JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [JSNum num])
851+ JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [val])
825− )]) = JSNum num
852+ )])
853+ | JSNum num <- val = val
854+ | JSOp op lhs rhs <- val =
855+ JSParens $ JSOp op (reduceConstant lhs) (reduceConstant rhs)
857+ | JSApp (JSProj lhs op) [rhs] <- val
858+ , op `elem` [ "subtract"
870+reduceConstant (JSApp ident [(JSParens js)]) =
871+ JSApp ident [reduceConstant js]
873+reduceConstant (JSOp op lhs rhs) =
874+ JSOp op (reduceConstant lhs) (reduceConstant rhs)
827reduceConstant (JSReturn ret) =
876reduceConstant (JSReturn ret) =
828 JSReturn (reduceConstant ret)
877 JSReturn (reduceConstant ret)
@@ -851,8 +900,12 @@ reduceConstant (JSSeq seq) =
851reduceConstant (JSFunction args body) =
900reduceConstant (JSFunction args body) =
852 JSFunction args (reduceConstant body)
901 JSFunction args (reduceConstant body)
903+reduceConstant (JSParens js) =
904+ JSParens $ reduceConstant js
854reduceConstant js = js
906reduceConstant js = js
856reduceConstants :: JS -> JS
909reduceConstants :: JS -> JS
857reduceConstants js
910reduceConstants js
858 | ret <- reduceConstant js
911 | ret <- reduceConstant js
@@ -929,6 +982,9 @@ optimizeEvalTailcalls (fun, tc) js =
929 optHelper f
982 optHelper f
985+ optHelper (JSParens js) =
986+ JSParens $ optHelper js
932 optHelper js = js
988 optHelper js = js
@@ -992,6 +1048,9 @@ removeInstanceChecks (JSTernary c t f) =
992 removeInstanceChecks f
1048 removeInstanceChecks f
1051+removeInstanceChecks (JSParens js) =
1052+ JSParens $ removeInstanceChecks js
995removeInstanceChecks js = js
1054removeInstanceChecks js = js
@@ -1071,6 +1130,9 @@ reduceLoop reduced (cons, program) =
1071 reduceCall funs (JSArray fields) =
1130 reduceCall funs (JSArray fields) =
1072 JSArray $ map (reduceCall funs) fields
1131 JSArray $ map (reduceCall funs) fields
1133+ reduceCall funs (JSParens js) =
1134+ JSParens $ reduceCall funs js
1074 reduceCall _ js = js
1136 reduceCall _ js = js