-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.cpp
More file actions
464 lines (420 loc) · 8.96 KB
/
basic.cpp
File metadata and controls
464 lines (420 loc) · 8.96 KB
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
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
vector<int> basic_line;
vector<int> asm_line;
int preced(char ch)
{
if (ch == '+' || ch == '-')
{
return 1; //Precedence of + or - is 1
}
else if (ch == '*' || ch == '/')
{
return 2; //Precedence of * or / is 2
}
else
{
return 0;
}
}
string inToPost(string infix)
{
stack<char> stk;
stk.push('#'); //add some extra character to avoid underflow
string postfix = ""; //initially the postfix string is empty
string::iterator it;
for (it = infix.begin(); it != infix.end(); it++)
{
if (isalnum(char(*it)))
postfix += *it; //add to postfix when character is letter or number
else if (*it == '(')
stk.push('(');
else if (*it == '^')
stk.push('^');
else if (*it == ')')
{
while (stk.top() != '#' && stk.top() != '(')
{
postfix += stk.top(); //store and pop until ( has found
stk.pop();
}
stk.pop(); //remove the '(' from stack
}
else
{
if (preced(*it) > preced(stk.top()))
stk.push(*it); //push if precedence is high
else
{
while (stk.top() != '#' && preced(*it) <= preced(stk.top()))
{
postfix += stk.top(); //store and pop until higher precedence is found
stk.pop();
}
stk.push(*it);
}
}
}
while (stk.top() != '#')
{
postfix += stk.top(); //store and pop until stack is not empty.
stk.pop();
}
return postfix;
}
/* trees */
// Data structure to store a binary tree node
struct Node
{
char data;
int loc;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
};
Node(int data, Node *left, Node *right)
{
this->data = data;
this->left = left;
this->right = right;
};
};
// Function to check if a given token is an operator
bool isOperator(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/');
}
// Print the postfix expression for an expression tree
void postorder(Node *root)
{
if (root == nullptr)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data;
}
int avilable_mem(int &a)
{
int t = a;
for (int i = 0; i < 32; i++)
{
if (!((t >> i) & 1))
{
a |= (1 << i);
return i; //72 and below for temp constants
}
}
return 72;
}
// CONST sets 99 to operand
void expr_traversal(Node *root, int &line, int &mem, string &as)
{
if (root == nullptr)
{
return;
}
expr_traversal(root->left, line, mem, as);
expr_traversal(root->right, line, mem, as);
if (isdigit(root->data))
{
root->loc = 99;
return;
}
else if (isalpha(root->data))
{
root->loc = 163 - (int)root->data;
return;
}
else if (isOperator(root->data))
{
if (isdigit(root->left->data))
{
as.append(to_string(line));
as.append(" CONST ");
as.append(to_string(root->left->data - 48));
as.append("\n");
line++;
}
as.append(to_string(line));
as.append(" LOAD ");
as.append(to_string(root->left->loc));
as.append("\n");
line++;
if (isdigit(root->right->data))
{
as.append(to_string(line));
as.append(" CONST ");
as.append(to_string(root->right->data - 48));
as.append("\n");
line++;
}
as.append(to_string(line));
if (root->data == '+')
{
as.append(" ADD ");
}
else if (root->data == '-')
{
as.append(" SUB ");
}
else if (root->data == '*')
{
as.append(" MUL ");
}
else if (root->data == '/')
{
as.append(" DIVIDE ");
}
as.append(to_string(root->right->loc));
as.append("\n");
line++;
root->loc = 72 - avilable_mem(mem);
as.append(to_string(line));
as.append(" STORE ");
as.append(to_string(root->loc));
as.append("\n");
line++;
}
}
// Function to construct an expression tree from the given postfix expression
Node *construct(string postfix)
{
// create an empty stack to store tree pointers
stack<Node *> s;
// traverse the postfix expression
for (char c : postfix)
{
// if the current token is an operator
if (isOperator(c))
{
// pop two nodes `x` and `y` from the stack
Node *x = s.top();
s.pop();
Node *y = s.top();
s.pop();
// construct a new binary tree whose root is the operator and whose
// left and right children point to `y` and `x`, respectively
Node *node = new Node(c, y, x);
// push the current node into the stack
s.push(node);
}
// if the current token is an operand, create a new binary tree node
// whose root is the operand and push it into the stack
else
{
s.push(new Node(c));
}
}
// a pointer to the root of the expression tree remains on the stack
return s.top();
}
//result of expression in accumulator
string parse_expr(string e, int ¤t_line)
{
string postfix = inToPost(e);
Node *root = construct(postfix);
int a = 0;
string ass;
expr_traversal(root, current_line, a, ass);
if (root->loc == 99)
{
ass.append(to_string(current_line++) + " CONST " + to_string(root->data - 48) + "\n");
}
ass.append(to_string(current_line) + " LOAD " + to_string(root->loc) + "\n");
int count = 1;
for (int i = 0; i < ass.length(); i++)
{
if (ass[i] == '\n')
count++;
}
asm_line.push_back(count);
return ass;
}
void find_and_replace(string &file_contents,
const string &from, const string &to)
{
size_t pos = file_contents.find(from);
while (pos != string::npos)
{
file_contents.replace(pos, from.length(), to);
pos = file_contents.find(from, pos);
}
}
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "Usage: %s input.bas output.sa\n", argv[0]);
exit(EXIT_FAILURE);
}
ifstream f(argv[1]);
stringstream of;
string token, line;
vector<string> lines;
while (getline(f, line, '\n'))
{
lines.push_back(line);
}
vector<string> syn;
int current_line = 0;
string asm_com;
int asm_op;
int asm_num;
for (int i = 0; i < lines.size(); i++)
{
//cout << lines[i] << "\n";
syn.clear();
stringstream ss(lines[i]);
while (getline(ss, token, ' '))
{
syn.push_back(token);
}
basic_line.push_back(stoi(syn[0]));
if (!syn[1].compare("REM"))
{
asm_line.push_back(1);
continue;
}
else if (!syn[1].compare("INPUT"))
{
asm_com = "READ";
asm_op = 163 - syn[2][0];
asm_line.push_back(1);
}
else if (!syn[1].compare("OUTPUT"))
{
asm_com = "WRITE";
asm_op = 163 - syn[2][0];
asm_line.push_back(1);
}
else if (!syn[1].compare("GOTO"))
{
asm_com = "JMPX";
asm_op = stoi(syn[2]);
asm_line.push_back(1);
}
else if (!syn[1].compare("IF")) //jump if condition is false - skip following THEN statement
{
if (!syn[3].compare("="))
{
of << parse_expr(syn[2] + '-' + syn[4], current_line);
current_line++;
asm_com = "JNZ";
}
else if (!syn[3].compare("<"))
{
of << parse_expr(syn[4] + '-' + syn[2], current_line);
current_line++;
asm_com = "JNEG";
}
else if (!syn[3].compare(">"))
{
of << parse_expr(syn[2] + '-' + syn[4], current_line);
current_line++;
asm_com = "JNEG";
}
else
{
return 1;
}
asm_num = current_line++;
asm_op = current_line + 1; //unless LET
string then_statement(to_string(current_line));
//repeat of other statements
if (!syn[5].compare("REM"))
{
continue;
}
else if (!syn[5].compare("INPUT"))
{
then_statement.append(" READ " + to_string(163 - syn[6][0]));
}
else if (!syn[5].compare("OUTPUT"))
{
then_statement.append(" WRITE " + to_string(163 - syn[6][0]));
}
else if (!syn[5].compare("GOTO"))
{
then_statement.append(" JMPX " + to_string(stoi(syn[6])));
}
else if (!syn[5].compare("LET"))
{
current_line++;
then_statement = parse_expr(syn[8], current_line);
current_line++;
then_statement.append(to_string(current_line) + " STORE " + to_string((163 - syn[6][0])));
asm_num = current_line + asm_line.back() + 2;
continue;
}
else if (!syn[5].compare("END"))
{
then_statement.append(" HALT");
}
else
{
return 1;
}
of << asm_num << " " << asm_com << " " << asm_op << "\n";
of << then_statement << "\n";
current_line++;
asm_line.back() += 1;
continue;
//repeat end
}
else if (!syn[1].compare("LET"))
{
of << parse_expr(syn[4], current_line);
current_line++;
asm_com = "STORE";
asm_op = (163 - syn[2][0]);
}
else if (!syn[1].compare("END"))
{
asm_line.push_back(1);
of << current_line << " HALT\n";
break;
}
else
{
return 1;
}
asm_num = current_line;
of << asm_num << " " << asm_com << " " << asm_op << "\n";
current_line++;
}
//goto fixup
for (int i = 1; i < basic_line.size(); i++)
{
asm_line[i] += asm_line[i - 1];
}
asm_line.insert(asm_line.begin(), 0);
for (int i = 0; i < basic_line.size(); i++)
{
cout << basic_line[i] << " " << asm_line[i] << "\n";
}
f.close();
of.clear();
of.seekg(0, of.beg);
ofstream ans(argv[2]);
lines.clear();
while (getline(of, line, '\n'))
{
for (int i = 0; i < basic_line.size(); i++)
{
find_and_replace(line, "JMPX " + to_string(basic_line[i]), "JUMP " + to_string(asm_line[i]));
}
ans << line << "\n";
}
ans.close();
return 0;
}