on master help sign in
javascript: indent generated code
eb54165raichoo
src/IRTS/CodegenJavaScript.hs+79−72
@@ -133,28 +133,31 @@ data JS = JSRaw String
133data FFI = FFICode Char | FFIArg Int | FFIError String
133data FFI = FFICode Char | FFIArg Int | FFIError String
134
134
135compileJS :: JS -> String
135compileJS :: JS -> String
136−compileJS JSNoop = ""
136+compileJS = compileJS' 0
137
137
138−compileJS (JSAnnotation annotation js) =
138+compileJS' :: Int -> JS -> String
139− "/** @" ++ show annotation ++ " */\n" ++ compileJS js
139+compileJS' indent JSNoop = ""
140
140
141−{-compileJS (JSFFI raw args) =-}
141+compileJS' indent (JSAnnotation annotation js) =
142− {-ffi raw (map compileJS args)-}
142+ "/** @" ++ show annotation ++ " */\n" ++ compileJS' indent js
143
143
144+{-compileJS' indent (JSFFI raw args) =-}
145+ {-ffi raw (map compileJS' indent args)-}
146+
144−compileJS (JSRaw code) =
147+compileJS' indent (JSRaw code) =
145 code
148 code
146
149
147−compileJS (JSIdent ident) =
150+compileJS' indent (JSIdent ident) =
148 ident
151 ident
149
152
150−compileJS (JSFunction args body) =
153+compileJS' indent (JSFunction args body) =
151− "function("
154+ replicate indent ' ' ++ "function("
152 ++ intercalate "," args
155 ++ intercalate "," args
153 ++ "){\n"
156 ++ "){\n"
154− ++ compileJS body
157+ ++ compileJS' (indent + 2) body
155 ++ "\n}\n"
158 ++ "\n}\n"
156
159
157−compileJS (JSType ty)
160+compileJS' indent (JSType ty)
158 | JSIntTy <- ty = idrRTNamespace ++ "Int"
161 | JSIntTy <- ty = idrRTNamespace ++ "Int"
159 | JSStringTy <- ty = idrRTNamespace ++ "String"
162 | JSStringTy <- ty = idrRTNamespace ++ "String"
160 | JSIntegerTy <- ty = idrRTNamespace ++ "Integer"
163 | JSIntegerTy <- ty = idrRTNamespace ++ "Integer"
@@ -163,125 +166,129 @@ compileJS (JSType ty)
163 | JSPtrTy <- ty = idrRTNamespace ++ "Ptr"
166 | JSPtrTy <- ty = idrRTNamespace ++ "Ptr"
164 | JSForgotTy <- ty = idrRTNamespace ++ "Forgot"
167 | JSForgotTy <- ty = idrRTNamespace ++ "Forgot"
165
168
166−compileJS (JSSeq seq) =
169+compileJS' indent (JSSeq seq) =
167− intercalate ";\n" (map compileJS $ filter (/= JSNoop) seq) ++ ";"
170+ intercalate ";\n" (map (((replicate indent ' ') ++) . (compileJS' indent)) $ filter (/= JSNoop) seq) ++ ";"
168
171
169−compileJS (JSReturn val) =
172+compileJS' indent (JSReturn val) =
170− "return " ++ compileJS val
173+ "return " ++ compileJS' indent val
171
174
172−compileJS (JSApp lhs rhs)
175+compileJS' indent (JSApp lhs rhs)
173 | JSFunction {} <- lhs =
176 | JSFunction {} <- lhs =
174− concat ["(", compileJS lhs, ")(", args, ")"]
177+ concat ["(", compileJS' indent lhs, ")(", args, ")"]
175 | otherwise =
178 | otherwise =
176− concat [compileJS lhs, "(", args, ")"]
179+ concat [compileJS' indent lhs, "(", args, ")"]
177 where args :: String
180 where args :: String
178− args = intercalate "," $ map compileJS rhs
181+ args = intercalate "," $ map (compileJS' 0) rhs
179
182
180−compileJS (JSNew name args) =
183+compileJS' indent (JSNew name args) =
181− "new " ++ name ++ "(" ++ intercalate "," (map compileJS args) ++ ")"
184+ "new " ++ name ++ "(" ++ intercalate "," (map (compileJS' 0) args) ++ ")"
182
185
183−compileJS (JSError exc) =
186+compileJS' indent (JSError exc) =
184 "throw new Error(\"" ++ exc ++ "\")"
187 "throw new Error(\"" ++ exc ++ "\")"
185
188
186−compileJS (JSBinOp op lhs rhs) =
189+compileJS' indent (JSBinOp op lhs rhs) =
187− compileJS lhs ++ " " ++ op ++ " " ++ compileJS rhs
190+ compileJS' indent lhs ++ " " ++ op ++ " " ++ compileJS' indent rhs
188
191
189−compileJS (JSPreOp op val) =
192+compileJS' indent (JSPreOp op val) =
190− op ++ compileJS val
193+ op ++ compileJS' indent val
191
194
192−compileJS (JSProj obj field)
195+compileJS' indent (JSProj obj field)
193 | JSFunction {} <- obj =
196 | JSFunction {} <- obj =
194− concat ["(", compileJS obj, ").", field]
197+ concat ["(", compileJS' indent obj, ").", field]
195 | JSAssign {} <- obj =
198 | JSAssign {} <- obj =
196− concat ["(", compileJS obj, ").", field]
199+ concat ["(", compileJS' indent obj, ").", field]
197 | otherwise =
200 | otherwise =
198− compileJS obj ++ '.' : field
201+ compileJS' indent obj ++ '.' : field
199
202
200−compileJS JSNull =
203+compileJS' indent JSNull =
201 "null"
204 "null"
202
205
203−compileJS JSUndefined =
206+compileJS' indent JSUndefined =
204 "undefined"
207 "undefined"
205
208
206−compileJS JSThis =
209+compileJS' indent JSThis =
207 "this"
210 "this"
208
211
209−compileJS JSTrue =
212+compileJS' indent JSTrue =
210 "true"
213 "true"
211
214
212−compileJS JSFalse =
215+compileJS' indent JSFalse =
213 "false"
216 "false"
214
217
215−compileJS (JSArray elems) =
218+compileJS' indent (JSArray elems) =
216− "[" ++ intercalate "," (map compileJS elems) ++ "]"
219+ "[" ++ intercalate "," (map (compileJS' 0) elems) ++ "]"
217
220
218−compileJS (JSString str) =
221+compileJS' indent (JSString str) =
219 "\"" ++ str ++ "\""
222 "\"" ++ str ++ "\""
220
223
221−compileJS (JSNum num)
224+compileJS' indent (JSNum num)
222 | JSInt i <- num = show i
225 | JSInt i <- num = show i
223 | JSFloat f <- num = show f
226 | JSFloat f <- num = show f
224 | JSInteger JSBigZero <- num = "__IDRRT__ZERO"
227 | JSInteger JSBigZero <- num = "__IDRRT__ZERO"
225 | JSInteger JSBigOne <- num = "__IDRRT__ONE"
228 | JSInteger JSBigOne <- num = "__IDRRT__ONE"
226 | JSInteger (JSBigInt i) <- num = show i
229 | JSInteger (JSBigInt i) <- num = show i
227− | JSInteger (JSBigIntExpr e) <- num = "__IDRRT__bigInt(" ++ compileJS e ++ ")"
230+ | JSInteger (JSBigIntExpr e) <- num = "__IDRRT__bigInt(" ++ compileJS' indent e ++ ")"
228
231
229−compileJS (JSAssign lhs rhs) =
232+compileJS' indent (JSAssign lhs rhs) =
230− compileJS lhs ++ " = " ++ compileJS rhs
233+ compileJS' indent lhs ++ " = " ++ compileJS' indent rhs
231
234
232−compileJS (JSAlloc name val) =
235+compileJS' indent (JSAlloc name val) =
233− "var " ++ name ++ maybe "" ((" = " ++) . compileJS) val
236+ "var " ++ name ++ maybe "" ((" = " ++) . compileJS' indent) val
234
237
235−compileJS (JSIndex lhs rhs) =
238+compileJS' indent (JSIndex lhs rhs) =
236− compileJS lhs ++ "[" ++ compileJS rhs ++ "]"
239+ compileJS' indent lhs ++ "[" ++ compileJS' indent rhs ++ "]"
237
240
238−compileJS (JSCond branches) =
241+compileJS' indent (JSCond branches) =
239 intercalate " else " $ map createIfBlock branches
242 intercalate " else " $ map createIfBlock branches
240 where
243 where
241 createIfBlock (JSNoop, e) =
244 createIfBlock (JSNoop, e) =
242 "{\n"
245 "{\n"
243− ++ compileJS e
246+ ++ compileJS' (indent + 2) e
244− ++ ";\n}"
247+ ++ ";\n" ++ replicate indent ' ' ++ "}"
248+ createIfBlock (cond, e@(JSSeq _)) =
249+ "if (" ++ compileJS' indent cond ++") {\n"
250+ ++ compileJS' (indent + 2) e
251+ ++ ";\n" ++ replicate indent ' ' ++ "}"
245 createIfBlock (cond, e) =
252 createIfBlock (cond, e) =
246− "if (" ++ compileJS cond ++") {\n"
253+ "if (" ++ compileJS' indent cond ++") {\n"
247− ++ compileJS e
254+ ++ replicate (indent + 2) ' ' ++ compileJS' (indent + 2) e
248− ++ ";\n}"
255+ ++ ";\n" ++ replicate indent ' ' ++ "}"
249
256
250−compileJS (JSSwitch val branches def) =
257+compileJS' indent (JSSwitch val branches def) =
251− "switch(" ++ compileJS val ++ "){\n"
258+ "switch(" ++ compileJS' indent val ++ "){\n"
252 ++ concatMap mkBranch branches
259 ++ concatMap mkBranch branches
253 ++ mkDefault def
260 ++ mkDefault def
254− ++ "}"
261+ ++ (replicate indent ' ') ++ "}"
255 where
262 where
256 mkBranch :: (JS, JS) -> String
263 mkBranch :: (JS, JS) -> String
257 mkBranch (tag, code) =
264 mkBranch (tag, code) =
258− "case " ++ compileJS tag ++ ":\n"
265+ replicate (indent + 2) ' ' ++ "case " ++ compileJS' indent tag ++ ":\n"
259− ++ compileJS code
266+ ++ compileJS' (indent + 4) code
260− ++ "\nbreak;\n"
267+ ++ "\n" ++ (replicate (indent + 4) ' ' ++ "break;\n")
261
268
262 mkDefault :: Maybe JS -> String
269 mkDefault :: Maybe JS -> String
263 mkDefault Nothing = ""
270 mkDefault Nothing = ""
264 mkDefault (Just def) =
271 mkDefault (Just def) =
265− "default:\n"
272+ replicate (indent + 2) ' ' ++ "default:\n"
266− ++ compileJS def
273+ ++ compileJS' (indent + 4)def
267 ++ "\n"
274 ++ "\n"
268
275
269
276
270−compileJS (JSTernary cond true false) =
277+compileJS' indent (JSTernary cond true false) =
271− let c = compileJS cond
278+ let c = compileJS' indent cond
272− t = compileJS true
279+ t = compileJS' indent true
273− f = compileJS false in
280+ f = compileJS' indent false in
274 "(" ++ c ++ ")?(" ++ t ++ "):(" ++ f ++ ")"
281 "(" ++ c ++ ")?(" ++ t ++ "):(" ++ f ++ ")"
275
282
276−compileJS (JSParens js) =
283+compileJS' indent (JSParens js) =
277− "(" ++ compileJS js ++ ")"
284+ "(" ++ compileJS' indent js ++ ")"
278
285
279−compileJS (JSWhile cond body) =
286+compileJS' indent (JSWhile cond body) =
280− "while (" ++ compileJS cond ++ ") {\n"
287+ "while (" ++ compileJS' indent cond ++ ") {\n"
281− ++ compileJS body
288+ ++ compileJS' (indent + 2) body
282− ++ "\n}"
289+ ++ "\n" ++ replicate indent ' ' ++ "}"
283
290
284−compileJS (JSWord word)
291+compileJS' indent (JSWord word)
285 | JSWord8 b <- word = "new Uint8Array([" ++ show b ++ "])"
292 | JSWord8 b <- word = "new Uint8Array([" ++ show b ++ "])"
286 | JSWord16 b <- word = "new Uint16Array([" ++ show b ++ "])"
293 | JSWord16 b <- word = "new Uint16Array([" ++ show b ++ "])"
287 | JSWord32 b <- word = "new Uint32Array([" ++ show b ++ "])"
294 | JSWord32 b <- word = "new Uint32Array([" ++ show b ++ "])"
@@ -355,7 +362,7 @@ splitFunction :: JS -> RWS () [(Int,JS)] Int JS
355splitFunction (JSAlloc name (Just (JSFunction args body@(JSSeq _)))) = do
362splitFunction (JSAlloc name (Just (JSFunction args body@(JSSeq _)))) = do
356 body' <- splitSequence body
363 body' <- splitSequence body
357 return $ JSAlloc name (Just (JSFunction args body'))
364 return $ JSAlloc name (Just (JSFunction args body'))
358− where
365+ where
359 splitCondition :: JS -> RWS () [(Int,JS)] Int JS
366 splitCondition :: JS -> RWS () [(Int,JS)] Int JS
360 splitCondition js
367 splitCondition js
361 | JSCond branches <- js =
368 | JSCond branches <- js =