ref: 000f80e000c340f1177bb7ae05c19caf210529ca mrsh/parser/arithm.c -rw-r--r-- 12.2 KiB View raw
                                                                                
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
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <errno.h>
#include <mrsh/arithm.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"

static bool parse_char(struct mrsh_parser *state, char c) {
	if (parser_peek_char(state) != c) {
		return false;
	}
	parser_read_char(state);
	return true;
}

static bool parse_whitespace(struct mrsh_parser *state) {
	if (!isspace(parser_peek_char(state))) {
		return false;
	}
	parser_read_char(state);
	return true;
}

static inline void consume_whitespace(struct mrsh_parser *state) {
	while (parse_whitespace(state)) {
		// This space is intentionally left blank
	}
}

static bool expect_char(struct mrsh_parser *state, char c) {
	if (parse_char(state, c)) {
		return true;
	}
	char msg[128];
	snprintf(msg, sizeof(msg), "expected '%c'", c);
	parser_set_error(state, msg);
	return false;
}

static bool parse_str(struct mrsh_parser *state, const char *str) {
	size_t len = strlen(str);

	for (size_t i = 0; i < len; ++i) {
		parser_peek(state, NULL, i + 1);

		if (state->buf.data[i] != str[i]) {
			return false;
		}
	}

	parser_read(state, NULL, len);
	return true;
}

static size_t peek_literal(struct mrsh_parser *state) {
	size_t i = 0;
	while (true) {
		parser_peek(state, NULL, i + 1);

		char c = state->buf.data[i];
		// TODO: 0x, 0b prefixes
		if (!isdigit(c)) {
			break;
		}

		++i;
	}

	return i;
}

static struct mrsh_arithm_literal *literal(struct mrsh_parser *state) {
	size_t len = peek_literal(state);
	if (len == 0) {
		return NULL;
	}

	char *str = strndup(state->buf.data, len);
	parser_read(state, NULL, len);

	char *end;
	errno = 0;
	long value = strtol(str, &end, 0);
	if (end[0] != '\0' || errno != 0) {
		free(str);
		parser_set_error(state, "failed to parse literal");
		return NULL;
	}
	free(str);

	return mrsh_arithm_literal_create(value);
}

static struct mrsh_arithm_expr *arithm_expr(struct mrsh_parser *state);

static struct mrsh_arithm_unop *unop(struct mrsh_parser *state) {
	enum mrsh_arithm_unop_type type;
	switch (parser_peek_char(state)) {
	case '+':
		type = MRSH_ARITHM_UNOP_PLUS;
		break;
	case '-':
		type = MRSH_ARITHM_UNOP_MINUS;
		break;
	case '~':
		type = MRSH_ARITHM_UNOP_TILDE;
		break;
	case '!':
		type = MRSH_ARITHM_UNOP_BANG;
		break;
	default:
		return NULL;
	}
	parser_read_char(state);

	struct mrsh_arithm_expr *body = arithm_expr(state);
	if (body == NULL) {
		parser_set_error(state,
			"expected an arithmetic expression after unary operator");
		return NULL;
	}

	return mrsh_arithm_unop_create(type, body);
}

static struct mrsh_arithm_expr *paren(struct mrsh_parser *state) {
	if (!parse_char(state, '(')) {
		return NULL;
	}

	consume_whitespace(state);
	struct mrsh_arithm_expr *expr = arithm_expr(state);
	// consume_whitespace() is not needed here, since the call to arithm_expr()
	// consumes the trailing whitespace.

	if (!expect_char(state, ')')) {
		mrsh_arithm_expr_destroy(expr);
		return NULL;
	}

	return expr;
}

static struct mrsh_arithm_expr *term(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *expr = paren(state);
	if (expr != NULL) {
		return expr;
	}

	struct mrsh_arithm_unop *au = unop(state);
	if (au != NULL) {
		return &au->expr;
	}

	struct mrsh_arithm_literal *al = literal(state);
	if (al != NULL) {
		return &al->expr;
	}

	return NULL;
}

static struct mrsh_arithm_expr *factor(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *expr = term(state);
	if (expr == NULL) {
		return NULL;
	}

	/* This loop ensures we parse factors as left-assossiative */
	while (true) {
		consume_whitespace(state);
		enum mrsh_arithm_binop_type type;
		if (parse_char(state, '*')) {
			type = MRSH_ARITHM_BINOP_ASTERISK;
		} else if (parse_char(state, '/')) {
			type = MRSH_ARITHM_BINOP_SLASH;
		} else if (parse_char(state, '%')) {
			type = MRSH_ARITHM_BINOP_PERCENT;
		} else {
			return expr;
		}
		consume_whitespace(state);

		/* Instead of calling ourselves recursively, we call term for
		 * left-associativity */
		struct mrsh_arithm_expr *right = term(state);
		if (right == NULL) {
			parser_set_error(state, "expected a term after *, / or % operator");
			return NULL;
		}

		struct mrsh_arithm_binop *bo =
			mrsh_arithm_binop_create(type, expr, right);
		expr = &bo->expr;
	}
}

