-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavm_library_funcs.hpp
392 lines (353 loc) · 10.4 KB
/
avm_library_funcs.hpp
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "avm_utility.hpp"
#include <math.h>
#ifndef avm_library_funcs
#define avm_library_funcs
library_func_t libraryFuncz[12];
/*Library Functions - Start*/
void libfunc_print() {
// cout<<endl<<"Output:"<<endl;
unsigned n = avm_totalActuals();
for (unsigned i = 0; i < n; ++i) {
if(avm_getActual(i)->type == userfunc_m) cout<<"user function: ";
if(avm_getActual(i)->type == libfunc_m) cout<<"library function: ";
cout << avm_toString(avm_getActual(i));
}
// cout<<endl<<"output_end"<<endl;
retval->type = nil_m;
}
void libfunc_typeof(void) {
unsigned n = avm_totalActuals();
// cout<<"this!"<<endl;
// printStack();
if (n != 1) {
avm_error("one argument expected in 'typeof' (not %d) !", n);
} else {
avm_memcellClear(retval);
retval->type = string_m;
retval->data.strVal = strdup(typeStrings[avm_getActual(0)->type]);
// cout<<"this!"<<endl;
// printStack();
}
}
void libfunc_sin(void) {
unsigned n = avm_totalActuals();
if (n != 1) {
executionFinished = true;
avm_error("one argument expected in 'sin' (not %d) !", n);
} else {
if (avm_getActual(0)->type != number_m) {
avm_error("Sin expects number!", n);
} else {
avm_memcellClear(retval);
retval->type = number_m;
retval->data.numVal = sin(avm_getActual(0)->data.numVal * 3.14159265 / 180);
}
}
}
void libfunc_cos(void) {
unsigned n = avm_totalActuals();
if (n != 1) {
avm_error("one argument expected in 'cos' (not %d) !", n);
} else {
if (avm_getActual(0)->type != number_m) {
avm_error("Cos expects number!", n);
} else {
avm_memcellClear(retval);
retval->type = number_m;
retval->data.numVal = cos(avm_getActual(0)->data.numVal * 3.14159265 / 180);
}
}
}
void libfunc_sqrt(void) {
unsigned n = avm_totalActuals();
if (n != 1) {
avm_error("one argument expected in 'sqrt' (not %d) !", n);
} else {
if (avm_getActual(0)->type != number_m) {
avm_error("Sqrt expects number!", n);
} else {
avm_memcellClear(retval);
retval->type = number_m;
double tmp = sqrt(avm_getActual(0)->data.numVal);
if (tmp != tmp)
retval->type = nil_m;
else
retval->data.numVal = tmp;
}
}
}
bool is_number(const string& s) {
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
string toLower(string s) {
const char* ss = strdup(s.c_str());
string news = "";
for (int i = 0; i < s.length(); i++) {
news += tolower(ss[i]);
}
return news;
}
void libfunc_input(void) {
string s;
getline(cin,s);
string sl = toLower(s);
char autakia = '\"';
avm_memcellClear(retval);
if (is_number(s)) {
retval->type = number_m;
retval->data.numVal = atof(s.c_str());
} else if (sl.compare("true") == 0) {
retval->type = bool_m;
retval->data.boolVal = true;
} else if (sl.compare("false") == 0) {
retval->type = bool_m;
retval->data.boolVal = false;
} else if (sl.compare("nil") == 0) {
retval->type = nil_m;
} else {
retval->type = string_m;
if (sl[0] == autakia && sl[s.length() - 1] == autakia) {
retval->data.strVal = strdup(s.substr(1, s.length() - 2).c_str());
} else {
retval->data.strVal = strdup(s.c_str());
}
}
}
void libfunc_argument(void) {
unsigned p_topsp = avm_get_envvalue(topsp + AVM_SAVEDTOPSP_OFFSET);
// cout<<endl<<"getting sum from "<<p_topsp<<endl;
unsigned n = avm_totalActuals();
avm_memcell* actual = avm_getActual(0);
if (n != 1) {
executionFinished = true;
avm_error("Invalid argument");
} else if (actual->type != number_m) {
executionFinished = true;
avm_error("Argument is not number_m");
} else if (actual->data.numVal > avm_get_envvalue(p_topsp + AVM_NUMACTUALS_OFFSET) ||
actual->data.numVal < 0) {
retval->type = nil_m;
avm_error("error in arguement3");
} else if (!p_topsp) {
retval->type = nil_m;
// avm_memcellClear(retval);
avm_error("error in arguement2");
} else {
// avm_memcellClear(retval);
int offset = (int)avm_getActual(0)->data.numVal;
if( (&stack_m[p_topsp + 2*AVM_NUMACTUALS_OFFSET + offset + 1])->type > undef_m) {
retval->type = nil_m;
}else {
retval = &stack_m[p_topsp + AVM_NUMACTUALS_OFFSET + offset + 1];
}
}
}
bool digitsOnly(char* str ){
for(int i=0; i<strlen(str); i++ ){
if( !isdigit(str[i]) && str[i] != '.' ) return false;
}
return true;
}
void libfunc_strtonum(void) {
unsigned n = avm_totalActuals();
if (n != 1) {
avm_error("Strtonum expects one parameter");
} else if (avm_getActual(0)->type != string_m) {
avm_error("String expected");
} else {
avm_memcellClear(retval);
if( !digitsOnly(avm_getActual(0)->data.strVal) ){
retval->type = nil_m;
}
else {
retval->type = number_m;
retval->data.numVal = atof(avm_getActual(0)->data.strVal);
}
}
}
avm_table* copy_table(avm_table* tocopy) {
avm_table* new_one = avm_tablenew();
avm_table_bucket** p = tocopy->strIndexed;
for (unsigned i = 0; i < AVM_TABLE_HASHSIZE; ++i) {
for (avm_table_bucket* b = p[i]; b;) {
avm_table_bucket* node = b;
b = b->next;
avm_tablesetelem(new_one,&node->key,&node->value);
}
}
p = tocopy->numIndexed;
for (unsigned i = 0; i < AVM_TABLE_HASHSIZE; ++i) {
for (avm_table_bucket* b = p[i]; b;) {
avm_table_bucket* node = b;
b = b->next;
avm_tablesetelem(new_one,&node->key,&node->value);
}
}
return new_one;
}
void libfunc_objectcopy(void) {
unsigned n = avm_totalActuals();
if (n != 1) {
avm_error("ObjectCopy expects one parameter");
} else if (avm_getActual(0)->type != table_m) {
avm_error("Table expected");
} else {
avm_memcellClear(retval);
retval->type = table_m;
retval->data.tableVal = copy_table(avm_getActual(0)->data.tableVal);
}
}
avm_table* getindexes(avm_table* derived) {
avm_table* new_one = avm_tablenew();
avm_table_bucket** p = derived->strIndexed;
avm_memcell new_index;
new_index.type = number_m;
new_index.data.numVal = 0;
for (unsigned i = 0; i < AVM_TABLE_HASHSIZE; ++i) {
for (avm_table_bucket* b = p[i]; b;) {
avm_table_bucket* node = b;
b = b->next;
avm_tablesetelem(new_one, &new_index, &node->key);
new_index.data.numVal ++;
}
}
p = derived->numIndexed;
for (unsigned i = 0; i < AVM_TABLE_HASHSIZE; ++i) {
for (avm_table_bucket* b = p[i]; b;) {
avm_table_bucket* node = b;
b = b->next;
avm_tablesetelem(new_one, &new_index, &node->key);
new_index.data.numVal ++;
}
}
return new_one;
}
void libfunc_objectmemberkeys(void) {
unsigned n = avm_totalActuals();
if (n != 1) {
avm_error("ObjectMemberKeys expects one parameter");
} else if (avm_getActual(0)->type != table_m) {
avm_error("Table expected");
} else {
avm_memcellClear(retval);
retval->type = table_m;
retval->data.tableVal = getindexes(avm_getActual(0)->data.tableVal);
}
}
void libfunc_objecttotalmembers(void) {
unsigned n = avm_totalActuals();
if (n != 1) {
avm_error("ObjectTotalMembers expects one parameter");
} else if (avm_getActual(0)->type != table_m) {
avm_error("Table expected");
} else {
avm_memcellClear(retval);
retval->type = number_m;
retval->data.numVal = avm_getActual(0)->data.tableVal->total;
}
}
void libfunc_totalarguments() {
unsigned p_topsp = avm_get_envvalue(topsp + AVM_SAVEDTOPSP_OFFSET);
avm_memcellClear(retval);
if ( p_topsp == 0) {
// avm_error("'totalargument' called outside a function!");
retval->type = nil_m;
} else {
retval->type = number_m;
retval->data.numVal = avm_get_envvalue(p_topsp + AVM_NUMACTUALS_OFFSET);
if( retval->data.numVal == (unsigned)-1) {
retval->type = nil_m;
}
}
}
unsigned libfuncs_newUsed(char* s) {
/*Check if we use its already*/
for (int i = 0; i < namedLibFuncsRead.size(); i++) {
if (!strcmp(namedLibFuncsRead[i], s)) {
/*Found it -> return the index*/
return i;
}
}
/*New lib func is used*/
char* dup = strdup(s);
namedLibFuncsRead.push_back(dup);
return namedLibFuncsRead.size() - 1;
}
void registerLibFunc(char* id, library_func_t addr) {
int lib_index =
libfuncs_newUsed(id); // retrieve index from the lib funcs array
libraryFuncz[lib_index] = addr;
}
string toString(vmopcode op) {
switch (op) {
case assign_v:
return "assign";
case add_v:
return "add";
case sub_v:
return "sub";
case mul_v:
return "mul";
case div_v:
return "div";
case mod_v:
return "mod";
case uminus_v:
return "uminus";
case and_v:
return "and";
case or_v:
return "or";
case not_v:
return "not";
case jeq_v:
return "jeq";
case jne_v:
return "jne";
case jle_v:
return "jle";
case jge_v:
return "jge";
case jlt_v:
return "jlt";
case jgt_v:
return "jgt";
case call_v:
return "call";
case pusharg_v:
return "pusharg";
case funcenter_v:
return "funcenter";
case funcexit_v:
return "funcexit";
case newtable_v:
return "newtable";
case tablegetelem_v:
return "tablegetelem";
case tablesetelem_v:
return "tablesetelem";
case jump_v:
return "jump";
case nop_v:
return "nop";
default:
assert(false);
}
}
void init_libfuncs(){
registerLibFunc("objecttotalmembers", libfunc_objecttotalmembers);
registerLibFunc("objectmemberkeys", libfunc_objectmemberkeys);
registerLibFunc("totalarguments", libfunc_totalarguments);
registerLibFunc("objectcopy", libfunc_objectcopy);
registerLibFunc("strtonum", libfunc_strtonum);
registerLibFunc("argument", libfunc_argument);
registerLibFunc("typeof", libfunc_typeof);
registerLibFunc("input", libfunc_input);
registerLibFunc("print", libfunc_print);
registerLibFunc("sqrt", libfunc_sqrt);
registerLibFunc("cos", libfunc_cos);
registerLibFunc("sin", libfunc_sin);
}
#endif