-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ast.hs
64 lines (55 loc) · 1.62 KB
/
Ast.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
module Ast where
import qualified Data.Map as Map
import Data.List (intersperse)
type Context = Map.Map String Ast
data Ast = Var String
| Function String Ast
| Application Ast Ast
| IfExpr Ast Ast Ast
| LetExpr [(String, Ast)] Ast
| LetRec [(String, Ast)] Ast
| Bool Bool
| String String
| Unit
| Number Integer
| BinaryExpr String Ast Ast
| Closure Ast Context
| List [Ast]
| Car Ast
| Cdr Ast
| Cons Ast Ast
| IsNil Ast
deriving (Eq)
data Type = TyVar String
| TyBool
| TyUnit
| TyNum
| TyString
| TyList Type
| TyFun Type Type
deriving (Eq)
data Scheme = Forall [String] Type
comparer = ["==", "!=", "<=", "<", ">=", ">"]
arith = ["+", "-", "*", "/"]
-- Print
instance Show Type where
show (TyFun t1@(TyFun _ _) t2) = "(" ++ show t1 ++ ") -> " ++ show t2
show (TyFun t1 t2) = show t1 ++ " -> " ++ show t2
show (TyVar v) = v
show TyBool = "Bool"
show TyNum = "Number"
show TyUnit = "Unit"
show TyString = "String"
show (TyList t) = "[" ++ show t ++ "]"
instance Show Ast where
show (Var v) = v
show (Closure _ _) = "func"
show (Bool b) = show b
show (String s) = s
show Unit = "()"
show (Number n) = show n
show (List l) = "[" ++ unwords (intersperse "," $ map show l) ++ "]"
show _ = ""
instance Show Scheme where
show (Forall [] ty) = show ty
show (Forall vs ty) = "forall " ++ unwords (intersperse "," vs) ++ ". " ++ show ty