-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocals.cpp
More file actions
299 lines (263 loc) · 6.83 KB
/
Copy pathlocals.cpp
File metadata and controls
299 lines (263 loc) · 6.83 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "errors.h"
#include "locals.h"
#include "parser.h"
#include "vm.h"
#include <string>
#include <vector>
void Locals::clear() {
vars_.clear();
frame_ = 0;
names_.clear();
}
uint Locals::size() const {
return static_cast<uint>(vars_.size());
}
void Locals::resize(uint size) {
vars_.resize(size);
}
uint Locals::frame() const {
return static_cast<uint>(frame_);
}
void Locals::set_frame(uint frame) {
frame_ = frame;
}
void Locals::enter_frame() {
VarValue v;
v.type = VarType::Frame;
v.value.frame = frame_;
frame_ = vars_.size() + 1;
vars_.push_back(v);
}
void Locals::leave_frame() {
if (vars_.empty()) {
// no frame_ to leave
frame_ = 0;
}
else {
// remove all vars_ in current frame_
vars_.resize(frame_);
frame_ = vars_.back().value.frame;
vars_.pop_back();
}
}
void Locals::add_local(const std::string& name, VarType type) {
auto it = names_.find(to_upper(name));
if (it != names_.end()) {
error(Error::DuplicateDefinition, name);
}
uint index = static_cast<uint>(names_.size());
VarName vname;
vname.type = type;
vname.name = name;
vname.index = index;
names_[to_upper(name)] = vname;
switch (vname.type) {
case VarType::Int:
comma(xtXW_TO_LOCAL);
break;
case VarType::DInt:
comma(xtXD_TO_LOCAL);
break;
case VarType::Float:
comma(xtXF_TO_LOCAL);
break;
default:
assert(0);
}
}
void Locals::init_int_local() {
int value = pop();
VarValue vv;
vv.type = VarType::Int;
vv.value.n = value;
vars_.push_back(vv);
}
void Locals::init_dint_local() {
dint value = dpop();
VarValue vv;
vv.type = VarType::DInt;
vv.value.d = value;
vars_.push_back(vv);
}
void Locals::init_float_local() {
double value = fpop();
VarValue vv;
vv.type = VarType::Float;
vv.value.f = value;
vars_.push_back(vv);
}
void Locals::get_local(uint index) {
size_t i = index + frame_;
if (i >= vars_.size()) {
error(Error::LocalsStackUnderflow);
}
switch (vars_[i].type) {
case VarType::Int:
push(vars_[i].value.n);
break;
case VarType::DInt:
dpush(vars_[i].value.d);
break;
case VarType::Float:
fpush(vars_[i].value.f);
break;
default:
assert(0);
}
}
void Locals::set_local(uint index) {
size_t i = index + frame_;
if (i >= vars_.size()) {
error(Error::LocalsStackUnderflow);
}
switch (vars_[i].type) {
case VarType::Int:
vars_[i].value.n = pop();
break;
case VarType::DInt:
vars_[i].value.d = dpop();
break;
case VarType::Float:
vars_[i].value.f = fpop();
break;
default:
assert(0);
}
}
bool Locals::find_local(const std::string& name, VarName& vname) const {
auto it = names_.find(to_upper(name));
if (it == names_.end()) {
return false;
}
else {
vname = it->second;
return true;
}
}
static void check_in_colon() {
if (cs_ddepth() != 1) {
error(Error::CompilerNesting);
}
dint pos_addr = cs_dpeek();
if (dcell_hi(pos_addr) != POS_COLON_START) {
error(Error::CompilerNesting);
}
}
void Locals::parse_declaration() {
check_in_colon();
int state = 0; // collecting locals to be initialized from stack
std::vector<VarName> init_locals;
std::vector<VarName> uninit_locals;
VarType type = VarType::Int;
while (true) {
uint size;
const char* word = parse_word(size, BL);
if (size == 0) {
break; // end of locals
}
std::string name = to_upper(std::string(word, word + size));
if (name == ":}") { // ANS
break; // end of locals
}
else if (name == "}") { // Gforth
break; // end of locals
}
else if (name == "|") { // ANS
state = 1; // collecting uninitialized locals
}
else if (name == "--") {
state = 2; // collecting and discarding output params
}
else if (name == "W:") {
type = VarType::Int;
}
else if (name == "D:") {
type = VarType::DInt;
}
else if (name == "F:") {
type = VarType::Float;
}
else if (state == 0) {
VarName def;
def.name = name;
def.type = type;
init_locals.push_back(def); // initialized locals
type = VarType::Int;
}
else if (state == 1) {
VarName def;
def.name = name;
def.type = type;
uninit_locals.push_back(def); // uninitialized locals
type = VarType::Int;
}
else if (state == 2) {
// discard output params
}
}
// build output code
while (!init_locals.empty()) {
VarName def = init_locals.back();
add_local(def.name, def.type);
init_locals.pop_back();
}
while (!uninit_locals.empty()) {
VarName def = uninit_locals.back();
switch (def.type) {
case VarType::Int:
comma(xtXLITERAL);
comma(0);
break;
case VarType::DInt:
comma(xtX2LITERAL);
dcomma(0);
break;
case VarType::Float:
comma(xtFLITERAL);
fcomma(0.0);
break;
default:
assert(0);
}
add_local(def.name, def.type);
uninit_locals.pop_back();
}
}
void f_paren_local() {
uint size = pop();
uint addr = pop();
const char* name = mem_char_ptr(addr, size);
if (size != 0) {
f_paren_local(name, size);
}
}
void f_paren_local(const char* name, uint size) {
f_paren_local(std::string(name, name + size));
}
void f_paren_local(const std::string& name) {
vm.locals.add_local(name, VarType::Int);
}
void f_locals_bar() {
while (true) {
uint size;
const char* word = parse_word(size, BL);
if (size == 0 || (size == 1 && word[0] == '|')) {
break; // end of locals
}
f_paren_local(word, size);
}
}
void f_locals_bracket() {
vm.locals.parse_declaration();
}
bool find_local(const std::string& name, VarName& vname) {
return vm.locals.find_local(name, vname);
}
bool find_local(const char* name, uint size, VarName& vname) {
return find_local(std::string(name, name + size), vname);
}