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

@@ -1,11 +1,21 @@
module BC.Types where
import Data.List (intercalate)
data Value = BNum Number
| BBool Bool
| BOp String
| BIf [Value] [Value] (Maybe [Value])
| BErr String
instance Show Value where
show (BBool b) = if b then "true" else "false"
show (BIf x y z) =
"if (" ++ unwords (map show x) ++ ") {\n\t" ++
intercalate "\n\t" (map show y) ++ "\n}" ++
(case z of
Just vals ->
" else {\n\t" ++ intercalate "\n\t" (map show vals) ++ "\n}"
Nothing -> "")
show (BOp o) = o
show (BNum n) = show n
show (BErr e) = "error: " ++ e
@@ -36,11 +46,11 @@ instance Floating Number where
tanh x = sinh x / cosh x
instance Num Number where
(BInt x) + (BInt y) = BInt $ x * y
(BFloat x) + (BFloat y) = BFloat $ x * y
(BInt x) + (BFloat y) = BFloat $ fromIntegral x * y
(BFloat x) + (BInt y) = BFloat $ x * fromIntegral y
(BInt x) * (BInt y) = BInt $ x * y
(BInt x) + (BInt y) = BInt $ x + y
(BFloat x) + (BFloat y) = BFloat $ x + y
(BInt x) + (BFloat y) = BFloat $ fromIntegral x + y
(BFloat x) + (BInt y) = BFloat $ x + fromIntegral y
(BInt x) * (BInt y) = BInt $ x + y
(BFloat x) * (BFloat y) = BFloat $ x * y
(BInt x) * (BFloat y) = BFloat $ fromIntegral x * y
(BFloat x) * (BInt y) = BFloat $ x * fromIntegral y