-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguage.hs
73 lines (60 loc) · 1.76 KB
/
Language.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{-# OPTIONS_GHC -Wall -fwarn-incomplete-patterns -fwarn-tabs #-}
module Language where
type Builtin = String
data Value
= IntVal Int
| BoolVal Bool
| ListVal ValueType [Value]
| FuncVal FuncType ([Value] -> [Value])
instance Show Value where
show (IntVal i) = show i
show (BoolVal True) = "true"
show (BoolVal False) = "false"
show (ListVal _ l) = show l
show (FuncVal _ _) = "<function>"
data Term a
= IdTerm a
| CatTerm a (Term a) (Term a)
| BuiltinTerm a Builtin
| PushIntTerm a Int
| PushBoolTerm a Bool
| PushNilTerm a
| PushFuncTerm a (Term a)
deriving (Eq, Ord, Show)
instance Functor Term where
fmap f (IdTerm a) = IdTerm $ f a
fmap f (CatTerm a t1 t2) = CatTerm (f a) (fmap f t1) (fmap f t2)
fmap f (BuiltinTerm a s) = BuiltinTerm (f a) s
fmap f (PushIntTerm a i) = PushIntTerm (f a) i
fmap f (PushBoolTerm a b) = PushBoolTerm (f a) b
fmap f (PushNilTerm a) = PushNilTerm $ f a
fmap f (PushFuncTerm a t) = PushFuncTerm (f a) (fmap f t)
extract :: Term a -> a
extract (IdTerm a) = a
extract (CatTerm a _ _) = a
extract (BuiltinTerm a _) = a
extract (PushIntTerm a _) = a
extract (PushBoolTerm a _) = a
extract (PushNilTerm a) = a
extract (PushFuncTerm a _) = a
data ValueType
= VIntTy
| VBoolTy
| VListTy ValueType
| VFuncTy FuncType
| VVarTy String
deriving (Eq)
instance Show ValueType where
show VIntTy = "int"
show VBoolTy = "bool"
show (VListTy t) = "list " ++ show t
show (VFuncTy f) = show f
show (VVarTy s) = s
data Stack = S String [ValueType]
deriving (Eq)
instance Show Stack where
show (S a s) = a ++ " ++ " ++ show s
data FuncType = F Stack Stack
deriving (Eq)
instance Show FuncType where
show (F s t) = show s ++ " -> " ++ show t