2Module : IRTS.JavaScript.Specialize
3Description : The JavaScript specializer.
6Maintainer : The Idris Community.
9{-# LANGUAGE OverloadedStrings, PatternGuards #-}
11module IRTS.JavaScript.Specialize
22import qualified Data.Map.Strict as Map
23import Data.Text (Text)
24import qualified Data.Text as T
26import IRTS.JavaScript.AST
28split :: Char -> String -> [String]
31 | c == x = "" : split c xs
33 let ~(h:t) = split c xs
36qualify :: String -> Name -> Name
38qualify ns n = sNS n (reverse $ split '.' ns)
40qualifyN :: String -> String -> Name
41qualifyN ns n = qualify ns $ sUN n
43-- special-cased constructors
44type SCtor = [JsExpr] -> JsExpr
46type STest = JsExpr -> JsExpr
48type SProj = JsExpr -> Int -> JsExpr
50constructorOptimizeDB :: Map.Map Name (SCtor, STest, SProj)
51constructorOptimizeDB =
53 [ item "Prelude.Bool" "True" (const $ JsBool True) trueTest cantProj
54 , item "Prelude.Bool" "False" (const $ JsBool False) falseTest cantProj
55 , item "Prelude.Interfaces" "LT" (const $ JsInt (0-1)) ltTest cantProj
56 , item "Prelude.Interfaces" "EQ" (const $ JsInt 0) eqTest cantProj
57 , item "Prelude.Interfaces" "GT" (const $ JsInt 1) gtTest cantProj
58 -- , item "Prelude.List" "::" cons fillList uncons
59 -- , item "Prelude.List" "Nil" nil emptyList cantProj
60 -- , item "Prelude.Maybe" "Just" (\[x] -> x) notNoneTest justProj
61 -- , item "Prelude.Maybe" "Nothing" (const $ JsUndefined) noneTest cantProj
65 nil = const $ JsArray []
66 cons [h, t] = JsMethod (JsArray [h]) "concat" [t]
68 --trueTest e = JsUniOp (T.pack "!") $ JsUniOp (T.pack "!") e
70 falseTest e = JsUniOp (T.pack "!") e
71 emptyList e = JsBinOp "===" (JsProp e "length") (JsInt 0)
72 fillList e = JsBinOp ">" (JsProp e "length") (JsInt 0)
73 ltTest e = JsBinOp "<" e (JsInt 0)
74 eqTest e = JsBinOp "===" e (JsInt 0)
75 gtTest e = JsBinOp ">" e (JsInt 0)
76 noneTest e = JsBinOp "===" e JsUndefined
77 notNoneTest e = JsBinOp "!==" e JsUndefined
80 uncons x 1 = JsArrayProj (JsInt 0) x
81 uncons x 2 = JsMethod x "slice" [JsInt 1]
82 cantProj x j = error $ "This type should be projected"
88 -> (Name, (SCtor, STest, SProj))
89 item ns n ctor test match = (qualifyN ns n, (ctor, test, match))
91specialCased :: Name -> Maybe (SCtor, STest, SProj)
92specialCased n = Map.lookup n constructorOptimizeDB
95type SSig = (Int, [JsExpr] -> JsExpr)
97callSpecializeDB :: Map.Map Name (SSig)
100 [ qb "Eq" "Int" "==" "==="
101 , qb "Ord" "Int" "<" "<"
102 , qb "Ord" "Int" ">" ">"
103 , qb "Ord" "Int" "<=" "<="
104 , qb "Ord" "Int" ">=" ">="
105 , qb "Eq" "Double" "==" "==="
106 , qb "Ord" "Double" "<" "<"
107 , qb "Ord" "Double" ">" ">"
108 , qb "Ord" "Double" "<=" "<="
109 , qb "Ord" "Double" ">=" ">="
113 ( qualify "Prelude.Interfaces" $
117 (qualify "Prelude.Interfaces" $
118 SN $ ImplementationN (qualifyN "Prelude.Interfaces" intf) [ty])
119 (SN $ MethodN $ UN op)
120 , (2, \[x, y] -> JsBinOp jsop x y))
122specialCall :: Name -> Maybe SSig
123specialCall n = Map.lookup n callSpecializeDB
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