all: added parenthesized expressions
This commit is contained in:
@@ -47,6 +47,7 @@ eval state [BSym x] =
|
|||||||
case M.lookup x state of
|
case M.lookup x state of
|
||||||
Just val -> (val, state)
|
Just val -> (val, state)
|
||||||
Nothing -> (BErr (x ++ " is undefined"), state)
|
Nothing -> (BErr (x ++ " is undefined"), state)
|
||||||
|
eval state [BBraced vals] = eval state vals
|
||||||
eval state [BCall (BSym name) args] =
|
eval state [BCall (BSym name) args] =
|
||||||
case M.lookup name state of
|
case M.lookup name state of
|
||||||
Just val@BFun{} -> funCall state val args
|
Just val@BFun{} -> funCall state val args
|
||||||
@@ -60,6 +61,9 @@ treeEval :: State -> [Value] -> [Value] -> [Value] -> Value
|
|||||||
treeEval _ [] [] (num:_) = num
|
treeEval _ [] [] (num:_) = num
|
||||||
treeEval state [] ops nums = handleOp state [] ops nums
|
treeEval state [] ops nums = handleOp state [] ops nums
|
||||||
treeEval state (x@(BNum _):xy) ops nums = treeEval state xy ops (x: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 (BBool x:xy) ops nums =
|
||||||
treeEval state xy ops ((BNum $ BInt $ if x then 1 else 0):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 =
|
treeEval state expr@(x@(BSym sym):xy) ops@(BSym op:_) nums =
|
||||||
|
@@ -131,6 +131,14 @@ fun = do
|
|||||||
return $ BFun name args body
|
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 :: P.Parser Value
|
||||||
call = do
|
call = do
|
||||||
name <- symbol
|
name <- symbol
|
||||||
@@ -149,6 +157,7 @@ expr = P.try bool
|
|||||||
P.<|> P.try while
|
P.<|> P.try while
|
||||||
P.<|> P.try parseIf
|
P.<|> P.try parseIf
|
||||||
P.<|> P.try fun
|
P.<|> P.try fun
|
||||||
|
P.<|> P.try braces
|
||||||
P.<|> P.try call
|
P.<|> P.try call
|
||||||
P.<|> P.try number
|
P.<|> P.try number
|
||||||
P.<|> symbol
|
P.<|> symbol
|
||||||
|
@@ -11,6 +11,7 @@ data Value = BNum Number
|
|||||||
| BDef Value [Value]
|
| BDef Value [Value]
|
||||||
| BFun String [String] [[Value]]
|
| BFun String [String] [[Value]]
|
||||||
| BCall Value [[Value]]
|
| BCall Value [[Value]]
|
||||||
|
| BBraced [Value]
|
||||||
| BErr String
|
| BErr String
|
||||||
instance Show Value where
|
instance Show Value where
|
||||||
show (BBool b) = if b then "true" else "false"
|
show (BBool b) = if b then "true" else "false"
|
||||||
|
Reference in New Issue
Block a user