summary refs log tree commit diff
path: root/parse.c
blob: b334db1e908e25cd2591cee2dc07753d80ad6e65 (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
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
/*
 * Copyright (c) 2026 Nakidai Perumenei <nakidai at disroot dot org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "thac.h"

#include <stddef.h>
#include <stdlib.h>


/*
 * expr = expr-assign
 */
#define pexpr pexpr_assign
static struct node *pexpr(void);
static struct node *pstmt(void);

static enum noper_t
associate(enum tok_t t)
{
	switch (t)
	{
	case TKEYLEN:    return OLEN;
	case TKEYASSERT: return OASSERT;
	case TARRLEFT:   return OCONNECT;
	case TCONCAT:    return OCONCAT;
	case TAMPER:     return OBAND;
	case TPIPE:      return OBOR;
	case TCARET:     return OBXOR;
	case TLSHIFT:    return OLSHIFT;
	case TRSHIFT:    return ORSHIFT;
	case TTILDE:     return OINV;
	case TAND:       return OAND;
	case TEXCLAM:    return ONOT;
	case TOR:        return OOR;
	case TEQ:        return OEQ;
	case TNEQ:       return ONEQ;
	case TLESS:      return OLESS;
	case TLESSEQ:    return OLESSEQ;
	case TGREAT:     return OGREAT;
	case TGREATEQ:   return OGREATEQ;
	case TPLUS:      return OSUM;
	case TMINUS:     return OSUB;
	case TASTER:     return OMUL;
	case TSLASH:     return ODIV;
	case TPERC:      return OMOD;
	case TPOW:       return OPOW;
	case TASSAMPER:  return OASSBAND;
	case TASSASTER:  return OASSMUL;
	case TASSCARET:  return OASSBXOR;
	case TASSIGN:    return OASSIGN;
	case TASSLSHIFT: return OASSLSHIFT;
	case TASSMINUS:  return OASSSUB;
	case TASSPERC:   return OASSMOD;
	case TASSPIPE:   return OASSBOR;
	case TASSPLUS:   return OASSSUM;
	case TASSRSHIFT: return OASSRSHIFT;
	case TASSSLASH:  return OASSDIV;
	default: complain(1, "%s cannot be associated", tokname(t));
	}

	/* not reached */
	return 0;
}

static struct node *
mknode(enum node_t t)
{
	struct node *res = malloc(sizeof(*res));
	if (!res)
		dieno(1, "malloc()");
	res->type = t;
	return res;
}

#define BINLEFT(NAME, NEXT, ...) static struct node *NAME(void) \
{ \
	struct node *child, *node; \
	if (!(child = NEXT())) \
		return NULL; \
loop: \
	if (!exptok(__VA_ARGS__)) \
	{ \
		ungettok(); \
		return child; \
	} \
	node = mknode(NOPER); \
	node->noper.type = associate(curtok.type); \
	node->noper.l = child; \
	if (!(node->noper.r = NEXT())) \
		complain(1, "no right side for %s", nopername(node->noper.type)); \
	child = node; \
	goto loop; \
}

#define BINRIGHT(NAME, NEXT, ...) static struct node *NAME(void) \
{ \
	struct node *child, *node; \
	if (!(child = NEXT())) \
		return NULL; \
	if (!exptok(__VA_ARGS__)) \
	{ \
		ungettok(); \
		return child; \
	} \
	node = mknode(NOPER); \
	node->noper.type = associate(curtok.type); \
	node->noper.l = child; \
	if (!(node->noper.r = NAME())) \
		complain(1, "no right side for %s", nopername(node->noper.type)); \
	return node; \
}

/*
 * comp = "{" *(stmt) "}"
 */
static struct node *
pcomp(void)
{
	struct node *node;
	ulong i = 0;

	if (!exptok(TOPBRACE))
		return NULL;
	node = mknode(NCOMP);
	node->ncomp.stmts = NULL;

loop:
	/* TODO: add cap and increase like in lex.c */
	node->ncomp.stmts = realloc(
		node->ncomp.stmts,
		sizeof(struct node *) * (i + 1)
	);

	switch (gettok())
	{
	break; case TEOF: complain(1, "no closing brace");
	break; case TCLBRACE: goto end;
	break; default:
		ungettok();
		if (!(node->ncomp.stmts[i] = pstmt()))
			complain(1, "no statement");
	}
	++i;
	goto loop;
end:
	node->ncomp.len = i;
	return node;
}

/*
 * expr-primary = NUMBER / id / "nil" / expr-node / expr-mod / "(" expr ")"
 * expr-node = "node" "(" [*(expr ",") expr] ")"
 * expr-mod = "mod" "(" [*(id ",") id] ")" comp
 */
static struct node *
pexpr_primary(void)
{
	struct node *node = NULL;
	ulong i = 0;

	switch (gettok())
	{
	break; case TEOF: return NULL;
	break; case TNUM:
		node = mknode(NNUM);
		node->nnum = atol(curtok.info.p);
	break; case TID:
		node = mknode(NVAR);
		node->nvar = curtok.info.p;
	break; case TOPPAREN:
		if (!(node = pexpr()))
			complain(1, "empty paretheses");
		if (!exptok(TCLPAREN))
			complain(1, "no closing parenthesis");
	break; case TKEYNODE:
		node = mknode(NNODE);
		node->nnode.params = NULL;

		if (!exptok(TOPPAREN))
			complain(1, "no parentheses after node");
nodeloop:
		/* TODO: add cap and increase like in lex.c */
		node->nnode.params = realloc(
			node->nnode.params,
			sizeof(struct node *) * (i + 1)
		);
		if (!node->nnode.params)
			dieno(1, "realloc()");

		switch(gettok())
		{
		break; case TEOF: complain(1, "no closing parenthesis");
		break; case TCLPAREN: goto nodeend;
		break; case TCOMMA:
			if (!i || !(node->nnode.params[i] = pexpr()))
				complain(1, "empty param in node");
		break; default:
			ungettok();
			if (i)
				complain(1, "no comma");
			else if (!(node->nnode.params[i] = pexpr()))
				complain(1, "empty param in node");
		}
		++i;
		goto nodeloop;
nodeend:
		node->nnode.len = i;
	break; case TKEYMOD:
		node = mknode(NMOD);
		node->nmod.params = NULL;

		if (!exptok(TOPPAREN))
			complain(1, "no parentheses after mod");
modloop:
		/* TODO: add cap and increase like in lex.c */
		node->nmod.params = realloc(
			node->nmod.params,
			sizeof(char *) * (i + 1)
		);
		if (!node->nmod.params)
			dieno(1, "realloc()");

		switch(gettok())
		{
		break; case TEOF: complain(1, "no closing parenthesis");
		break; case TCLPAREN: goto modend;
		break; case TCOMMA:
			if (!i || gettok() != TID)
				complain(1, "expected id as a param name");
			else
				node->nmod.params[i] = curtok.info.p;
		break; default:
			ungettok();
			if (i)
				complain(1, "no comma");
			else if (gettok() != TID)
				complain(1, "expected id as a param name");
			else
				node->nmod.params[i] = curtok.info.p;
		}
		++i;
		goto modloop;
modend:
		node->nmod.len = i;

		if (!(node->nmod.code = pcomp()))
			complain(1, "no comp for mod");
	break; default:
		ungettok();
	}

	return node;
}

/*
 * expr-post = expr-primary *("--" / "++" / "[" expr [":" expr] "]")
 */
static struct node *
pexpr_post(void)
{
	struct node *child, *node;

	child = pexpr_primary();
	if (!child)
		return NULL;

loop:
	switch (gettok())
	{
	break; case TEOF: return child;
	break; case TDECR: case TINCR:
		node = mknode(NOPER);
		node->noper.type = curtok.type == TDECR ? OPOSTDECR : OPOSTINCR;
		node->noper.m = child;
	break; case TOPBRACK:
		node = mknode(NOPER);
		node->noper.type = OINDEX;
		node->noper.l = child;

		if (!(node->noper.r = pexpr()))
			complain(1, "no inside for []");
		switch (gettok())
		{
		break; case TEOF: complain(1, "no closing bracket");
		break; case TCOL:
			node->noper.type = OSLICE;
			node->noper.m = node->noper.r;
			if (!(node->noper.r = pexpr()))
				complain(1, "no right side for []");
			if (!exptok(TCLBRACK))
				complain(1, "invalid syntax for []");
		/*FT*/ case TCLBRACK:
		break; default: complain(1, "invalid syntax for []");
		}
	break; default:
		ungettok();
		return child;
	}

	child = node;
	goto loop;
}

/*
 * expr-pref = expr-post / ("assert" / "len" / "~" / "!" / "--" / "++") expr-pref
 */
static struct node *
pexpr_pref(void)
{
	struct node *node;

	switch (gettok())
	{
	break; case TEOF: return NULL;
	break; case TKEYASSERT: case TKEYLEN: case TTILDE:
	case TEXCLAM: case TDECR: case TINCR:
		node = mknode(NOPER);
		node->noper.type = curtok.type == TDECR ? OPREDECR :
			   curtok.type == TINCR ? OPREINCR :
			   associate(curtok.type);
		if (!(node->noper.m = pexpr_pref()))
			complain(1, "no operand for %s", nopername(node->noper.type));
	break; case TOPBRACK:
		node = mknode(NOPER);
		node->noper.type = OARRAY;

		if (!(node->noper.l = pexpr()))
			complain(1, "no inside for []");
		if (!exptok(TCLBRACK))
			complain(1, "invalid syntax for []");
		if (!(node->noper.r = pexpr_pref()))
			complain(1, "no operand for []");
	break; default:
		ungettok();
		return pexpr_post();
	}

	return node;
}

/* expr-pow   = expr-pref ["**" expr-pow] */
BINRIGHT(pexpr_pow, pexpr_pref, TPOW)
/* expr-mult  = expr-pow *(("*" / "><" / "%" / "/") expr-pow) */
BINLEFT(pexpr_mult, pexpr_pow, TASTER, TCONCAT, TPERC, TSLASH)
/* expr-add   = expr-mult *(("<-" / "-" / "+") expr-mult) */
BINLEFT(pexpr_add, pexpr_mult, TARRLEFT, TMINUS, TPLUS)
/* expr-shift = expr-add *(("<<" / ">>") expr-add) */
BINLEFT(pexpr_shift, pexpr_add, TLSHIFT, TRSHIFT)
/* expr-rel   = expr-shift *(("<" / "<=" / ">" / ">=") expr-shift) */
BINLEFT(pexpr_rel, pexpr_shift, TLESS, TLESSEQ, TGREAT, TGREATEQ)
/* expr-eq    = expr-rel *(("==" / "!=") expr-rel) */
BINLEFT(pexpr_eq, pexpr_rel, TEQ, TNEQ)
/* expr-band  = expr-eq *("&" expr-eq) */
BINLEFT(pexpr_band, pexpr_eq, TAMPER)
/* expr-bxor  = expr-band *("^" expr-band) */
BINLEFT(pexpr_bxor, pexpr_band, TCARET)
/* expr-bor   = expr-bxor *("|" expr-bxor) */
BINLEFT(pexpr_bor, pexpr_bxor, TPIPE)
/* expr-and   = expr-bor *("&&" expr-bor) */
BINLEFT(pexpr_and, pexpr_bor, TAND)
/* expr-or    = expr-and *("||" expr-and) */
BINLEFT(pexpr_or, pexpr_and, TOR)

/*
 * expr-cond = expr-or ? expr : expr-cond
 */
static struct node *
pexpr_cond(void)
{
	struct node *child, *node;

	child = pexpr_or();
	if (!child)
		return NULL;

	if (!exptok(TQUEST))
	{
		ungettok();
		return child;
	}

	node = mknode(NOPER);
	node->noper.type = OCOND;
	node->noper.l = child;

	if (!(node->noper.m = pexpr()))
		complain(1, "no then branch in ternary if");

	if (!exptok(TCOL))
		complain(1, "no colon for ternary if");

	if (!(node->noper.r = pexpr_cond()))
		complain(1, "no else branch for ternary if");

	return node;
}

/*
 * expr-assign      =  expr-cond [expr-assign-oper expr-assign]
 * expr-assign-oper =  "+=" / "-=" / "*=" / "/=" / "%=" / "&="
 * expr-assign-oper =/ "|=" / "^=" / "=" / ">>=" / "<<=" / "%="
 */
BINRIGHT(
	pexpr_assign, pexpr_cond,
	TASSAMPER, TASSASTER, TASSCARET, TASSIGN, TASSLSHIFT,
	TASSMINUS, TASSPERC, TASSPIPE, TASSPLUS, TASSRSHIFT,
	TASSSLASH
)

/*
 * for = "for" "(" [expr] ";" [expr] ";" [expr] ")" stmt
 */
struct node *
pfor(void)
{
	struct node *node;

	if (!exptok(TKEYFOR))
	{
		say("%s:%d: pfor selected but no TKEYFOR?", __FILE__, __LINE__);
		ungettok();
		return NULL;
	}

	node = mknode(NFOR);

	if (!exptok(TOPPAREN))
		complain(1, "no parentheses after for");
	node->nfor.before = pexpr();
	if (!exptok(TSEMICOL))
		complain(1, "invalid for parameters");
	node->nfor.cond = pexpr();
	if (!exptok(TSEMICOL))
		complain(1, "invalid for parameters");
	node->nfor.between = pexpr();
	if (!exptok(TCLPAREN))
		complain(1, "invalid for parameters");
	if (!(node->nfor.code = pstmt()))
		complain(1, "no statement for for");

	return node;
}

/*
 * foreach = "foreach" "(" expr ")" stmt
 */
struct node *
pforeach(void)
{
	struct node *node;

	if (!exptok(TKEYFOREACH))
	{
		say("%s:%d: pforeach selected but no TKEYFOREACH?", __FILE__, __LINE__);
		ungettok();
		return NULL;
	}

	node = mknode(NFOREACH);

	if (!exptok(TOPPAREN)
	 || !(node->nforeach.obj = pexpr())
	 || !exptok(TCLPAREN))
		complain(1, "invalid foreach parameter");
	if (!(node->nforeach.code = pstmt()))
		complain(1, "no statement for foreach");

	return node;
}

/*
 * if = "if" "(" expr ")" stmt ["else" stmt]
 */
struct node *
pif(void)
{
	struct node *node;

	if (!exptok(TKEYIF))
	{
		say("%s:%d: pif selected but no TKEYIF?", __FILE__, __LINE__);
		ungettok();
		return NULL;
	}

	node = mknode(NCOND);

	if (!exptok(TOPPAREN)
	 || !(node->ncond.cond = pexpr())
	 || !exptok(TCLPAREN))
		complain(1, "invalid if parameter");
	if (!(node->ncond.t = pstmt()))
		complain(1, "no statement for then if branch");

	node->ncond.f = NULL;
	if (exptok(TKEYELSE))
		if (!(node->ncond.f = pstmt()))
			complain(1, "no statement for else if branch");
		else;
	else
		ungettok();

	return node;
}

/*
 * with = "with" expr "(" [*(expr ",") expr] ")" stmt
 */
struct node *
pwith(void)
{
	struct node *node;
	ulong i = 0;

	if (!exptok(TKEYWITH))
	{
		say("%s:%d: pwith selected but no TKEYWITH?", __FILE__, __LINE__);
		ungettok();
		return NULL;
	}

	node = mknode(NWITH);
	node->nwith.args = NULL;

	if (!(node->nwith.mod = pexpr()))
		complain(1, "no module specified");
	if (!exptok(TOPPAREN))
		complain(1, "no parameters in with");
loop:
	/* TODO: add cap and increase like in lex.c */
	node->nwith.args = realloc(
		node->nwith.args,
		sizeof(struct nnode *) * (i + 1)
	);
	if (!node->nwith.args)
		dieno(1, "realloc()");

	switch(gettok())
	{
	break; case TEOF: complain(1, "no closing parenthesis");
	break; case TCLPAREN: goto end;
	break; case TCOMMA:
		if (!i || !(node->nwith.args[i] = pexpr()))
			complain(1, "empty param in with");
	break; default:
		ungettok();
		if (i)
			complain(1, "no comma");
		else if (!(node->nwith.args[i] = pexpr()))
			complain(1, "empty param in with");
	}
	++i;
	goto loop;
end:
	if (!(node->nwith.code = pstmt()))
		complain(1, "no statement for with");
	node->nwith.len = i;

	return node;
}

/*
 * break = "break" ";"
 * continue = "continue" ";"
 */
struct node *
pbreak(void)
{
	struct node *node;
	enum tok_t t;

	if (!exptok(TKEYBREAK, TKEYCONT))
	{
		say("%s:%d: break selected but no break/continue?", __FILE__, __LINE__);
		ungettok();
		return NULL;
	}

	node = mknode(NWITH);
	node->type = (t = curtok.type) == TKEYBREAK ? NBREAK : NCONT;

	if (!exptok(TSEMICOL))
		complain(1, "expected `;' after %s", tokname(t));

	return node;
}

/*
 * stmt = comp / for / foreach / if / with / expr ";" / break / continue
 */
struct node *
pstmt(void)
{
	struct node *node;

	if (gettok() == TEOF)
		return NULL;
	ungettok();

	switch (curtok.type)
	{
	break; case TOPBRACE:    return pcomp();
	break; case TKEYFOR:     return pfor();
	break; case TKEYFOREACH: return pforeach();
	break; case TKEYIF:      return pif();
	break; case TKEYWITH:    return pwith();
	break; case TKEYBREAK:
	/*FT*/ case TKEYCONT:    return pbreak();
	break; default:
		node = pexpr();
		if (!exptok(TSEMICOL))
			complain(1, "expected `;' at the end of an expression");
		return node ? node : pstmt();
	}

	/* not reached */
	return NULL;
}

struct node *
getstmt(void)
{
	return pstmt();
}