-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.sml
51 lines (45 loc) · 1.86 KB
/
env.sml
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
signature ENV =
sig
type ty
datatype enventry = VarEntry of {access: Translate.access, ty: ty}
| FunEntry of {level: Translate.level,
label: Temp.label,
formals: ty list,
result: ty}
val baseTenv : ty Symbol.table
val baseVenv : enventry Symbol.table
end
structure Env : ENV =
struct
structure S = Symbol
structure T = Types
type ty = T.ty
datatype enventry = VarEntry of {access: Translate.access, ty: ty}
| FunEntry of {level: Translate.level,
label: Temp.label,
formals: ty list,
result: ty}
val baseTenv = let
val tenv = S.enter(S.empty, S.symbol("int"), T.INT)
in S.enter(tenv, S.symbol("string"), T.STRING)
end
val baseVenv = let
val funcs = [("print", [T.STRING], T.UNIT),
("flush", [], T.UNIT),
("getchar", [], T.STRING),
("ord", [T.STRING], T.INT),
("chr", [T.INT], T.STRING),
("size", [T.STRING], T.INT),
("substring", [T.STRING,T.INT,T.INT], T.STRING),
("concat", [T.STRING,T.STRING], T.STRING),
("not", [T.INT], T.INT),
("exit", [T.INT], T.UNIT)
]
fun enterFunc ((id,ft,rt), venv) =
S.enter(venv, S.symbol id, FunEntry{level = Translate.outermost,
label = Temp.newlabel(),
formals = ft, result = rt})
in
foldl enterFunc S.empty funcs
end
end