2Module : IRTS.JavaScript.AST
3Description : Data structures and functions used with the JavaScript codegen.
6Maintainer : The Idris Community.
8{-# LANGUAGE OverloadedStrings, PatternGuards #-}
9module IRTS.JavaScript.AST where
11import Data.Char (isDigit)
12import qualified Data.Text as T
25data JSInteger = JSBigZero
38data JSWord = JSWord8 Word8
45data JSAnnotation = JSConstructor deriving Eq
48instance Show JSAnnotation where
49 show JSConstructor = "constructor"
54 | JSFunction [String] JS
61 | JSBinOp String JS JS
75 | JSAlloc String (Maybe JS)
77 | JSSwitch JS [(JS, JS)] (Maybe JS)
83 | JSAnnotation JSAnnotation JS
90data FFI = FFICode Char | FFIArg Int | FFIError String
92ffi :: String -> [String] -> T.Text
93ffi code args = let parsed = ffiParse code in
94 case ffiError parsed of
96 Nothing -> renderFFI parsed args
98 ffiParse :: String -> [FFI]
100 ffiParse ['%'] = [FFIError "FFI - Invalid positional argument"]
101 ffiParse ('%':'%':ss) = FFICode '%' : ffiParse ss
105 read $ s : takeWhile isDigit ss
106 ) : ffiParse (dropWhile isDigit ss)
108 [FFIError "FFI - Invalid positional argument"]
109 ffiParse (s:ss) = FFICode s : ffiParse ss
112 ffiError :: [FFI] -> Maybe String
113 ffiError [] = Nothing
114 ffiError ((FFIError s):xs) = Just s
115 ffiError (x:xs) = ffiError xs
118 renderFFI :: [FFI] -> [String] -> T.Text
120 renderFFI (FFICode c : fs) args = c `T.cons` renderFFI fs args
121 renderFFI (FFIArg i : fs) args
122 | i < length args && i >= 0 =
124 `T.append` renderFFI fs args
125 | otherwise = error "FFI - Argument index out of bounds"
127compileJS :: JS -> T.Text
128compileJS = compileJS' 0
130compileJS' :: Int -> JS -> T.Text
131compileJS' indent JSNoop = ""
133compileJS' indent (JSAnnotation annotation js) =
135 `T.append` T.pack (show annotation)
137 `T.append` compileJS' indent js
139compileJS' indent (JSDelete js) =
140 "delete " `T.append` compileJS' 0 js
142compileJS' indent (JSClear js) =
143 compileJS' 0 js `T.append` " = undefined"
145compileJS' indent (JSFFI raw args) =
146 ffi raw (map (T.unpack . compileJS' indent) args)
148compileJS' indent (JSRaw code) =
151compileJS' indent (JSIdent ident) =
154compileJS' indent (JSFunction args body) =
155 T.replicate indent " " `T.append` "function("
156 `T.append` T.intercalate "," (map T.pack args)
158 `T.append` compileJS' (indent + 2) body
161compileJS' indent (JSType ty)
162 | JSIntTy <- ty = "i$Int"
163 | JSStringTy <- ty = "i$String"
164 | JSIntegerTy <- ty = "i$Integer"
165 | JSFloatTy <- ty = "i$Float"
166 | JSCharTy <- ty = "i$Char"
167 | JSPtrTy <- ty = "i$Ptr"
168 | JSForgotTy <- ty = "i$Forgot"
170compileJS' indent (JSSeq seq) =
171 T.intercalate ";\n" (
173 (T.replicate indent " " `T.append`) . (compileJS' indent)
174 ) $ filter (/= JSNoop) seq
177compileJS' indent (JSReturn val) =
178 "return " `T.append` compileJS' indent val
180compileJS' indent (JSApp lhs rhs)
181 | JSFunction {} <- lhs =
182 T.concat ["(", compileJS' indent lhs, ")(", args, ")"]
184 T.concat [compileJS' indent lhs, "(", args, ")"]
186 args = T.intercalate "," $ map (compileJS' 0) rhs
188compileJS' indent (JSNew name args) =
190 `T.append` T.pack name
192 `T.append` T.intercalate "," (map (compileJS' 0) args)
195compileJS' indent (JSError exc) =
196 "(function(){throw new Error(\"" `T.append` T.pack exc `T.append` "\")})()"
198compileJS' indent (JSBinOp op lhs rhs) =
199 compileJS' indent lhs
203 `T.append` compileJS' indent rhs
205compileJS' indent (JSPreOp op val) =
206 T.pack op `T.append` "(" `T.append` compileJS' indent val `T.append` ")"
208compileJS' indent (JSProj obj field)
209 | JSFunction {} <- obj =
210 T.concat ["(", compileJS' indent obj, ").", T.pack field]
211 | JSAssign {} <- obj =
212 T.concat ["(", compileJS' indent obj, ").", T.pack field]
214 compileJS' indent obj `T.append` ('.' `T.cons` T.pack field)
216compileJS' indent JSNull =
219compileJS' indent JSUndefined =
222compileJS' indent JSThis =
225compileJS' indent JSTrue =
228compileJS' indent JSFalse =
231compileJS' indent (JSArray elems) =
232 "[" `T.append` T.intercalate "," (map (compileJS' 0) elems) `T.append` "]"
234compileJS' indent (JSString str) =
235 "\"" `T.append` T.pack str `T.append` "\""
237compileJS' indent (JSNum num)
238 | JSInt i <- num = T.pack (show i)
239 | JSFloat f <- num = T.pack (show f)
240 | JSInteger JSBigZero <- num = T.pack "i$ZERO"
241 | JSInteger JSBigOne <- num = T.pack "i$ONE"
242 | JSInteger (JSBigInt i) <- num = T.pack (show i)
243 | JSInteger (JSBigIntExpr e) <- num =
244 "i$bigInt(" `T.append` compileJS' indent e `T.append` ")"
246compileJS' indent (JSAssign lhs rhs) =
247 compileJS' indent lhs `T.append` " = " `T.append` compileJS' indent rhs
249compileJS' 0 (JSAlloc name (Just val@(JSNew _ _))) =
251 `T.append` T.pack name
253 `T.append` compileJS' 0 val
256compileJS' indent (JSAlloc name val) =
258 `T.append` T.pack name
259 `T.append` maybe "" ((" = " `T.append`) . compileJS' indent) val
261compileJS' indent (JSIndex lhs rhs) =
262 compileJS' indent lhs
264 `T.append` compileJS' indent rhs
267compileJS' indent (JSCond branches) =
268 T.intercalate " else " $ map createIfBlock branches
270 createIfBlock (JSNoop, e@(JSSeq _)) =
272 `T.append` compileJS' (indent + 2) e
273 `T.append` "\n" `T.append` T.replicate indent " " `T.append` "}"
274 createIfBlock (JSNoop, e) =
276 `T.append` compileJS' (indent + 2) e
277 `T.append` ";\n" `T.append` T.replicate indent " " `T.append` "}"
278 createIfBlock (cond, e@(JSSeq _)) =
279 "if (" `T.append` compileJS' indent cond `T.append`") {\n"
280 `T.append` compileJS' (indent + 2) e
281 `T.append` "\n" `T.append` T.replicate indent " " `T.append` "}"
282 createIfBlock (cond, e) =
283 "if (" `T.append` compileJS' indent cond `T.append`") {\n"
284 `T.append` T.replicate (indent + 2) " "
285 `T.append` compileJS' (indent + 2) e
287 `T.append` T.replicate indent " "
290compileJS' indent (JSSwitch val [(_,JSSeq seq)] Nothing) =
291 let (h,t) = splitAt 1 seq in
292 (T.concat (map (compileJS' indent) h) `T.append` ";\n")
294 T.intercalate ";\n" $ map (
295 (T.replicate indent " " `T.append`) . compileJS' indent
299compileJS' indent (JSSwitch val branches def) =
300 "switch(" `T.append` compileJS' indent val `T.append` "){\n"
301 `T.append` T.concat (map mkBranch branches)
302 `T.append` mkDefault def
303 `T.append` T.replicate indent " " `T.append` "}"
305 mkBranch :: (JS, JS) -> T.Text
306 mkBranch (tag, code) =
307 T.replicate (indent + 2) " "
309 `T.append` compileJS' indent tag
311 `T.append` compileJS' (indent + 4) code
313 `T.append` (T.replicate (indent + 4) " " `T.append` "break;\n")
315 mkDefault :: Maybe JS -> T.Text
316 mkDefault Nothing = ""
317 mkDefault (Just def) =
318 T.replicate (indent + 2) " " `T.append` "default:\n"
319 `T.append` compileJS' (indent + 4)def
323compileJS' indent (JSTernary cond true false) =
324 let c = compileJS' indent cond
325 t = compileJS' indent true
326 f = compileJS' indent false in
335compileJS' indent (JSParens js) =
336 "(" `T.append` compileJS' indent js `T.append` ")"
338compileJS' indent (JSWhile cond body) =
339 "while (" `T.append` compileJS' indent cond `T.append` ") {\n"
340 `T.append` compileJS' (indent + 2) body
341 `T.append` "\n" `T.append` T.replicate indent " " `T.append` "}"
343compileJS' indent (JSWord word)
344 | JSWord8 b <- word =
345 "new Uint8Array([" `T.append` T.pack (show b) `T.append` "])"
346 | JSWord16 b <- word =
347 "new Uint16Array([" `T.append` T.pack (show b) `T.append` "])"
348 | JSWord32 b <- word =
349 "new Uint32Array([" `T.append` T.pack (show b) `T.append` "])"
350 | JSWord64 b <- word =
351 "i$bigInt(\"" `T.append` T.pack (show b) `T.append` "\")"
353jsInstanceOf :: JS -> String -> JS
354jsInstanceOf obj cls = JSBinOp "instanceof" obj (JSIdent cls)
356jsOr :: JS -> JS -> JS
357jsOr lhs rhs = JSBinOp "||" lhs rhs
359jsAnd :: JS -> JS -> JS
360jsAnd lhs rhs = JSBinOp "&&" lhs rhs
362jsMeth :: JS -> String -> [JS] -> JS
363jsMeth obj meth args = JSApp (JSProj obj meth) args
365jsCall :: String -> [JS] -> JS
366jsCall fun args = JSApp (JSIdent fun) args
369jsTypeOf js = JSPreOp "typeof " js
371jsEq :: JS -> JS -> JS
372jsEq lhs@(JSNum (JSInteger _)) rhs = JSApp (JSProj lhs "equals") [rhs]
373jsEq lhs rhs@(JSNum (JSInteger _)) = JSApp (JSProj lhs "equals") [rhs]
374jsEq lhs rhs = JSBinOp "==" lhs rhs
376jsNotEq :: JS -> JS -> JS
377jsNotEq lhs rhs = JSBinOp "!=" lhs rhs
379jsIsNumber :: JS -> JS
380jsIsNumber js = (jsTypeOf js) `jsEq` (JSString "number")
383jsIsNull js = JSBinOp "==" js JSNull
386jsBigInt (JSString "0") = JSNum (JSInteger JSBigZero)
387jsBigInt (JSString "1") = JSNum (JSInteger JSBigOne)
388jsBigInt js = JSNum $ JSInteger $ JSBigIntExpr js
390jsUnPackBits :: JS -> JS
391jsUnPackBits js = JSIndex js $ JSNum (JSInt 0)
393jsPackUBits8 :: JS -> JS
394jsPackUBits8 js = JSNew "Uint8Array" [JSArray [js]]
396jsPackUBits16 :: JS -> JS
397jsPackUBits16 js = JSNew "Uint16Array" [JSArray [js]]
399jsPackUBits32 :: JS -> JS
400jsPackUBits32 js = JSNew "Uint32Array" [JSArray [js]]
402jsPackSBits8 :: JS -> JS
403jsPackSBits8 js = JSNew "Int8Array" [JSArray [js]]
405jsPackSBits16 :: JS -> JS
406jsPackSBits16 js = JSNew "Int16Array" [JSArray [js]]
408jsPackSBits32 :: JS -> JS
409jsPackSBits32 js = JSNew "Int32Array" [JSArray [js]]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list