-
Notifications
You must be signed in to change notification settings - Fork 1
/
Runtime.hs
48 lines (41 loc) · 1 KB
/
Runtime.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Runtime(
typeCast,
defaultVal,
toBool,
boolType,
true,
false,
isTrue,
isFalse,
voidType,
void,
intType,
makePtr
)where
import Grammar
import Parse
import Type
import Value
import Control.Applicative
-- Input: Target type, a RIGHT value
typeCast :: Type -> Value -> Value
typeCast t1 v@(RVal t2 x)
| t1 == t1 = v
typeCast (Type "bool" 0) (RVal _ x) = if x == "0" then false else true
typeCast (Type "int" 0) (RVal Polymorphism x) = RVal intType $ show $ (read x :: Integer)
typeCast Polymorphism (RVal _ x) = RVal Polymorphism x
typeCast x v = error $ "Type Cast error: " ++ show x ++ " | " ++ show v
defaultVal :: Type -> Value
defaultVal (Type "bool" 0) = false
defaultVal (Type "int" 0) = RVal intType "0"
toBool = typeCast boolType
boolType = Type "bool" 0
true = RVal boolType "true"
false = RVal boolType "false"
isTrue = (== true) . toBool
isFalse = not . isTrue
voidType = Type "void" 0
void = RVal voidType ""
intType = Type "int" 0
makeInt = RVal intType . (show :: Integer -> String)
makePtr = Type