summaryrefslogtreecommitdiff
path: root/sys/src/cmd/forp/parse.c
blob: 0aefbc70a22668afb0a87cf9be33452f03a3f96f (plain)
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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>
#include <mp.h>
#include "dat.h"
#include "fns.h"

Biobuf *bin;
Line line;
char lexbuf[512];
int peektok;

enum {
	TEOF = -1,
	TSYM = -2,
	TNUM = -3,
	TBIT = -4,
	TOBVIOUSLY = -5,
	TEQ = -6,
	TNEQ = -7,
	TLSH = -8,
	TRSH = -9,
	TLE = -10,
	TGE = -11,
	TLAND = -12,
	TLOR = -13,
	TASSUME = -14,
	TIMP = -15,
	TEQV = -16,
	TSIGNED = -17,
};

typedef struct Keyword Keyword;
typedef struct Oper Oper;
struct Keyword {
	char *name;
	int tok;
};
/* both tables must be sorted */
static Keyword kwtab[] = {
	"assume", TASSUME,
	"bit", TBIT,
	"obviously", TOBVIOUSLY,
	"signed", TSIGNED,
};
/* <=> is implemented through a hack below */
static Keyword koptab[] = {
	"!=", TNEQ,
	"&&", TLAND,
	"<<", TLSH,
	"<=", TLE,
	"==", TEQ,
	"=>", TIMP,
	">=", TGE,
	">>", TRSH,
	"||", TLOR,
};
static Keyword *kwjmp[128];
static Keyword *kopjmp[128];
struct Oper {
	int tok;
	int type;
	int pred;
	char *str;
};
#define MAXPREC 15
static Oper optab[] = {
	'*', OPMUL, 14, "*",
	'/', OPDIV, 14, "/",
	'%', OPMOD, 14, "%",
	'+', OPADD, 13, "+",
	'-', OPSUB, 13, "-",
	TLSH, OPLSH, 12, "<<",
	TRSH, OPRSH, 12, ">>",
	'<', OPLT, 11, "<",
	TLE, OPLE, 11, "<=",
	'>', OPGT, 11, ">",
	TGE, OPGE, 11, ">=",
	TEQ, OPEQ, 10, "==",
	TNEQ, OPNEQ, 10, "!=",
	'&', OPAND, 9, "&",
	'^', OPXOR, 8, "^",
	'|', OPOR, 7, "|",
	TLAND, OPLAND, 6, "&&",
	TLOR, OPLOR, 5, "||",
	TEQV, OPEQV, 4, "<=>",
	TIMP, OPIMP, 4, "=>",
	/* ?: */
	'=', OPASS, 2, "=",
	',', OPCOMMA, 1, ",",
	-1, OPNOT, MAXPREC, "!",
	-1, OPCOM, MAXPREC, "~",
	-1, OPNEG, MAXPREC, "-",
};

void
error(Line *l, char *msg, ...)
{
	char buf[256];
	Fmt f;
	va_list va;

	if(l == nil) l = &line;
	fmtfdinit(&f, 2, buf, sizeof(buf));
	fmtprint(&f, "%s:%d: ", l->filen, l->lineno);
	va_start(va, msg);
	fmtvprint(&f, msg, va);
	va_end(va);
	fmtrune(&f, '\n');
	fmtfdflush(&f);
	exits("error");
}

static int
tokfmt(Fmt *f)
{
	int t;
	Keyword *k;
	
	t = va_arg(f->args, int);
	if(t >= ' ' && t < 0x7f) return fmtprint(f, "%c", t);
	for(k = kwtab; k < kwtab + nelem(kwtab); k++)
		if(k->tok == t)
			return fmtprint(f, "%s", k->name);
	for(k = koptab; k < koptab + nelem(koptab); k++)
		if(k->tok == t)
			return fmtprint(f, "%s", k->name);
	switch(t){
	case TSYM: return fmtprint(f, "TSYM"); break;
	case TNUM: return fmtprint(f, "TNUM"); break;
	case TEOF: return fmtprint(f, "eof"); break;
	default: return fmtprint(f, "%d", t); break;
	}
}

