-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpl_code.h
More file actions
209 lines (192 loc) · 4.24 KB
/
pl_code.h
File metadata and controls
209 lines (192 loc) · 4.24 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once
#include <cstring>
#include <sstream>
#include <list>
#include <vector>
enum opcode_type {
op_nop,
op_lit,
op_opr,
op_lod,
op_sto,
op_cal,
op_int,
op_jmp,
op_jpc,
op_red,
op_wrt
};
const char* opcode_name[] = {
"nop",
"lit",
"opr",
"lod",
"sto",
"cal",
"int",
"jmp",
"jpc",
"red",
"wrt",
nullptr
};
enum opr_type {
calc_ret,
calc_nega,
calc_plus,
calc_minus,
calc_mult,
calc_div,
calc_odd,
calc_eq,
calc_neq,
calc_les,
calc_leq,
calc_grt,
calc_geq
};
enum sym_type {
sym_var = 1,
sym_const,
sym_proc
};
struct symbol
{
std::string name;
int type;
int number;
int procedure_arg_size;
symbol(int t=sym_var,int num=0,std::string n="",int arg=0)
{
type=t;
number=num;
name=n;
procedure_arg_size=arg;
return;
}
symbol(const symbol& tmp)
{
name=tmp.name;
number=tmp.number;
type=tmp.type;
procedure_arg_size=tmp.procedure_arg_size;
return;
}
};
struct bytecode
{
unsigned char opcode;
unsigned char level;
int opnum;
bytecode(unsigned char oc,unsigned char lvl,int num)
{
opcode=oc;
level=lvl;
opnum=num;
return;
}
bytecode(const bytecode& tmp)
{
opcode=tmp.opcode;
level=tmp.level;
opnum=tmp.opnum;
return;
}
};
std::list<std::vector<symbol>> symbol_table;
std::vector<bytecode> exec_code;
void emit(unsigned char opcode=op_nop,unsigned char level=0,int opnum=0)
{
bytecode new_code(opcode,level,opnum);
exec_code.push_back(new_code);
return;
}
void add_scope()
{
std::vector<symbol> new_scope;
symbol_table.push_front(new_scope);
return;
}
// type=sym_const,const_number means the constant
// type=sym_proc ,const_number means the entry
void add_new_symbol(int type=sym_var,int const_number=0,std::string symbol_name="")
{
int size=symbol_table.front().size();
for(int i=0;i<size;++i)
if(symbol_table.front()[i].name==symbol_name)
{
die("redefinition of symbol: "+symbol_name);
return;
}
symbol new_symbol(type,const_number,symbol_name);
symbol_table.front().push_back(new_symbol);
return;
}
void set_procedure_arg_size(std::string symbol_name,int arg_size)
{
for (auto& scope : symbol_table)
{
int size = scope.size();
for(int i = 0; i < size; ++i)
if(scope[i].name==symbol_name)
{
scope[i].procedure_arg_size = arg_size;
return;
}
}
return;
}
int get_symbol_place(std::string symbol_name)
{
int level=0;
for(std::list<std::vector<symbol> >::iterator i=symbol_table.begin();i!=symbol_table.end();++i)
{
int size=i->size();
int cnt=0;
for(int j=0;j<size;++j)
{
if((*i)[j].type==sym_var)++cnt;
if((*i)[j].name==symbol_name)
return ((level<<16)+cnt-1);
}
++level;
}
die("undefined symbol: "+symbol_name);
return -1;
}
int get_arg_size(std::string symbol_name)
{
for(std::list<std::vector<symbol> >::iterator i=symbol_table.begin();i!=symbol_table.end();++i)
{
int size=i->size();
for(int j=0;j<size;++j)
if((*i)[j].name==symbol_name)
return (*i)[j].procedure_arg_size;
}
return 0;
}
symbol get_symbol(std::string symbol_name)
{
for(std::list<std::vector<symbol> >::iterator i=symbol_table.begin();i!=symbol_table.end();++i)
{
int size=i->size();
for(int j=0;j<size;++j)
if((*i)[j].name==symbol_name)
return (*i)[j];
}
die("undefined symbol: "+symbol_name);
symbol error_sym(-1);
return error_sym;
}
int to_number(std::string num)
{
int sum=0;
for(int i=0;i<num.length();++i)
sum=sum*10+(num[i]-'0');
return sum;
}
void del_scope()
{
symbol_table.pop_front();
return;
}