-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.js
179 lines (170 loc) · 3.55 KB
/
run.js
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
// var preprocess = require("./preprocesser");
var tokenize = require("./src/tokenizer");
var parse = require("./src/parser");
var Runtime = require("./src/runtime");
var test1 = `
float foo(int x) {
int y = 7;
return x;
}
int main() {
if(1 + 6 + 9) {
int x = 6;
print(foo(y));
print(y);
for(x = 0; x < 6; x = x + 1) {
if(x / 2 == 0) {
print(x);
}
}
print(1 + 2 + 5 / 7 + 9);
}
}`;
var test2 = `
int x = 5, **a = 7, b[8];
int y = 6;
char c[8] = 6;
char *d;
char e;
int f;
int foo(int x) {
if(x) {
int x = 7;
int y = 8;
d = 7;
}
}
int main() {
int x;
int z;
x = 6;
y = 8;
z = 7;
foo(x);
}`;
var test3 = `
struct node {
int y;
int z;
}
int x;
char y;
int main() {
int x;
int y;
struct node test;
x = y;
test.y;
test.z;
int z;
}`;
var test4 = `
struct node {
struct node *next;
}
int x = 7;
int y = 4 + 9;
int main() {
struct node a, b, c, *current;
a.next = &b;
b.next = &c;
c.next = 0;
current = &a;
while (current != 0) {
current = (*current).next;
}
}`;
/*
Assign:
Loc(12 + esp)
to be:
Loc(Deref(Location(x + esp)) + 0)
*/
function run(ptu) {
// try {
// var tu = preprocess(ptu);
// console.log("Preprocessed:");
// console.log(tu);
var tu = ptu;
var tok = tokenize(tu);
// console.log("Tokenized:");
// console.log(tok);
var res = parse(tok);
console.log(res);
// console.log(res.functions["main"].body[9].ast.e2.offset.e1);
// console.log(res.globalObjects);
var rt = new Runtime(res);
for(var i = 0; i < 19; i++) {
rt.step(1);
//
// console.log(rt.guessDataStructure().nodes);
// console.log(rt.guessDataStructure().edges);
// }
console.log("\n======================================\n");
console.log(rt.getCurrentLine());
//console.log("Globals:");
//console.log(rt.dumpGlobals());
console.log("Stack:");
var stack = rt.dumpStack();
for(var j = 0; j < stack.length; j++) {
console.log(stack[j]);
}
}
// var functions = res.functions;
// var globals = res.globals;
// console.log("Functions:");
// console.log(functions);
// console.log("Globals:");
// console.log(globals);
// console.log("Main:");
// console.log(res.functions["main"].frame);
// var body = res.functions["main"].body;
// for(var i = 0; i < body.length; i++) {
// console.log("I: " + i)
// console.log(body[i]);
// }
// console.log(res.globalData);
// console.log("Foo:");
// console.log(functions["foo"].frame);
// var body = functions["foo"].body;
// for(var i = 0; i < body.length; i++) {
// console.log(body[i].ast);
// }
// for(var i = 0; i < globals.vars.length; i++) {
// console.log(globals.vars[i]);
// }
//
// catch (err) {
// console.log("Error:");
// console.log(err.message);
// console.log(err.stack);
// process.exit();
// }
// for(var i = 0; i < functions.length; i++) {
// for(var j = 0; j < functions[i].value.length; j++) {
// var line = functions[i].value[j];
// if(line instanceof Redirect) {
//
// }
// else {
// console.log(`${j}: ${ptu.slice(line.start, line.end)}`);
// }
// // console.log(` (Parsed from \"${code.slice(line.start, line.end)}\" [${line.start}:${line.end}])`);
// }
// }
}
// run(`
// int main() {
// int a, b, *c, *d;
// a;
// a = 6;
// b = a;
// c = &a;
// }
// `)
run( `int main() {
int x, y;
}
int main() {
int x, y;
}`);