-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.c
125 lines (98 loc) · 3.49 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include "env.h"
#include "symbol.h"
#include "util.h"
/*Lab4: Your implementation of lab4*/
E_enventry E_VarEntry(Tr_access access, Ty_ty ty) {
E_enventry entry = checked_malloc(sizeof(*entry));
entry->kind = E_varEntry;
entry->u.var.access = access;
entry->u.var.ty = ty;
return entry;
}
E_enventry E_ROVarEntry(Tr_access access, Ty_ty ty) {
E_enventry entry = checked_malloc(sizeof(*entry));
entry->kind = E_varEntry;
entry->readonly = TRUE;
entry->u.var.access = access;
entry->u.var.ty = ty;
entry->readonly = 1;
return entry;
}
E_enventry E_FunEntry(Tr_level level, Temp_label label, Ty_tyList formals,
Ty_ty result) {
E_enventry entry = checked_malloc(sizeof(*entry));
entry->kind = E_funEntry;
entry->u.fun.level = level;
entry->u.fun.label = label;
entry->u.fun.formals = formals;
entry->u.fun.result = result;
return entry;
}
// sym->value
// type_id(name, S_symbol) -> type (Ty_ty)
S_table E_base_tenv(void) {
S_table table;
S_symbol ty_int;
S_symbol ty_string;
table = S_empty();
// basic type: string
ty_int = S_Symbol("int");
S_enter(table, ty_int, Ty_Int());
// basic type: string
ty_string = S_Symbol("string");
S_enter(table, ty_string, Ty_String());
return table;
}
S_table E_base_venv(void) {
S_table venv;
Ty_ty result;
Ty_tyList formals;
Tr_level level;
level = Tr_outermost();
venv = S_empty();
S_enter(venv, S_Symbol("flush"),
E_FunEntry(level,Temp_namedlabel("flush"), NULL, NULL));
result = Ty_Int();
formals = checked_malloc(sizeof(*formals));
formals->head = Ty_Int();
formals->tail = NULL;
S_enter(venv, S_Symbol("exit"),
E_FunEntry(level, Temp_namedlabel("exit"), formals, NULL));
S_enter(venv, S_Symbol("not"),
E_FunEntry(level, Temp_namedlabel("not"), formals, result));
result = Ty_String();
S_enter(venv, S_Symbol("chr"),
E_FunEntry(level, Temp_namedlabel("chr"), formals, result));
S_enter(venv, S_Symbol("getchar"),
E_FunEntry(level, Temp_namedlabel("getchar"), NULL, result));
formals = checked_malloc(sizeof(*formals));
formals->head = Ty_String();
formals->tail = NULL;
S_enter(venv, S_Symbol("print"),
E_FunEntry(level, Temp_namedlabel("print"), formals, Ty_Void()));
S_enter(venv, S_Symbol("printi"),
E_FunEntry(level, Temp_namedlabel("printi"),
Ty_TyList(Ty_Int(), NULL), Ty_Void()));
result = Ty_Int();
S_enter(venv, S_Symbol("ord"),
E_FunEntry(level, Temp_namedlabel("ord"), formals, result));
S_enter(venv, S_Symbol("size"),
E_FunEntry(level, Temp_namedlabel("size"), formals, result));
result = Ty_String();
formals = checked_malloc(sizeof(*formals));
formals->head = Ty_String();
formals->tail = checked_malloc(sizeof(*formals));
formals->tail->head = Ty_String();
S_enter(venv, S_Symbol("concat"),
E_FunEntry(level, Temp_namedlabel("concat"), formals, result));
formals = checked_malloc(sizeof(*formals));
formals->head = Ty_String();
formals->tail = checked_malloc(sizeof(*formals));
formals->tail->head = Ty_Int();
formals->tail->tail = checked_malloc(sizeof(*formals));
formals->tail->tail->head = Ty_Int();
S_enter(venv, S_Symbol("substring"),
E_FunEntry(level, Temp_namedlabel("substring"), formals, result));
return venv;
}