all: added parenthesized expressions

This commit is contained in:
2017-05-22 17:19:34 -04:00
parent b31965fe8a
commit a357895321
3 changed files with 14 additions and 0 deletions

View File

@@ -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 =

View File

@@ -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

View File

@@ -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"