all: added function definitions and calls

This commit is contained in:
2017-05-19 15:45:46 +02:00
parent ca11f51e33
commit a011b5647f
4 changed files with 110 additions and 23 deletions

View File

@@ -8,20 +8,27 @@ data Value = BNum Number
| BIf [Value] [Value] (Maybe [Value])
| BWhile [Value] [Value]
| BDef Value [Value]
| BFun String [String] [Value]
| BCall Value [[Value]]
| BErr String
instance Show Value where
show (BBool b) = if b then "true" else "false"
show (BDef sym expr) = show sym ++ " = " ++ unwords (map show expr)
show (BIf cond body alt) =
"if (" ++ unwords (map show cond) ++ ") {\n\t" ++
intercalate "\n\t" (map show body) ++ "\n}" ++
"if (" ++ unwords (map show cond) ++ ") {\n " ++
unwords (map show body) ++ "\n}" ++
(case alt of
Just vals ->
" else {\n\t" ++ intercalate "\n\t" (map show vals) ++ "\n}"
" else {\n " ++ unwords (map show vals) ++ "\n}"
Nothing -> "")
show (BWhile cond body) =
"while (" ++ unwords (map show cond) ++ ") {\n\t" ++
intercalate "\n\t" (map show body) ++ "\n}"
"while (" ++ unwords (map show cond) ++ ") {\n " ++
unwords (map show body) ++ "\n}"
show (BFun name args body) =
"define " ++ name ++ "(" ++ intercalate ", " args ++ ") {\n " ++
unwords (map show body) ++ "\n}"
show (BCall name args) =
show name ++ "(" ++ intercalate ", " (map show args) ++ ")"
show (BSym o) = o
show (BNum n) = show n
show (BErr e) = "error: " ++ e