static struct mrsh_arithm_expr *addend(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *expr = factor(state);
	if (expr == NULL) {
		return NULL;
	}

	/* This loop ensures we parse addends as left-assossiative */
	while (true) {
		// consume_whitespace() is not needed here, since the call to factor()
		// consumes trailing whitespace.
		enum mrsh_arithm_binop_type type;
		if (parse_char(state, '+')) {
			type = MRSH_ARITHM_BINOP_PLUS;
		} else if (parse_char(state, '-')) {
			type = MRSH_ARITHM_BINOP_MINUS;
		} else {
			return expr;
		}
		consume_whitespace(state);

		/* Instead of calling ourselves recursively, we call factor for
		 * left-associativity */
		struct mrsh_arithm_expr *right = factor(state);
		if (right == NULL) {
			parser_set_error(state, "expected a factor after + or - operator");
			return NULL;
		}

		struct mrsh_arithm_binop *bo =
			mrsh_arithm_binop_create(type, expr, right);
		expr = &bo->expr;
	}
}

static struct mrsh_arithm_expr *shift(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *left = addend(state);
	if (left == NULL) {
		return NULL;
	}

	// consume_whitespace() is not needed here, since the call to addend()
	// consumes the trailing whitespace.
	enum mrsh_arithm_binop_type type;
	if (parse_str(state, "<<")) {
		type = MRSH_ARITHM_BINOP_DLESS;
	} else if (parse_str(state, ">>")) {
		type = MRSH_ARITHM_BINOP_DGREAT;
	} else {
		return left;
	}
	consume_whitespace(state);

	struct mrsh_arithm_expr *right = shift(state);
	if (right == NULL) {
		mrsh_arithm_expr_destroy(left);
		parser_set_error(state, "expected a term");
		return NULL;
	}

	struct mrsh_arithm_binop *bo = mrsh_arithm_binop_create(type, left, right);
	return &bo->expr;
}

static struct mrsh_arithm_expr *comp(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *left = shift(state);
	if (left == NULL) {
		return NULL;
	}

	enum mrsh_arithm_binop_type type;
	if (parse_str(state, "<=")) {
		type = MRSH_ARITHM_BINOP_LESSEQ;
	} else if (parse_char(state, '<')) {
		type = MRSH_ARITHM_BINOP_LESS;
	} else if (parse_str(state, ">=")) {
		type = MRSH_ARITHM_BINOP_GREATEQ;
	} else if (parse_char(state, '>')) {
		type = MRSH_ARITHM_BINOP_GREAT;
	} else {
		return left;
	}
	consume_whitespace(state);

	struct mrsh_arithm_expr *right = comp(state);
	if (right == NULL) {
		mrsh_arithm_expr_destroy(left);
		parser_set_error(state, "expected a term");
		return NULL;
	}

	struct mrsh_arithm_binop *bo = mrsh_arithm_binop_create(type, left, right);
	return &bo->expr;
}

static struct mrsh_arithm_expr *equal(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *left = comp(state);
	if (left == NULL) {
		return NULL;
	}

	enum mrsh_arithm_binop_type type;
	if (parse_str(state, "==")) {
		type = MRSH_ARITHM_BINOP_DEQ;
	} else if (parse_str(state, "!=")) {
		type = MRSH_ARITHM_BINOP_BANGEQ;
	} else {
		return left;
	}

	struct mrsh_arithm_expr *right = equal(state);
	if (right == NULL) {
		mrsh_arithm_expr_destroy(left);
		parser_set_error(state, "expected a term");
		return NULL;
	}

	struct mrsh_arithm_binop *bo = mrsh_arithm_binop_create(type, left, right);
	return &bo->expr;
}

static bool parse_binop(struct mrsh_parser *state, const char *str) {
	size_t len = strlen(str);

	for (size_t i = 0; i < len; ++i) {
		parser_peek(state, NULL, i + 1);

		if (state->buf.data[i] != str[i]) {
			return false;
		}
	}

	// Make sure we don't parse "&&" as "&"
	parser_peek(state, NULL, len + 1);
	switch (state->buf.data[len]) {
	case '|':
	case '&':
		return false;
	}

	parser_read(state, NULL, len);
	return true;
}

static struct mrsh_arithm_expr *binop(struct mrsh_parser *state,
		enum mrsh_arithm_binop_type type, const char *str,
		struct mrsh_arithm_expr *(*term)(struct mrsh_parser *state)) {
	struct mrsh_arithm_expr *left = term(state);
	if (left == NULL) {
		return NULL;
	}
	if (!parse_binop(state, str)) {
		return left;
	}
	consume_whitespace(state);

	struct mrsh_arithm_expr *right = binop(state, type, str, term);
	if (right == NULL) {
		mrsh_arithm_expr_destroy(left);
		parser_set_error(state, "expected a term");
		return NULL;
	}

	struct mrsh_arithm_binop *bo = mrsh_arithm_binop_create(type, left, right);
	return &bo->expr;
}

static struct mrsh_arithm_expr *bitwise_and(struct mrsh_parser *state) {
	return binop(state, MRSH_ARITHM_BINOP_AND, "&", equal);
}