static int
exprfmt(Fmt *f)
{
	Node *n;
	Oper *o;
	int w;
	
	n = va_arg(f->args, Node *);
	if(n == nil) return fmtprint(f, "nil");
	switch(n->type){
	case ASTSYM: return fmtprint(f, "%s", n->sym->name);
	case ASTBIN:
		for(o = optab; o < optab + nelem(optab); o++)
			if(o->type == n->op)
				break;
		if(o == optab + nelem(optab)) return fmtprint(f, "[unknown operation %O]", n->op);
		w = f->width;
		if(w > o->pred) fmtrune(f, '(');
		fmtprint(f, "%*ε %s %*ε", o->pred, n->n1, o->str, o->pred + 1, n->n2);
		if(w > o->pred) fmtrune(f, ')');
		return 0;
	case ASTNUM: return fmtprint(f, "0x%B", n->num);
	default: return fmtprint(f, "???(%α)", n->type);
	}
}

static int
issymchar(int c)
{
	return c >= 0 && (isalnum(c) || c == '_' || c >= 0x80);
}

static int
lex(void)
{
	int c, d;
	char *p;
	Keyword *kw;

	if(peektok != 0){
		c = peektok;
		peektok = 0;
		return c;
	}
loop:
	do{
		c = Bgetc(bin);
		if(c == '\n') line.lineno++;
	}while(c >= 0 && isspace(c));
	if(c < 0) return TEOF;
	if(c == '/'){
		c = Bgetc(bin);
		if(c == '/'){
			do
				c = Bgetc(bin);
			while(c >= 0 && c != '\n');
			if(c < 0) return TEOF;
			line.lineno++;
			goto loop;
		}else if(c == '*'){
		s0:
			c = Bgetc(bin);
			if(c != '*') goto s0;
		s1:
			c = Bgetc(bin);
			if(c == '*') goto s1;
			if(c != '/') goto s0;
			goto loop;
		}else{
			Bungetc(bin);
			return '/';
		}
	}
	if(isdigit(c)){
		p = lexbuf;
		*p++ = c;
		while(c = Bgetc(bin), issymchar(c))
			if(p < lexbuf + sizeof(lexbuf) - 1)
				*p++ = c;
		Bungetc(bin);
		*p = 0;
		strtol(lexbuf, &p, 0);
		if(p == lexbuf || *p != 0)
			error(nil, "invalid number %q", lexbuf);
		return TNUM;
	}
	if(issymchar(c)){
		p = lexbuf;
		*p++ = c;
		while(c = Bgetc(bin), issymchar(c))
			if(p < lexbuf + sizeof(lexbuf) - 1)
				*p++ = c;
		Bungetc(bin);
		*p = 0;
		c = lexbuf[0];
		if((signed char)c>= 0 && (kw = kwjmp[c], kw != nil))
			for(; kw < kwtab + nelem(kwtab) && kw->name[0] == c; kw++)
				if(strcmp(lexbuf, kw->name) == 0)
					return kw->tok;
		return TSYM;
	}
	if(kw = kopjmp[c], kw != nil){
		d = Bgetc(bin);
		for(; kw < koptab + nelem(koptab) && kw->name[0] == c; kw++)
			if(kw->name[1] == d){
				if(kw->tok == TLE){
					c = Bgetc(bin);
					if(c == '>')
						return TEQV;
					Bungetc(bin);
				}
				return kw->tok;
			}
		Bungetc(bin);
	}
	return c;
}

static void
superman(int t)
{
	assert(peektok == 0);
	peektok = t;
}

static int
peek(void)
{
	if(peektok != 0) return peektok;
	return peektok = lex();
}

static void
expect(int t)
{
	int s;
	
	s = lex();
	if(t != s)
		error(nil, "expected %t, got %t", t, s);
}

static int
got(int t)
{
	return peek() == t && (lex(), 1);
}

