-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.c
110 lines (103 loc) · 3.11 KB
/
env.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include "util.h"
#include "symbol.h"
#include "types.h"
#include "env.h"
#include "translate.h"
#include "frame.h"
/*Lab4: Your implementation of lab4*/
S_table E_base_tenv(void) {
S_table init_t = S_empty();
S_enter(init_t, S_Symbol("int"), Ty_Int());
S_enter(init_t, S_Symbol("string"), Ty_String());
return init_t;
}
S_table E_base_venv(void) {
S_table t = S_empty();
S_enter(
t,
S_Symbol("print"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("print"), Ty_TyList(Ty_String(), NULL), Ty_Void())
);
S_enter(
t,
S_Symbol("printi"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("printi"), Ty_TyList(Ty_Int(), NULL), Ty_Void())
);
S_enter(
t,
S_Symbol("flush"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("flush"), NULL, Ty_Void())
);
S_enter(
t,
S_Symbol("getchar"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("getchar"), NULL, Ty_String())
);
S_enter(
t,
S_Symbol("ord"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("ord"), Ty_TyList(Ty_String(), NULL), Ty_Int())
);
S_enter(
t,
S_Symbol("chr"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("chr"), Ty_TyList(Ty_Int(), NULL), Ty_String())
);
// S_enter(
// t,
// S_Symbol("allocRecord"),
// E_FunEntry(Tr_outermost(), Temp_namedlabel("allocRecord"), Ty_TyList(Ty_Int(), NULL), Ty_Int())
// );
S_enter(
t,
S_Symbol("size"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("size"), Ty_TyList(Ty_String(), NULL), Ty_Int())
);
S_enter(
t,
S_Symbol("substring"),
E_FunEntry(
Tr_outermost(),
Temp_namedlabel("substring"),
Ty_TyList(Ty_String(), Ty_TyList(Ty_Int(), Ty_TyList(Ty_Int(), NULL))),
Ty_String())
);
S_enter(
t,
S_Symbol("concat"),
E_FunEntry(
Tr_outermost(),
Temp_namedlabel("concat"),
Ty_TyList(Ty_String(), Ty_TyList(Ty_String(), NULL)),
Ty_String())
);
S_enter(
t,
S_Symbol("not"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("not"), Ty_TyList(Ty_Int(), NULL), Ty_Int())
);
S_enter(
t,
S_Symbol("exit"),
E_FunEntry(Tr_outermost(), Temp_namedlabel("exit"), Ty_TyList(Ty_Int(), NULL), Ty_Void())
);
return t;
}
E_enventry E_VarEntry(Tr_access a, Ty_ty ty) {
E_enventry final;
final = checked_malloc(sizeof(*final));
final->kind = E_varEntry;
final->u.var.access = a;
final->u.var.ty = ty;
return final;
}
E_enventry E_FunEntry(Tr_level level, Temp_label label, Ty_tyList fms, Ty_ty resl) {
E_enventry final = checked_malloc(sizeof(*final));
final->kind = E_funEntry;
final->u.fun.formals = fms;
final->u.fun.level = level;
final->u.fun.label = label;
final->u.fun.result = resl;
return final;
}