-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitespace.c
655 lines (583 loc) · 23.1 KB
/
whitespace.c
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
TODO:
sort jump lookup table once after construnction
binary search jump lookup table - faster than linear search
fix better function calling stack table
if (stackSize <= stackPointer) {
stackSize = stackSize * 2;
stack = realloc(stack, stackSize);
}
could be: (then only one realloc would be nessesary for each increase of the heap)
if (stackSize <= stackPointer) {
stackSize = stackSize * 2;
}
stack = realloc(stack, stackSize);
*/
typedef struct jumps {
int label;
int to;
} jump_t;
// set to debug mode
int debug = 0;
void debugPrint(void *str) {
if (debug) {
printf("%s\n", str);
}
}
char getChar(char c) {
debugPrint(&c);
return c;
}
int freeAll(int *stack, int *callStack, int *heap, jump_t *jump, char *chars, int ret) {
free(stack);
free(callStack);
free(heap);
free(jump);
free(chars);
return ret;
}
int main(int argc, char const *argv[]) {
if (argc < 2) { // basic error handling
printf("No file to run\n");
return -1;
}
else if (argc > 2) {
printf("Too many input arguments\n");
return -1;
}
else {
int len = strlen(argv[1]);
if (!(argv[1][len-3] == '.' && argv[1][len-2] == 'w' && argv[1][len-1] == 's')) {
printf("Wrong filetype\n");
return -1;
}
}
FILE* fp;
fp = fopen(argv[1],"r");
if (fp == NULL) {
printf("File not found\n");
return -1;
}
int charPointer = 0;
char imp[2] = "00";
char command[2] = "00";
int parse = 1;
int stackPointer = 0;
int stackSize = 64;
int *stack = calloc(stackSize, sizeof(*stack));
int callSize = 0;
int callStackPointer = 0;
int *callStack = calloc(callSize, sizeof(*callStack));
int heapSize = 64;
int *heap = calloc(heapSize, sizeof(*heap));
int numberOfLabels = 0;
jump_t *jump = calloc(numberOfLabels, sizeof(*jump));
int maximumNumberOfChars = 1024;
int numberOfChars = 0;
char *chars = calloc(maximumNumberOfChars, sizeof(*chars)); // ish filestream
char buffer;
while (fscanf(fp, "%c", &buffer) != EOF) { // clean file from other than whitespace chars
/*
if (buffer == ' ') {
printf("Space\n");
}
else if (buffer == '\t') {
printf("Tab\n");
}
else if (buffer == '\n') {
printf("Linefeed\n");
}
else {
printf("%c\n", buffer);
}
*/
if (buffer == ' ' || buffer == '\t' || buffer == '\n') {
chars[numberOfChars] = buffer;
numberOfChars++;
if (numberOfChars == maximumNumberOfChars) {
maximumNumberOfChars = maximumNumberOfChars * 2;
chars = realloc(chars, maximumNumberOfChars*sizeof(*chars));
if (chars == NULL) {
printf("Whole file could not be cleaned\n");
free(fp);
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
}
}
fclose(fp);
while (charPointer <= numberOfChars && parse) {
/* get instrunction:
read type of instrunction i.e. imp
based of off imp, get instrunction and
maybe argument to instrunction
*/
// find IMP
imp[0] = chars[charPointer];
charPointer++;
if (imp[0] == ' ' && imp[1] == '0') { // Stack Manipulation
command[0] = chars[charPointer];
charPointer++;
if (command[0] == ' ') { // Push number to stack
//debugPrint("Push number to stack\n");
// read number until '/n'
while (chars[charPointer] != '\n') {
charPointer++;
}
charPointer++;
} else {
command[1] = chars[charPointer];
charPointer++;
}
}
else if (imp[0] == '\n' && imp[1] == '0') { // Flow control
//debugPrint("Flow Control\n");
command[0] = chars[charPointer];
command[1] = chars[charPointer + 1];
charPointer = charPointer + 2;
if (command[0] == ' ' && command[1] == ' ') { // set label
//debugPrint("Set label\n");
int number = 0;
jump_t temp;
while (chars[charPointer] != '\n') {
number = number * 2;
if (chars[charPointer] == '\t') {
number++;
}
charPointer++;
}
temp.label = number;
temp.to = charPointer;
jump = realloc(jump, (numberOfLabels + 1)*sizeof(*jump));
jump[numberOfLabels] = temp;
numberOfLabels++;
charPointer++;
}
else if (command[0] == ' ' && command[1] == '\t') { // call a subroutine
//debugPrint("Call a subruotine\n");
while (chars[charPointer] != '\n') {
charPointer++;
}
charPointer++;
}
else if (command[0] == ' ' && command[1] == '\n') { // Jump to label
//debugPrint("Jump to label\n");
while (chars[charPointer] != '\n') {
charPointer++;
}
charPointer++;
}
else if (command[0] == '\t' && command[1] == ' ') { // jump to label if top of stack is 0
//debugPrint("Jump to label if the top of stack is 0\n");
while (chars[charPointer] != '\n') {
charPointer++;
}
charPointer++;
}
else if (command[0] == '\t' && command[1] == '\t') { // Jump to label if top is < 0
//debugPrint("Jump to label if the top of stack is <0\n");
while (chars[charPointer] != '\n') {
charPointer++;
}
charPointer++;
}
else if (command[0] == '\t' && command[1] == '\n') { // end subroutine and transfer control to caller
//debugPrint("End subruotine and transfer control back to caller\n");
}
else if (command[0] == '\n' && command[1] == '\n') { // end program
//debugPrint("End the program\n");
parse = 0;
}
else {
//printf("No correct command for Flow Control\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
else {
imp[1] = chars[charPointer];
charPointer++;
if (imp[0] == '\t' && imp[1] == ' ') { // Arithmetic
command[0] = chars[charPointer];
command[1] = chars[charPointer + 1];
charPointer = charPointer + 2;
}
else if (imp[0] == '\t' && imp[1] == '\t') { // Heap accsess
command[0] = chars[charPointer];
charPointer++;
}
else if (imp[0] == '\t' && imp[1] == '\n') { // I/O
command[0] = chars[charPointer];
command[1] = chars[charPointer + 1];
charPointer = charPointer + 2;
}
else {
//printf("No correct IMP\n");
//printf("Here\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
//reset IMP
imp[0] = '0';
imp[1] = '0';
// reset command
command[0] = '0';
command[1] = '0';
}
charPointer = 0;
parse = 1;
/*
for (int i = 0; i < numberOfLabels; i++) {
printf("Label: %d, to: %d\n", jump[i].label, jump[i].to);
}
*/
while (charPointer <= numberOfChars && parse) {
/* get instrunction:
read type of instrunction i.e. imp
based of off imp, get instrunction and
maybe argument to instrunction
*/
// find IMP
imp[0] = chars[charPointer];
charPointer++;
if (imp[0] == ' ' && imp[1] == '0') { // Stack Manipulation
debugPrint("Stack Manipulation\n");
command[0] = chars[charPointer];
charPointer++;
if (command[0] == ' ') { // Push number to stack
debugPrint("Push number to stack\n");
// read number until '/n'
// push that to stack
int sign = 0;
int number = 0;
while (chars[charPointer] != '\n') {
if (sign == 0) {
if (chars[charPointer] == ' ') {
sign = 1;
}
else {
sign = -1;
}
}
else {
number = number * 2;
if (chars[charPointer] == '\t') {
number++;
}
}
charPointer++;
}
charPointer++;
stackPointer++;
if (stackSize <= stackPointer) {
stackSize = stackSize * 2;
stack = realloc(stack, stackSize*sizeof(*stack));
}
stack[stackPointer] = sign * number;
//printf("%d\n", stack[stackPointer]);
} else {
command[1] = chars[charPointer];
charPointer++;
if (command[0] == '\n' && command[1] == ' ') { // Duplicate top number
debugPrint("Duplicate top number\n");
stackPointer++;
if (stackSize <= stackPointer) {
stackSize = stackSize * 2;
stack = realloc(stack, stackSize*sizeof(*stack));
}
stack[stackPointer] = stack[stackPointer - 1];
}
else if (command[0] == '\n' && command[1] == '\t') { // swap top numbers
debugPrint("Swap top numbers\n");
/*
stack[stackPointer] = a
stack[stackPointer - 1] = b
a = a_init - b_init
a = a - b
b = a_init - b_init + b_init = a_init
b = a + b
a = a_init - (a_init - b_init) = b_init
a = b - a
*/
stack[stackPointer] = stack[stackPointer] - stack[stackPointer - 1];
stack[stackPointer - 1] = stack[stackPointer] + stack[stackPointer - 1];
stack[stackPointer] = stack[stackPointer - 1] - stack[stackPointer];
}
else if (command[0] == '\n' && command[1] == '\n') { // Discard top number
debugPrint("Discard top number\n");
stack[stackPointer] = 0;
stackPointer--;
}
else {
//printf("No correct command for Stack Manipulation\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
}
else if (imp[0] == '\n' && imp[1] == '0') { // Flow control
debugPrint("Flow Control\n");
command[0] = chars[charPointer];
command[1] = chars[charPointer + 1];
charPointer = charPointer + 2;
if (command[0] == ' ' && command[1] == ' ') { // set label
debugPrint("Set label\n");
while (chars[charPointer] != '\n') { // read past label
charPointer++;
}
charPointer++;
}
else if (command[0] == ' ' && command[1] == '\t') { // call a subroutine
debugPrint("Call a subruotine\n");
int number = 0;
while (chars[charPointer] != '\n') {
number = number * 2;
if (chars[charPointer] == '\t') {
number++;
}
charPointer++;
}
callStackPointer++;
if (callSize < callStackPointer) {
callSize = callSize * 2;
callStack = realloc(callStack, callSize*sizeof(*callStack));
if (callStack == NULL) {
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
callStack[callStackPointer] = charPointer;
callStackPointer = callStackPointer + 1;
for (int i = 0; i < numberOfLabels; i++) {
if (jump[i].label == number) {
charPointer = jump[i].to;
}
}
charPointer++;
}
else if (command[0] == ' ' && command[1] == '\n') { // Jump to label
debugPrint("Jump to label\n");
int number = 0;
while (chars[charPointer] != '\n') {
number = number * 2;
if (chars[charPointer] == '\t') {
number++;
}
charPointer++;
}
for (int i = 0; i < numberOfLabels; i++) {
if (jump[i].label == number) {
charPointer = jump[i].to;
}
}
charPointer++;
}
else if (command[0] == '\t' && command[1] == ' ') { // jump to label if top of stack is 0
debugPrint("Jump to label if the top of stack is 0\n");
int number = 0;
while (chars[charPointer] != '\n') {
number = number * 2;
if (chars[charPointer] == '\t') {
number++;
}
charPointer++;
}
int top = stack[stackPointer];
stack[stackPointer] = 0;
stackPointer--;
if (top == 0) {
for (int i = 0; i < numberOfLabels; i++) {
if (jump[i].label == number) {
charPointer = jump[i].to;
}
}
}
charPointer++;
}
else if (command[0] == '\t' && command[1] == '\t') { // Jump to label if top is < 0
debugPrint("Jump to label if the top of stack is <0\n");
int number = 0;
while (chars[charPointer] != '\n') {
number = number * 2;
if (chars[charPointer] == '\t') {
number++;
}
charPointer++;
}
int top = stack[stackPointer];
stack[stackPointer] = 0;
stackPointer--;
if (top < 0) {
for (int i = 0; i < numberOfLabels; i++) {
if (jump[i].label == number) {
charPointer = jump[i].to;
}
}
}
charPointer++;
}
else if (command[0] == '\t' && command[1] == '\n') { // end subroutine and transfer control to caller
debugPrint("End subruotine and transfer control back to caller\n");
charPointer = callStack[callStackPointer - 1];
callStackPointer = callStackPointer - 1;
charPointer++;
}
else if (command[0] == '\n' && command[1] == '\n') { // end program
debugPrint("End the program\n");
printf("\n");
parse = 0;
}
else {
//printf("No correct command for Flow Control\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
else {
imp[1] = chars[charPointer];
charPointer++;
if (imp[0] == '\t' && imp[1] == ' ') { // Arithmetic
debugPrint("Arithmetic\n");
command[0] = chars[charPointer];
command[1] = chars[charPointer + 1];
charPointer = charPointer + 2;
if (command[0] == ' ' && command[1] == ' ') { // Addition
debugPrint("Addition\n");
stackPointer--;
stack[stackPointer] = stack[stackPointer] + stack[stackPointer + 1];
stack[stackPointer + 1] = 0;
}
else if (command[0] == ' ' && command[1] == '\t') { // Subtraction
debugPrint("Subtraction\n");
stackPointer--;
stack[stackPointer] = stack[stackPointer] - stack[stackPointer + 1];
stack[stackPointer + 1] = 0;
}
else if (command[0] == ' ' && command[1] == '\n') { // Multiplication
debugPrint("Multiplication\n");
stackPointer--;
stack[stackPointer] = stack[stackPointer] * stack[stackPointer + 1];
stack[stackPointer + 1] = 0;
}
else if (command[0] == '\n' && command[1] == ' ') { // Integer division
debugPrint("Integer Division\n");
stackPointer--;
stack[stackPointer] = stack[stackPointer] / stack[stackPointer + 1];
stack[stackPointer + 1] = 0;
}
else if (command[0] == '\t' && command[1] == '\t') { // Modulo
debugPrint("Modulo\n");
stackPointer--;
stack[stackPointer] = stack[stackPointer] % stack[stackPointer + 1];
stack[stackPointer + 1] = 0;
}
else {
//printf("No correct command for Arithmetic\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
else if (imp[0] == '\t' && imp[1] == '\t') { // Heap accsess
debugPrint("Heap Access\n");
command[0] = chars[charPointer];
charPointer++;
if (command[0] == ' ') { // Store in heap
debugPrint("Store in heap\n");
while (heapSize <= stack[stackPointer - 1]) { // increase heap if nessesary
heapSize = heapSize * 2;
heap = realloc(heap, heapSize*sizeof(*heap));
if (heap == NULL) {
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
heap[stack[stackPointer - 1]] = stack[stackPointer]; // store value in heap
stack[stackPointer] = 0;
stack[stackPointer - 1] = 0;
stackPointer = stackPointer - 2;
}
else if (command[0] == '\t') { // Retrieve from heap
debugPrint("Retrieve from heap\n");
//stackPointer++;
if (stackSize <= stackPointer) {
stackSize = stackSize * 2;
stack = realloc(stack, stackSize*sizeof(*stack));
if (stack == NULL) {
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
if (heapSize > stack[stackPointer]) {
stack[stackPointer] = heap[stack[stackPointer]];
}
else {
stack[stackPointer] = 0;
}
}
else {
//printf("No correct command for Heap Access\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
else if (imp[0] == '\t' && imp[1] == '\n') { // I/O
debugPrint("I/O\n");
command[0] = chars[charPointer];
command[1] = chars[charPointer + 1];
charPointer = charPointer + 2;
if (command[0] == ' ' && command[1] == ' ') { // Output char at top of stack
debugPrint("Output char at top of stack\n");
printf("%c", stack[stackPointer]);
stack[stackPointer] = 0;
stackPointer--;
}
else if (command[0] == ' ' && command[1] == '\t') { // output number at top of stack
debugPrint("Output number at top of stack\n");
printf("%d", stack[stackPointer]);
stack[stackPointer] = 0;
stackPointer--;
}
else if (command[0] == '\t' && command[1] == ' ') { // read a char and place it in location given by top of stack
debugPrint("Read a char and place it in the location given by top of stack\n");
char buffer;
scanf(" %c", &buffer);
while (heapSize <= stack[stackPointer]) {
heapSize = heapSize * 2;
heap = realloc(heap, heapSize*sizeof(*heap));
if (heap == NULL) {
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
heap[stack[stackPointer]] = buffer;
stack[stackPointer] = 0;
stackPointer--;
}
else if (command[0] == '\t' && command[1] == '\t') { // read a number and place it in location given by top of stack
debugPrint("Read number and place it in the location given by top of stack\n");
int buffer;
scanf(" %d", &buffer);
while (heapSize <= stack[stackPointer]) {
heapSize = heapSize * 2;
heap = realloc(heap, heapSize*sizeof(*heap));
if (heap == NULL) {
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
heap[stack[stackPointer]] = buffer;
stack[stackPointer] = 0;
stackPointer--;
}
else {
//printf("No correct command for I/O\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
else {
//printf("No correct IMP\n");
return freeAll(stack, callStack, heap, jump, chars, -1);
}
}
//reset IMP
imp[0] = '0';
imp[1] = '0';
// reset command
command[0] = '0';
command[1] = '0';
}
return freeAll(stack, callStack, heap, jump, chars, 0);
}