~bl4ckb0ne/boson

ref: 1eebe4d73a4484cd26d3b5569bb7737127c12bd0 boson/src/parser.c -rw-r--r-- 9.9 KiB
1eebe4d7Simon Zeni improve build process a month ago
                                                                                
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
#include "parser.h"
#include "ast.h"
#include "lexer.h"
#include "token.h"
#include "log.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define PATH_MAX 4096

struct parser
{
	struct lexer lexer;
	struct token *cur;
	struct token *last;
};

static bool
accept(struct parser *parser, enum token_type type) {
	if (parser->cur->type == type) {
		free(parser->last);
		parser->last = parser->cur;
		parser->cur = lexer_tokenize(&parser->lexer);
		return true;
	}
	return false;
}

static void
expect(struct parser *parser, enum token_type type)
{
	if (!accept(parser, type)) {
		fatal("expected %s, got %s", token_type_to_string(type),
				token_to_string(parser->cur));
	}
}

struct ast_expression *parse_expression(struct parser *);

void
expression_list_appened(struct ast_expression_list *list,
		struct ast_expression *expression)
{
	if (list == NULL) {
		fatal("cannot appened expression to empty list");
	}

	const size_t new_size = list->n + 1;

	list->expressions = realloc(list->expressions,
			new_size * sizeof(struct ast_expression));
	list->expressions[list->n] = expression;
	list->n = new_size;
}

void
keyword_list_appened(struct ast_keyword_list *list,
		struct ast_identifier *key, struct ast_expression *value)
{
	if (list == NULL) {
		fatal("cannot appened keyword to empty list");
	}

	const size_t new_size = list->n + 1;

	list->keys = realloc(list->keys,
			new_size * sizeof(struct ast_identifier));
	list->values = realloc(list->values,
			new_size * sizeof(struct ast_expression));
	list->keys[list->n] = key;
	list->values[list->n] = value;
	list->n = new_size;
}

struct ast_identifier *
parse_identifier(struct parser *parser)
{
	struct ast_identifier *identifier = calloc(1,
			sizeof(struct ast_identifier));
	if (!identifier) {
		fatal("failed to allocate identifier node");
	}

	identifier->data = calloc(parser->last->n + 1, sizeof(char));
	strncpy(identifier->data, parser->last->data, parser->last->n);
	identifier->n = parser->last->n;

	return identifier;
}

struct ast_string *
parse_string(struct parser *parser)
{
	struct ast_string *string = calloc(1, sizeof(struct ast_string));
	if (!string) {
		fatal("failed to allocate string node");
	}

	string->data = calloc(parser->last->n + 1, sizeof(char));
	strncpy(string->data, parser->last->data, parser->last->n);
	string->n = parser->last->n;

	return string;
}

/*
 * An array is a list containing an arbitrary number of any types
 * It is delimited by brackets, and separated by commas.
 *
 * arr = [1, 2, 3, 'soleil']
 */
struct ast_expression_list *
parse_array(struct parser *parser)
{
	struct ast_expression_list *list = calloc(1,
			sizeof(struct ast_expression_list));
	if (!list) {
		fatal("failed to allocate array");
	}

	for (;;) {
		while (accept(parser, TOKEN_EOL));

		if (accept(parser, TOKEN_RBRACK)) {
			break;
		}

		expression_list_appened(list, parse_expression(parser));

		if (accept(parser, TOKEN_RBRACK)) {
			break;
		}

		expect(parser, TOKEN_COMMA);
	}

	return list;
}

struct ast_bool *
parse_bool(struct parser *parser)
{
	struct ast_bool *boolean = calloc(1, sizeof(struct ast_bool));
	assert(boolean);

	if (parser->last->type == TOKEN_TRUE) {
		boolean->value = true;
	} else if (parser->last->type == TOKEN_FALSE) {
		boolean->value = false;
	}

	return boolean;
}