static struct mrsh_arithm_expr *bitwise_xor(struct mrsh_parser *state) {
	return binop(state, MRSH_ARITHM_BINOP_CIRC, "^", bitwise_and);
}

static struct mrsh_arithm_expr *bitwise_or(struct mrsh_parser *state) {
	return binop(state, MRSH_ARITHM_BINOP_OR, "|", bitwise_xor);
}

static struct mrsh_arithm_expr *logical_and(struct mrsh_parser *state) {
	return binop(state, MRSH_ARITHM_BINOP_DAND, "&&", bitwise_or);
}

static struct mrsh_arithm_expr *logical_or(struct mrsh_parser *state) {
	return binop(state, MRSH_ARITHM_BINOP_DOR, "||", logical_and);
}

static struct mrsh_arithm_expr *ternary(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *expr = logical_or(state);
	if (expr == NULL) {
		return NULL;
	}
	if (!parse_char(state, '?')) {
		return expr;
	}
	struct mrsh_arithm_expr *condition = expr;

	struct mrsh_arithm_expr *body = ternary(state);
	if (body == NULL) {
		parser_set_error(state, "expected a logical or term");
		goto error_body;
	}

	if (!expect_char(state, ':')) {
		goto error_semi;
	}

	struct mrsh_arithm_expr *else_part = ternary(state);
	if (else_part == NULL) {
		parser_set_error(state, "expected an or term");
		goto error_else_part;
	}

	struct mrsh_arithm_cond *c =
		mrsh_arithm_cond_create(condition, body, else_part);
	return &c->expr;

error_else_part:
error_semi:
	mrsh_arithm_expr_destroy(body);
error_body:
	mrsh_arithm_expr_destroy(condition);
	return NULL;
}

static bool peek_assign_op(struct mrsh_parser *state, size_t *offset,
		const char *str) {
	size_t len = strlen(str);

	for (size_t i = 0; i < len; ++i) {
		parser_peek(state, NULL, *offset + i + 1);

		if (state->buf.data[*offset + i] != str[i]) {
			return false;
		}
	}

	*offset += len;
	return true;
}

static struct mrsh_arithm_expr *assignment(struct mrsh_parser *state) {
	size_t name_len = peek_name(state, false);
	if (name_len == 0) {
		return NULL;
	}

	enum mrsh_arithm_assign_op op;
	size_t offset = name_len;
	if (peek_assign_op(state, &offset, "=")) {
		op = MRSH_ARITHM_ASSIGN_NONE;
	} else if (peek_assign_op(state, &offset, "*=")) {
		op = MRSH_ARITHM_ASSIGN_ASTERISK;
	} else if (peek_assign_op(state, &offset, "/=")) {
		op = MRSH_ARITHM_ASSIGN_SLASH;
	} else if (peek_assign_op(state, &offset, "%=")) {
		op = MRSH_ARITHM_ASSIGN_PERCENT;
	} else if (peek_assign_op(state, &offset, "+=")) {
		op = MRSH_ARITHM_ASSIGN_PLUS;
	} else if (peek_assign_op(state, &offset, "-=")) {
		op = MRSH_ARITHM_ASSIGN_MINUS;
	} else if (peek_assign_op(state, &offset, "<<=")) {
		op = MRSH_ARITHM_ASSIGN_DLESS;
	} else if (peek_assign_op(state, &offset, ">>=")) {
		op = MRSH_ARITHM_ASSIGN_DGREAT;
	} else if (peek_assign_op(state, &offset, "&=")) {
		op = MRSH_ARITHM_ASSIGN_AND;
	} else if (peek_assign_op(state, &offset, "^=")) {
		op = MRSH_ARITHM_ASSIGN_CIRC;
	} else if (peek_assign_op(state, &offset, "|=")) {
		op = MRSH_ARITHM_ASSIGN_OR;
	} else {
		return NULL;
	}
	// offset is now the offset till the end of the operator

	char *name = malloc(name_len + 1);
	parser_read(state, name, name_len);
	name[name_len] = '\0';

	parser_read(state, NULL, offset - name_len); // operator

	struct mrsh_arithm_expr *value = arithm_expr(state);
	if (value == NULL) {
		free(name);
		parser_set_error(state, "expected an assignment value");
		return NULL;
	}

	struct mrsh_arithm_assign *a = mrsh_arithm_assign_create(op, name, value);
	return &a->expr;
}

static struct mrsh_arithm_expr *arithm_expr(struct mrsh_parser *state) {
	struct mrsh_arithm_expr *expr = assignment(state);
	if (expr != NULL) {
		return expr;
	}

	return ternary(state);
}

struct mrsh_arithm_expr *mrsh_parse_arithm_expr(struct mrsh_parser *state) {
	consume_whitespace(state);

	struct mrsh_arithm_expr *expr = arithm_expr(state);
	if (expr == NULL) {
		return NULL;
	}

	if (parser_peek_char(state) != '\0') {
		parser_set_error(state,
			"garbage at the end of the arithmetic expression");
		mrsh_arithm_expr_destroy(expr);
		return NULL;
	}

	return expr;
}