forked from adamdunkels/ubasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubasic.c
641 lines (601 loc) · 16.5 KB
/
ubasic.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
/*
* Copyright (c) 2006, Adam Dunkels
* 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.
* 3. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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.
*
*/
#define DEBUG 0
#if DEBUG
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
#else
#define DEBUG_PRINTF(...)
#endif
#include "ubasic.h"
#include "tokenizer.h"
#include <stdio.h> /* printf() */
#include <stdlib.h> /* exit() */
static char const *program_ptr;
#define MAX_STRINGLEN 40
static char string[MAX_STRINGLEN];
#define MAX_GOSUB_STACK_DEPTH 10
static int gosub_stack[MAX_GOSUB_STACK_DEPTH];
static int gosub_stack_ptr;
struct for_state {
int line_after_for;
int for_variable;
int to;
};
#define MAX_FOR_STACK_DEPTH 4
static struct for_state for_stack[MAX_FOR_STACK_DEPTH];
static int for_stack_ptr;
struct line_index {
int line_number;
char const *program_text_position;
struct line_index *next;
};
struct line_index *line_index_head = NULL;
struct line_index *line_index_current = NULL;
#define MAX_VARNUM 26
static VARIABLE_TYPE variables[MAX_VARNUM];
static int ended;
static VARIABLE_TYPE expr(void);
static void line_statement(void);
static void statement(void);
static void index_free(void);
peek_func peek_function = NULL;
poke_func poke_function = NULL;
/*---------------------------------------------------------------------------*/
void
ubasic_init(const char *program)
{
program_ptr = program;
for_stack_ptr = gosub_stack_ptr = 0;
index_free();
tokenizer_init(program);
ended = 0;
}
/*---------------------------------------------------------------------------*/
void
ubasic_init_peek_poke(const char *program, peek_func peek, poke_func poke)
{
program_ptr = program;
for_stack_ptr = gosub_stack_ptr = 0;
index_free();
peek_function = peek;
poke_function = poke;
tokenizer_init(program);
ended = 0;
}
/*---------------------------------------------------------------------------*/
static void
accept(int token)
{
if(token != tokenizer_token()) {
DEBUG_PRINTF("Token not what was expected (expected %d, got %d)\n",
token, tokenizer_token());
tokenizer_error_print();
exit(1);
}
DEBUG_PRINTF("Expected %d, got it\n", token);
tokenizer_next();
}
/*---------------------------------------------------------------------------*/
static int
varfactor(void)
{
int r;
DEBUG_PRINTF("varfactor: obtaining %d from variable %d\n", variables[tokenizer_variable_num()], tokenizer_variable_num());
r = ubasic_get_variable(tokenizer_variable_num());
accept(TOKENIZER_VARIABLE);
return r;
}
/*---------------------------------------------------------------------------*/
static int
factor(void)
{
int r;
DEBUG_PRINTF("factor: token %d\n", tokenizer_token());
switch(tokenizer_token()) {
case TOKENIZER_NUMBER:
r = tokenizer_num();
DEBUG_PRINTF("factor: number %d\n", r);
accept(TOKENIZER_NUMBER);
break;
case TOKENIZER_LEFTPAREN:
accept(TOKENIZER_LEFTPAREN);
r = expr();
accept(TOKENIZER_RIGHTPAREN);
break;
default:
r = varfactor();
break;
}
return r;
}
/*---------------------------------------------------------------------------*/
static int
term(void)
{
int f1, f2;
int op;
f1 = factor();
op = tokenizer_token();
DEBUG_PRINTF("term: token %d\n", op);
while(op == TOKENIZER_ASTR ||
op == TOKENIZER_SLASH ||
op == TOKENIZER_MOD) {
tokenizer_next();
f2 = factor();
DEBUG_PRINTF("term: %d %d %d\n", f1, op, f2);
switch(op) {
case TOKENIZER_ASTR:
f1 = f1 * f2;
break;
case TOKENIZER_SLASH:
f1 = f1 / f2;
break;
case TOKENIZER_MOD:
f1 = f1 % f2;
break;
}
op = tokenizer_token();
}
DEBUG_PRINTF("term: %d\n", f1);
return f1;
}
/*---------------------------------------------------------------------------*/
static VARIABLE_TYPE
expr(void)
{
int t1, t2;
int op;
t1 = term();
op = tokenizer_token();
DEBUG_PRINTF("expr: token %d\n", op);
while(op == TOKENIZER_PLUS ||
op == TOKENIZER_MINUS ||
op == TOKENIZER_AND ||
op == TOKENIZER_OR) {
tokenizer_next();
t2 = term();
DEBUG_PRINTF("expr: %d %d %d\n", t1, op, t2);
switch(op) {
case TOKENIZER_PLUS:
t1 = t1 + t2;
break;
case TOKENIZER_MINUS:
t1 = t1 - t2;
break;
case TOKENIZER_AND:
t1 = t1 & t2;
break;
case TOKENIZER_OR:
t1 = t1 | t2;
break;
}
op = tokenizer_token();
}
DEBUG_PRINTF("expr: %d\n", t1);
return t1;
}
/*---------------------------------------------------------------------------*/
static int
relation(void)
{
int r1, r2;
int op;
r1 = expr();
op = tokenizer_token();
DEBUG_PRINTF("relation: token %d\n", op);
while(op == TOKENIZER_LT ||
op == TOKENIZER_GT ||
op == TOKENIZER_EQ) {
tokenizer_next();
r2 = expr();
DEBUG_PRINTF("relation: %d %d %d\n", r1, op, r2);
switch(op) {
case TOKENIZER_LT:
r1 = r1 < r2;
break;
case TOKENIZER_GT:
r1 = r1 > r2;
break;
case TOKENIZER_EQ:
r1 = r1 == r2;
break;
}
op = tokenizer_token();
}
return r1;
}
/*---------------------------------------------------------------------------*/
static void
index_free(void) {
if(line_index_head != NULL) {
line_index_current = line_index_head;
do {
DEBUG_PRINTF("Freeing index for line %d.\n", line_index_current);
line_index_head = line_index_current;
line_index_current = line_index_current->next;
free(line_index_head);
} while (line_index_current != NULL);
line_index_head = NULL;
}
}
/*---------------------------------------------------------------------------*/
static char const*
index_find(int linenum) {
struct line_index *lidx;
lidx = line_index_head;
#if DEBUG
int step = 0;
#endif
while(lidx != NULL && lidx->line_number != linenum) {
lidx = lidx->next;
#if DEBUG
if(lidx != NULL) {
DEBUG_PRINTF("index_find: Step %3d. Found index for line %d: %d.\n",
step, lidx->line_number,
lidx->program_text_position);
}
step++;
#endif
}
if(lidx != NULL && lidx->line_number == linenum) {
DEBUG_PRINTF("index_find: Returning index for line %d.\n", linenum);
return lidx->program_text_position;
}
DEBUG_PRINTF("index_find: Returning NULL.\n", linenum);
return NULL;
}
/*---------------------------------------------------------------------------*/
static void
index_add(int linenum, char const* sourcepos) {
if(line_index_head != NULL && index_find(linenum)) {
return;
}
struct line_index *new_lidx;
new_lidx = malloc(sizeof(struct line_index));
new_lidx->line_number = linenum;
new_lidx->program_text_position = sourcepos;
new_lidx->next = NULL;
if(line_index_head != NULL) {
line_index_current->next = new_lidx;
line_index_current = line_index_current->next;
} else {
line_index_current = new_lidx;
line_index_head = line_index_current;
}
DEBUG_PRINTF("index_add: Adding index for line %d: %d.\n", linenum,
sourcepos);
}
/*---------------------------------------------------------------------------*/
static void
jump_linenum_slow(int linenum)
{
tokenizer_init(program_ptr);
while(tokenizer_num() != linenum) {
do {
do {
tokenizer_next();
} while(tokenizer_token() != TOKENIZER_CR &&
tokenizer_token() != TOKENIZER_ENDOFINPUT);
if(tokenizer_token() == TOKENIZER_CR) {
tokenizer_next();
}
} while(tokenizer_token() != TOKENIZER_NUMBER);
DEBUG_PRINTF("jump_linenum_slow: Found line %d\n", tokenizer_num());
}
}
/*---------------------------------------------------------------------------*/
static void
jump_linenum(int linenum)
{
char const* pos = index_find(linenum);
if(pos != NULL) {
DEBUG_PRINTF("jump_linenum: Going to line %d.\n", linenum);
tokenizer_goto(pos);
} else {
/* We'll try to find a yet-unindexed line to jump to. */
DEBUG_PRINTF("jump_linenum: Calling jump_linenum_slow.\n", linenum);
jump_linenum_slow(linenum);
}
}
/*---------------------------------------------------------------------------*/
static void
goto_statement(void)
{
accept(TOKENIZER_GOTO);
jump_linenum(tokenizer_num());
}
/*---------------------------------------------------------------------------*/
static void
print_statement(void)
{
accept(TOKENIZER_PRINT);
do {
DEBUG_PRINTF("Print loop\n");
if(tokenizer_token() == TOKENIZER_STRING) {
tokenizer_string(string, sizeof(string));
printf("%s", string);
tokenizer_next();
} else if(tokenizer_token() == TOKENIZER_COMMA) {
printf(" ");
tokenizer_next();
} else if(tokenizer_token() == TOKENIZER_SEMICOLON) {
tokenizer_next();
} else if(tokenizer_token() == TOKENIZER_VARIABLE ||
tokenizer_token() == TOKENIZER_NUMBER) {
printf("%d", expr());
} else {
break;
}
} while(tokenizer_token() != TOKENIZER_CR &&
tokenizer_token() != TOKENIZER_ENDOFINPUT);
printf("\n");
DEBUG_PRINTF("End of print\n");
tokenizer_next();
}
/*---------------------------------------------------------------------------*/
static void
if_statement(void)
{
int r;
accept(TOKENIZER_IF);
r = relation();
DEBUG_PRINTF("if_statement: relation %d\n", r);
accept(TOKENIZER_THEN);
if(r) {
statement();
} else {
do {
tokenizer_next();
} while(tokenizer_token() != TOKENIZER_ELSE &&
tokenizer_token() != TOKENIZER_CR &&
tokenizer_token() != TOKENIZER_ENDOFINPUT);
if(tokenizer_token() == TOKENIZER_ELSE) {
tokenizer_next();
statement();
} else if(tokenizer_token() == TOKENIZER_CR) {
tokenizer_next();
}
}
}
/*---------------------------------------------------------------------------*/
static void
let_statement(void)
{
int var;
var = tokenizer_variable_num();
accept(TOKENIZER_VARIABLE);
accept(TOKENIZER_EQ);
ubasic_set_variable(var, expr());
DEBUG_PRINTF("let_statement: assign %d to %d\n", variables[var], var);
accept(TOKENIZER_CR);
}
/*---------------------------------------------------------------------------*/
static void
gosub_statement(void)
{
int linenum;
accept(TOKENIZER_GOSUB);
linenum = tokenizer_num();
accept(TOKENIZER_NUMBER);
accept(TOKENIZER_CR);
if(gosub_stack_ptr < MAX_GOSUB_STACK_DEPTH) {
gosub_stack[gosub_stack_ptr] = tokenizer_num();
gosub_stack_ptr++;
jump_linenum(linenum);
} else {
DEBUG_PRINTF("gosub_statement: gosub stack exhausted\n");
}
}
/*---------------------------------------------------------------------------*/
static void
return_statement(void)
{
accept(TOKENIZER_RETURN);
if(gosub_stack_ptr > 0) {
gosub_stack_ptr--;
jump_linenum(gosub_stack[gosub_stack_ptr]);
} else {
DEBUG_PRINTF("return_statement: non-matching return\n");
}
}
/*---------------------------------------------------------------------------*/
static void
next_statement(void)
{
int var;
accept(TOKENIZER_NEXT);
var = tokenizer_variable_num();
accept(TOKENIZER_VARIABLE);
if(for_stack_ptr > 0 &&
var == for_stack[for_stack_ptr - 1].for_variable) {
ubasic_set_variable(var,
ubasic_get_variable(var) + 1);
if(ubasic_get_variable(var) <= for_stack[for_stack_ptr - 1].to) {
jump_linenum(for_stack[for_stack_ptr - 1].line_after_for);
} else {
for_stack_ptr--;
accept(TOKENIZER_CR);
}
} else {
DEBUG_PRINTF("next_statement: non-matching next (expected %d, found %d)\n", for_stack[for_stack_ptr - 1].for_variable, var);
accept(TOKENIZER_CR);
}
}
/*---------------------------------------------------------------------------*/
static void
for_statement(void)
{
int for_variable, to;
accept(TOKENIZER_FOR);
for_variable = tokenizer_variable_num();
accept(TOKENIZER_VARIABLE);
accept(TOKENIZER_EQ);
ubasic_set_variable(for_variable, expr());
accept(TOKENIZER_TO);
to = expr();
accept(TOKENIZER_CR);
if(for_stack_ptr < MAX_FOR_STACK_DEPTH) {
for_stack[for_stack_ptr].line_after_for = tokenizer_num();
for_stack[for_stack_ptr].for_variable = for_variable;
for_stack[for_stack_ptr].to = to;
DEBUG_PRINTF("for_statement: new for, var %d to %d\n",
for_stack[for_stack_ptr].for_variable,
for_stack[for_stack_ptr].to);
for_stack_ptr++;
} else {
DEBUG_PRINTF("for_statement: for stack depth exceeded\n");
}
}
/*---------------------------------------------------------------------------*/
static void
peek_statement(void)
{
VARIABLE_TYPE peek_addr;
int var;
accept(TOKENIZER_PEEK);
peek_addr = expr();
accept(TOKENIZER_COMMA);
var = tokenizer_variable_num();
accept(TOKENIZER_VARIABLE);
accept(TOKENIZER_CR);
ubasic_set_variable(var, peek_function(peek_addr));
}
/*---------------------------------------------------------------------------*/
static void
poke_statement(void)
{
VARIABLE_TYPE poke_addr;
VARIABLE_TYPE value;
accept(TOKENIZER_POKE);
poke_addr = expr();
accept(TOKENIZER_COMMA);
value = expr();
accept(TOKENIZER_CR);
poke_function(poke_addr, value);
}
/*---------------------------------------------------------------------------*/
static void
end_statement(void)
{
accept(TOKENIZER_END);
ended = 1;
}
/*---------------------------------------------------------------------------*/
static void
statement(void)
{
int token;
token = tokenizer_token();
switch(token) {
case TOKENIZER_PRINT:
print_statement();
break;
case TOKENIZER_IF:
if_statement();
break;
case TOKENIZER_GOTO:
goto_statement();
break;
case TOKENIZER_GOSUB:
gosub_statement();
break;
case TOKENIZER_RETURN:
return_statement();
break;
case TOKENIZER_FOR:
for_statement();
break;
case TOKENIZER_PEEK:
peek_statement();
break;
case TOKENIZER_POKE:
poke_statement();
break;
case TOKENIZER_NEXT:
next_statement();
break;
case TOKENIZER_END:
end_statement();
break;
case TOKENIZER_LET:
accept(TOKENIZER_LET);
/* Fall through. */
case TOKENIZER_VARIABLE:
let_statement();
break;
default:
DEBUG_PRINTF("ubasic.c: statement(): not implemented %d\n", token);
exit(1);
}
}
/*---------------------------------------------------------------------------*/
static void
line_statement(void)
{
DEBUG_PRINTF("----------- Line number %d ---------\n", tokenizer_num());
index_add(tokenizer_num(), tokenizer_pos());
accept(TOKENIZER_NUMBER);
statement();
return;
}
/*---------------------------------------------------------------------------*/
void
ubasic_run(void)
{
if(tokenizer_finished()) {
DEBUG_PRINTF("uBASIC program finished\n");
return;
}
line_statement();
}
/*---------------------------------------------------------------------------*/
int
ubasic_finished(void)
{
return ended || tokenizer_finished();
}
/*---------------------------------------------------------------------------*/
void
ubasic_set_variable(int varnum, VARIABLE_TYPE value)
{
if(varnum >= 0 && varnum <= MAX_VARNUM) {
variables[varnum] = value;
}
}
/*---------------------------------------------------------------------------*/
VARIABLE_TYPE
ubasic_get_variable(int varnum)
{
if(varnum >= 0 && varnum <= MAX_VARNUM) {
return variables[varnum];
}
return 0;
}
/*---------------------------------------------------------------------------*/