diff --git a/src/BC/Eval.hs b/src/BC/Eval.hs index 738db66..9d390c0 100644 --- a/src/BC/Eval.hs +++ b/src/BC/Eval.hs @@ -47,6 +47,7 @@ eval state [BSym x] = case M.lookup x state of Just val -> (val, state) Nothing -> (BErr (x ++ " is undefined"), state) +eval state [BBraced vals] = eval state vals eval state [BCall (BSym name) args] = case M.lookup name state of Just val@BFun{} -> funCall state val args @@ -60,6 +61,9 @@ treeEval :: State -> [Value] -> [Value] -> [Value] -> Value treeEval _ [] [] (num:_) = num treeEval state [] ops nums = handleOp state [] ops nums treeEval state (x@(BNum _):xy) ops nums = treeEval state xy ops (x:nums) +treeEval state (BBraced x:xy) ops nums = + let (res, nstate) = eval state x + in treeEval nstate (res:xy) ops nums treeEval state (BBool x:xy) ops nums = treeEval state xy ops ((BNum $ BInt $ if x then 1 else 0):nums) treeEval state expr@(x@(BSym sym):xy) ops@(BSym op:_) nums = diff --git a/src/BC/Parse.hs b/src/BC/Parse.hs index cffdac0..8ed077c 100644 --- a/src/BC/Parse.hs +++ b/src/BC/Parse.hs @@ -131,6 +131,14 @@ fun = do return $ BFun name args body +braces :: P.Parser Value +braces = do + _ <- P.string "(" + parsed <- outerparser + _ <- P.string ")" + return $ BBraced parsed + + call :: P.Parser Value call = do name <- symbol @@ -149,6 +157,7 @@ expr = P.try bool P.<|> P.try while P.<|> P.try parseIf P.<|> P.try fun + P.<|> P.try braces P.<|> P.try call P.<|> P.try number P.<|> symbol diff --git a/src/BC/Types.hs b/src/BC/Types.hs index 2b289c6..3ccb1cc 100644 --- a/src/BC/Types.hs +++ b/src/BC/Types.hs @@ -11,6 +11,7 @@ data Value = BNum Number | BDef Value [Value] | BFun String [String] [[Value]] | BCall Value [[Value]] + | BBraced [Value] | BErr String instance Show Value where show (BBool b) = if b then "true" else "false"