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
|
/* SPDX-License-Identifier: Unlicense
*
* This program translates Sierra m68k assembly dialect to GNU AS m68k dialect.
*
* NOTE: Unicode is not supported, ASCII only.
*/
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifndef TRACE_LEX
#define TRACE_LEX 1
#endif
#define ERR 0
#define OK 1
#define CONTINUE 2
enum token_type {
TT_NONE = 0,
TT_SPACE,
TT_TAB,
TT_NEWLINE,
TT_DOT,
TT_COMMA,
TT_PLUS,
TT_MINUS,
TT_EQ,
TT_COLON,
TT_PERCENT,
TT_HASH,
TT_ASTERISK,
TT_STRING,
TT_IDENTIFIER,
TT_NUMDEC,
TT_NUMOCT,
TT_NUMHEX,
TT_LPAREN,
TT_RPAREN,
TT_COMMENT_ASTERISK,
TT_COMMENT_SEMICOLON,
};
struct token {
enum token_type type;
size_t offset;
size_t length;
};
enum lex_error {
LE_NONE = 0,
LE_SOME,
};
enum lex_state {
LS_FREE = 0,
LS_CR,
LS_SPACE,
LS_TAB,
LS_IDENTIFIER,
LS_NUMOCTHEX,
LS_NUMOCT,
LS_NUMHEX,
LS_NUMDEC,
LS_STRING,
LS_STRING_ESC,
LS_COMMENT_ASTERISK,
LS_COMMENT_SEMICOLON,
LS_ERROR,
LS_EOF,
};
struct line_pos_info {
unsigned long line_num;
unsigned long column_num;
unsigned long line_offset;
};
struct lex {
// State variables
enum lex_state state;
enum lex_error error;
size_t cursor;
size_t tok_offset;
size_t tokens_count;
bool inside_line;
// Input data buffer
FILE *input_stream;
char *input;
size_t input_size;
// Tokens table
FILE *tokbuf_stream;
struct token *tokbuf;
size_t tokbuf_size;
};
enum stmt_kind {
SK_NONE = 0,
SK_LABEL,
SK_INSTRUCTION,
SK_TEXT,
SK_DIR_FILE,
SK_DIR_TEXT,
SK_DIR_ALIGN,
SK_DIR_DEF_ENDEF,
SK_DIR_GLOBL,
SK_DIR_LINE,
};
enum opcode {
OPCODE_NONE,
OPCODE_NOP,
};
enum opsize {
OPSIZE_NONE = 0,
OPSIZE_S,
OPSIZE_B,
OPSIZE_W,
OPSIZE_L,
};
enum arg_kind {
ARG_NONE = 0,
ARG_DN,
ARG_AN,
ARG_AN_ADDR,
ARG_AN_ADDR_INCR,
ARG_AN_ADDR_DECR,
ARG_AN_ADDR_16,
ARG_AN_ADDR_8_XN,
ARG_ADDR_WORD,
ARG_ADDR_LONG,
ARG_ADDR_UNSPEC,
ARG_PC_ADDR_16,
ARG_PC_ADDR_8_XN,
ARG_IMMEDIATE,
};
struct arg_8_xn {
int8_t val;
int8_t an;
int8_t xi;
};
struct instruction {
enum opcode opcode;
enum opsize opsize;
enum arg_kind arg1_kind, arg2_kind;
union {
int32_t imm, addr;
struct arg_8_xn arg_8_xn; // For (d,An,Xi) and (d,PC,Xn)
} arg1, arg2;
};
struct def_endef {
size_t sym_id;
size_t tag_sym_id;
int32_t size;
int32_t storage_class;
int32_t type;
};
struct stmt {
enum stmt_kind type;
union {
struct instruction instruction;
int32_t align;
size_t globl_sym_id;
size_t file_sym_id;
};
size_t first_token, num_tokens; // Statement tokens span, may be NULL
size_t comment_token;
};
struct symbol {
size_t offset; // Byte offset in continuous null terminated symbol buffer
// Instead of strcmp every item in symtab we can compare hashes and get O(N)
// for search.
uint32_t hash;
};
enum pars_error {
PE_NONE = 0,
PE_LEX,
PE_SOME,
};
struct pars {
const struct lex *lex;
// State
size_t cur_tok_id;
enum pars_error error;
// Statement table
FILE *stmttab_stream;
struct stmt *stmttab;
size_t stmttab_size;
// Symbol table
FILE *symtab_stream;
struct sym *symtab;
size_t symtab_size;
// Symbol buffer for symbol table
FILE *symbuf_stream;
char *symbuf;
size_t symbuf_size;
};
struct assem {
const struct pars *pars;
};
const char *const g_escape_table[256] = {
"\\x00", "\\x01", "\\x02", "\\x03", "\\x04", "\\x05", "\\x06", "\\x07",
"\\x08", "\\t", "\\n", "\\x0b", "\\x0c", "\\r", "\\x0e", "\\x0f", "\\x10",
"\\x11", "\\x12", "\\x13", "\\x14", "\\x15", "\\x16", "\\x17", "\\x18",
"\\x19", "\\x1a", "\\x1b", "\\x1c", "\\x1d", "\\x1e", "\\x1f", " ", "!",
"\\\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0",
"1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "\\<", "=", "\\>", "?",
"@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\\\",
"]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"{", "|", "}", "~", "\\x7f", "\\x80", "\\x81", "\\x82", "\\x83", "\\x84",
"\\x85", "\\x86", "\\x87", "\\x88", "\\x89", "\\x8a", "\\x8b", "\\x8c",
"\\x8d", "\\x8e", "\\x8f", "\\x90", "\\x91", "\\x92", "\\x93", "\\x94",
"\\x95", "\\x96", "\\x97", "\\x98", "\\x99", "\\x9a", "\\x9b", "\\x9c",
"\\x9d", "\\x9e", "\\x9f", "\\xa0", "\\xa1", "\\xa2", "\\xa3", "\\xa4",
"\\xa5", "\\xa6", "\\xa7", "\\xa8", "\\xa9", "\\xaa", "\\xab", "\\xac",
"\\xad", "\\xae", "\\xaf", "\\xb0", "\\xb1", "\\xb2", "\\xb3", "\\xb4",
"\\xb5", "\\xb6", "\\xb7", "\\xb8", "\\xb9", "\\xba", "\\xbb", "\\xbc",
"\\xbd", "\\xbe", "\\xbf", "\\xc0", "\\xc1", "\\xc2", "\\xc3", "\\xc4",
"\\xc5", "\\xc6", "\\xc7", "\\xc8", "\\xc9", "\\xca", "\\xcb", "\\xcc",
"\\xcd", "\\xce", "\\xcf", "\\xd0", "\\xd1", "\\xd2", "\\xd3", "\\xd4",
"\\xd5", "\\xd6", "\\xd7", "\\xd8", "\\xd9", "\\xda", "\\xdb", "\\xdc",
"\\xdd", "\\xde", "\\xdf", "\\xe0", "\\xe1", "\\xe2", "\\xe3", "\\xe4",
"\\xe5", "\\xe6", "\\xe7", "\\xe8", "\\xe9", "\\xea", "\\xeb", "\\xec",
"\\xed", "\\xee", "\\xef", "\\xf0", "\\xf1", "\\xf2", "\\xf3", "\\xf4",
"\\xf5", "\\xf6", "\\xf7", "\\xf8", "\\xf9", "\\xfa", "\\xfb", "\\xfc",
"\\xfd", "\\xfe",
};
static bool should_be_escaped(const int c)
{
return c < ' ' || c == '"' || c == '\\' || c == '<' || c == '>' || c > '~';
}
static bool is_oct(const int c)
{
return c >= '0' && c <= '7';
}
static bool is_dec(const int c)
{
return c >= '0' && c <= '9';
}
static bool is_hex(const int c)
{
return is_dec(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
static bool is_alphabetic(const int c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
static bool is_alphanum(const int c)
{
return is_dec(c) || is_alphabetic(c);
}
static int printed_size(const char c)
{
if (c < ' ' || c > '~') {
return sizeof("\\x00")-1;
}
if (c == '"' || c == '\\') {
return sizeof("\\\\")-1;
}
return 1;
}
static int fprint_string_escaped(
const char *const str, const size_t length, FILE *const stream)
{
int written = 0;
for (size_t i = 0; i < length; i++, written += printed_size(str[i])) {
if (should_be_escaped(str[i])) {
fputs(g_escape_table[(unsigned char)str[i]], stream);
} else {
fputc(str[i], stream);
}
}
return written;
}
static const char *tok_kind_to_string(const enum token_type type)
{
switch (type) {
case TT_NONE: return "NONE";
case TT_SPACE: return "SPACE";
case TT_TAB: return "TAB";
case TT_NEWLINE: return "NEWLINE";
case TT_DOT: return "DOT";
case TT_COMMA: return "COMMA";
case TT_PLUS: return "PLUS";
case TT_MINUS: return "MINUS";
case TT_EQ: return "EQ";
case TT_COLON: return "COLON";
case TT_PERCENT: return "PERCENT";
case TT_HASH: return "HASH";
case TT_STRING: return "STRING";
case TT_ASTERISK: return "ASTERISK";
case TT_IDENTIFIER: return "IDENTIFIER";
case TT_NUMDEC: return "NUMDEC";
case TT_NUMOCT: return "NUMOCT";
case TT_NUMHEX: return "NUMHEX";
case TT_LPAREN: return "PARENL";
case TT_RPAREN: return "PARENR";
case TT_COMMENT_ASTERISK: return "COMMENT";
case TT_COMMENT_SEMICOLON: return "COMMENT";
}
assert(0);
return "UNKNOWN";
}
static int fprint_tok(const char *const input, struct token *token, FILE *const stream)
{
int res = fprintf(stream, "%s<", tok_kind_to_string(token->type));
if (res == -1) {
return -1;
}
int written = res;
res = fprint_string_escaped(input + token->offset, token->length, stream);
if (res == -1) {
return -1;
}
written += res;
res = fputs(">\n", stream);
if (res == -1) {
return -1;
}
written += res;
return written;
}
static int lex_init(struct lex *const self)
{
*self = (struct lex){
.input_stream = open_memstream(&self->input, &self->input_size),
.tokbuf_stream = open_memstream(
(char **)&self->tokbuf, &self->tokbuf_size),
};
assert(self->input_stream != NULL);
assert(self->tokbuf_stream != NULL);
return OK;
}
static int fwrite_token(const struct token *const token, FILE *const stream)
{
const int res = fwrite(token, sizeof *token, 1, stream);
assert(res == 1);
return res;
}
static void lex_yield_token(struct lex *const self, const struct token *const token)
{
self->inside_line = token->type != TT_NEWLINE;
fwrite_token(token, self->tokbuf_stream);
self->tokens_count++;
}
static const char *lex_state_error_string(
const enum lex_state state, const bool inside_line)
{
if (!inside_line) {
assert(state == LS_FREE);
return "'*', ';', '0', '[1-9]', '[a-zA-Z_]', ',', '.', '(', ')', '+', "
"'-', '=', ':', '%', '#', ' ', '\\t', '\\r', '\\n', '\\r\\n' "
"or EOF";
}
switch (state) {
case LS_FREE:
return "';', '0', '[1-9]', '[a-zA-Z_]', ',', '.', '(', ')', '+', "
"'-', '=', ':', '%', '#', ' ', '\\t', '\\r', '\\n', '\\r\\n' "
"or EOF";
case LS_NUMOCTHEX:
return "';', '[0-7]', [xX], ',', '.', '(', ')', '+', "
"'-', '=', ':', '%', '#', ' ', '\\t', '\\r', '\\n', '\\r\\n' "
"or EOF";
case LS_NUMOCT:
return "';', '[0-7]' , ',', '.', '(', ')', '+', "
"'-', '=', ':', '%', '#', ' ', '\\t', '\\r', '\\n', '\\r\\n' "
"or EOF";
case LS_NUMHEX:
return "';', '[0-9a-zA-Z]' , ',', '.', '(', ')', '+', "
"'-', '=', ':', '%', '#', ' ', '\\t', '\\r', '\\n', '\\r\\n' "
"or EOF";
case LS_NUMDEC:
return "';', '[0-9]' , ',', '.', '(', ')', '+', "
"'-', '=', ':', '%', '#', ' ', '\\t', '\\r', '\\n', '\\r\\n' "
"or EOF";
case LS_CR:
case LS_SPACE:
case LS_TAB:
case LS_IDENTIFIER:
case LS_STRING:
case LS_STRING_ESC:
case LS_COMMENT_ASTERISK:
case LS_COMMENT_SEMICOLON:
case LS_ERROR:
case LS_EOF:
assert(0);
break;
}
return "???";
}
static struct line_pos_info lex_get_line_pos_info(const struct lex *const self)
{
struct line_pos_info l = {0, 0, 0};
bool cr = false;
// `input` is null terminated, that's why we subtract 1 here
for (size_t i = 0; i < self->input_size - 1; i++) {
const char c = self->input[i];
if (c == '\r') {
cr = true;
l.line_offset = i + 1;
l.line_num++;
l.column_num = 0;
} else if (c == '\n') {
cr = false;
l.line_offset = i + 1;
if (!cr) {
l.line_num++;
}
l.column_num = 0;
} else {
cr = false;
l.column_num++;
}
}
return l;
}
static int lex_yield_error(struct lex *const self, const int c)
{
fflush(self->input_stream);
const struct line_pos_info l = lex_get_line_pos_info(self);
{
// Read out the rest of the line
int c;
do {
c = getc(stdin);
const char c_char = (c == EOF) ? 0 : c;
fwrite(&c_char, sizeof c_char, 1, self->input_stream);
} while (c != EOF && c != '\n' && c != '\r');
fflush(self->input_stream);
}
fprintf(
stderr,
"<stdin>:%lu:%lu: lexing error: expected %s, found '%c'\n",
l.line_num + 1,
l.column_num + 1,
lex_state_error_string(self->state, self->inside_line),
c);
fprintf(stderr, "%5lu | %s\n", l.line_num + 1, self->input + l.line_offset);
fputs(" | ", stderr);
for (size_t i = 0; i < l.column_num; i++) {
if (self->input[l.line_offset + i] == '\t') {
fputc('\t', stderr);
} else {
fputc(' ', stderr);
}
}
fputs("^\n", stderr);
self->state = LS_ERROR;
return ERR;
}
static int lex_handle_next(struct lex *const self, const int c)
{
switch (self->state) {
case LS_FREE:
if (is_alphabetic(c) || c == '_') {
self->inside_line = false;
self->tok_offset = self->cursor;
self->state = LS_IDENTIFIER;
} else if (c == '0') {
self->tok_offset = self->cursor;
self->state = LS_NUMOCTHEX;
} else if (is_dec(c)) {
self->tok_offset = self->cursor;
self->state = LS_NUMDEC;
} else if (c == '"') {
self->tok_offset = self->cursor;
self->state = LS_STRING;
} else if (c == ';') {
self->tok_offset = self->cursor;
self->state = LS_COMMENT_SEMICOLON;
} else if (c == '*') {
if (self->inside_line) {
return lex_yield_error(self, c);
}
self->tok_offset = self->cursor;
self->state = LS_COMMENT_ASTERISK;
} else if (c == ',') {
lex_yield_token(self, &(struct token){TT_COMMA, self->cursor, 1});
} else if (c == '.') {
lex_yield_token(self, &(struct token){TT_DOT, self->cursor, 1});
} else if (c == '(') {
lex_yield_token(self, &(struct token){TT_LPAREN, self->cursor, 1});
} else if (c == ')') {
lex_yield_token(self, &(struct token){TT_RPAREN, self->cursor, 1});
} else if (c == '+') {
lex_yield_token(self, &(struct token){TT_PLUS, self->cursor, 1});
} else if (c == '-') {
lex_yield_token(self, &(struct token){TT_MINUS, self->cursor, 1});
} else if (c == '=') {
lex_yield_token(self, &(struct token){TT_EQ, self->cursor, 1});
} else if (c == ':') {
lex_yield_token(self, &(struct token){TT_COLON, self->cursor, 1});
} else if (c == '%') {
lex_yield_token(self, &(struct token){TT_PERCENT, self->cursor, 1});
} else if (c == '#') {
lex_yield_token(self, &(struct token){TT_HASH, self->cursor, 1});
} else if (c == '\r') {
self->tok_offset = self->cursor;
self->state = LS_CR;
} else if (c == '\n') {
lex_yield_token(self, &(struct token){TT_NEWLINE, self->cursor, 1});
} else if (c == ' ') {
self->tok_offset = self->cursor;
self->state = LS_SPACE;
} else if (c == '\t') {
self->tok_offset = self->cursor;
self->state = LS_TAB;
} else if (c == EOF) {
self->state = LS_EOF;
} else {
return lex_yield_error(self, c);
}
break;
case LS_CR: // Accumulate CRLF into single token
{
const size_t size = c == '\n' ? 2 : 1; // 2 for CRLF, 1 for just CR
const struct token token = {TT_NEWLINE, self->tok_offset, size};
lex_yield_token(self, &token);
self->state = LS_FREE;
if (c != '\n') {
// It is just CR, handle this char in LS_FREE state then
return lex_handle_next(self, c);
}
}
break;
case LS_SPACE: // Accumulate multiple spaces into single token
if (c != ' ') {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_SPACE, self->tok_offset, length};
lex_yield_token(self, &token);
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_TAB: // Accumulate multiple tabs into single token
if (c != '\t') {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_TAB, self->tok_offset, length};
lex_yield_token(self, &token);
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_IDENTIFIER:
if (!is_alphanum(c) && c != '_') {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_IDENTIFIER, self->tok_offset, length};
lex_yield_token(self, &token);
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_NUMOCTHEX:
if (c == 'x' || c == 'X') {
self->state = LS_NUMHEX;
} else if (is_oct(c)) {
self->state = LS_NUMOCT;
} else if (is_alphabetic(c) || c == '_') {
return lex_yield_error(self, c);
} else {
assert((self->cursor - self->tok_offset) == 1);
const struct token token = {TT_NUMDEC, self->tok_offset, 1};
lex_yield_token(self, &token);
// It was just zero, handle this char in LS_FREE state then
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_NUMOCT:
if (is_alphabetic(c) || c == '_') {
return lex_yield_error(self, c);
} else if (!is_oct(c)) {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_NUMOCT, self->tok_offset, length};
lex_yield_token(self, &token);
// This token is finished, handle this char in LS_FREE state
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_NUMHEX:
if (is_hex(c)) {
// Keep calm
} else if (is_alphabetic(c) || c == '_') {
// Panik!
return lex_yield_error(self, c);
} else {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_NUMHEX, self->tok_offset, length};
lex_yield_token(self, &token);
// This token is finished, handle this char in LS_FREE state
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_NUMDEC:
if (is_alphabetic(c) || c == '_') {
return lex_yield_error(self, c);
} else if (!is_dec(c)) {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_NUMDEC, self->tok_offset, length};
lex_yield_token(self, &token);
// This token is finished, handle this char in LS_FREE state
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_STRING:
if (c == '\\') {
self->state = LS_STRING_ESC;
} else if (c == '"') {
const size_t length = self->cursor - self->tok_offset + 1;
const struct token token = {TT_STRING, self->tok_offset, length};
lex_yield_token(self, &token);
// This token is finished
self->state = LS_FREE;
}
break;
case LS_STRING_ESC:
self->state = LS_STRING;
break;
case LS_COMMENT_ASTERISK:
if (c == '\r' || c == '\n') {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_COMMENT_ASTERISK, self->tok_offset, length};
lex_yield_token(self, &token);
// This token is finished, handle this char in LS_FREE state
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_COMMENT_SEMICOLON:
if (c == '\r' || c == '\n') {
const size_t length = self->cursor - self->tok_offset;
const struct token token = {TT_COMMENT_SEMICOLON, self->tok_offset, length};
lex_yield_token(self, &token);
// This token is finished, handle this char in LS_FREE state
self->state = LS_FREE;
return lex_handle_next(self, c);
}
break;
case LS_ERROR:
return ERR;
case LS_EOF:
assert(0);
}
return CONTINUE;
}
/** Advance lexer to produce new token.
* \returns EOF if end of file reached.
* \returns ERR if error encountered and lexing cannot continue.
* \returns OK has one or more new tokens parsed.
*/
static int lex_next(struct lex *const self, FILE *const stream)
{
for (;; self->cursor++) {
const int c = fgetc(stream);
const char c_char = (c == EOF) ? 0 : c;
fwrite(&c_char, sizeof c_char, 1, self->input_stream);
const int ret = lex_handle_next(self, c);
if (OK == ret) {
return OK;
} else if (ERR == ret) {
// TODO handle errors
return ERR;
}
if (c == EOF) {
break;
}
}
return EOF;
}
/** Run lexer until the end of the input reached
* \returns OK if lexing finished successfully
* \returns ERR if error encountered and lexing cannot continue.
*/
static int lex_run(struct lex *const self, FILE *const stream)
{
int res;
do {
res = lex_next(self, stream);
if (res == OK) {
res = 0;
} else if (res == ERR) {
return ERR;
}
} while (res != EOF);
fflush(self->input_stream);
fflush(self->tokbuf_stream);
return OK;
}
static void lex_destroy(struct lex *const self)
{
fclose(self->input_stream);
free(self->input);
fclose(self->tokbuf_stream);
free(self->tokbuf);
}
static int pars_init(struct pars *const self, const struct lex *const lex)
{
*self = (struct pars){
.lex = lex,
.stmttab_stream = open_memstream(
(char **)&self->stmttab, &self->stmttab_size),
.symbuf_stream = open_memstream(&self->symbuf, &self->symbuf_size),
};
assert(self->stmttab_stream != NULL);
assert(self->symbuf_stream != NULL);
return OK;
}
/** Run parser until the end of the input reached
* \returns OK if parsing finished successfully
* \returns ERR if error encountered and parsing cannot continue.
*/
static int pars_run(struct pars *const self)
{
(void) self;
// TODO
return OK;
}
static void pars_destroy(struct pars *const self)
{
fclose(self->stmttab_stream);
free(self->stmttab);
fclose(self->symbuf_stream);
free(self->symbuf);
}
static int assem_init(struct assem *const self, const struct pars *const pars)
{
*self = (struct assem){
.pars = pars,
};
return OK;
}
static int assem_resolve(struct assem *const self)
{
(void) self;
return OK;
}
static int assem_emit(struct assem *const self, FILE *const stream)
{
if (TRACE_LEX) {
const struct lex *const lex = self->pars->lex;
for (size_t i = 1; i < lex->tokbuf_size / (sizeof *lex->tokbuf); i++) {
fprint_tok(lex->input, &lex->tokbuf[i], stream);
}
}
return OK;
}
static void assem_destroy(struct assem *const self)
{
(void) self;
}
int main(const int argc, char *const argv[])
{
// No fucks given about arguments for now
(void)argc;
(void)argv;
struct lex lex;
struct pars pars;
if (OK != lex_init(&lex)) {
return EXIT_FAILURE;
}
// Tokenize assembly program text
if (OK != lex_run(&lex, stdin)) {
lex_destroy(&lex);
return EXIT_FAILURE;
}
// Parser needs final lexer state to access parsed tokens and input data
if (OK != pars_init(&pars, &lex)) {
lex_destroy(&lex);
return EXIT_FAILURE;
}
// Parse assembly program text
if (OK != pars_run(&pars)) {
pars_destroy(&pars);
lex_destroy(&lex);
return EXIT_FAILURE;
}
struct assem assem;
// Allocate and populate code table and metadata table from parsed data.
// Assembler needs parser's and lexer's final state to access parsed
// structure, tokens and input.
if (OK != assem_init(&assem, &pars)) {
pars_destroy(&pars);
lex_destroy(&lex);
return EXIT_FAILURE;
}
// Resolve all ambiguities
if (OK != assem_resolve(&assem)) {
assem_destroy(&assem);
pars_destroy(&pars);
lex_destroy(&lex);
return EXIT_FAILURE;
}
// Emit unambiguous assembly language program text for specified dialect
// (currently m68k GNU AS only is supported)
if (OK != assem_emit(&assem, stdout)) {
assem_destroy(&assem);
pars_destroy(&pars);
lex_destroy(&lex);
return EXIT_FAILURE;
}
assem_destroy(&assem);
pars_destroy(&pars);
lex_destroy(&lex);
}
|