This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.cc
649 lines (533 loc) · 11.6 KB
/
token.cc
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
/*
Copyright (c) 2002-2012 René Ladan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "token.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Token::Token() {
myname = strdup("TOKEN");
mytype = ttToken;
}
Token::~Token() {
free(myname);
myname = NULL;
}
int Token::getValue() {
return 0;
}
int Token::getOffset() {
return 0;
}
int Token::getCount() {
return 0;
}
bool Token::isAbsoluteValue() {
return false;
}
bool Token::isAbsoluteOffset() {
return false;
}
int Token::getOffsetDestination() {
return 0;
}
int Token::getOffsetSource() {
return 0;
}
int Token::getMultiplier() {
return 0;
}
doType Token::getDoType() {
return dtWhile;
}
bool Token::isWrapped() {
return false;
}
bool Token::isAbsoluteOffsetDestination() {
return false;
}
bool Token::isAbsoluteOffsetSource() {
return false;
}
bool Token::isUsable() {
return false;
}
tokenType Token::getType() {
return mytype;
}
char *Token::getName() {
return myname;
}
char *Token::getCode() {
return strdup("/* generic Token code */");
}
bool Token::interpret(unsigned**, unsigned*, bool, unsigned) {
return false;
}
void Token::setValue(int value) {
}
void Token::setOffset(int offset) {
}
void Token::setCount(int count) {
}
void Token::makeAbsoluteValue(bool absoluteValue) {
}
void Token::makeAbsoluteOffset(bool absoluteOffset) {
}
void Token::setDoType(doType dotype) {
}
void Token::setOffsetDestination(int ofsdest) {
}
void Token::setOffsetSource(int ofssrc) {
}
void Token::setMultiplier(int mult) {
}
void Token::setWrapped(bool wrapped) {
}
void Token::makeAbsoluteOffsetDestination(bool absofssrc) {
}
void Token::makeAbsoluteOffsetSource(bool absofssrc) {
}
void Token::format(char **dest, char *head, int value, bool absolutevalue) {
if (absolutevalue) {
asprintf(dest, "%s = %i;", head, value);
} else {
if (value == 1) {
asprintf(dest, "%s++;", head);
} else if (value == -1) {
asprintf(dest, "%s--;", head);
} else if (value > 1) {
asprintf(dest, "%s += %i;", head, value);
} else if (value < -1) {
asprintf(dest, "%s -= %i;" ,head, -value);
}
}
}
Ptr::Ptr() {
myname = strdup("PTR");
mytype = ttPtr;
myvalue = 0;
myabsolutevalue = false;
}
char *Ptr::getCode() {
char *r;
format(&r, strdup("p"), myvalue, myabsolutevalue);
return r;
}
bool Ptr::interpret(unsigned **array, unsigned *pointer, bool, unsigned) {
if (myabsolutevalue) {
*pointer = myvalue;
} else {
*pointer += myvalue;
}
return true;
}
bool Ptr::isUsable() {
return myabsolutevalue || (myvalue != 0);
}
int Ptr::getValue() {
return myvalue;
}
bool Ptr::isAbsoluteValue() {
return myabsolutevalue;
}
void Ptr::setValue(int value) {
myvalue = value;
}
void Ptr::makeAbsoluteValue(bool absoluteValue) {
myabsolutevalue = absoluteValue;
}
Val::Val() {
myname = strdup("VAL");
mytype = ttVal;
myvalue = 0;
myoffset = 0;
myabsolutevalue = false;
myabsoluteoffset = false;
}
int Val::getValue() {
return myvalue;
}
int Val::getOffset() {
return myoffset;
}
bool Val::isAbsoluteValue() {
return myabsolutevalue;
}
bool Val::isAbsoluteOffset() {
return myabsoluteoffset;
}
bool Val::isUsable() {
return myabsolutevalue || myabsoluteoffset || (myvalue != 0) ||
(myoffset != 0);
}
char *Val::getCode() {
char *r, *dest;
if (myabsoluteoffset) {
asprintf(&dest, "a[%i]", myoffset);
format(&r, dest, myvalue, myabsolutevalue);
} else {
if (myoffset != 0) {
asprintf(&dest, "a[p%s%i]", myoffset > 0 ? "+" : "",
myoffset);
} else {
dest = strdup("a[p]");
}
format(&r, dest, myvalue, myabsolutevalue);
}
return r;
}
bool Val::interpret(unsigned **array, unsigned *pointer, bool, unsigned) {
if (myabsoluteoffset) {
if (myabsolutevalue) {
(*array)[myoffset] = myvalue;
} else {
(*array)[myoffset] += myvalue;
}
} else {
if (myabsolutevalue) {
(*array)[*pointer + myoffset] = myvalue;
} else {
(*array)[*pointer + myoffset] += myvalue;
}
}
return true;
}
void Val::setValue(int value) {
myvalue = value;
}
void Val::setOffset(int offset) {
myoffset = offset;
}
void Val::makeAbsoluteValue(bool absoluteValue) {
myabsolutevalue = absoluteValue;
}
void Val::makeAbsoluteOffset(bool absoluteOffset) {
myabsoluteoffset = absoluteOffset;
}
Put::Put() {
myname = strdup("PUT");
mytype = ttPut;
mycount = 1;
myoffset = 0;
myabsoluteoffset = false;
myvalue = 0;
myabsolutevalue = false;
}
bool Put::isUsable() {
return true;
}
int Put::getCount() {
return mycount;
}
int Put::getOffset() {
return myoffset;
}
int Put::getValue() {
return myvalue;
}
bool Put::isAbsoluteOffset() {
return myabsoluteoffset;
}
bool Put::isAbsoluteValue() {
return myabsolutevalue;
}
char* Put::getCode() {
char *r, *r0;
asprintf(&r0, "putchar(");
if (!myabsolutevalue) {
asprintf(&r0, "%sa[", r0);
if (myabsoluteoffset) {
asprintf(&r0, "%s%i]);", r0, myoffset);
} else {
if (myoffset == 0) {
asprintf(&r0, "%sp]);", r0);
} else {
asprintf(&r0, "p%s%i]);",
myoffset < 0 ? "" : "+", myoffset);
}
}
} else {
asprintf(&r0, "%s%i);", r0, myvalue);
// output ASCII comment
if (myvalue >= 32) {
asprintf(&r0, "%s /* %c */", r0, myvalue);
}
}
if (mycount > 1) {
asprintf(&r, "for (i = 0; i < %i; i++) { %s }",
mycount, r0);
return r;
} else {
return r0;
}
}
bool Put::interpret(unsigned **array, unsigned *pointer, bool running, unsigned) {
for (int i = 0; running && (i < mycount); i++) {
if (myabsolutevalue) {
putchar(myvalue);
} else {
if (myabsoluteoffset) {
putchar((*array)[myoffset]);
} else {
putchar((*array)[myoffset + *pointer]);
}
}
}
return true;
}
void Put::setCount(int count) {
mycount = count;
}
void Put::setOffset(int offset) {
myoffset = offset;
}
void Put::makeAbsoluteOffset(bool absoluteoffset) {
myabsoluteoffset = absoluteoffset;
}
void Put::setValue(int value) {
myvalue = value;
}
void Put::makeAbsoluteValue(bool absolutevalue) {
myabsolutevalue = absolutevalue;
}
Get::Get() {
myname = strdup("GET");
mytype = ttGet;
mycount = 1;
myoffset = 0;
myabsoluteoffset = false;
}
bool Get::isUsable() {
return true;
}
int Get::getCount() {
return mycount;
}
int Get::getOffset() {
return myoffset;
}
bool Get::isAbsoluteOffset() {
return myabsoluteoffset;
}
char *Get::getCode() {
char *r, *r0;
if (myabsoluteoffset) {
asprintf(&r0, "a[%i", myoffset);
} else {
if (myoffset == 0) {
r0 = strdup("a[p");
} else {
asprintf(&r0, "a[p%s%i",
myoffset < 0 ? "" : "+", myoffset);
}
}
asprintf(&r0, "%s] = getchar();", r0);
if (mycount > 1) {
asprintf(&r, "for (i = 0; i < %i; i++) { %s }",
mycount, r0);
return r;
} else {
return r0;
}
}
bool Get::interpret(unsigned **array, unsigned *pointer, bool running, unsigned) {
for (int i = 0; running && (i < mycount); i++) {
if (myabsoluteoffset) {
(*array)[myoffset] = getchar();
} else {
(*array)[myoffset + *pointer] = getchar();
}
}
return true;
}
void Get::setCount(int count) {
mycount = count;
}
void Get::setOffset(int offset) {
myoffset = offset;
}
void Get::makeAbsoluteOffset(bool absoluteoffset) {
myabsoluteoffset = absoluteoffset;
}
Do::Do() {
myname = strdup("DO");
mytype = ttDo;
mydotype = dtWhile;
}
bool Do::isUsable() {
return false; // requires special handling anyway
}
doType Do::getDoType() {
return mydotype;
}
char *Do::getCode() {
char *r;
switch (mydotype) {
case dtWhile:
asprintf(&r,"while (a[p]) {");
break;
case dtIf:
asprintf(&r,"if (a[p]) {");
break;
case dtFor:
// TODO research this case -- temp code
asprintf(&r,"for (i = 0; i < 1; i++) {");
break;
}
return r;
}
void Do::setDoType(doType dotype) {
// TODO research this method in case of dtFor
mydotype = dotype;
}
bool Do::interpret(unsigned **array, unsigned *pointer, bool, unsigned) {
return (*array)[*pointer] != 0;
}
Od::Od() {
myname = strdup("OD");
mytype = ttOd;
}
bool Od::isUsable() {
return false; // this token is never used in the parse tree
}
char* Od::getCode() {
return strdup("}");
}
bool Od::interpret(unsigned **, unsigned *, bool, unsigned) {
return false;
}
Eof::Eof() {
myname = strdup("EOF");
mytype = ttEof;
}
bool Eof::isUsable() {
return false; // not in the tree
}
char* Eof::getCode() {
return strdup("/* EOF code */");
}
bool Eof::interpret(unsigned **, unsigned *, bool, unsigned) {
return false;
}
Mulzero::Mulzero() {
myname = strdup("MULZERO");
mytype = ttMulzero;
myofsdest = 0;
myofssrc = 0;
mymult = 0;
mywrapped = false;
myabsofsdest = false;
myabsofssrc = false;
}
bool Mulzero::isUsable() {
return true; // generated token
}
int Mulzero::getOffsetDestination() {
return myofsdest;
}
int Mulzero::getOffsetSource() {
return myofssrc;
}
int Mulzero::getMultiplier() {
return mymult;
}
bool Mulzero::isWrapped() {
return mywrapped;
}
bool Mulzero::isAbsoluteOffsetDestination() {
return myabsofsdest;
}
bool Mulzero::isAbsoluteOffsetSource() {
return myabsofssrc;
}
char *Mulzero::getCode() {
char *r, *r1, *r2, *r3o, *r3c, *r3;
if (myabsofsdest) {
asprintf(&r1, "a[%i]", myofsdest);
} else {
if (myofsdest == 0) {
r1 = strdup("a[p]");
} else {
asprintf(&r1, "a[p%s%i]", myofsdest > 0 ? "+" : "",
myofsdest);
}
}
if (mymult < 0) {
r2 = strdup("-=");
} else {
r2 = strdup("+=");
}
if (mywrapped) {
asprintf(&r3o, "(RANGE-a[");
asprintf(&r3c, "])");
} else {
asprintf(&r3o, "a[");
asprintf(&r3c, "]");
}
if (myabsofssrc) {
asprintf(&r3, "%s%i%s", r3o, myofssrc, r3c);
} else {
if (myofssrc == 0) {
asprintf(&r3, "%sp%s", r3o, r3c);
} else {
asprintf(&r3, "%sp%s%i%s", r3o,
myofssrc < 0 ? "" : "+",
myofssrc, r3c);
}
}
if (abs(mymult) != 1) {
asprintf(&r, "%s %s %i * %s;", r1, r2, abs(mymult), r3);
} else {
asprintf(&r, "%s %s %s;", r1, r2, r3);
}
return r;
}
bool Mulzero::interpret(unsigned **array, unsigned *pointer, bool, unsigned range) {
unsigned dest = myofsdest;
unsigned src = myofssrc;
src += myabsofssrc ? 0 : *pointer;
dest += myabsofsdest ? 0 : *pointer;
if (mywrapped) {
(*array)[src] = range - (*array)[src];
}
(*array)[dest] += mymult * (*array)[src];
return true;
}
void Mulzero::setOffsetDestination(int ofsdest) {
myofsdest = ofsdest;
}
void Mulzero::setOffsetSource(int ofssrc) {
myofssrc = ofssrc;
}
void Mulzero::setMultiplier(int mult) {
mymult = mult;
}
void Mulzero::setWrapped(bool wrapped) {
mywrapped = wrapped;
}
void Mulzero::makeAbsoluteOffsetDestination(bool absofsdest) {
myabsofsdest = absofsdest;
}
void Mulzero::makeAbsoluteOffsetSource(bool absofssrc) {
myabsofssrc = absofssrc;
}