@@ -28,6 +28,8 @@ import Language.PureScript.CodeGen.Common (identToJs)
2828import Language.PureScript.Optimizer.Common
2929import Language.PureScript.Names
3030
31+import qualified Language.PureScript.Constants as C
32+
3133shouldInline :: JS -> Bool
3234shouldInline (JSVar _) = True
3335shouldInline (JSNumericLiteral _) = True
@@ -71,42 +73,46 @@ inlineOperator op f = everywhere (mkT convert)
7173 convert :: JS -> JS
7274 convert (JSApp (JSApp op' [x]) [y]) | isOp op' = f x y
7375 convert other = other
74− isOp (JSAccessor longForm (JSAccessor "Prelude" (JSVar "_ps"))) | longForm == identToJs (Op op) = True
75− isOp (JSIndexer (JSStringLiteral op') (JSAccessor "Prelude" (JSVar "_ps"))) | op == op' = True
76+ isOp (JSAccessor longForm (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
77+ _ps == C._ps &&
78+ longForm == identToJs (Op op) = True
79+ isOp (JSIndexer (JSStringLiteral op') (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
80+ _ps == C._ps &&
81+ op == op' = True
7682 isOp _ = False
7783
7884inlineCommonOperators :: JS -> JS
7985inlineCommonOperators = applyAll
80− [ binary "numNumber" "+" Add
81− , binary "numNumber" "-" Subtract
82− , binary "numNumber" "*" Multiply
83− , binary "numNumber" "/" Divide
84− , binary "numNumber" "%" Modulus
85− , unary "numNumber" "negate" Negate
86+ [ binary C.numNumber (C.+) Add
87+ , binary C.numNumber (C.-) Subtract
88+ , binary C.numNumber (C.*) Multiply
89+ , binary C.numNumber (C./) Divide
90+ , binary C.numNumber (C.%) Modulus
91+ , unary C.numNumber C.negate Negate
8692
87− , binary "ordNumber" "<" LessThan
88− , binary "ordNumber" ">" GreaterThan
89− , binary "ordNumber" "<=" LessThanOrEqualTo
90− , binary "ordNumber" ">=" GreaterThanOrEqualTo
93+ , binary C.ordNumber (C.<) LessThan
94+ , binary C.ordNumber (C.>) GreaterThan
95+ , binary C.ordNumber (C.<=) LessThanOrEqualTo
96+ , binary C.ordNumber (C.>=) GreaterThanOrEqualTo
9197
92− , binary "eqNumber" "==" EqualTo
93− , binary "eqNumber" "/=" NotEqualTo
94− , binary "eqString" "==" EqualTo
95− , binary "eqString" "/=" NotEqualTo
96− , binary "eqBoolean" "==" EqualTo
97− , binary "eqBoolean" "/=" NotEqualTo
98+ , binary C.eqNumber (C.==) EqualTo
99+ , binary C.eqNumber (C./=) NotEqualTo
100+ , binary C.eqString (C.==) EqualTo
101+ , binary C.eqString (C./=) NotEqualTo
102+ , binary C.eqBoolean (C.==) EqualTo
103+ , binary C.eqBoolean (C./=) NotEqualTo
98104
99− , binaryFunction "bitsNumber" "shl" ShiftLeft
100− , binaryFunction "bitsNumber" "shr" ShiftRight
101− , binaryFunction "bitsNumber" "zshr" ZeroFillShiftRight
102− , binary "bitsNumber" "&" BitwiseAnd
103− , binary "bitsNumber" "|" BitwiseOr
104− , binary "bitsNumber" "^" BitwiseXor
105− , unary "bitsNumber" "complement" BitwiseNot
105+ , binaryFunction C.bitsNumber C.shl ShiftLeft
106+ , binaryFunction C.bitsNumber C.shr ShiftRight
107+ , binaryFunction C.bitsNumber C.zshr ZeroFillShiftRight
108+ , binary C.bitsNumber (C.&) BitwiseAnd
109+ , binary C.bitsNumber C.bar BitwiseOr
110+ , binary C.bitsNumber (C.^) BitwiseXor
111+ , unary C.bitsNumber C.complement BitwiseNot
106112
107− , binary "boolLikeBoolean" "&&" And
108− , binary "boolLikeBoolean" "||" Or
109− , unary "boolLikeBoolean" "not" Not
113+ , binary C.boolLikeBoolean (C.&&) And
114+ , binary C.boolLikeBoolean (C.||) Or
115+ , unary C.boolLikeBoolean C.not Not
110116 ]
111117 where
112118 binary :: String -> String -> BinaryOperator -> JS -> JS
@@ -115,8 +121,11 @@ inlineCommonOperators = applyAll
115121 convert :: JS -> JS
116122 convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName dict = JSBinary op x y
117123 convert other = other
118− isOp (JSAccessor longForm (JSAccessor "Prelude" (JSVar _))) | longForm == identToJs (Op opString) = True
119− isOp (JSIndexer (JSStringLiteral op') (JSAccessor "Prelude" (JSVar "_ps"))) | opString == op' = True
124+ isOp (JSAccessor longForm (JSAccessor prelude (JSVar _))) | prelude == C.prelude &&
125+ longForm == identToJs (Op opString) = True
126+ isOp (JSIndexer (JSStringLiteral op') (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
127+ _ps == C._ps &&
128+ opString == op' = True
120129 isOp _ = False
121130 binaryFunction :: String -> String -> BinaryOperator -> JS -> JS
122131 binaryFunction dictName fnName op = everywhere (mkT convert)
@@ -124,7 +133,9 @@ inlineCommonOperators = applyAll
124133 convert :: JS -> JS
125134 convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName dict = JSBinary op x y
126135 convert other = other
127− isOp (JSAccessor fnName' (JSAccessor "Prelude" (JSVar "_ps"))) | fnName == fnName' = True
136+ isOp (JSAccessor fnName' (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
137+ _ps == C._ps &&
138+ fnName == fnName' = True
128139 isOp _ = False
129140 unary :: String -> String -> UnaryOperator -> JS -> JS
130141 unary dictName fnName op = everywhere (mkT convert)
@@ -132,7 +143,11 @@ inlineCommonOperators = applyAll
132143 convert :: JS -> JS
133144 convert (JSApp (JSApp fn [dict]) [x]) | isOp fn && isOpDict dictName dict = JSUnary op x
134145 convert other = other
135− isOp (JSAccessor fnName' (JSAccessor "Prelude" (JSVar "_ps"))) | fnName' == fnName = True
146+ isOp (JSAccessor fnName' (JSAccessor prelude (JSVar _ps))) | prelude == C.prelude &&
147+ _ps == C._ps &&
148+ fnName' == fnName = True
136149 isOp _ = False
137− isOpDict dictName (JSApp (JSAccessor prop (JSAccessor "Prelude" (JSVar "_ps"))) [JSObjectLiteral []]) | prop == dictName = True
150+ isOpDict dictName (JSApp (JSAccessor prop (JSAccessor prelude (JSVar _ps))) [JSObjectLiteral []]) | prelude == C.prelude &&
151+ _ps == C._ps &&
152+ prop == dictName = True
138153 isOpDict _ _ = False