git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
If-Then-Else expressions
536281bPhil Freeman
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
63 match (Var ident) = Just (identToJs ident)
63 match (Var ident) = Just (identToJs ident)
64 match _ = Nothing
64 match _ = Nothing
65
65
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+
66accessor :: Pattern Value (String, Value)
72accessor :: Pattern Value (String, Value)
67accessor = Pattern $ A.Kleisli match
73accessor = Pattern $ A.Kleisli match
68 where
74 where
@@ -116,6 +122,7 @@ valueToJs = fromMaybe (error "Incomplete pattern") . pattern matchValue
116 , [ Wrap indexer $ \index val -> val ++ "[" ++ index ++ "]" ]
122 , [ Wrap indexer $ \index val -> val ++ "[" ++ index ++ "]" ]
117 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
123 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
118 , [ Split lam $ \args val -> "function (" ++ intercalate "," args ++ ") { return " ++ valueToJs val ++ "; }" ]
124 , [ Split lam $ \args val -> "function (" ++ intercalate "," args ++ ") { return " ++ valueToJs val ++ "; }" ]
125+ , [ Wrap ifThenElse $ \(th, el) cond -> cond ++ " ? " ++ valueToJs th ++ " : " ++ valueToJs el ]
119 , [ binary LessThan "<" ]
126 , [ binary LessThan "<" ]
120 , [ binary LessThanOrEqualTo "<=" ]
127 , [ binary LessThanOrEqualTo "<=" ]
121 , [ binary GreaterThan ">" ]
128 , [ binary GreaterThan ">" ]
src/PureScript/Parser/Values.hs+6
@@ -74,6 +74,11 @@ parseCase = Case <$> (P.between (C.reserved "case") (C.indented *> C.reserved "o
74parseCaseAlternative :: P.Parsec String P.Column (Binder, Value)
74parseCaseAlternative :: P.Parsec String P.Column (Binder, Value)
75parseCaseAlternative = (,) <$> (parseGuardedBinder <* C.lexeme (P.string "->")) <*> parseValue
75parseCaseAlternative = (,) <$> (parseGuardedBinder <* C.lexeme (P.string "->")) <*> parseValue
76
76
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+
77parseBlock :: P.Parsec String P.Column Value
82parseBlock :: P.Parsec String P.Column Value
78parseBlock = Block <$> (C.reserved "do" *> parseManyStatements)
83parseBlock = Block <$> (C.reserved "do" *> parseManyStatements)
79
84
@@ -92,6 +97,7 @@ parseValueAtom = C.indented *> P.choice (map P.try
92 , parseConstructor
97 , parseConstructor
93 , parseBlock
98 , parseBlock
94 , parseCase
99 , parseCase
100+ , parseIfThenElse
95 , C.parens parseValue ])
101 , C.parens parseValue ])
96
102
97parseValue :: P.Parsec String P.Column Value
103parseValue :: P.Parsec String P.Column Value
src/PureScript/TypeChecker/Types.hs+5
@@ -266,6 +266,11 @@ typeConstraints m (Case val binders) = do
266 ret <- fresh
266 ret <- fresh
267 cs2 <- typeConstraintsForBinders m n1 ret binders
267 cs2 <- typeConstraintsForBinders m n1 ret binders
268 return (cs1 ++ cs2, ret)
268 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)
269typeConstraints m (TypedValue val poly@(PolyType idents ty)) = do
274typeConstraints m (TypedValue val poly@(PolyType idents ty)) = do
270 kind <- kindOf poly
275 kind <- kindOf poly
271 guardWith ("Expected type of kind *, was " ++ show kind) $ kind == Star
276 guardWith ("Expected type of kind *, was " ++ show kind) $ kind == Star
src/PureScript/Values.hs+1
@@ -61,6 +61,7 @@ data Value
61 | Abs [Ident] Value
61 | Abs [Ident] Value
62 | App Value [Value]
62 | App Value [Value]
63 | Var Ident
63 | Var Ident
64+ | IfThenElse Value Value Value
64 | Block [Statement]
65 | Block [Statement]
65 | Constructor String
66 | Constructor String
66 | Case Value [(Binder, Value)]
67 | Case Value [(Binder, Value)]