-
Notifications
You must be signed in to change notification settings - Fork 2
/
AsmVM.cpp
469 lines (423 loc) · 11.8 KB
/
AsmVM.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// AsmVM.cpp : Defines the entry point for the console application.
//
//add multiple returns perhaps?
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
#include <stdio.h>
typedef wchar_t _TCHAR;
#include <stack>
#include <vector>
#define CODE(code) #code
#include <jetscript/JetContext.h>
#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <math.h>
#ifdef _WIN32
#include <io.h>
#include <Windows.h>
#endif
#include <functional>
using namespace Jet;
int main(int argc, char* argv[])
{
#ifdef _WIN32
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
COORD x;
x.X = 80;
x.Y = 1000;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), x);
#endif
printf("Jet Language Console:\n");
//ok, make an execution context
JetContext context;
//bind basic math functions
/*JetBind(context, tan);
JetBind(context, sin);
JetBind(context, cos);
JetBind(context, log);
JetBind2(context, pow, double);
JetBind(context, sqrt);
JetBind(context, exp);
JetBind(context, atan);
JetBind2(context, atan2, double);
JetBind(context, acos);
JetBind(context, asin);*/
Value args[3];
args[0] = Value(3);
args[1] = Value(4);
args[2] = Value(5);
printf("Sizeof Value: %d, Sizeof Instruction: %d\n\n", (int)sizeof(Value), (int)sizeof(Instruction));
if (argc > 1)
{
if (argv[1])
{
try
{
std::ifstream t(argv[1], std::ios::in | std::ios::binary);
if (t)
{
int length;
t.seekg(0, std::ios::end); // go to the end
length = t.tellg(); // report location (this is the length)
t.seekg(0, std::ios::beg); // go back to the beginning
char* buffer = new char[length]; // allocate memory for a buffer of appropriate dimension
t.read(buffer, length); // read the whole file into the buffer
buffer[length] = 0;
t.close();
context.Script(buffer, argv[1]);
delete[] buffer;
}
else
{
printf("Could not find file!");
}
}
catch(CompilerException E)
{
printf("Exception found:\n");
printf("%s (%d): %s\n", E.file.c_str(), E.line, E.ShowReason());
}
}
}
/*{
std::ifstream t("coroutines.txt", std::ios::in | std::ios::binary);
int length;
t.seekg(0, std::ios::end); // go to the end
length = t.tellg(); // report location (this is the length)
t.seekg(0, std::ios::beg); // go back to the beginning
char* buffer = new char[length]; // allocate memory for a buffer of appropriate dimension
t.read(buffer, length); // read the whole file into the buffer
buffer[length] = 0;
t.close();
for (int i = 0; i < 10000; i++)
{
context.Script(buffer, "coroutines.txt");
}
delete[] buffer;
}*/
//_crtBreakAlloc = 22970;
while (true)
{
printf("\n>");
char command[800];
char arg[150]; char command2[150];
memset(arg, 0, 150);
memset(command2, 0, 150);
std::cin.getline(command, 800);
sscanf(command, "%s %s\n", command2, arg);
if (strcmp(command2, "run") == 0)
{
char* buffer = 0;
try
{
std::ifstream t(arg, std::ios::in | std::ios::binary);
if (t)
{
t.seekg(0, std::ios::end); // go to the end
std::streamoff length = t.tellg(); // report location (this is the length)
t.seekg(0, std::ios::beg); // go back to the beginning
buffer = new char[length+1]; // allocate memory for a buffer of appropriate dimension
t.read(buffer, length); // read the whole file into the buffer
buffer[length] = 0;
t.close();
context.Script(buffer, arg);
delete[] buffer;
}
else
{
printf("Could not find file!");
}
}
catch(CompilerException E)
{
delete[] buffer;
printf("Exception found:\n");
printf("%s (%d): %s\n", E.file.c_str(), E.line, E.ShowReason());
}
catch(RuntimeException E)
{
printf("Exception found:\n");
printf("%s\n", E.reason.c_str());
}
}
else if (strcmp(command2, "test") == 0 && arg[0] == 0)
{
//add a bunch of garbage collector test cases
try
{
JetContext tcontext;//create new context for this
printf("Running tests....\n");
//make me into a test for unary operators
tcontext["effect_move"] = [](JetContext* context, Value* arguments, int nargs)
{
Jet::Value a = arguments[0]; // Number , -100 <- Lost string value
Jet::Value b = arguments[1]; // Number , -100
Jet::Value c = arguments[2]; // Number , 120
Jet::Value d = arguments[3]; // Number , 6
return Value();
};
tcontext.Script("effect_move(\"hi\", -100, 120, 6);");
try
{
Value ret = tcontext.Script(
"fail = 0;"
"if (++5 != 6) fail = 1;"
"if (5++ != 5) fail = 1;"
"x = -5;"
"y = -5;"
"5++;"
"++5;"
"print(++x);"
"return ++x;");
if ((int)ret != -3)
throw 7;
if ((int)tcontext["y"] != -5)
throw 7;
if ((int)tcontext["fail"] != 1)
throw 7;
}
catch (...)
{
throw CompilerException("", 0, "unary operator test failed\n");
}
try
{
tcontext.Script("fun main(dt, f2, a3) { print(dt); return dt; } ");
Value va = 52;
auto out = tcontext.Call("main", &va, 1);
if ((int)out != 52)
throw 7;
}
catch (...)
{
throw CompilerException("", 0, "context.Call test failed\n");
}
try
{
tcontext.Script("fun struct(aa,bb) {"
"local x = fun (a,b) {"
"local value = {};"
"value[aa] = a;"
"value[bb] = b;"
"return value;"
"};"
"return x;"
"}"
"Point = struct(\"x\", \"y\");"
"local p1 = Point(1, 2);"
"print(p1.x, p1.y);"
"local p2 = Point(10, 20);"
"print(p2.x, p2.y);"
);
}
catch(...)
{
throw RuntimeException("A General Test Failed!");
}
try
{
Value out = tcontext.Script("x = { _call = fun(x,y,z) { return z; }};"
"y = {}; setprototype(y,x); return y(1,2,3);");
if ((int)out != 3)
throw 7;
}
catch (...)
{
throw CompilerException("", 0, "call operator overload test failed\n");
}
//test reading and setting vars
tcontext["x"] = 52;
int vp = tcontext["x"];
if (vp != 52)
throw CompilerException("", 0, "getting/setting var test failed\n");
tcontext.Set("x", 56);
if ((int)tcontext.Get("x") != 56)
throw CompilerException("", 0, "explicit getting/setting var test failed\n");
//recursive calling checks
auto f = [](JetContext* context,Value* args, int numargs)
{
return context->Call("h");
};
tcontext["inception"] = Value(f);
tcontext.Script("fun h() { return 7; } inception(); print(\"hi im still alive\"); print(inception());");
//this should not crash or leave anything on the callstack
//context.Script("fun h() { return p[7]; } inception(); print(\"hi im still alive\");");
Value v = tcontext.Script("if (5 >= 5) return \"ok\"; else return \"not ok\";", "Test 1");
//if test
v = tcontext.Script("if (0) return 5; else if (2) return 6; else return 7;");
if ((double)v != 6.0f)
{
printf("If Statement Test Failed!\n");
}
//native function test
tcontext["ih"] = Value([](JetContext* context, Value* args, int numargs)
{
return args[0];
});
if ((v = tcontext.Script("return ih(\"test\");")).ToString() != "test")
{
printf("Native Function Test Failed!\n");
}
//test meta tables and userdata
try
{
ValueRef meta = tcontext.NewPrototype("Test");//ok, lets give me a type
meta["t1"] = [](JetContext* context, Value* v, int args)
{
printf("hi from metatable\n");
//Value() + Value();
//throw 7;
return Value();
};
meta["t2"] = [](JetContext* context, Value* v, int args)
{
return Value(7);
};
meta["t3"] = [](JetContext* context, Value* v, int args)
{
return Value();
};
tcontext["mttest"] = tcontext.NewUserdata(0, meta);
auto out = tcontext.Script("mttest.t1();\n"
"getprototype(mttest).t3 = fun() { print(\"hi from t3\n\");}; mttest.t3(); return mttest.t2();");
if ((double)out != 7.0)
throw 7;
ValueRef t2 = tcontext.NewPrototype("hi");
}
catch(...)
{
throw CompilerException("", 0, "Metatable test failed!\n");
}
//loop test
try
{
tcontext.Script("x = [1,2,3,4,5];"
"z = [];"
"for (local i in x)"
" z:add(i);");
if ((int)tcontext["z"][0] != 1)
throw 7;
if ((int)tcontext["z"][4] != 5)
throw 7;
tcontext.Script("x = {a=1,b=2,c=3,d=4,e=5};"
"z = [];"
"print(x);"
"for (local i in x) {"
" print(i);"
" z:add(i); }");
if ((int)tcontext["x"]["a"] != 1)
throw 7;
if ((int)tcontext["x"]["e"] != 5)
throw 7;
auto v = tcontext["z"][0];
if (v == Value())
throw 7;
tcontext.Script("for (local i in {}) print(i);");
tcontext.Script("for (local i in []) print(i);");
}
catch(RuntimeException e)
{
throw CompilerException("", 0, "ForEach Loop test failed!\n");
}
catch(...)
{
throw CompilerException("", 0, "ForEach Loop test failed!\n");
}
//closure test
try
{
Value result = tcontext.Script(
"fun startAt(x) {"
"return fun (y) { print(x,y); x += y; return x; };"
"}"
"local counter = startAt(1);"
"counter(10);"
"return counter(2);");
if ((int)result != 13)
throw 7;
}
catch(RuntimeException e)
{
throw CompilerException("", 0, "Closure test failed!\n");
}
catch (CompilerException e)
{
throw e;
}
catch(...)
{
throw CompilerException("", 0, "Closure test failed!\n");
}
//== operator test
try
{
tcontext.Script("fun h(v) { return v == null; } x = h(1==1);");
if ((double)tcontext["x"] != 0.0f)
{
throw 7;
}
}
catch(...)
{
throw CompilerException("", 0, "== operator precedence test failed\n");
}
tcontext.Script("apples = {};", "Test 2");
tcontext.Script("while(1) { print(\"this should print\"); break; print(\"this should not print\"); continue; } ", "Test 3");
tcontext.Script("test = [5,6,7,6,\"hello\"]; return 1;", "Test 4");
tcontext.Script("local apple = {}; gc(); global = apple; global[\"test\"] = 7; return 2;", "Test 5");
tcontext.Script("fun hi() { local z = 2; z++; x++; fun test(a,b,x) { return a+b+x;} test(); while (true) { h = 2; } ap = \"test\"; } return 7;", "Test 6");
tcontext.Script("for (i = 0; i < 10; i++) { for (j = 0; j < 2; j++) h = 7; } apple = fun(x,y) { return x+y;}; return apple(1,2);", "Test 7");
tcontext.Script("y = 0; if (x) y++; else y--;", "Test 8");
tcontext.Script("apples = {hi = 2, apple = 3, twenty = 4}; apples.two = 7; apples[\"hello\"] = \"testing\"; apples.a7 = 6; print(apples.a7); return 4;", "Test 9");
//context.RunGC();
//context["hi"]("hi", "apples", 1);
printf("Tests complete!\n");
}
catch(CompilerException E)
{
printf("Tests failed: Exception found:\n");
printf("%s (%d): %s\n", E.file.c_str(), E.line, E.ShowReason());
}
catch(RuntimeException E)
{
printf("Tests failed: Exception found:\n");
printf("%s\n",E.reason.c_str());
}
}
else if (strcmp(command2, "quit") == 0 && arg[0] == 0)
{
break;
}
else
{
try
{
//try and execute
Value ret = context.Script(command);
if (ret.type != ValueType::Null)
{
printf("%s\n", ret.ToString().c_str());
}
}
catch(CompilerException E)
{
printf("Exception found:\n");
printf("%s (%d): %s\n", E.file.c_str(), E.line, E.ShowReason());
}
catch(RuntimeException E)
{
printf("Exception found:\n");
printf("%s\n", E.reason.c_str());
}
}
}
return 0;
}