-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlib.cpp
255 lines (202 loc) · 5.68 KB
/
mlib.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
#include "mlib.h"
#include <map>
#include <cmath>
using namespace std;
int addr;
vector<int> stack;
map<string, Value> env;
map<string, Proc0> p0map = {
{"system", fsystem}
};
map<string, Proc1> p1map = {
{"not", pnot}, {"+", ppos}, {"-", pneg},
{"atn", fatn}, {"cos", fcos}, {"sin", fsin}, {"tan", ftan}, {"sqr", fsqr},
{"exp", fexp}, {"log", flog}, {"abs", fabs}, {"fix", ffix}, {"int", fint}
};
map<string, Proc2> f2map = {
{"and", pand}, {"or", por}, {"xor", pxor}, {"imp", pimp}, {"eqv", peqv},
{"=", peq}, {"<", plt}, {">", pgt}, {"<=", ple}, {">=", pge}, {"<>", pne},
{"mod", pmod}, {"+", padd}, {"-", psub}, {"*", pmul}, {"/", pdiv},
{"\\", pidiv}, {"^", ppow}
};
// Value fgoto(Value x) {
// addr = x.i(); return false;
// }
// Value fgosub(Value x) {
// rets.push_back(at);
// at = x.i(); return false;
// }
// Value freturn() {
// at = rets.back();
// rets.pop_back();
// return false;
// }
Value pand(Value x, Value y) { switch (max(x.t, y.t)) {
case BOL: return x.b() && y.b();
case INT: return x.i() & y.i();
}}
Value por(Value x, Value y) { switch(max(x.t, y.t)) {
case BOL: return x.b() || y.b();
case INT: return x.i() | y.i();
}}
Value pxor(Value x, Value y) { switch (max(x.t, y.t)) {
case BOL: return (bool) (x.b() ^ y.b());
case INT: return x.i() ^ y.i();
}}
Value pimp(Value x, Value y) { switch (max(x.t, y.t)) {
case BOL: return !x.b() && y.b();
case INT: return ~x.i() & y.i();
}}
Value peqv(Value x, Value y) { switch (max(x.t, y.t)) {
case BOL: return x.b() == y.b();
case INT: return x.i() ^ ~y.i();
}}
Value pnot(Value x) { switch (x.t) {
case BOL: return !x.b();
case INT: return ~x.i();
}}
Value peq(Value x, Value y) { switch (max(x.t, y.t)) {
case BOL: return x.b() == y.b();
case INT: return x.i() == y.i();
case DEC: return x.d() == y.d();
case STR: return x.s() == y.s();
}}
Value plt(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() < y.i();
case DEC: return x.d() < y.d();
}}
Value pgt(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() > y.i();
case DEC: return x.d() > y.d();
}}
Value ple(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() <= y.i();
case DEC: return x.d() <= y.d();
}}
Value pge(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() >= y.i();
case DEC: return x.d() >= y.d();
}}
Value pne(Value x, Value y) { switch (max(x.t, y.t)) {
case BOL: return x.b() != y.b();
case INT: return x.i() != y.i();
case DEC: return x.d() != y.d();
case STR: return x.s() != y.s();
}}
Value pmod(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() % y.i();
case DEC: return fmod(x.d(), y.d());
}}
Value ppos(Value x) { switch(x.t) {
case INT: return x.i();
case DEC: return x.d();
}}
Value pneg(Value x) { switch(x.t) {
case INT: return -x.i();
case DEC: return -x.d();
}}
// not sure about strings: memory leak?
Value padd(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() + y.i();
case DEC: return x.d() + y.d();
case STR: return new string(x.s() + y.s());
}}
Value psub(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() - y.i();
case DEC: return x.d() - y.d();
}}
Value pmul(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() * y.i();
case DEC: return x.d() * y.d();
}}
Value pdiv(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: case DEC: return x.d() / y.d();
}}
Value pidiv(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return x.i() / y.i();
case DEC: return x.d() / y.d();
}}
Value ppow(Value x, Value y) { switch (max(x.t, y.t)) {
case INT: return (long) pow(x.i(), y.i());
case DEC: return pow(x.d(), y.d());
}}
// functions:
Value fatn(Value x) { switch (x.t) {
case INT: case DEC: return atan(x.d());
}}
Value fcos(Value x) { switch (x.t) {
case INT: case DEC: return cos(x.d());
}}
Value fsin(Value x) { switch (x.t) {
case INT: case DEC: return sin(x.d());
}}
Value ftan(Value x) { switch (x.t) {
case INT: case DEC: return tan(x.d());
}}
Value fsqr(Value x) { switch(x.t) {
case INT: case DEC: return sqrt(x.d());
}}
Value fexp(Value x) { switch (x.t) {
case INT: case DEC: return exp(x.d());
}}
Value flog(Value x) { switch (x.t) {
case INT: case DEC: return log(x.d());
}}
Value fabs(Value x) { switch (x.t) {
case INT: return abs(x.i());
case DEC: return abs(x.d());
}}
Value ffix(Value x) { switch (x.t) {
case INT: case DEC: return (long) x.d();
}}
Value fint(Value x) { switch (x.t) {
case INT: case DEC: return (long) floor(x.d());
}}
// hex$, oct$
Value fhex(Value x) { switch (x.t) {
case INT: case DEC: return new string(to_string((long) x.d()));
}}
// cint, clng
Value fcint(Value x) { switch (x.t) {
case BOL: case INT: case DEC: return (long) x.d();
}}
// cdbl, csng
Value fcdec(Value x) { switch (x.t) {
case BOL: case INT: case DEC: return x.d();
}}
// ucase$
Value fucase(Value x) { switch (x.t) {
case STR: return new string(x.s()); // to uppercase
}}
// lcase$
Value flcase(Value x) { switch (x.t) {
case STR: return new string(x.s()); // to lowercase
}}
// string$
Value fstring(Value l, Value s) { switch (s.t) {
case INT: case DEC: return fstring(l, new string(s.s()));
case STR: string *a = new string(); char c = s.s()[0];
for (int i=l.i(); i>=0; i--) *a+=c;
return a;
}}
// str$
Value fstr(Value x) {
return new string(x.s());
}
Value fval(Value x) { switch (x.t) {
case STR: return strtod(x.s().c_str(), NULL);
}}
Value fasc(Value x) { switch (x.t) {
case STR: return (long) x.s()[0];
}}
// chr$
Value fchr(Value x) { switch (x.t) {
case INT: case DEC:
string *s = new string();
s[0] = (char) x.d();
return s;
}}
Value fsystem() {
exit(0);
return false;
}