forked from pavel987/wspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wspace.cpp
772 lines (756 loc) · 19.8 KB
/
wspace.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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/**
* Copyright (C) 2008 Pavel Shub
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* Version:
* $Id: wspace.cpp,v 1.8 2008/04/04 21:51:02 cogent Exp cogent $
* Revisions:
* $Log: wspace.cpp,v $
* Revision 1.8 2008/04/04 21:51:02 cogent
* fixed up command line parameters, done?
*
* Revision 1.7 2007/07/24 02:40:50 cogent
* all bugs fixed
*
* Revision 1.6 2007/07/22 05:27:20 cogent
* fixed a lot of errors. works for some files not all.
*
* Revision 1.5 2007/07/22 03:21:47 cogent
* added lots of flags and command line arguments
* still errors to debug
*
* Revision 1.4 2007/07/22 00:07:49 cogent
* changed pcs from iterator to unsigned long and it works
*
* Revision 1.3 2007/07/21 23:01:40 cogent
* tried to fix bugs. Going for a new strategy
*
* Revision 1.2 2007/07/21 19:50:22 cogent
* done, bugs exist?
*
* Revision 1.1 2007/05/26 17:19:24 cogent
* Initial revision
*
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <map>
#include <vector>
using std::cout;
using std::cerr;
using std::cin;
using std::endl;
using std::ios;
using std::ifstream;
using std::string;
using std::map;
using std::vector;
// Constants
const char C('\n'); // Newline
const char B('\t'); // Tab
const char A(' '); // Space
// Stuff to kill computer memory:
// Memory
map<long, long> heap;
vector<long> stack;
// Internal tables
map<long, unsigned long> symtab;
map<unsigned long, long> numtab;
vector<unsigned long> return_stack;
vector<int> program_stack;
// Global flags
bool print_asm(false);
bool print_debug(false);
bool print_letters(false);
bool print_detail(false);
// Function definitions
long convert_num(string wsnum, bool sign=false);
void show_usage();
void show_help();
// Program start
int main (int argc, char *argv[])
{
// Pre-game
if(argc < 2 ){
show_usage();
return EXIT_FAILURE;
}
// filter the command line parameters
if(argc > 2){
if(strchr(argv[1], 'a') != NULL) print_asm = true;
if(strchr(argv[1], 'g') != NULL) print_debug = true;
if(strchr(argv[1], 'l') != NULL) print_letters = true;
if(strchr(argv[1], 'd') != NULL) print_detail = true;
}
// print the help screen and exit
if(strcmp(argv[1], "h") == 0 || strcmp(argv[1], "--help") == 0){
show_help();
return EXIT_SUCCESS;
}
// Get the whole file and put it into c_string called program_start
long length;
ifstream in;
in.open(argv[argc-1]);
if ( in.fail() ){
cerr << "Error opening file." << endl;
return EXIT_FAILURE;
}
in.seekg(0, ios::end);
length = in.tellg();
in.seekg(0, ios::beg);
char *program_start = new char[length];
char *program_end = program_start + length;
in.read(program_start,length);
in.close();
// Variables for processing
string num("");
int stage(0);
long tmp;
long in_num(0);
char *pc(program_start); // Program counter
// Start the first pass.
// All the commands will be read and put into the program_stack. All labels
// will be put into symtab and all numbers will be read, coverted to long and
// put into numtab. This also does syntax checking.
while(pc < program_end){
// This conditional skips letters that aren't whitespace
if(*pc != A && *pc != B && *pc != C){
pc++;
continue;
}
// If the argument 'l' was passed then this program will only print the
// whitespace program as letters ABC.
if(print_letters){
if(*pc == A) cout << "A";
if(*pc == B) cout << "B";
if(*pc == C) cout << "C" << endl;
pc++;
continue;
}
switch(stage){
case 0: switch(*pc){
case A: stage = 2; break;
case B: stage = 1; break;
case C: stage = 9; break;
} break;
case 1: switch(*pc){
case A: stage = 5; break;
case B: stage = 8; break;
case C: stage = 13;break;
} break;
case 2: switch(*pc){
case A: stage = 16; num = ""; break;
case B: stage = 3; break;
case C: stage = 4; break;
} break;
case 3: switch(*pc){
case A: stage = 17; num = "";
break;
case B: stage = -1; break;
case C: stage = 18; num = "";
break;
} break;
case 4: switch(*pc){
case A: if(print_asm){
cout << "dup" << endl;
}
program_stack.push_back(104);
stage = 0;
break;
case B: if(print_asm){
cout << "swap" << endl;
}
program_stack.push_back(105);
stage = 0;
break;
case C: if(print_asm){
cout << "discard" << endl;
}
program_stack.push_back(106);
stage = 0;
break;
} break;
case 5: switch(*pc){
case A: stage = 6; break;
case B: stage = 7; break;
case C: stage = -2; break;
} break;
case 6: switch(*pc){
case A: if(print_asm){
cout << "add" << endl;
}
program_stack.push_back(114);
stage = 0;
break;
case B: if(print_asm){
cout << "sub" << endl;
}
program_stack.push_back(115);
stage = 0;
break;
case C: if(print_asm){
cout << "mul" << endl;
}
program_stack.push_back(116);
stage = 0;
break;
} break;
case 7: switch(*pc){
case A: if(print_asm){
cout << "div" << endl;
}
program_stack.push_back(117);
stage = 0;
break;
case B: if(print_asm){
cout << "mod" << endl;
}
program_stack.push_back(118);
stage = 0;
break;
case C: stage = -3; break;
} break;
case 8: switch(*pc){
case A: if(print_asm){
cout << "store" << endl;
}
program_stack.push_back(119);
stage = 0;
break;
case B: if(print_asm){
cout << "retrieve" << endl;
}
program_stack.push_back(120);
stage = 0;
break;
case C: stage = -4; break;
} break;
case 9: switch(*pc){
case A: stage = 10; break;
case B: stage = 11; break;
case C: stage = 12; break;
} break;
case 10: switch(*pc){
case A: stage = 19; num = ""; break;
case B: stage = 20; num = ""; break;
case C: stage = 21; num = ""; break;
} break;
case 11: switch(*pc){
case A: stage = 22; num = ""; break;
case B: stage = 23; num = ""; break;
case C: if(print_asm){
cout << "ret" << endl;
}
program_stack.push_back(112);
stage = 0;
break;
} break;
case 12: switch(*pc){
case A: stage = -5; break;
case B: stage = -6; break;
case C: if(print_asm){
cout << "exit" << endl;
}
program_stack.push_back(113);
stage = 0;
break;
} break;
case 13: switch(*pc){
case A: stage = 14; break;
case B: stage = 15; break;
case C: stage = -7; break;
} break;
case 14: switch(*pc){
case A: if(print_asm){
cout << "outchar" << endl;
}
program_stack.push_back(121);
stage = 0;
break;
case B: if(print_asm){
cout << "outnum" << endl;
}
program_stack.push_back(122);
stage = 0;
break;
case C: stage = -8; break;
} break;
case 15: switch(*pc){
case A: if(print_asm){
cout << "readchar" << endl;
}
program_stack.push_back(123);
stage = 0;
break;
case B: if(print_asm){
cout << "readnum" << endl;
}
program_stack.push_back(124);
stage = 0;
break;
case C: stage = -9; break;
} break;
// Cascade push, copy, slide. Because code is the same
// only stage varies, 16+85 = 101, 18 + 85 = 103
case 16:
case 17:
case 18: switch(*pc){
case A: num += *pc; break;
case B: num += *pc; break;
case C: if(num.length() < 2){
stage = -10;
break;
}
tmp = convert_num(num, true);
if(print_asm){
switch(stage){
case 16: cout << "push " << tmp << endl;
break;
case 17: cout << "copy " << tmp << endl;
break;
case 18: cout << "slide " << tmp << endl;
break;
}
}
program_stack.push_back(stage + 85);
numtab[program_stack.size()-1] = tmp;
stage = 0;
break;
} break;
// Handle labels
case 19:
switch(*pc){
case A: num += *pc; break;
case B: num += *pc; break;
case C: if(num.length() < 1){
stage = -10;
break;
}
tmp = convert_num(num,false);
if(print_asm){
cout << "label " << tmp << endl;
}
symtab[tmp] = program_stack.size();
program_stack.push_back(107);
stage = 0;
break;
} break;
// Same thing as last cascade, but for call, jump, jn,
// and jz
case 20:
case 21:
case 22:
case 23: switch(*pc){
case A: num += *pc; break;
case B: num += *pc; break;
case C: if(num.length() < 1){
stage = -10;
break;
}
tmp = convert_num(num, false);
if(print_asm){
switch(stage){
case 20: cout << "call " << tmp << endl;
break;
case 21: cout << "jump " << tmp << endl;
break;
case 22: cout << "jz " << tmp << endl;
break;
case 23: cout << "jn " << tmp << endl;
break;
}
}
program_stack.push_back(stage + 88);
numtab[program_stack.size()-1] = tmp;
stage = 0;
break;
} break;
}
// Print a meaningful message if a syntax error is encountered
if( stage < 0 ){
cerr << "Syntax Error" << endl;
if(stage == -10){
cerr << "Numerical Value not Entered." << endl;
return EXIT_FAILURE;
}
cerr << "Non-Existant command entered: ";
switch(stage){
case -1: cerr << "[Spase][Tab][Tab]";
break;
case -2: cerr << "[Tab][Space][LF]";
break;
case -3: cerr << "[Tab][Space][Tab][LF]";
break;
case -4: cerr << "[Tab][Tab][LF]";
break;
case -5: cerr << "[LF][LF][Space]";
break;
case -6: cerr << "[LF][LF][Tab]";
break;
case -7: cerr << "[Tab][LF][LF]";
break;
case -8: cerr << "[Tab][LF][Space][LF]";
break;
case -9: cerr << "[Tab][LF][Tab][LF]";
break;
}
cerr << " after command # " << program_stack.back() << endl;
return EXIT_FAILURE;
}
pc++;
}
// Delete the ws file from memory
delete[] program_start;
// If they wanted to see the letters or ws assembly then exit and don't execute
if(print_asm || print_letters){
return EXIT_SUCCESS;
}
// variables that are needed for the execution of the program
unsigned long pcs_size = program_stack.size();
unsigned long pcs = 0; // program counter for stack
map<long, unsigned long>::iterator check; // check var for pcs
map<long, long>::iterator hcheck; // check var for heap
char debug_char('s');
long arg1, arg2;
// now do the actual execution of the program
while( pcs < pcs_size ){
if(print_debug){
cout << "#" << pcs+1 << " ";
}
switch( program_stack[pcs] ){
// push
case 101: stack.push_back(numtab[pcs]);
if(print_debug) cout << " push " << numtab[pcs] << "\t: ";
break;
// copy
case 102: if(stack.empty()){
cerr << "Error: Cannot copy, stack is empty." << endl;
return EXIT_FAILURE;
}
stack.push_back( *(stack.end() - ( numtab[pcs]+1 )) );
if(print_debug) cout << " copy " << numtab[pcs] << "\t: ";
break;
// slide
case 103: if(stack.empty()){
cerr << "Error: Cannot slide, stack is empty." << endl;
return EXIT_FAILURE;
}
stack.erase( stack.end() - (numtab[pcs]+1), stack.end() - 1 );
if(print_debug) cout << " slide " << numtab[pcs] << "\t: ";
break;
// dup
case 104: if(stack.empty()){
cerr << "Error: Cannot dup, stack is empty." << endl;
return EXIT_FAILURE;
}
stack.push_back( stack.back() );
if(print_debug) cout << " dup\t: ";
break;
// swap
case 105: if(stack.size() < 2){
cerr << "Error: Cannot swap, not enough elements on the stack." << endl;
return EXIT_FAILURE;
}
arg1 = stack.back();
stack.pop_back();
arg2 = stack.back();
stack.pop_back();
stack.push_back(arg1);
stack.push_back(arg2);
if(print_debug) cout << " swap\t: ";
break;
// discard
case 106: stack.pop_back();
if(print_debug) cout << " discard\t: ";
break;
// label
case 107:
if(print_debug) cout << " label " << numtab[pcs] << "\t: ";
break;
// call
case 108: return_stack.push_back(pcs);
check = symtab.find(numtab[pcs]);
if(check != symtab.end()){
pcs = check->second;
}
else{
cerr << "Error: Cannot jump to " << numtab[pcs] << endl;
return EXIT_FAILURE;
}
if(print_debug) cout << " call " << numtab[pcs] << "\t: ";
break;
// jump
case 109:
check = symtab.find(numtab[pcs]);
if(check != symtab.end()){
pcs = check->second;
}
else{
cerr << "Error: Cannot jump to " << numtab[pcs] << endl;
return EXIT_FAILURE;
}
if(print_debug) cout << " jump " << numtab[pcs] << "\t: ";
break;
// jz
case 110: if(stack.back() == 0){
check = symtab.find(numtab[pcs]);
if(check != symtab.end()){
pcs = check->second;
}
else{
cerr << "Error: Cannot jump to " << numtab[pcs] << endl;
return EXIT_FAILURE;
}
}
stack.pop_back();
if(print_debug) cout << " jz " << numtab[pcs] << "\t: ";
break;
// jn
case 111: if(stack.back() < 0){
check = symtab.find(numtab[pcs]);
if(check != symtab.end()){
pcs = check->second;
}
else{
cerr << "Error: Cannot jump to " << numtab[pcs] << endl;
return EXIT_FAILURE;
}
}
stack.pop_back();
if(print_debug) cout << " jn " << numtab[pcs] << "\t: ";
break;
// ret
case 112: if(!return_stack.empty()){
pcs = *(return_stack.end()-1);
}
else{
cerr << "Error: Cannot return, nothing to return to." << endl;
return EXIT_FAILURE;
}
return_stack.pop_back();
if(print_debug) cout << " push\t: ";
break;
// exit
case 113: pcs = program_stack.size() - 1;
if(print_debug) cout << " exit\t: ";
break;
// add
case 114: arg1 = stack.back();
stack.pop_back();
arg2 = stack.back();
stack.pop_back();
stack.push_back(arg2 + arg1);
if(print_debug) cout << " add\t: ";
break;
// sub
case 115: arg1 = stack.back();
stack.pop_back();
arg2 = stack.back();
stack.pop_back();
stack.push_back(arg2 - arg1);
if(print_debug) cout << " sub\t: ";
break;
// mul
case 116: arg1 = stack.back();
stack.pop_back();
arg2 = stack.back();
stack.pop_back();
stack.push_back(arg2 * arg1);
if(print_debug) cout << " mul\t: ";
break;
// div
case 117: arg1 = stack.back();
stack.pop_back();
arg2 = stack.back();
stack.pop_back();
if(arg1 == 0){
cerr << "Error: Cannot div by zero." << endl;
return EXIT_FAILURE;
}
stack.push_back(arg2 / arg1);
if(print_debug) cout << " div\t: ";
break;
// mod
case 118: arg1 = stack.back();
stack.pop_back();
arg2 = stack.back();
stack.pop_back();
if(arg1 == 0){
cerr << "Error: Cannot mod by zero." << endl;
return EXIT_FAILURE;
}
stack.push_back(arg2 % arg1);
if(print_debug) cout << " mod\t: ";
break;
// store
case 119: arg1 = stack.back();
stack.pop_back();
arg2 = stack.back();
stack.pop_back();
heap[arg2] = arg1;
if(print_debug) cout << " store\t: ";
break;
// retrieve
case 120: arg1 = stack.back();
stack.pop_back();
hcheck = heap.find(arg1);
if(hcheck != heap.end()){
stack.push_back(hcheck->second);
}
else{
cerr << "Error: Cannot retrieve from " << arg1 << endl;
return EXIT_FAILURE;
}
if(print_debug) cout << " retrieve\t: ";
break;
// outchar
case 121: arg1 = stack.back();
stack.pop_back();
cout << (char)arg1;
if(print_debug) cout << " outchar\t: ";
break;
// outnum
case 122: arg1 = stack.back();
stack.pop_back();
cout << arg1;
if(print_debug) cout << " outnum\t: ";
break;
// readchar
case 123:
if( stack.empty() ){
cerr << "Error: Cannot readchar, stack is empty." << endl;
return EXIT_FAILURE;
}
else{
in_num = cin.get();
heap[ stack.back() ] = in_num;
stack.pop_back();
}
if(print_debug) cout << " readchar\t: ";
break;
// readnum
case 124:
if( stack.empty() ){
cerr << "Error: Cannot readnum, stack is empty." << endl;
return EXIT_FAILURE;
}
else{
cin >> in_num;
heap[ stack.back() ] = in_num;
stack.pop_back();
}
if(print_debug) cout << " readnum\t: ";
break;
}
// Debugging mode. r for return stack
// h for heap values
// f to exit debugging mode and finish execution
// e to halt execution
if(print_debug){
for(unsigned int i = 0; i < stack.size(); i++){
cout << "-> " << stack[i];
}
cout << endl;
debug_char = getchar();
while(debug_char == 'r' || debug_char == 'h' || debug_char == 'f' || debug_char == 'e'){
// print return stack
if(debug_char == 'r'){
cout << "Return stack: ";
for(unsigned int i = 0; i < return_stack.size(); i++){
cout << "-> " << return_stack[i]+1;
}
cout << endl;
}
if(debug_char == 'h'){
cout << "Heap: ";
map<long, long>::iterator it;
for ( it=heap.begin() ; it != heap.end(); it++ ){
cout << (*it).first << " => " << (*it).second << ", ";
}
cout << endl;
}
if(debug_char == 'f'){
print_debug = false;
}
if(debug_char == 'e'){
return EXIT_SUCCESS;
}
debug_char = getchar();
}
}
pcs++;
}
// if the 'd' flag was specified then print the heap and stack size like
// the original ws interpreter
if(print_detail){
cout << "Done.\nStack size " << stack.size() << endl;
cout << "Heap size " << heap.size() << endl;
}
return EXIT_SUCCESS;
}
void show_usage(){
cout << "Usage: wspace [hgdal] file.ws" << endl;
}
void show_help(){
show_usage();
// general help
cout << "General Help:" << endl;
cout << " a\tprint whitespace assembly language instead of program execution" << endl;
cout << " l\tprints the code using ABC instead of space, tab, and new line \n\trespectively"
<< " then exits." << endl;
cout << " g\tdebugging mode, will step through each command of the program manually" << endl;
cout << " h\tprint this menu then exit" << endl;
cout << " d\tprints stack and heap size after program execution\n\tsimilar to original ws interpreter" << endl;
// debugging mode help
cout << "Debug Mode:" << endl;
cout << " Debug mode steps through the code line by line printing the stack after\n command execution" << endl;
cout << " Entering these letters will have the following effects: " << endl;
cout << " r\tprints the return stack" << endl;
cout << " h\tprints the heap key value pairs" << endl;
cout << " f\tfinishes execution of program" << endl;
cout << " e\texits the program" << endl;
cout << " Entering any other values will advance program execution." << endl;
// Copyright and license information.
cout << endl;
cout << "Whitespace C++ interpreter (c) 2008 Pavel Shub. original by Edwin Brady" << endl;
cout << "This program is published under GNU General Public license." << endl;
}
long convert_num(string wsnum, bool sign){
long composite(0);
long pwr(1);
string::reverse_iterator it;
for(it = wsnum.rbegin(); it < wsnum.rend()-1; it++){
if(*it == B){
composite += pwr;
}
pwr *= 2;
}
if(sign){
if(wsnum[0] == B){
return (composite * -1);
}
else{
return composite;
}
}
if(wsnum[0] == B){
composite += pwr;
}
return composite;
}