~bl4ckb0ne/boson

ref: 1eebe4d73a4484cd26d3b5569bb7737127c12bd0 boson/src/interpreter.c -rw-r--r-- 5.7 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
#include "interpreter.h"
#include "parser.h"
#include "options.h"
#include "ast.h"
#include "hash_table.h"
#include "log.h"

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

/*
static struct hash_table *variables = NULL;

struct variable *
get_variable_value(const char *name)
{
	struct variable *var = hash_table_get(variables, name);
	if (!var) {
		fatal("unknown variable '%s'", name);
	}

	return var;
}
*/

struct object *
eval_string(struct ast_string *string)
{
	struct object *obj = calloc(1, sizeof(struct object));
	if (!obj) {
		fatal("failed to allocate string object");
	}

	obj->string.data = calloc(string->n, sizeof(char));
	strncpy(obj->string.data, string->data, string->n);
	obj->string.n = string->n;

	obj->type = OBJECT_TYPE_STRING;
	return obj;
}

static void
string_format(struct context *ctx, struct object *object,
		struct ast_arguments *arguments)
{
	if (arguments->kwargs->n != 0) {
		fatal("string format doesn't support kwargs");
	}

	char *fmt = object->string.data;
	for(size_t i = 0; i < arguments->args->n; ++i) {
		struct object *arg = eval_expression(ctx,
				arguments->args->expressions[i]);
		const char *str_arg = object_to_str(arg);

		char needle[8] = {0};
		sprintf(needle, "@%zu@", i);

		const char *found = strstr(fmt, needle);
		if (!found) {
			fatal("%s not found\n", needle);
		}
		const ptrdiff_t idx = found - fmt;

		// new size of out
		const size_t s = (strlen(fmt) - strlen(needle))
				+ strlen(str_arg) + 1;
		char *out = calloc(s + 1, sizeof(char));

		// copy until needle
		strncpy(out, fmt, idx);

		// copy argument
		strcat(out, str_arg);

		// copy remaining
		strcat(out, fmt + idx + strlen(needle));

		fmt = out;
	}
	object->string.data = fmt;
	object->string.n = strlen(fmt);
}

static struct object *
eval_string_method(struct context *ctx, struct ast_method *method)
{
	struct object *obj = eval_expression(ctx, method->left);

	struct ast_function *func = method->right;
	if (strcmp(func->left->data, "format") == 0) {
		string_format(ctx, obj, func->right);
	} else {
		fatal("TODO string method %s", func->left->data);
	}

	return obj;
}

static struct object *
eval_identifier_method(struct context *ctx, struct ast_method *method)
{
	struct ast_identifier *id = method->left->data.identifier;
	struct object *obj = NULL;
	if (strcmp(id->data, "meson") == 0) {
		obj = eval_meson_object(ctx, method->right);
	} else {
		fatal("todo identifier method");
	}
	return obj;
}

struct object *
eval_method(struct context *ctx, struct ast_method *method)
{
	struct object *obj = NULL;
	struct ast_expression *expr = method->left;
	switch (expr->type) {
	case EXPRESSION_STRING:
		obj = eval_string_method(ctx, method);
		break;
	case EXPRESSION_IDENTIFIER:
		obj = eval_identifier_method(ctx, method);
		break;
	default:
		fatal("todo method %s", ast_expression_to_str(expr));
		break;
	}

	return obj;
}

static struct object *
eval_assignment(struct context *ctx, struct ast_assignment *assignment)
{
	struct object *obj = eval_expression(ctx, assignment->right);
	obj->id = assignment->left->data;

	void **value = hash_table_put(ctx->env, obj->id);

	switch (assignment->op) {
	case ASSIGNMENT_ASSIGN:
		*value = obj;
		break;
	default:
		fatal("todo assignment");
	}

	return NULL;
}

static struct object *
eval_identifier(struct context *ctx, struct ast_identifier *identifier)
{
	return hash_table_get(ctx->env, identifier->data);
}

static struct object *
eval_array(struct context *ctx, struct ast_expression_list *array)
{
	struct object *obj = calloc(1, sizeof(struct object));
	if (!obj) {
		fatal("failed to allocate array");
	}

	obj->type = OBJECT_TYPE_ARRAY;
	obj->array.n = array->n;
	obj->array.objects = calloc(array->n, sizeof(struct object*));

	for (size_t i = 0; i < array->n; ++i) {
		struct object *item = eval_expression(ctx, array->expressions[i]);
		if (!item) {
			fatal("array item at %zu is empty", i);
		}
		obj->array.objects[i] = item;
	}

	return obj;
}

struct object *
eval_expression(struct context *ctx, struct ast_expression *expression)
{
	struct object *obj = NULL;
	switch (expression->type) {
	case EXPRESSION_FUNCTION:
		obj = eval_function(ctx, expression->data.function);
		break;
	case EXPRESSION_METHOD:
		obj = eval_method(ctx, expression->data.method);
		break;
	case EXPRESSION_STRING:
		obj = eval_string(expression->data.string);
		break;
	case EXPRESSION_ASSIGNMENT:
		obj = eval_assignment(ctx, expression->data.assignment);
		break;
	case EXPRESSION_IDENTIFIER:
		obj = eval_identifier(ctx, expression->data.identifier);
		break;
	case EXPRESSION_ARRAY:
		obj = eval_array(ctx, expression->data.array);
		break;
	default:
		fatal("todo handle expression %s",
				ast_expression_to_str(expression));
		break;
	}

	return obj;
}

static void
check_first(struct ast_statement *statement)
{
	if (statement->type != STATEMENT_EXPRESSION) {
		goto check_first_err;
	}

	struct ast_expression *expr = statement->data.expression;
	if (expr->type != EXPRESSION_FUNCTION) {
		goto check_first_err;
	}

	if (strcmp(expr->data.function->left->data, "project") != 0) {
		goto check_first_err;
	}

	return;
check_first_err:
	fatal("first statement must be a call to function 'project'");
	return;
}

struct context
interpret_ast(struct ast_root *root)
{
	struct context ctx = {0};

	ctx.options = options_create();
	ctx.env = hash_table_create(8u);

	check_first(root->statements[0]);

	for (size_t i = 0; i < root->n - 1; ++i) {
		struct ast_statement *statement = root->statements[i];

		switch(statement->type) {
		case STATEMENT_EXPRESSION:
			eval_expression(&ctx, statement->data.expression);
			break;
		case STATEMENT_SELECTION:
		case STATEMENT_ITERATION:
		default:
			fatal("unknown statement");
		}
	}

	return ctx;
}