Radicalc  Diff

Differences From Artifact [c05c1b326e]:

  • File bootstrap/main.c — part of check-in [84723ea2ee] at 2017-07-04 12:30:51 on branch trunk — actually using return type in output... (user: athaudia size: 8255)

To Artifact [35ddb2589d]:

  • File bootstrap/main.c — part of check-in [361458f8d6] at 2017-07-04 15:55:04 on branch trunk — function definitions aren't part of one big tree (user: athaudia size: 8993)

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
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







-
+




+
+
+
+
+








+












+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







/*                                                                            */
/*   PARSER                                                                   */
/*                                                                            */
/******************************************************************************/

int parse_pos;

enum node_type { NODE_ROOT, NODE_FUNDEF, NODE_FUNCALL, NODE_LITERAL_INT };
enum node_type { NODE_ROOT, NODE_FUNBODY, NODE_FUNDEF, NODE_FUNCALL, NODE_LITERAL_INT };

struct data_type {
	char* name;
};

struct function_parameter {
	struct data_type data_type;
	char* name;
};

struct node {
	struct node** childs;
	int child_count;
	char* name;
	enum node_type type;
	char* val;
	struct data_type data_type;
	
};

struct node* node_new(enum node_type type) {
	struct node* node = malloc(sizeof(struct node));
	node->childs = 0;
	node->child_count = 0;
	node->name = 0;
	node->type = type;
	node->val = 0;
	node->data_type.name = 0;
	return node;
}

struct function {
	char* name;
	struct data_type return_type;
	struct function_parameter* params;
	int param_count;
	struct node* body;
};

struct function* function_new() {
	struct function* fun = malloc(sizeof(struct function));
	fun->name = 0;
	fun->return_type.name = 0;
	fun->params = 0;
	fun->param_count = 0;
	fun->body = node_new(NODE_FUNBODY);
}

void node_add_child(struct node* parent, struct node* child) {
	int new_size = sizeof(struct node*) * (++parent->child_count);
	parent->childs = realloc(parent->childs, new_size);
	parent->childs[parent->child_count-1] = child;
}

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
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







-
+
-
-
+

-
+




-
+



-
+



-
+


+
+
-
-
+
+
+
+
+
+
+
+
+

+
-
-
-
-
-
-
+
+
+
+
+
+
-
+







		default:
			node = p_expr();
			expect(TOK_NEWLINE);
	}
	return node;
}

struct node* p_fun() {
struct function* p_fun() {
	printf("p_fun\n");
	struct node* node = node_new(NODE_FUNDEF);
	struct function* fun = function_new();
	expect(TOK_FUN);
	node->name = expect(TOK_IDENT)->str;
	fun->name = expect(TOK_IDENT)->str;
	expect(TOK_PAREN_OPEN);
	expect(TOK_PAREN_CLOSE);
	if(tokens[parse_pos].type == TOK_COLON) {
		expect(TOK_COLON);
		node->data_type.name = expect(TOK_IDENT)->str;
		fun->return_type.name = expect(TOK_IDENT)->str;
	}
	expect(TOK_NEWLINE);
	while(!is_next_tok(TOK_END)) {
		node_add_child(node, p_statement());
		node_add_child(fun->body, p_statement());
	}
	expect(TOK_END);
	expect(TOK_NEWLINE);
	return node;
	return fun;
}

struct function** functions;
int function_count;
struct node* parse() {
	struct node* node = node_new(NODE_ROOT);

void add_function(struct function* function) {
	functions = realloc(functions, sizeof(struct function*)*(++function_count));
	functions[function_count-1] = function;
}

void parse() {
	functions = 0;
	function_count = 0;
	parse_pos = 0;
	while(tokens[parse_pos].type != TOK_EOF) {
	switch(tokens[parse_pos].type) {
		case TOK_EOF:
			break;
		case TOK_FUN:
			node_add_child(node, p_fun());
	}
		switch(tokens[parse_pos].type) {
			case TOK_NEWLINE:
				break;
			case TOK_FUN:
				add_function(p_fun());
		}
	return node;
	}
}


/******************************************************************************/
/*                                                                            */
/*   MAIN                                                                     */
/*                                                                            */
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
356
357
358
359
360
361
362











363
364
365
366
367
368
369







-
-
-
-
-
-
-
-
-
-
-







	switch(node->type) {
		case NODE_ROOT:
			for(int j = 0; j < node->child_count; ++j) {
				print_tree(node->childs[j], 0);
				printf("\n");
			}
			break;
		case NODE_FUNDEF:
			print_indent(indent);
			printf("%s %s() {\n",
			       node->data_type.name?node->data_type.name:"void",
				   node->name);
			for(int j = 0; j < node->child_count; ++j) {
				print_tree(node->childs[j], indent+1);
				printf(";\n");
			}
			printf("}\n");
			break;
		case NODE_FUNCALL:
			print_indent(indent);
			printf("%s(\n", node->name);
			for(int j = 0; j < node->child_count; ++j) {
				print_tree(node->childs[j], indent+1);
				if(j < node->child_count-1)
					printf(",\n");
356
357
358
359
360
361
362














363
364
365
366
367
368



369
370
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+




-
-
+
+
+


			print_indent(indent);
			printf("%s", node->val);
			break;
		default:
			printf("(UNKNOWN TOKEN)");
	}
}

void print_functions() {
	for(int i = 0; i < function_count; ++i) {
		struct function* fun = functions[i];
		printf("%s %s() {\n",
			   fun->return_type.name?fun->return_type.name:"void",
			   fun->name);
		for(int j = 0; j < fun->body->child_count; ++j) {
			print_tree(fun->body->childs[j], 1);
			printf(";\n");
		}
		printf("}\n\n");
	}
}

int main() {
	tokenise();
	print_tokens();
	struct node* node = parse();
	print_tree(node, 0);
	parse();
	print_functions();
	printf("-- %d\n", function_count);
	return 0;
}