struct ast_expression *
parse_primary(struct parser *parser)
{
	struct ast_expression *expression = calloc(1,
			sizeof(struct ast_expression));
	if (!expression) {
		fatal("failed to allocate expression node");
	}

	if (accept(parser, TOKEN_IDENTIFIER)) {
		expression->type = EXPRESSION_IDENTIFIER;
		expression->data.identifier = parse_identifier(parser);
	} else if (accept(parser, TOKEN_STRING)) {
		expression->type = EXPRESSION_STRING;
		expression->data.string = parse_string(parser);
	} else if (accept(parser, TOKEN_LBRACK)) {
		expression->type = EXPRESSION_ARRAY;
		expression->data.array = parse_array(parser);
	} else if (accept(parser, TOKEN_TRUE) || accept(parser, TOKEN_FALSE)) {
		expression->type = EXPRESSION_BOOL;
		expression->data.boolean = parse_bool(parser);
	} else {
		fatal("unexpected token %s", token_to_string(parser->cur));
	}

	return expression;
}

struct ast_arguments *
parse_arguments(struct parser *parser)
{
	struct ast_arguments *arguments = calloc(1,
			sizeof(struct ast_arguments));
	assert(arguments);

	arguments->args = calloc(1, sizeof(struct ast_expression_list));
	assert(arguments->args);
	arguments->kwargs = calloc(1, sizeof(struct ast_keyword_list));
	assert(arguments->kwargs);

	for (;;) {
		while (accept(parser, TOKEN_EOL));

		if (accept(parser, TOKEN_RPAREN)) {
			break;
		}

		struct ast_expression *expression = parse_expression(parser);
		if (accept(parser, TOKEN_COLON)) {
			if (expression->type != EXPRESSION_IDENTIFIER) {
				fatal("kwarg key must be an identifier");
			}
			keyword_list_appened(arguments->kwargs,
					expression->data.identifier,
					parse_expression(parser));
		} else {
			expression_list_appened(arguments->args, expression);
		}

		if (accept(parser, TOKEN_RPAREN)) {
			break;
		}

		expect(parser, TOKEN_COMMA);
	}

	return arguments;
}

static struct ast_expression *
parse_function(struct parser *parser, struct ast_expression *left)
{
	if (left->type != EXPRESSION_IDENTIFIER) {
		fatal("function should be an identifier");
	}

	struct ast_expression *expression = calloc(1,
			sizeof(struct ast_expression));
	assert(expression);

	expression->type = EXPRESSION_FUNCTION;
	expression->data.function = calloc(1, sizeof(struct ast_function));
	assert(expression->data.function);

	expression->data.function->left = left->data.identifier;
	expression->data.function->right = parse_arguments(parser);

	return expression;
}

struct ast_expression *
parse_method(struct parser *parser, struct ast_expression *left)
{
	assert(left);
	if (left->type != EXPRESSION_IDENTIFIER
			&& left->type != EXPRESSION_STRING) {
		fatal("method must be called on an identifier or a string");
	}

	struct ast_expression *expression = calloc(1,
			sizeof(struct ast_expression));
	assert(expression);

	expression->type = EXPRESSION_METHOD;
	expression->data.method = calloc(1, sizeof(struct ast_method));
	assert(expression->data.method);

	expression->data.method->left = left;
	assert(expression->data.method->left);

	struct ast_expression *right = parse_expression(parser);
	if (right->type != EXPRESSION_FUNCTION) {
		fatal("method right side must be a function");
	}

	expression->data.method->right = right->data.function;

	return expression;
}

struct ast_expression *
parse_postfix(struct parser *parser)
{
	struct ast_expression *expression = parse_primary(parser);

	if (accept(parser, TOKEN_LPAREN)) {
		return parse_function(parser, expression);
	} else if (accept(parser, TOKEN_DOT)) {
		return parse_method(parser, expression);
	}

	return expression;
}

