on master help sign in
Allow full expressions as the last member of binary expressions (t + t + e)
cd83c4cEvan Czaplicki
compiler/Parse/Binop.hs+15−7
@@ -1,5 +1,6 @@
1module Parse.Binop (binops, infixStmt, OpTable) where
1module Parse.Binop (binops, infixStmt, OpTable) where
2
2
3+import Control.Applicative ((<$>))
3import Control.Monad.Error
4import Control.Monad.Error
4import Data.List (intercalate)
5import Data.List (intercalate)
5import qualified Data.Map as Map
6import qualified Data.Map as Map
@@ -38,15 +39,22 @@ opAssoc table op = Map.findWithDefault R op dict
38hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
39hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
39hasLevel table n (op,_) = opLevel table op == n
40hasLevel table n (op,_) = opLevel table op == n
40
41
42+binops :: OpTable
41−binops :: OpTable -> IParser (LExpr t v) -> IParser String -> IParser (LExpr t v)
43+ -> IParser (LExpr t v)
44+ -> IParser (LExpr t v)
45+ -> IParser String
46+ -> IParser (LExpr t v)
42−binops table term anyOp =
47+binops table term last anyOp =
43 do e <- term
48 do e <- term
44− split (table ++ preludeTable) 0 e =<< many nextOp
49+ split (table ++ preludeTable) 0 e =<< nextOps
45 where
50 where
46− nextOp = commitIf (whitespace >> anyOp) $ do
51+ nextOps = choice [ commitIf (whitespace >> anyOp) $ do
47− whitespace ; op <- anyOp
52+ whitespace ; op <- anyOp ; whitespace
48− whitespace ; e <- term
53+ expr <- Left <$> try term <|> Right <$> last
54+ case expr of
55+ Left t -> (:) (op,t) <$> nextOps
49− return (op,e)
56+ Right e -> return [(op,e)]
57+ , return [] ]
50
58
51split :: OpTable
59split :: OpTable
52 -> Int
60 -> Int
compiler/Parse/Expression.hs+3−1
@@ -127,7 +127,9 @@ appExpr = do
127-------- Normal Expressions --------
127-------- Normal Expressions --------
128
128
129binaryExpr :: IParser (LExpr t v)
129binaryExpr :: IParser (LExpr t v)
130−binaryExpr = binops [] appExpr anyOp
130+binaryExpr = binops [] appExpr lastExpr anyOp
131+ where lastExpr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
132+ <|> lambdaExpr
131
133
132ifExpr :: IParser (Expr t v)
134ifExpr :: IParser (Expr t v)
133ifExpr = reserved "if" >> whitespace >> (normal <|> multiIf)
135ifExpr = reserved "if" >> whitespace >> (normal <|> multiIf)