on master help sign in
javascript: whitespace
6f23501raichoo
src/IRTS/JavaScript/AST.hs+57−15
@@ -94,7 +94,9 @@ ffi code args = let parsed = ffiParse code in
94 ffiParse ('%':'%':ss) = FFICode '%' : ffiParse ss
94 ffiParse ('%':'%':ss) = FFICode '%' : ffiParse ss
95 ffiParse ('%':s:ss)
95 ffiParse ('%':s:ss)
96 | isDigit s =
96 | isDigit s =
97+ FFIArg (
98+ read $ s : takeWhile isDigit ss
97− FFIArg (read $ s : takeWhile isDigit ss) : ffiParse (dropWhile isDigit ss)
99+ ) : ffiParse (dropWhile isDigit ss)
98 | otherwise =
100 | otherwise =
99 [FFIError "FFI - Invalid positional argument"]
101 [FFIError "FFI - Invalid positional argument"]
100 ffiParse (s:ss) = FFICode s : ffiParse ss
102 ffiParse (s:ss) = FFICode s : ffiParse ss
@@ -110,7 +112,9 @@ ffi code args = let parsed = ffiParse code in
110 renderFFI [] _ = ""
112 renderFFI [] _ = ""
111 renderFFI (FFICode c : fs) args = c `T.cons` renderFFI fs args
113 renderFFI (FFICode c : fs) args = c `T.cons` renderFFI fs args
112 renderFFI (FFIArg i : fs) args
114 renderFFI (FFIArg i : fs) args
115+ | i < length args && i >= 0 =
116+ T.pack (args !! i)
113− | i < length args && i >= 0 = T.pack (args !! i) `T.append` renderFFI fs args
117+ `T.append` renderFFI fs args
114 | otherwise = error "FFI - Argument index out of bounds"
118 | otherwise = error "FFI - Argument index out of bounds"
115
119
116compileJS :: JS -> T.Text
120compileJS :: JS -> T.Text
@@ -120,7 +124,10 @@ compileJS' :: Int -> JS -> T.Text
120compileJS' indent JSNoop = ""
124compileJS' indent JSNoop = ""
121
125
122compileJS' indent (JSAnnotation annotation js) =
126compileJS' indent (JSAnnotation annotation js) =
127+ "/** @"
123− "/** @" `T.append` T.pack (show annotation) `T.append` " */\n" `T.append` compileJS' indent js
128+ `T.append` T.pack (show annotation)
129+ `T.append` " */\n"
130+ `T.append` compileJS' indent js
124
131
125compileJS' indent (JSFFI raw args) =
132compileJS' indent (JSFFI raw args) =
126 ffi raw (map (T.unpack . compileJS' indent) args)
133 ffi raw (map (T.unpack . compileJS' indent) args)
@@ -166,13 +173,21 @@ compileJS' indent (JSApp lhs rhs)
166 args = T.intercalate "," $ map (compileJS' 0) rhs
173 args = T.intercalate "," $ map (compileJS' 0) rhs
167
174
168compileJS' indent (JSNew name args) =
175compileJS' indent (JSNew name args) =
176+ "new "
177+ `T.append` T.pack name
178+ `T.append` "("
169− "new " `T.append` T.pack name `T.append` "(" `T.append` T.intercalate "," (map (compileJS' 0) args) `T.append` ")"
179+ `T.append` T.intercalate "," (map (compileJS' 0) args)
180+ `T.append` ")"
170
181
171compileJS' indent (JSError exc) =
182compileJS' indent (JSError exc) =
172 "throw new Error(\"" `T.append` T.pack exc `T.append` "\")"
183 "throw new Error(\"" `T.append` T.pack exc `T.append` "\")"
173
184
174compileJS' indent (JSBinOp op lhs rhs) =
185compileJS' indent (JSBinOp op lhs rhs) =
186+ compileJS' indent lhs
187+ `T.append` " "
188+ `T.append` T.pack op
189+ `T.append` " "
175− compileJS' indent lhs `T.append` " " `T.append` T.pack op `T.append` " " `T.append` compileJS' indent rhs
190+ `T.append` compileJS' indent rhs
176
191
177compileJS' indent (JSPreOp op val) =
192compileJS' indent (JSPreOp op val) =
178 T.pack op `T.append` compileJS' indent val
193 T.pack op `T.append` compileJS' indent val
@@ -212,19 +227,29 @@ compileJS' indent (JSNum num)
212 | JSInteger JSBigZero <- num = T.pack "i$ZERO"
227 | JSInteger JSBigZero <- num = T.pack "i$ZERO"
213 | JSInteger JSBigOne <- num = T.pack "i$ONE"
228 | JSInteger JSBigOne <- num = T.pack "i$ONE"
214 | JSInteger (JSBigInt i) <- num = T.pack (show i)
229 | JSInteger (JSBigInt i) <- num = T.pack (show i)
230+ | JSInteger (JSBigIntExpr e) <- num =
215− | JSInteger (JSBigIntExpr e) <- num = "i$bigInt(" `T.append` compileJS' indent e `T.append` ")"
231+ "i$bigInt(" `T.append` compileJS' indent e `T.append` ")"
216
232
217compileJS' indent (JSAssign lhs rhs) =
233compileJS' indent (JSAssign lhs rhs) =
218 compileJS' indent lhs `T.append` " = " `T.append` compileJS' indent rhs
234 compileJS' indent lhs `T.append` " = " `T.append` compileJS' indent rhs
219
235
220compileJS' 0 (JSAlloc name (Just val@(JSNew _ _))) =
236compileJS' 0 (JSAlloc name (Just val@(JSNew _ _))) =
237+ "var "
238+ `T.append` T.pack name
239+ `T.append` " = "
221− "var " `T.append` T.pack name `T.append` " = " `T.append` compileJS' 0 val `T.append` ";\n"
240+ `T.append` compileJS' 0 val
241+ `T.append` ";\n"
222
242
223compileJS' indent (JSAlloc name val) =
243compileJS' indent (JSAlloc name val) =
244+ "var "
245+ `T.append` T.pack name
224− "var " `T.append` T.pack name `T.append` maybe "" ((" = " `T.append`) . compileJS' indent) val
246+ `T.append` maybe "" ((" = " `T.append`) . compileJS' indent) val
225
247
226compileJS' indent (JSIndex lhs rhs) =
248compileJS' indent (JSIndex lhs rhs) =
249+ compileJS' indent lhs
250+ `T.append` "["
227− compileJS' indent lhs `T.append` "[" `T.append` compileJS' indent rhs `T.append` "]"
251+ `T.append` compileJS' indent rhs
252+ `T.append` "]"
228
253
229compileJS' indent (JSCond branches) =
254compileJS' indent (JSCond branches) =
230 T.intercalate " else " $ map createIfBlock branches
255 T.intercalate " else " $ map createIfBlock branches
@@ -243,13 +268,20 @@ compileJS' indent (JSCond branches) =
243 `T.append` "\n" `T.append` T.replicate indent " " `T.append` "}"
268 `T.append` "\n" `T.append` T.replicate indent " " `T.append` "}"
244 createIfBlock (cond, e) =
269 createIfBlock (cond, e) =
245 "if (" `T.append` compileJS' indent cond `T.append`") {\n"
270 "if (" `T.append` compileJS' indent cond `T.append`") {\n"
246− `T.append` T.replicate (indent + 2) " " `T.append` compileJS' (indent + 2) e
271+ `T.append` T.replicate (indent + 2) " "
272+ `T.append` compileJS' (indent + 2) e
273+ `T.append` ";\n"
247− `T.append` ";\n" `T.append` T.replicate indent " " `T.append` "}"
274+ `T.append` T.replicate indent " "
275+ `T.append` "}"
248
276
249compileJS' indent (JSSwitch val [(_,JSSeq seq)] Nothing) =
277compileJS' indent (JSSwitch val [(_,JSSeq seq)] Nothing) =
250 let (h,t) = splitAt 1 seq in
278 let (h,t) = splitAt 1 seq in
251 (T.concat (map (compileJS' indent) h) `T.append` ";\n")
279 (T.concat (map (compileJS' indent) h) `T.append` ";\n")
280+ `T.append` (
281+ T.intercalate ";\n" $ map (
252− `T.append` (T.intercalate ";\n" $ map ((T.replicate indent " " `T.append`) . compileJS' indent) t)
282+ (T.replicate indent " " `T.append`) . compileJS' indent
283+ ) t
284+ )
253
285
254compileJS' indent (JSSwitch val branches def) =
286compileJS' indent (JSSwitch val branches def) =
255 "switch(" `T.append` compileJS' indent val `T.append` "){\n"
287 "switch(" `T.append` compileJS' indent val `T.append` "){\n"
@@ -259,9 +291,13 @@ compileJS' indent (JSSwitch val branches def) =
259 where
291 where
260 mkBranch :: (JS, JS) -> T.Text
292 mkBranch :: (JS, JS) -> T.Text
261 mkBranch (tag, code) =
293 mkBranch (tag, code) =
294+ T.replicate (indent + 2) " "
295+ `T.append` "case "
262− T.replicate (indent + 2) " " `T.append` "case " `T.append` compileJS' indent tag `T.append` ":\n"
296+ `T.append` compileJS' indent tag
297+ `T.append` ":\n"
263 `T.append` compileJS' (indent + 4) code
298 `T.append` compileJS' (indent + 4) code
299+ `T.append` "\n"
264− `T.append` "\n" `T.append` (T.replicate (indent + 4) " " `T.append` "break;\n")
300+ `T.append` (T.replicate (indent + 4) " " `T.append` "break;\n")
265
301
266 mkDefault :: Maybe JS -> T.Text
302 mkDefault :: Maybe JS -> T.Text
267 mkDefault Nothing = ""
303 mkDefault Nothing = ""
@@ -275,7 +311,13 @@ compileJS' indent (JSTernary cond true false) =
275 let c = compileJS' indent cond
311 let c = compileJS' indent cond
276 t = compileJS' indent true
312 t = compileJS' indent true
277 f = compileJS' indent false in
313 f = compileJS' indent false in
278− "(" `T.append` c `T.append` ")?(" `T.append` t `T.append` "):(" `T.append` f `T.append` ")"
314+ "("
315+ `T.append` c
316+ `T.append` ")?("
317+ `T.append` t
318+ `T.append` "):("
319+ `T.append` f
320+ `T.append` ")"
279
321
280compileJS' indent (JSParens js) =
322compileJS' indent (JSParens js) =
281 "(" `T.append` compileJS' indent js `T.append` ")"
323 "(" `T.append` compileJS' indent js `T.append` ")"