static Node *
expr(int level)
{
	Node *a, *b, *c;
	Oper *op;
	Symbol *s;
	mpint *num;
	int t;
	
	if(level == MAXPREC+2){
		switch(t = lex()){
		case '(':
			a = expr(0);
			expect(')');
			return a;
		case TSYM:
			s = symget(lexbuf);
			switch(s->type){
			case SYMNONE:
				error(nil, "%#q undefined", s->name);
			default:
				error(nil, "%#q symbol type error", s->name);
			case SYMBITS:
				break;
			}
			return node(ASTSYM, s);
		case TNUM:
			num = strtomp(lexbuf, nil, 0, nil);
			return node(ASTNUM, num);
		default:
			error(nil, "unexpected %t", t);
		}
	}else if(level == MAXPREC+1){
		a = expr(level + 1);
		if(got('[')){
			b = expr(0);
			if(got(':'))
				c = expr(0);
			else
				c = nil;
			expect(']');
			a = node(ASTIDX, a, b, c);
		}
		return a;
	}else if(level == MAXPREC){
		switch(t = lex()){
		case '~': return node(ASTUN, OPCOM, expr(level));
		case '!': return node(ASTUN, OPNOT, expr(level));
		case '+': return expr(level);
		case '-': return node(ASTUN, OPNEG, expr(level));
		default: superman(t); return expr(level+1); break;
		}
	}else if(level == 3){
		a = expr(level + 1);
		if(got('?')){
			b = expr(level);
			expect(':');
			c = expr(level);
			a = node(ASTTERN, a, b, c);
		}
		return a;
	}
	a = expr(level+1);
	for(;;){
		t = peek();
		for(op = optab; op < optab + nelem(optab); op++)
			if(op->tok == t && op->pred >= level)
				break;
		if(op == optab+nelem(optab)) return a;
		lex();
		a = node(ASTBIN, op->type, a, expr(level+1));
	}
}

static void
vardecl(void)
{
	Symbol *s;
	int l, flags;

	flags = 0;
	for(;;)
		switch(l = lex()){
		case TBIT: if((flags & 1) != 0) goto err; flags |= 1; break;
		case TSIGNED: if((flags & 2) != 0) goto err; flags |= 2; break;
		default: superman(l); goto out;
		}
out:
	do{
		expect(TSYM);
		s = symget(lexbuf);
		if(s->type != SYMNONE) error(nil, "%#q redefined", s->name);
		s->type = SYMBITS;
		if((flags & 2) != 0)
			s->flags |= SYMFSIGNED;
		s->size = 1;
		if(got('[')){
			expect(TNUM);
			s->type = SYMBITS;
			s->size = strtol(lexbuf, nil, 0);
			expect(']');
		}
		s->vars = emalloc(sizeof(int) * s->size);
	}while(got(','));
	expect(';');
	return;
err:	error(nil, "syntax error");
}

static int
statement(void)
{
	Node *n;
	int t;

	switch(t=peek()){
	case TEOF:
		return 0;
	case TBIT:
	case TSIGNED:
		vardecl();
		break;
	case TASSUME:
	case TOBVIOUSLY:
		lex();
		n = expr(0);
		expect(';');
		convert(n, -1);
		if(t == TASSUME)
			assume(n);
		else
			obviously(n);
		break;
	case ';':
		lex();
		break;
	default:
		n = expr(0);
		convert(n, -1);
		expect(';');
	}
	return 1;
}

void
parsinit(void)
{
	Keyword *k;

	fmtinstall('t', tokfmt);
	fmtinstall(L'ε', exprfmt);
	for(k = kwtab; k < kwtab + nelem(kwtab); k++)
		if(kwjmp[k->name[0]] == nil)
			kwjmp[k->name[0]] = k;
	for(k = koptab; k < koptab + nelem(koptab); k++)
		if(kopjmp[k->name[0]] == nil)
			kopjmp[k->name[0]] = k;
}

void
parse(char *fn)
{
	if(fn == nil){
		bin = Bfdopen(0, OREAD);
		line.filen = "<stdin>";
	}else{
		bin = Bopen(fn, OREAD);
		line.filen = strdup(fn);
	}
	if(bin == nil) sysfatal("open: %r");
	line.lineno = 1;
	while(statement())
		;
}