1-----------------------------------------------------------------------------
3-- Module : PureScript.CodeGen.JS
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15module PureScript.CodeGen.JS (
20import Data.Maybe (fromMaybe)
21import Data.List (intercalate)
22import qualified Control.Arrow as A
23import Control.Arrow ((<+>))
24import Control.Applicative
26import PureScript.Types
27import PureScript.Values
28import PureScript.Names
29import PureScript.Declarations
30import PureScript.CodeGen.Common
31import PureScript.CodeGen.Common.Gen
33declToJs :: Declaration -> Maybe String
34declToJs (ValueDeclaration ident (Abs args ret)) = Just $ "function " ++ identToJs ident ++ "(" ++ intercalate "," (map identToJs args) ++ ") { return " ++ valueToJs ret ++ "; }"
35declToJs (ValueDeclaration ident val) = Just $ "var " ++ identToJs ident ++ " = " ++ valueToJs val ++ ";"
36declToJs (DataDeclaration _ _ ctors) =
37 Just $ flip concatMap ctors $ \(ctor, maybeTy) ->
39 Nothing -> "var " ++ ctor ++ " = { ctor: '" ++ ctor ++ "' };"
40 Just _ -> "var " ++ ctor ++ " = function (value) { return { ctor: '" ++ ctor ++ "', value: value }; };"
43literals :: Pattern Value String
44literals = Pattern $ A.Kleisli match
46 match (NumericLiteral n) = Just $ either show show n
47 match (StringLiteral s) = Just $ show s
48 match (BooleanLiteral True) = Just "true"
49 match (BooleanLiteral False) = Just "false"
50 match (ArrayLiteral xs) = Just $ "[" ++ intercalate "," (map valueToJs xs) ++ "]"
51 match (ObjectLiteral ps) = Just $ "{" ++ intercalate "," (map objectPropertyToJs ps) ++ "}"
52 match (Constructor name) = Just name
53 match (Block sts) = Just $ "(function () {" ++ intercalate ";" (map statementToJs sts) ++ "})()"
54 match (Case value binders) = Just $ "(" ++ runGen (bindersToJs binders) ++ ")(" ++ valueToJs value ++ ")"
56 bindersToJs :: [(Binder, Value)] -> Gen String
57 bindersToJs binders = do
59 jss <- flip mapM binders $ \(binder, result) -> do
60 let js = valueToJs result
61 binderToJs valName ("return " ++ js ++ ";") binder
62 return $ "function (" ++ valName ++ ") {" ++ concat jss ++ "throw \"Failed pattern match\"; }"
63 match (Var ident) = Just (identToJs ident)
66accessor :: Pattern Value (String, Value)
67accessor = Pattern $ A.Kleisli match
69 match (Accessor prop val) = Just (prop, val)
72indexer :: Pattern Value (String, Value)
73indexer = Pattern $ A.Kleisli match
75 match (Indexer index val) = Just (valueToJs index, val)
78app :: Pattern Value (String, Value)
79app = Pattern $ A.Kleisli match
81 match (App val args) = Just (intercalate "," (map valueToJs args), val)
84lam :: Pattern Value ([String], Value)
85lam = Pattern $ A.Kleisli match
87 match (Abs args val) = Just (map identToJs args, val)
90unary :: UnaryOperator -> String -> Operator Value String
91unary op str = Wrap pattern (++)
93 pattern :: Pattern Value (String, Value)
94 pattern = Pattern $ A.Kleisli match
96 match (Unary op' val) | op' == op = Just (str, val)
99binary :: BinaryOperator -> String -> Operator Value String
100binary op str = AssocR pattern (\v1 v2 -> v1 ++ " " ++ str ++ " " ++ v2)
102 pattern :: Pattern Value (Value, Value)
103 pattern = Pattern $ A.Kleisli match
105 match (Binary op' v1 v2) | op' == op = Just (v1, v2)
108valueToJs :: Value -> String
109valueToJs = fromMaybe (error "Incomplete pattern") . pattern matchValue
111 matchValue :: Pattern Value String
112 matchValue = buildPrettyPrinter operators (literals <+> fmap parens matchValue)
113 operators :: OperatorTable Value String
115 OperatorTable $ [ [ Wrap accessor $ \prop val -> val ++ "." ++ prop ]
116 , [ Wrap indexer $ \index val -> val ++ "[" ++ index ++ "]" ]
117 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
118 , [ Split lam $ \args val -> "function (" ++ intercalate "," args ++ ") { return " ++ valueToJs val ++ "; }" ]
119 , [ binary LessThan "<" ]
120 , [ binary LessThanOrEqualTo "<=" ]
121 , [ binary GreaterThan ">" ]
122 , [ binary GreaterThanOrEqualTo ">=" ]
124 , [ unary BitwiseNot "~" ]
125 , [ unary Negate "-" ]
126 , [ binary Multiply "*" ]
127 , [ binary Divide "/" ]
128 , [ binary Modulus "%" ]
129 , [ binary Concat "+" ]
131 , [ binary Subtract "-" ]
132 , [ binary ShiftLeft "<<" ]
133 , [ binary ShiftRight ">>" ]
134 , [ binary ZeroFillShiftRight ">>>" ]
135 , [ binary EqualTo "===" ]
136 , [ binary NotEqualTo "!==" ]
137 , [ binary BitwiseAnd "&" ]
138 , [ binary BitwiseXor "^" ]
139 , [ binary BitwiseOr "|" ]
140 , [ binary And "&&" ]
144unaryOperatorString :: UnaryOperator -> String
145unaryOperatorString Negate = "-"
146unaryOperatorString Not = "!"
147unaryOperatorString BitwiseNot = "~"
149binaryOperatorString :: BinaryOperator -> String
150binaryOperatorString Add = "+"
151binaryOperatorString Subtract = "-"
152binaryOperatorString Multiply = "*"
153binaryOperatorString Divide = "/"
154binaryOperatorString Modulus = "%"
155binaryOperatorString LessThan = "<"
156binaryOperatorString LessThanOrEqualTo = "<="
157binaryOperatorString GreaterThan = ">"
158binaryOperatorString GreaterThanOrEqualTo = ">="
159binaryOperatorString BitwiseAnd = "&"
160binaryOperatorString BitwiseOr = "|"
161binaryOperatorString BitwiseXor = "^"
162binaryOperatorString ShiftLeft = "<<"
163binaryOperatorString ShiftRight = ">>"
164binaryOperatorString ZeroFillShiftRight = ">>>"
165binaryOperatorString EqualTo = "==="
166binaryOperatorString NotEqualTo = "!=="
167binaryOperatorString And = "&&"
168binaryOperatorString Or = "||"
169binaryOperatorString Concat = "+"
171binderToJs :: String -> String -> Binder -> Gen String
172binderToJs varName done NullBinder = return done
173binderToJs varName done (StringBinder str) =
174 return $ "if (" ++ varName ++ " === \"" ++ str ++ "\") {" ++ done ++ " }"
175binderToJs varName done (NumberBinder num) =
176 return $ "if (" ++ varName ++ " === " ++ either show show num ++ ") {" ++ done ++ " }"
177binderToJs varName done (BooleanBinder True) =
178 return $ "if (" ++ varName ++ ") {" ++ done ++ " }"
179binderToJs varName done (BooleanBinder False) =
180 return $ "if (!" ++ varName ++ ") {" ++ done ++ " }"
181binderToJs varName done (VarBinder ident) =
182 return $ "var " ++ identToJs ident ++ " = " ++ varName ++ "; " ++ done
183binderToJs varName done (NullaryBinder ctor) =
184 return $ "if (" ++ varName ++ ".ctor === \"" ++ ctor ++ "\") { " ++ done ++ " }"
185binderToJs varName done (UnaryBinder ctor b) = do
187 js <- binderToJs value done b
188 return $ "if (" ++ varName ++ ".ctor === \"" ++ ctor ++ "\") { " ++ "var " ++ value ++ " = " ++ varName ++ ".value; " ++ js ++ " }"
189binderToJs varName done (ObjectBinder bs) = go done bs
191 go done [] = return done
192 go done ((prop, binder):bs) = do
195 js <- binderToJs propVar done' binder
196 return $ "var " ++ propVar ++ " = " ++ varName ++ "." ++ prop ++ ";" ++ js
197binderToJs varName done (ArrayBinder bs rest) = do
198 js <- go done rest 0 bs
199 return $ "if (" ++ varName ++ ".length " ++ cmp ++ " " ++ show (length bs) ++ ") { " ++ js ++ " }"
201 cmp = maybe "===" (const ">=") rest
202 go done Nothing _ [] = return done
203 go done (Just binder) index [] = do
205 js <- binderToJs restVar done binder
206 return $ "var " ++ restVar ++ " = " ++ varName ++ ".slice(" ++ show index ++ "); " ++ js
207 go done rest index (binder:bs) = do
209 done' <- go done rest (index + 1) bs
210 js <- binderToJs elVar done' binder
211 return $ "var " ++ elVar ++ " = " ++ varName ++ "[" ++ show index ++ "]; " ++ js
212binderToJs varName done (NamedBinder ident binder) = do
213 js <- binderToJs varName done binder
214 return $ "var " ++ identToJs ident ++ " = " ++ varName ++ "; " ++ js
215binderToJs varName done (GuardedBinder cond binder) = binderToJs varName done' binder
217 done' = "if (" ++ valueToJs cond ++ ") { " ++ done ++ "}"
219objectPropertyToJs :: (String, Value) -> String
220objectPropertyToJs (key, value) = key ++ ":" ++ valueToJs value
222statementToJs :: Statement -> String
223statementToJs (VariableIntroduction ident value) = "var " ++ identToJs ident ++ " = " ++ valueToJs value
224statementToJs (Assignment target value) = assignmentTargetToJs target ++ " = " ++ valueToJs value
225statementToJs (While cond sts) = "while ("
226 ++ valueToJs cond ++ ") {"
227 ++ intercalate ";" (map statementToJs sts) ++ "}"
228statementToJs (For ident start end sts) = "for (" ++
229 identToJs ident ++ " = " ++ valueToJs start ++ ";"
230 ++ identToJs ident ++ " < " ++ valueToJs end ++ ";"
231 ++ identToJs ident ++ "++) {"
232 ++ intercalate ";" (map statementToJs sts) ++ "}"
233statementToJs (ForEach ident arr sts) = valueToJs arr
234 ++ ".forEach(function(" ++ identToJs ident ++ ") {"
235 ++ intercalate ";" (map statementToJs sts) ++ "})"
236statementToJs (If ifst) = ifStatementToJs ifst
237statementToJs (Return value) = "return " ++ valueToJs value
239ifStatementToJs :: IfStatement -> String
240ifStatementToJs (IfStatement cond thens elst) =
242 ++ valueToJs cond ++ ") {"
243 ++ intercalate ";" (map statementToJs thens) ++ "}"
244 ++ maybe "" elseStatementToJs elst
246elseStatementToJs :: ElseStatement -> String
247elseStatementToJs (Else sts) = " else {" ++ intercalate ";" (map statementToJs sts) ++ "}"
248elseStatementToJs (ElseIf ifst) = " else " ++ ifStatementToJs ifst
250assignmentTargetToJs :: AssignmentTarget -> String
251assignmentTargetToJs (AssignVariable ident) = identToJs ident
252assignmentTargetToJs (AssignArrayIndex index tgt) = assignmentTargetToJs tgt ++ "[" ++ valueToJs index ++ "]"
253assignmentTargetToJs (AssignObjectProperty prop tgt) = assignmentTargetToJs tgt ++ "." ++ prop
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