-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapper.y
374 lines (333 loc) · 7.82 KB
/
mapper.y
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
%{
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "lowl.h"
#include "emitter.h"
/* Checks for optional characters. */
#define _CHECK_CHAR(_str) \
if ( strlen((_str)) != 1 ) { \
yyerror("expected literal character"); \
exit(-1); \
}
#define _CHECK_CH1(_str, _c1) \
if ( strlen((_str)) != 1 ) { \
yyerror("Expected character"); \
exit(-1); \
} \
if ( *(_str) != (_c1) ) { \
yyerror("Wrong character in statement");\
exit(-1); \
}
#define _CHECK_CH2(_str, _c1, _c2) \
if ( strlen((_str)) != 1 ) { \
yyerror("Expected character"); \
exit(-1); \
} \
if ( *(_str) != (_c1) && *(_str) != (_c2) ) { \
yyerror("Wrong character in statement");\
exit(-1); \
}
#define _CHECK_CH3(_str, _c1, _c2, _c3) \
if ( strlen((_str)) != 1 ) { \
yyerror("Expected character"); \
exit(-1); \
} \
if ( *(_str) != (_c1) \
&& *(_str) != (_c2) \
&& *(_str) != (_c3) ) { \
yyerror("Wrong character in statement");\
exit(-1); \
}
#define _CHECK_CHSTR(_str, _str2, _c) \
if ( strlen((_str)) != 1 \
&& strcmp((_str), (_str2)) ) { \
yyerror("Wrong symbol in statement"); \
exit(-1); \
} \
if ( *(_str) != _c ) { \
yyerror("Wrong character in statement");\
exit(-1); \
}
/*
* Special macro for code statements.
* This helps keeping track of the pseudo-PC.
* CEMIT is used for continuation instruction, while
* SEMIT is used for stopping instruction.
*/
#define CEMIT(_f) { emit_##_f; emit_newpc(0); }
#define SEMIT(_f) { emit_##_f; emit_newpc(1); }
extern int yylineno;
void yyerror(char *s);
int yylex();
void add_idsym(char *sym, uintptr_t val);
%}
%union {
char chr;
char *str;
uintptr_t num;
}
%token <str> LABEL
%token <str> SYMBOL
%token <str> STRING
%token <num> NUMBER
%token DCL EQU IDENT
%token CON NCH STR
%token LAV LBV LAL LCN LAM LCM LAI LCI LAA STV STI
%token CLEAR AAV ABV AAL SAV SBV SAL SBL MULTL BUMP ANDV ANDL
%token CAV CAL CCL CCN CAI CCI
%token SUBR EXIT GOSUB GOADD CSS
%token GO GOEQ GONE GOGE GOGR GOLE GOLT GOPC GOND
%token FSTK BSTK CFSTK UNSTK FMOVE BMOVE
%token MESS NB
%token PRGST PRGEN
%token ALIGN
%token LCH LNM LICH LHV
%token OF
%token ',' '+' '-' '*' '(' ')'
%token NLREP SPREP TABREP QUTREP
%token EOL
/* ML/I extensions tokens. */
%token HASH THASH RL WTHS STOPCD SLREP LINKR LINKB ORL
%type <chr> ml1_charname
%type <str> v
%type <num> nof distance signnum of_macro expr parnm
%type <chr> rx dc px ax ex ctx charname
%left '+' '-'
%left '*'
%%
program:
program command
|
;
command:
table_label table_statement EOL
| label code_statement EOL
| var_statement EOL
| EOL { emit_eol(); }
;
table_label:
LABEL { emit_table_label($1); }
|
;
label:
LABEL { emit_label($1); }
|
;
var_statement:
DCL v { emit_dcl($2); }
| EQU v ',' v { emit_equ($2, $4); }
| IDENT v ',' NUMBER {
add_idsym($2, $4);
emit_ident($2, $4);
}
;
table_statement:
CON nof { emit_con($2); }
| NCH charname { emit_nch($2); }
| STR STRING { emit_str($2); }
| ml1_table_statement
;
code_statement:
LAV v ',' rx { CEMIT(lav($2,$4)); }
| LBV v { CEMIT(lbv($2)); }
| LAL nof { CEMIT(lal($2)); }
| LCN charname { CEMIT(lcn($2)); }
| LAM nof { CEMIT(lam($2)); }
| LCM nof { CEMIT(lcm($2)); }
| LAI v ',' rx { CEMIT(lai($2,$4)); }
| LCI v ',' rx { CEMIT(lci($2,$4)); }
| LAA v ',' dc { CEMIT(laa($2,$4)); }
| STV v ',' px { CEMIT(stv($2,$4)); }
| STI v ',' px { CEMIT(sti($2,$4)); }
| CLEAR v { CEMIT(clear($2)); }
| AAV v { CEMIT(aav($2)); }
| ABV v { CEMIT(abv($2)); }
| AAL nof { CEMIT(aal($2)); }
| SAV v { CEMIT(sav($2)); }
| SBV v { CEMIT(sbv($2)); }
| SAL nof { CEMIT(sal($2)); }
| SBL nof { CEMIT(sbl($2)); }
| MULTL nof { CEMIT(multl($2)); }
| BUMP v ',' nof { CEMIT(bump($2, $4)); }
| ANDV v { CEMIT(andv($2)); }
| ANDL NUMBER { CEMIT(andl($2)); }
| CAV v ',' ax { CEMIT(cav($2)); }
| CAL nof { CEMIT(cal($2)); }
| CCL STRING { _CHECK_CHAR($2); CEMIT(ccl($2)); }
| CCN charname { CEMIT(ccn($2)); }
| CAI v ',' ax { CEMIT(cai($2,$4)); }
| CCI v { CEMIT(cci($2)); }
| SUBR SYMBOL ',' parnm ',' NUMBER { CEMIT(subr($2, $4, $6)); }
| EXIT NUMBER ',' SYMBOL { SEMIT(exit($2, $4)); }
| GOSUB SYMBOL ',' distance { SEMIT(gosub($2, $4)); }
| GOADD v { SEMIT(goadd($2)); }
| CSS { CEMIT(css()); }
| GO v ',' distance ',' ex ',' ctx { SEMIT(go($2,$4,$6,$8)); }
| GOEQ v ',' distance ',' ex ',' ctx { CEMIT(goeq($2,$4,$6,$8)); }
| GONE v ',' distance ',' ex ',' ctx { CEMIT(gone($2,$4,$6,$8)); }
| GOGE v ',' distance ',' ex ',' ctx { CEMIT(goge($2,$4,$6,$8)); }
| GOGR v ',' distance ',' ex ',' ctx { CEMIT(gogr($2,$4,$6,$8)); }
| GOLE v ',' distance ',' ex ',' ctx { CEMIT(gole($2,$4,$6,$8)); }
| GOLT v ',' distance ',' ex ',' ctx { CEMIT(golt($2,$4,$6,$8)); }
| GOPC v ',' distance ',' ex ',' ctx { CEMIT(gopc($2,$4,$6,$8)); }
| GOND v ',' distance ',' ex ',' ctx { CEMIT(gond($2,$4,$6,$8)); }
| FSTK { CEMIT(fstk()); }
| BSTK { CEMIT(bstk()); }
| CFSTK { CEMIT(cfstk()); }
| UNSTK v { CEMIT(unstk($2)); }
| FMOVE { CEMIT(fmove()); }
| BMOVE { CEMIT(bmove()); }
| MESS STRING { CEMIT(mess($2)); }
| NB STRING { emit_nb($2); }
| PRGST STRING { emit_prgst($2); }
| PRGEN { emit_prgen(); }
| ALIGN { emit_align(); }
| ml1_code_statement
;
charname:
NLREP { $$ = '\n'; }
| SPREP { $$ = ' '; }
| TABREP { $$ = '\t'; }
| QUTREP { $$ = '"'; }
| ml1_charname { $$ = $1; }
;
v: SYMBOL { $$ = $1; }
;
distance:
signnum { $$ = $1; }
| SYMBOL {
_CHECK_CH1($1, 'X');
$$ = 0;
}
;
nof:
signnum { $$ = $1; }
| of_macro { $$ = $1; }
;
signnum:
NUMBER { $$ = $1; }
| '-' NUMBER { $$ = -$2; }
;
of_macro:
OF '(' expr ')' { $$ = $3; }
| '-' OF '(' expr ')' { $$ = -$4; }
;
expr:
NUMBER
| LCH { $$ = LCH_VAL; }
| LNM { $$ = LNM_VAL; }
| LICH { $$ = LICH_VAL; }
| LHV { $$ = LHV_VAL; } /* ML/I */
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
;
rx: SYMBOL {
_CHECK_CH2($1, 'R', 'X');
$$ = *$1;
}
;
dc: SYMBOL {
_CHECK_CH2($1, 'D', 'C');
$$ = *$1;
}
;
px: SYMBOL {
_CHECK_CH2($1, 'P', 'X');
$$ = *$1;
}
;
ax: SYMBOL {
_CHECK_CH2($1, 'A', 'X');
$$ = *$1;
}
;
ex: SYMBOL {
_CHECK_CH2($1, 'E', 'X');
$$ = *$1;
}
;
ctx: SYMBOL {
_CHECK_CH3($1, 'C', 'T', 'X');
$$ = *$1;
}
;
parnm:
SYMBOL {
if ( !strcmp($1, "PARNM") )
$$ = 1;
else {
_CHECK_CH1($1, 'X');
$$ = 0;
}
}
;
ml1_table_statement:
HASH STRING { emit_hash($2); } /* ML/I */
| THASH { emit_thash(); } /* ML/I */
| RL SYMBOL ',' nof { emit_rl($2,$4); } /* ML/I */
| WTHS { emit_con(WTHS_VAL); } /* ML/I */
;
ml1_code_statement:
LINKR SYMBOL { emit_linkr($2); } /* ML/I */
| LINKB { emit_linkb(); } /* ML/I */
| ORL nof { emit_orl($2); } /* ML/I */
;
ml1_charname:
STOPCD { $$ = 0; } /* ML/I */
| SLREP { $$ = -1; } /* ML/I */
;
%%
#define IDSYM_HASHSZ 13
struct idsym_e {
char *sym;
uintptr_t val;
struct idsym_e *next;
} *idsym_tbl[IDSYM_HASHSZ];
int hashfn(char *sym)
{
return (*sym) % IDSYM_HASHSZ;
}
void
add_idsym(char *sym, uintptr_t val)
{
int slot;
struct idsym_e *e;
slot = hashfn(sym);
e = malloc(sizeof(struct idsym_e));
if ( e == NULL ) oom();
e->sym = sym;
e->val = val;
e->next = idsym_tbl[slot];
idsym_tbl[slot] = e;
}
int
get_idsym(char *sym, uintptr_t *val)
{
int slot;
struct idsym_e *e;
slot = hashfn(sym);
e = idsym_tbl[slot];
while ( e != NULL ) {
if ( !strcmp(sym, e->sym) ) {
*val = e->val;
return 1;
}
e = e->next;
}
return 0;
}
void yyerror(char *s)
{
fprintf(stderr, "%d:%s\n", yylineno, s);
}
int main(int argc, char **argv)
{
emitter_init(argv[1]);
yyparse();
emitter_fini();
return 0;
}