bool
is_assignment_op(struct parser *parser)
{
	static const enum token_type ops[] = {
		TOKEN_ASSIGN,
		TOKEN_STAREQ,
		TOKEN_SLASHEQ,
		TOKEN_MODEQ,
		TOKEN_PLUSEQ,
		TOKEN_MINEQ,
	};

	for (size_t i = 0; i < sizeof(ops) / sizeof(ops[0]); ++i) {
		if (parser->cur->type == ops[i]) {
			return true;
		}
	}

	return false;
}

enum ast_assignment_op
parse_assignment_op(struct parser *parser)
{
	if (accept(parser, TOKEN_ASSIGN)) {
		return ASSIGNMENT_ASSIGN;
	} else if (accept(parser, TOKEN_STAREQ)) {
		return ASSIGNMENT_STAREQ;
	} else if (accept(parser, TOKEN_SLASHEQ)) {
		return ASSIGNMENT_SLASHEQ;
	} else if (accept(parser, TOKEN_MODEQ)) {
		return ASSIGNMENT_MODEQ;
	} else if (accept(parser, TOKEN_PLUSEQ)) {
		return ASSIGNMENT_PLUSEQ;
	} else if (accept(parser, TOKEN_MINEQ)) {
		return ASSIGNMENT_MINEQ;
	} else {
		fatal("%s is not an assignment operation",
				token_to_string(parser->cur));
	}

	return -1;
}

struct ast_expression *
parse_assignment(struct parser *parser, struct ast_expression *left)
{
	if (left->type != EXPRESSION_IDENTIFIER) {
		fatal("assignment target must be an identifier");
	}

	struct ast_expression *expression = calloc(1,
			sizeof(struct ast_expression));
	assert(expression);

	expression->type = EXPRESSION_ASSIGNMENT;
	expression->data.assignment = calloc(1, sizeof(struct ast_assignment));
	assert(expression->data.assignment);

	expression->data.assignment->left = left->data.identifier;
	expression->data.assignment->op = parse_assignment_op(parser);
	expression->data.assignment->right = parse_expression(parser);

	return expression;
}

struct ast_expression *
parse_expression(struct parser *parser)
{
	//struct ast_expression *left = parse_or(parser);
	struct ast_expression *left = parse_postfix(parser);
	if (is_assignment_op(parser)) {
		return parse_assignment(parser, left);
	} else if (accept(parser, TOKEN_QM)) {
		fatal("todo condition expression");
	}

	return left;
}

struct ast_statement *
parse_statement(struct parser *parser)
{
	struct ast_statement *statement = calloc(1,
			sizeof(struct ast_statement));
	if (!statement) {
		fatal("failed to allocate statement node");
	}

	while (accept(parser, TOKEN_EOL));

	if (accept(parser, TOKEN_EOF)) {
		free(statement);
		return NULL;
	} else if (accept(parser, TOKEN_FOREACH)) {
		statement->type = STATEMENT_ITERATION;
		fatal("TODO iteration statement");
	} else if (accept(parser, TOKEN_IF)) {
		statement->type = STATEMENT_SELECTION;
		fatal("TODO selection statement");
	} else {
		statement->type = STATEMENT_EXPRESSION;
		statement->data.expression = parse_expression(parser);
	}

	return statement;
}

struct ast_root
parse(const char *source_dir)
{
	info("Source dir: %s", source_dir);

	char source_path[PATH_MAX] = {0};
	snprintf(source_path, sizeof(source_path), "%s/%s", source_dir,
			"meson.build");

	struct parser parser = {0};
	lexer_init(&parser.lexer, source_path);
	parser.cur = lexer_tokenize(&parser.lexer);

	struct ast_root root = { 0 };
	while (parser.cur->type != TOKEN_EOF) {
		root.statements = realloc(root.statements,
				++root.n * sizeof(struct ast_statement *));
		root.statements[root.n - 1] = parse_statement(&parser);
	}

	lexer_finish(&parser.lexer);

	return root;
}