Radicalc  Diff

Differences From Artifact [359b285bd6]:

  • File bootstrap/main.c — part of check-in [af6546d101] at 2017-07-03 00:31:17 on branch trunk — started compiling to C (user: athaudia size: 6918)

To Artifact [6f6c9039e6]:

  • File bootstrap/main.c — part of check-in [f08e11090d] at 2017-07-03 17:25:07 on branch trunk — int literal node (user: athaudia size: 7876)

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*                                                                            */
/*   TOKENISER                                                                */
/*                                                                            */
/******************************************************************************/

enum token_type {
	TOK_NONE,
	TOK_PAREN_OPEN, TOK_PAREN_CLOSE, TOK_NEWLINE, TOK_EOF,
	TOK_FUN, TOK_END,
	TOK_IDENT, TOK_LITERAL_INT
};

struct token {
	enum token_type type;
	char* str;







|







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*                                                                            */
/*   TOKENISER                                                                */
/*                                                                            */
/******************************************************************************/

enum token_type {
	TOK_NONE,
	TOK_PAREN_OPEN, TOK_PAREN_CLOSE, TOK_COMMA, TOK_NEWLINE, TOK_EOF,
	TOK_FUN, TOK_END,
	TOK_IDENT, TOK_LITERAL_INT
};

struct token {
	enum token_type type;
	char* str;
81
82
83
84
85
86
87





88
89
90
91
92
93
94
					next_ch();
					skip_ws();
				}
				else if(ch == ')') {
					add_token(TOK_PAREN_CLOSE, 0);
					next_ch();
					skip_ws();





				}
				else if(ch == '\n' || ch == '\r') {
					add_token(TOK_NEWLINE, 0);
					next_ch();
					/* collapse all versions, and multiples to a single token */
					while(ch == '\n' || ch == '\r') next_ch();
					skip_ws();







>
>
>
>
>







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
					next_ch();
					skip_ws();
				}
				else if(ch == ')') {
					add_token(TOK_PAREN_CLOSE, 0);
					next_ch();
					skip_ws();
				}
				else if(ch == ',') {
					add_token(TOK_COMMA, 0);
					next_ch();
					skip_ws();
				}
				else if(ch == '\n' || ch == '\r') {
					add_token(TOK_NEWLINE, 0);
					next_ch();
					/* collapse all versions, and multiples to a single token */
					while(ch == '\n' || ch == '\r') next_ch();
					skip_ws();
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
enum node_type { NODE_ROOT, NODE_FUNDEF, NODE_FUNCALL, NODE_LITERAL_INT };

struct node {
	struct node** childs;
	int child_count;
	char* name;
	enum node_type 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;

	return node;
}

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

struct token* expect(enum token_type type) {
	if(type == tokens[parse_pos].type) {
		++parse_pos;
		return &tokens[parse_pos-1];
	}
	else {
		printf("expected (%d), got (%d)\n", type, tokens[parse_pos].type);

		exit(-1);
	}
}

int is_next_tok(enum token_type type) {
	return tokens[parse_pos].type == type;
}



struct node* p_func_call() {
	struct node* node = node_new(NODE_FUNCALL);
	node->name = expect(TOK_IDENT)->str;
	expect(TOK_PAREN_OPEN);







	expect(TOK_LITERAL_INT);



	expect(TOK_PAREN_CLOSE);
	return node;
}

struct node* p_statement() {



	struct node* node;
	switch(tokens[parse_pos].type) {
		case TOK_IDENT:

			node = p_func_call();





			expect(TOK_NEWLINE);
			break;

		default:
			printf("can't parse statement, (%d)\n", tokens[parse_pos].type);
			exit(-1);
	}










	return node;
}

struct node* p_fun() {

	struct node* node = node_new(NODE_FUNDEF);
	expect(TOK_FUN);
	node->name = expect(TOK_IDENT)->str;
	expect(TOK_PAREN_OPEN);
	expect(TOK_PAREN_CLOSE);
	expect(TOK_NEWLINE);
	while(!is_next_tok(TOK_END)) {







>








>















|
>







>
>





>
>
>
>
>
>
>
|
>
>
>




|
>
>
>
|


>
|
>
>
>
>
>
|
|
>

|


>
>
>
>
>
>
>
>
>
>




>







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
enum node_type { NODE_ROOT, NODE_FUNDEF, NODE_FUNCALL, NODE_LITERAL_INT };

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

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;
	return node;
}

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

struct token* expect(enum token_type type) {
	if(type == tokens[parse_pos].type) {
		++parse_pos;
		return &tokens[parse_pos-1];
	}
	else {
		printf("expected (%d), got (%d), at (%d)\n",
		       type, tokens[parse_pos].type, parse_pos);
		exit(-1);
	}
}

int is_next_tok(enum token_type type) {
	return tokens[parse_pos].type == type;
}

struct node* p_expr();

struct node* p_func_call() {
	struct node* node = node_new(NODE_FUNCALL);
	node->name = expect(TOK_IDENT)->str;
	expect(TOK_PAREN_OPEN);
	printf("1\n");
	if(tokens[parse_pos].type != TOK_PAREN_CLOSE) {
		printf("2\n");
		node_add_child(node, p_expr());
		printf("3\n");
		while(tokens[parse_pos].type == TOK_COMMA) {
			printf("4\n");
			expect(TOK_COMMA);
			node_add_child(node, p_expr());
		}
	}
	expect(TOK_PAREN_CLOSE);
	return node;
}

struct node* p_var_use() {
	/* todo: implement */
}

struct node* p_expr() {
	switch(tokens[parse_pos].type) {
		case TOK_IDENT:
			if(tokens[parse_pos+1].type == TOK_PAREN_OPEN)
				return p_func_call();
			else
				return p_var_use();
		case TOK_LITERAL_INT:
		{
			struct node* node = node_new(NODE_LITERAL_INT);
			node->val = expect(TOK_LITERAL_INT)->str;
			return node;
		}
		default:
			printf("can't parse expression, (%d)\n", tokens[parse_pos].type);
			exit(-1);
	}
}

struct node* p_statement() {
	printf("p_statement\n");
	struct node* node;
	switch(tokens[parse_pos].type) {
		default:
			node = p_expr();
			expect(TOK_NEWLINE);
	}
	return node;
}

struct node* p_fun() {
	printf("p_fun\n");
	struct node* node = node_new(NODE_FUNDEF);
	expect(TOK_FUN);
	node->name = expect(TOK_IDENT)->str;
	expect(TOK_PAREN_OPEN);
	expect(TOK_PAREN_CLOSE);
	expect(TOK_NEWLINE);
	while(!is_next_tok(TOK_END)) {
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
			}
			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);
				if(j < node->child_count) printf(",\n");



			}
			print_indent(indent);
			printf(")");
			break;




		default:
			printf("(UNKNOWN TOKEN)");
	}
}

int main() {
	tokenise();
	print_tokens();
	struct node* node = parse();
	print_tree(node, 0);
	return 0;
}







|
|
>
>
>




>
>
>
>












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
			}
			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");
				else
					printf("\n");
			}
			print_indent(indent);
			printf(")");
			break;
		case NODE_LITERAL_INT:
			print_indent(indent);
			printf("%s", node->val);
			break;
		default:
			printf("(UNKNOWN TOKEN)");
	}
}

int main() {
	tokenise();
	print_tokens();
	struct node* node = parse();
	print_tree(node, 0);
	return 0;
}