git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
If-Then-Else expressions
commit536281bba91ed7ed8fe8a567d02cb246dbf45aa3 authorPhil Freeman <paf31@cantab.net> dateSat 26 Oct 2013, 17:04 (13 years ago) parent071d687
5 files changed+24
examples/Conditional.ps+5 src/PureScript/CodeGen/JS.hs+7 src/PureScript/Parser/Values.hs+6 src/PureScript/TypeChecker/Types.hs+5 src/PureScript/Values.hs+1
examples/Conditional.psnew file+5
@@ -0,0 +1,5 @@
1+fns = \f -> if f true then f else \x -> x
2+
3+bin = \x -> if x && !x then x else x
4+
5+not = \x -> if x then false else true
src/PureScript/CodeGen/JS.hs+7
@@ -63,6 +63,12 @@ literals = Pattern $ A.Kleisli match
6363 match (Var ident) = Just (identToJs ident)
6464 match _ = Nothing
6565
66+ifThenElse :: Pattern Value ((Value, Value), Value)
67+ifThenElse = Pattern $ A.Kleisli match
68+ where
69+ match (IfThenElse cond th el) = Just ((th, el), cond)
70+ match _ = Nothing
71+
6672accessor :: Pattern Value (String, Value)
6773accessor = Pattern $ A.Kleisli match
6874 where
@@ -116,6 +122,7 @@ valueToJs = fromMaybe (error "Incomplete pattern") . pattern matchValue
116122 , [ Wrap indexer $ \index val -> val ++ "[" ++ index ++ "]" ]
117123 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
118124 , [ Split lam $ \args val -> "function (" ++ intercalate "," args ++ ") { return " ++ valueToJs val ++ "; }" ]
125+ , [ Wrap ifThenElse $ \(th, el) cond -> cond ++ " ? " ++ valueToJs th ++ " : " ++ valueToJs el ]
119126 , [ binary LessThan "<" ]
120127 , [ binary LessThanOrEqualTo "<=" ]
121128 , [ binary GreaterThan ">" ]
src/PureScript/Parser/Values.hs+6
@@ -74,6 +74,11 @@ parseCase = Case <$> (P.between (C.reserved "case") (C.indented *> C.reserved "o
7474parseCaseAlternative :: P.Parsec String P.Column (Binder, Value)
7575parseCaseAlternative = (,) <$> (parseGuardedBinder <* C.lexeme (P.string "->")) <*> parseValue
7676
77+parseIfThenElse :: P.Parsec String P.Column Value
78+parseIfThenElse = IfThenElse <$> (C.reserved "if" *> C.indented *> parseValue)
79+ <*> (C.indented *> C.reserved "then" *> C.indented *> parseValue)
80+ <*> (C.indented *> C.reserved "else" *> C.indented *> parseValue)
81+
7782parseBlock :: P.Parsec String P.Column Value
7883parseBlock = Block <$> (C.reserved "do" *> parseManyStatements)
7984
@@ -92,6 +97,7 @@ parseValueAtom = C.indented *> P.choice (map P.try
9297 , parseConstructor
9398 , parseBlock
9499 , parseCase
100+ , parseIfThenElse
95101 , C.parens parseValue ])
96102
97103parseValue :: P.Parsec String P.Column Value
src/PureScript/TypeChecker/Types.hs+5
@@ -266,6 +266,11 @@ typeConstraints m (Case val binders) = do
266266 ret <- fresh
267267 cs2 <- typeConstraintsForBinders m n1 ret binders
268268 return (cs1 ++ cs2, ret)
269+typeConstraints m (IfThenElse cond th el) = do
270+ (cs1, n1) <- typeConstraints m cond
271+ (cs2, n2) <- typeConstraints m th
272+ (cs3, n3) <- typeConstraints m el
273+ return (TypeConstraint n1 Boolean : TypeConstraint n2 (TUnknown n3) : cs1 ++ cs2 ++ cs3, n2)
269274typeConstraints m (TypedValue val poly@(PolyType idents ty)) = do
270275 kind <- kindOf poly
271276 guardWith ("Expected type of kind *, was " ++ show kind) $ kind == Star
src/PureScript/Values.hs+1
@@ -61,6 +61,7 @@ data Value
6161 | Abs [Ident] Value
6262 | App Value [Value]
6363 | Var Ident
64+ | IfThenElse Value Value Value
6465 | Block [Statement]
6566 | Constructor String
6667 | Case Value [(Binder, Value)]