all: add stub for if

This commit is contained in:
2017-05-17 13:21:03 +02:00
parent fa032ff54c
commit 30ecca462d
3 changed files with 63 additions and 9 deletions

View File

@@ -2,10 +2,22 @@ module BC.Eval (eval) where
import BC.Types
truthy :: Value -> Bool
truthy (BNum num) = num /= 0
truthy (BBool x) = x
truthy _ = False
eval :: [Value] -> Value
eval [x@(BNum _)] = x
eval [x@(BBool _)] = x
eval [x@(BErr _)] = x
eval [x@(BIf cond body alt)] =
if truthy (eval cond)
then eval body
else case alt of
Just vals -> eval vals
Nothing -> BBool False
eval [(BOp x)] = BErr ("operation " ++ x ++ " requires arguments")
eval [] = BOp ""
eval l = treeEval l [] []
@@ -13,6 +25,7 @@ eval l = treeEval l [] []
treeEval :: [Value] -> [Value] -> [Value] -> Value
treeEval [] [] (num:_) = num
treeEval [] ops nums = handleOp [] ops nums
treeEval (x@(BIf _ _ _):xy) ops nums = treeEval (eval [x]:xy) ops nums
treeEval (x@(BNum _):xy) ops nums = treeEval xy ops (x:nums)
treeEval ((BBool x):xy) ops nums =
treeEval xy ops ((BNum $ BInt $ if x then 1 else 0):nums)
@@ -50,6 +63,6 @@ binOp :: Value -> Maybe (Number -> Number -> Number)
binOp (BOp "*") = Just (*)
binOp (BOp "/") = Just (/)
binOp (BOp "+") = Just (+)
binOp (BOp "-") = Just (-)
binOp (BOp "-") = Just ( - )
binOp (BOp "^") = Just (**)
binOp _ = Nothing