-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.cpp
262 lines (208 loc) · 6.96 KB
/
operators.cpp
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
#include <iostream>
#include <deque>
#include "extend.hpp"
#include "operators.hpp"
#include "operators/basic_math.hpp"
#include "operators/boolean_ops.hpp"
#include "operators/const.hpp"
#include "operators/ctl_flow.hpp"
#include "operators/data_types.hpp"
#include "operators/errors.hpp"
#include "operators/generic_operators.hpp"
#include "operators/io.hpp"
#include "operators/list_stuff.hpp"
#include "operators/literals.hpp"
#include "operators/ref_ops.hpp"
#include "operators/stack_ctl.hpp"
#include "operators/types.hpp"
#include "operators/threads.hpp"
#include "operators/objects.hpp"
#include "operators/libraries.hpp"
#include "operators/math.hpp"
namespace operators {
bool callToken(Frame& f, Frame::Exit& exit) {
for (const Token t : tokens)
if (t.condition(f)) {
exit = t.act(f);
return true;
}
return false;
}
bool callOperator(Frame& f, Frame::Exit& exit) {
auto d = operators.find(f.feed.tok);
if (d != operators.end()) {
if (d->second.native) {
exit = d->second.act(f);
return true;
} else if (d->second.run) {
f.stack.emplace_back(*d->second._val);
//f.feed.offset--;
return callByName(f, "@", exit);
} else {
f.stack.emplace_back(*d->second._val);
return true;
}
}
return false;
}
bool callOperator(Frame& f, Frame::Exit& exit, const Namespace& ns) {
auto d = ns.find(f.feed.tok);
if (d != ns.end()) {
if (d->second.native) {
exit = d->second.act(f);
return true;
} else if (d->second.run) {
f.stack.emplace_back(*d->second._val);
//f.feed.offset--;
return callByName(f, "@", exit);
} else {
f.stack.emplace_back(*d->second._val);
return true;
}
}
return false;
}
inline std::vector<struct Token> genTokens() {
// loads relevant tokens from operators/*
return std::vector<struct Token>({
// fast literals
OP_NS_TO_TOK(op_var_literal),
OP_NS_TO_TOK(op_ns_member_req),
// comments
OP_NS_TO_TOK(op_line_comment),
OP_NS_TO_TOK(op_multiline_comment),
// literals
OP_NS_TO_TOK(op_const_string),
OP_NS_TO_TOK(op_const_macro),
OP_NS_TO_TOK(op_const_list),
// more literals (slower execution time
OP_NS_TO_TOK(op_const_int),
OP_NS_TO_TOK(op_const_number),
OP_NS_TO_TOK(op_obj_mem_acc),
});
}
inline const Namespace genOperators() {
return Namespace({
// cmp
{ op_equals_to::name, op_equals_to::act }, // same value?
{ "=?", op_equals_to::act }, // ^
OP_NS_TO_PAIR(op_ne), // !=
OP_NS_TO_PAIR(op_gt), // >
OP_NS_TO_PAIR(op_lt),
// lam + macro stuff
OP_NS_TO_PAIR(op_exec), // @ runs macro/lambda
// references
OP_NS_TO_PAIR(op_var_op), // converts str to ref
OP_NS_TO_PAIR(op_equals), // change the depest value of reference
OP_NS_TO_PAIR(op_set), // give a variable a new value
OP_NS_TO_PAIR(op_copy_value), // get depest value and put on stack
OP_NS_TO_PAIR(op_vars), // debug variables
OP_NS_TO_PAIR(op_const), // converts ref to IMR
// stack
OP_NS_TO_PAIR(op_stack), // stack:clear, stack:pop, stack:dup, stack:reverse, stack:swap, stack:...
OP_NS_TO_PAIR(op_stk_pop), // pops top elem
// structured programming
OP_NS_TO_PAIR(op_repeat_loop), // run given number of times
OP_NS_TO_PAIR(op_cond), // unlabled elseelseifif statements
// const values
OP_NS_TO_PAIR(op_const_empty), // empty value
OP_NS_TO_PAIR(op_const_null), // null reference
OP_NS_TO_PAIR(op_const_true), // 1
OP_NS_TO_PAIR(op_const_false), // 0
// boolean
OP_NS_TO_PAIR(op_not), // !
OP_NS_TO_PAIR(op_truthy), // !! -- test for truthiness
OP_NS_TO_PAIR(op_and), // &&
OP_NS_TO_PAIR(op_or), // ||
// cio
OP_NS_TO_PAIR(op_println), // prints value + std::endl
OP_NS_TO_PAIR(op_print), // prints value
OP_NS_TO_PAIR(op_input), // gets value
// type conversion
OP_NS_TO_PAIR(op_str), // toString
OP_NS_TO_PAIR(op_depict), // raw-data depiction
OP_NS_TO_PAIR(op_typeof), // datatype
OP_NS_TO_PAIR(op_int),
OP_NS_TO_PAIR(op_size),
OP_NS_TO_PAIR(op_namespace), // namespace
OP_NS_TO_PAIR(op_ns_mem_req_op),// : operator
OP_NS_TO_PAIR(op_def), // define
OP_NS_TO_PAIR(op_index), // ]
OP_NS_TO_PAIR(op_while), // {body} {condition} while
OP_NS_TO_PAIR(op_add),
OP_NS_TO_PAIR(op_minus),
OP_NS_TO_PAIR(op_multiply),
OP_NS_TO_PAIR(op_divide),
OP_NS_TO_PAIR(op_int_divide),
OP_NS_TO_PAIR(op_pow),
OP_NS_TO_PAIR(op_remainder),
OP_NS_TO_PAIR(op_abs),
OP_NS_TO_PAIR(op_shift_left),
OP_NS_TO_PAIR(op_shift_right),
OP_NS_TO_PAIR(op_bw_and),
OP_NS_TO_PAIR(op_bw_xor),
OP_NS_TO_PAIR(op_bw_or),
OP_NS_TO_PAIR(op_object),
OP_NS_TO_PAIR(op_lambda),
OP_NS_TO_PAIR(op_return),
OP_NS_TO_PAIR(op_up),
OP_NS_TO_PAIR(op_escape),
OP_NS_TO_PAIR(op_keys),
OP_NS_TO_PAIR(op_quit),
OP_NS_TO_PAIR(op_obj_mem_acc_op),
OP_NS_TO_PAIR(op_load_lib),
OP_NS_TO_PAIR(op_math),
OP_NS_TO_PAIR(ns_object),
OP_NS_TO_PAIR(op_list_ns),
OP_NS_TO_PAIR(op_apply_namespace),
OP_NS_TO_PAIR(op_var_name),
OP_NS_TO_PAIR(op_defs),
OP_NS_TO_PAIR(op_strong),
OP_NS_TO_PAIR(op_ref),
OP_NS_TO_PAIR(op_trace_ref),
OP_NS_TO_PAIR(op_let),
OP_NS_TO_PAIR(op_throw),
OP_NS_TO_PAIR(op_catch),
// implementing these natively would involve >1000 lines of code
{ "<=", Def("> !", true) }, // le is opposite of gt
{ ">=", Def("< !", true) }, // ge is opposite of lt
{ "++", Def("Stack:dup 1 + =", true) },
{ "--", Def("Stack:dup 1 - =", true) },
{ "+=", Def("'v' let Stack:swap = Stack:dup $v + =", true) },
{ "-=", Def("'v' let Stack:swap = Stack:dup $v - =", true) },
{ "*=", Def("'v' let Stack:swap = Stack:dup $v * =", true) },
{ "/=", Def("'v' let Stack:swap = Stack:dup $v / =", true) },
{ "<<=", Def("'v' let Stack:swap = Stack:dup $v << =", true) },
{ ">>=", Def("'v' let Stack:swap = Stack:dup $v >> =", true) },
{ "%=", Def("'v' let Stack:swap = Stack:dup $v % =", true) },
{ "&=", Def("'v' let Stack:swap = Stack:dup $v & =", true) },
{ "|=", Def("'v' let Stack:swap = Stack:dup $v | =", true) },
{ "^=", Def("'v' let Stack:swap = Stack:dup $v ^ =", true) },
{ "**=", Def("'v' let Stack:swap = Stack:dup $v ** =", true) },
});
}
Namespace operators = genOperators();
std::vector<struct Token> tokens = genTokens();
bool callByName(Frame &f, const std::string &name, Frame::Exit &exit) {
auto d = operators.find(name);
if (d != operators.end()) { // found
if (d->second.native) {
exit = d->second.act(f);
return true;
} else if (d->second.run) {
f.stack.emplace_back(*d->second._val);
//f.feed.offset--;
return callByName(f, "@", exit);
} else {
f.stack.emplace_back(*d->second._val);
return true;
}
}
for (const Token& t : tokens)
if (t.name == name) {
exit = t.act(f);
return true;
}
return false;
}
}