forked from nssilva/TinyBASIC-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbc.c
913 lines (794 loc) · 17 KB
/
tbc.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
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
/*
Luiji's Tiny BASIC Compiler
Copyright (C) 2012 Entertaining Software, Inc.
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/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#ifdef __GNUC__
# define PRINTF(f, va) __attribute__ ((__format__ (__printf__, f, va)))
#else
# define PRINTF(f, va) /* unsupported */
#endif
#if defined (__STDC__) || defined (_AIX) \
|| (defined (__mips) && defined (_SYSTYPE_SVR4)) \
|| defined (WIN32) || defined (__cplusplus)
# define PARAMS(protos) protos
#else
# define PARAMS(protos) (/* unsupported */)
#endif
enum
{
T_PRINT,
T_IF,
T_GOTO,
T_INPUT,
T_LET,
T_GOSUB,
T_RETURN,
T_REM,
T_RUN,
T_END
};
enum
{
O_EQUAL,
O_NOT_EQUAL,
O_LESS,
O_MORE,
O_LESS_OR_EQUAL,
O_MORE_OR_EQUAL
};
static char look = 0;
static int line = 1;
static int use_print_i = 0;
static int use_print_s = 0;
static int use_print_t = 0;
static int use_print_n = 0;
static int use_input_i = 0;
static int vars_used[26] = { 0 };
static char ** strings = NULL;
static int string_count = 0;
static const char *program_name = "<?>";
static const char *input_name = NULL;
static const char *output_name = NULL;
static void shutdown PARAMS ((void));
static void *xmalloc PARAMS ((size_t size));
static void *xrealloc PARAMS ((void *original, size_t size));
static void xfree PARAMS ((void *pointer));
static void parse_opts PARAMS ((int argc, char **argv));
static void print_help PARAMS ((void));
static void print_version PARAMS ((void));
static void begin PARAMS ((void));
static void finish PARAMS ((void));
static void error PARAMS ((const char *format, ...)) PRINTF (1, 2);
static void eat_blanks PARAMS ((void));
static void match PARAMS ((int what));
static void get_char PARAMS ((void));
static char get_name PARAMS ((void));
static int get_num PARAMS ((void));
static int get_keyword PARAMS ((void));
static int do_line PARAMS ((void));
static void do_statement PARAMS ((void));
static void do_print PARAMS ((void));
static void do_print_item PARAMS ((void));
static void do_if PARAMS ((void));
static void do_goto PARAMS ((void));
static void do_input PARAMS ((void));
static void do_let PARAMS ((void));
static void do_gosub PARAMS ((void));
static void do_return PARAMS ((void));
static void do_rem PARAMS ((void));
static void do_end PARAMS ((void));
static void do_expression PARAMS ((void));
static void do_term PARAMS ((void));
static void do_factor PARAMS ((void));
static void do_string PARAMS ((void));
int
main (argc, argv)
int argc;
char **argv;
{
atexit (shutdown);
parse_opts (argc, argv);
begin ();
while (do_line ());
finish ();
return EXIT_SUCCESS;
}
static void
shutdown ()
{
int i;
for (i = 0; i < string_count; ++i)
xfree (strings[i]);
xfree (strings);
}
static void *
xmalloc (size)
size_t size;
{
void *data = malloc (size);
if (!data)
{
fprintf (stderr, "%s: failed to allocate memory\n", program_name);
exit (EXIT_FAILURE);
}
return data;
}
static void *
xrealloc (original, size)
void *original;
size_t size;
{
void *data = realloc (original, size);
if (!data)
{
fprintf (stderr, "%s: failed to allocate memory\n", program_name);
exit (EXIT_FAILURE);
}
return data;
}
static void
xfree (pointer)
void *pointer;
{
if (pointer)
free (pointer);
}
static void
parse_opts (argc, argv)
int argc;
char **argv;
{
extern char *optarg;
extern int optind;
int getopt PARAMS ((int argc, char *const *argv, const char *optstring));
int was_error = 0, opt;
program_name = strrchr (argv[0], '/');
if (program_name)
++program_name;
else
program_name = argv[0];
while ((opt = getopt (argc, argv, "hVo:")) != -1)
switch (opt)
{
case 'h':
print_help ();
exit (EXIT_SUCCESS);
case 'V':
print_version ();
exit (EXIT_SUCCESS);
case 'o':
output_name = optarg;
break;
case '?':
was_error = 1;
break;
default:
assert (0);
break;
}
if (was_error)
exit (EXIT_FAILURE);
if (optind < argc)
input_name = argv[optind];
for (++optind; optind < argc; ++optind)
fprintf (stderr, "%s: ignoring argument '%s'\n", program_name,
argv[optind]);
if (input_name)
{
if (!freopen (input_name, "r", stdin))
{
fprintf (stderr, "%s: failed to open for reading\n", input_name);
exit (EXIT_FAILURE);
}
}
else
{
input_name = "<stdin>";
}
if (output_name)
{
if (!freopen (output_name, "w", stdout))
{
fprintf (stderr, "%s: failed to open for writing\n", output_name);
exit (EXIT_FAILURE);
}
}
else
{
output_name = "<stdout>";
}
}
static void
print_help ()
{
printf ("Usage: %s [-o OUTPUT] [INPUT]\n", program_name);
puts ("");
puts ("Compiles Tiny BASIC programs into Netwide Assembler programs.");
puts ("");
puts ("Options:");
puts (" -o set output file name");
puts (" -h print this help message and exit");
puts (" -V print this version message adn exit");
puts ("");
puts ("Input and output defaults to stdin and stdout.");
puts ("");
puts ("Report bugs to: [email protected]");
puts ("tbc home page: <https://github.com/Luiji/TinyBASIC>");
}
static void
print_version ()
{
puts ("Luiji's Tiny BASIC Compiler 0.0.0");
puts ("Copyright (C) 2012 Entertaining Software, Inc.");
puts ("License GPLv3+: GNU GPL version 3 or later \
<http://gnu.org/licenses/gpl.html>");
puts ("This is free software: you are free to change and redistribute it.");
puts ("There is NO WARRANTY, to the extent permitted by law.");
}
static void
begin ()
{
puts ("; Generated by Luiji's Tiny BASIC Compiler");
puts ("");
puts ("section .text");
puts ("");
puts ("\torg 0x100");
get_char ();
}
static void
finish ()
{
int i;
puts ("");
puts ("\t; exit to operating system");
puts ("\t; program falls through to here when there is no explicit end");
puts ("exit:");
puts ("\tmov ax, 0x4c00");
puts ("\tint 0x21");
puts ("");
if (use_print_i)
{
puts ("\t; print an integer to the terminal");
puts ("print_i:");
puts ("\t; number of digits pushed and divisor for ax");
puts ("\tmov cx, 0");
puts ("\tmov bx, 10");
puts ("\t; print out a '-' if the number is negative");
puts ("\tcmp ax, 0");
puts ("\tjge print_i_loop_1");
puts ("\tpush ax");
puts ("\tmov ah, 0x2");
puts ("\tmov dl, 0x2d");
puts ("\tint 0x21");
puts ("\tpop ax");
puts ("\tneg ax");
puts ("\t; push the asciified digits onto the stack");
puts ("\t; (direct output would be backwards)");
puts ("print_i_loop_1:");
puts ("\txor dx, dx");
puts ("\tdiv bx");
puts ("\tadd dl, 0x30");
puts ("\tpush dx");
puts ("\tinc cx");
puts ("\tcmp ax, 0");
puts ("\tjnz print_i_loop_1");
puts ("\t; output the digits in the correct order");
puts ("print_i_loop_2:");
puts ("\tmov ah, 2");
puts ("\tpop dx");
puts ("\tint 21h");
puts ("\tloop print_i_loop_2");
puts ("\tret");
}
else
{
puts ("; print_i excluded");
}
puts ("");
if (use_print_s)
{
puts ("\t; print string to terminal");
puts ("print_s:");
puts ("\tmov dx, ax");
puts ("\tmov ah, 0x9");
puts ("\tint 0x21");
puts ("\tret");
}
else
{
puts ("; print_s excluded");
}
puts ("");
if (use_print_t)
{
puts ("\t; print a tab to the terminal");
puts ("print_t:");
puts ("\tmov ah, 0x2");
puts ("\tmov dl, 0x9");
puts ("\tint 0x21");
puts ("\tret");
}
else
{
puts ("; print_t excluded");
}
puts ("");
if (use_print_n)
{
puts ("\t; print a newline to the terminal");
puts ("print_n:");
puts ("\tmov ah, 0x2");
puts ("\tmov dl, 13");
puts ("\tint 0x21");
puts ("\tmov ah, 0x2");
puts ("\tmov dl, 10");
puts ("\tint 0x21");
puts ("\tret");
}
else
{
puts ("; print_n excluded");
}
puts ("");
if (use_input_i)
{
puts ("\t; read a number from the terminal");
puts ("input_i:");
puts ("\t; return value is stored in the stack; bx = 10 for mul");
puts ("\tpush 0");
puts ("\tmov bx, 10");
puts ("\t; read in the digits");
puts ("input_i_loop:");
puts ("\tmov ah, 0x1");
puts ("\tint 0x21");
puts ("\tcmp al, 0xd");
puts ("\tje input_i_done");
puts ("\tmov ah, 0");
puts ("\tmov cx, ax");
puts ("\tpop ax");
puts ("\txor dx, dx");
puts ("\tmul bx");
puts ("\tsub cx, 0x30");
puts ("\tadd ax, cx");
puts ("\tpush ax");
puts ("\tjmp input_i_loop");
puts ("input_i_done:");
puts ("\tpop ax");
puts ("\tret");
}
else
{
puts ("; input_i excluded");
}
puts ("");
puts ("section .bss");
puts ("");
for (i = 0; i < 26; ++i)
if (vars_used[i])
printf ("\tv%c: resw 1\n", 'a' + i);
puts ("");
puts ("section .text");
puts ("");
for (i = 0; i < string_count; ++i)
printf ("\ts%i: db \"%s\", '$'\n", i, strings[i]);
puts ("");
puts ("; Task Completed -- Assemble with NASM or YASM");
}
static void
error (format)
const char *format;
/* ... */
{
va_list args;
fprintf (stderr, "%s:%i: error: ", input_name, line);
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
fputs ("\n", stderr);
exit (EXIT_FAILURE);
}
static void
eat_blanks ()
{
while (look == ' ' || look == '\t')
get_char ();
}
static void
match (what)
int what;
{
if (look == what)
get_char ();
else
error ("expected '%c' got '%c'", what, look);
eat_blanks ();
}
static void
get_char ()
{
look = fgetc (stdin);
}
static char
get_name ()
{
char name = tolower (look);
if (isalpha (name))
get_char ();
else
error ("expected identifier got '%c'", name);
vars_used[name - 'a'] = 1;
eat_blanks ();
return name;
}
static int
get_num ()
{
int result = 0;
if (isdigit (look))
for (; isdigit (look); get_char ())
{
result *= 10;
result += look - '0';
}
else
error ("expected integer got '%c'", look);
eat_blanks ();
return result;
}
static int
get_keyword ()
{
int i;
char token[7];
if (!isalpha (look))
error ("expected keyword got '%c'", look);
for (i = 0; i < (int) sizeof (token) - 1 && isalpha (look); ++i)
{
token[i] = tolower (look);
get_char ();
}
token[i] = 0;
if (!strcmp (token, "print"))
i = T_PRINT;
else if (!strcmp (token, "if"))
i = T_IF;
else if (!strcmp (token, "goto"))
i = T_GOTO;
else if (!strcmp (token, "input"))
i = T_INPUT;
else if (!strcmp (token, "let"))
i = T_LET;
else if (!strcmp (token, "gosub"))
i = T_GOSUB;
else if (!strcmp (token, "return"))
i = T_RETURN;
else if (!strcmp (token, "rem"))
i = T_REM;
else if (!strcmp (token, "run"))
i = T_RUN;
else if (!strcmp (token, "end"))
i = T_END;
else
error ("expected keyword got '%s'", token);
eat_blanks ();
return i;
}
static int
do_line ()
{
while (look == '\n')
{
++line;
get_char ();
}
if (feof (stdin))
return 0;
if (tolower (look) == 'r')
{
get_char ();
if (tolower (look) == 'u')
{
get_char ();
if (tolower (look) == 'n')
return 0;
else
error ("expected statement or 'run'");
}
ungetc (look, stdin);
look = 'r';
}
printf ("\n\t; debug line %i\n", line);
do_statement ();
if (look != '\n')
error ("expected newline got '%c'", look);
else
get_char ();
++line;
return 1;
}
static void
do_statement ()
{
if (isdigit (look))
printf ("l%i:\n", get_num ());
switch (get_keyword ())
{
case T_PRINT: do_print (); break;
case T_IF: do_if (); break;
case T_GOTO: do_goto (); break;
case T_INPUT: do_input (); break;
case T_LET: do_let (); break;
case T_GOSUB: do_gosub (); break;
case T_RETURN: do_return (); break;
case T_REM: do_rem (); break;
case T_END: do_end (); break;
default: assert (0); break;
}
}
static void
do_print ()
{
use_print_n = 1;
do_print_item ();
for (;;)
{
if (look == ',')
{
match (',');
use_print_t = 1;
puts ("\tcall print_t");
}
else if (look == ';')
{
match (';');
}
else
{
break;
}
do_print_item ();
}
puts ("\tcall print_n");
}
static void
do_print_item ()
{
if (look == '"')
{
use_print_s = 1;
do_string ();
puts ("\tcall print_s");
}
else
{
use_print_i = 1;
do_expression ();
puts ("\tcall print_i");
}
}
static void
do_if ()
{
int op;
do_expression ();
puts ("\tmov dx, ax");
if (look == '=')
{
match ('=');
if (look == '<')
{
match ('<');
op = O_LESS_OR_EQUAL;
}
else if (look == '>')
{
match ('>');
op = O_MORE_OR_EQUAL;
}
else
{
op = O_EQUAL;
}
}
else if (look == '<')
{
match ('<');
if (look == '=')
{
match ('=');
op = O_LESS_OR_EQUAL;
}
else if (look == '>')
{
match ('>');
op = O_NOT_EQUAL;
}
else
{
op = O_LESS;
}
}
else if (look == '>')
{
match ('>');
if (look == '=')
{
match ('=');
op = O_MORE_OR_EQUAL;
}
else if (look == '<')
{
match ('<');
op = O_NOT_EQUAL;
}
else
{
op = O_MORE;
}
}
else
{
error ("expected =, <>, ><, <=, =<, >= or => got '%c'", look);
}
do_expression ();
fputs ("\tcmp dx, ax\n\tj", stdout);
/* note inversal of operators */
if (op == O_EQUAL)
fputs ("ne", stdout);
else if (op == O_NOT_EQUAL)
fputs ("e", stdout);
else if (op == O_LESS)
fputs ("ge", stdout);
else if (op == O_MORE)
fputs ("le", stdout);
else if (op == O_LESS_OR_EQUAL)
fputs ("g", stdout);
else if (op == O_MORE_OR_EQUAL)
fputs ("l", stdout);
printf (" i%i\n", line);
do_statement ();
printf ("i%i:\n", line);
}
static void
do_goto ()
{
printf ("\tjmp l%i\n", get_num ());
}
static void
do_input ()
{
use_input_i = 1;
do
printf ("\tcall input_i\n\tmov [v%c], ax\n", get_name ());
while (look == ',');
}
static void
do_let ()
{
char name = get_name ();
match ('=');
do_expression ();
printf ("\tmov [v%c], ax\n", name);
}
static void
do_gosub ()
{
printf ("\tcall l%i\n", get_num ());
}
static void
do_return ()
{
puts ("\tret");
}
static void
do_rem ()
{
while (look != '\n')
get_char ();
}
static void
do_end ()
{
puts ("\tjmp exit");
}
static void
do_expression ()
{
if (look == '+' || look == '-')
puts ("\txor ax, ax");
else
do_term ();
while (look == '+' || look == '-')
{
int op = look;
puts ("\tpush ax");
match (look);
do_term ();
puts ("\tpop bx");
if (op == '+')
puts ("\tadd ax, bx");
else if (op == '-')
puts ("\tsub ax, bx\n\tneg ax");
}
}
static void
do_term ()
{
do_factor ();
while (look == '*' || look == '/')
{
int op = look;
puts ("\tpush ax");
match (look);
do_factor ();
puts ("\tpop bx");
puts ("\txor dx, dx");
if (op == '*')
puts ("\timul bx");
else if (op == '/')
puts ("\txchg ax, bx\n\tidiv bx");
}
}
static void
do_factor ()
{
if (look == '(')
{
match ('(');
do_expression ();
match (')');
}
else if (isalpha (look))
{
printf ("\tmov ax, [v%c]\n", get_name ());
}
else
{
printf ("\tmov ax, %i\n", get_num ());
}
}
static void
do_string ()
{
int i;
size_t string_size = 0;
char *string;
string = xmalloc (1);
string[0] = 0;
for (get_char (); look != '"'; get_char ())
{
++string_size;
string = xrealloc (string, string_size + 1);
string[string_size - 1] = look;
string[string_size] = 0;
}
get_char ();
for (i = 0; i < string_count; ++i)
if (!strcmp (strings[i], string))
{
printf ("\tmov ax, s%i\n", i);
return;
}
++string_count;
strings = xrealloc (strings, string_count * sizeof (char *));
strings[string_count - 1] = string;
printf ("\tmov ax, s%i\n", i);
}
/* vim:set ts=8 sts=2 sw=2 noet: */