Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | annotate expressions and function calls |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
c163bb063cb1e540db8a56a36ac8321d |
| User & Date: | athaudia 2017-07-13 00:26:16.482 |
Context
|
2017-07-13
| ||
| 00:33 | mangled names check-in: fd180a44da user: athaudia tags: trunk | |
| 00:26 | annotate expressions and function calls check-in: c163bb063c user: athaudia tags: trunk | |
|
2017-07-11
| ||
| 01:03 | annotating parameters check-in: 2b6942b843 user: athaudia tags: trunk | |
Changes
Changes to bootstrap/main.c.
| ︙ | ︙ | |||
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
enum node_type { NODE_ROOT, NODE_FUNBODY, NODE_FUNDEF, NODE_FUNCALL,
NODE_VAR_USE, NODE_LITERAL_INT };
struct data_type {
char* name;
};
struct node {
struct node** childs;
int child_count;
char* name;
enum node_type type;
char* val;
char* data_type_str;
struct data_type* data_type;
| > | > | 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 |
enum node_type { NODE_ROOT, NODE_FUNBODY, NODE_FUNDEF, NODE_FUNCALL,
NODE_VAR_USE, NODE_LITERAL_INT };
struct data_type {
char* name;
};
struct function;
struct node {
struct node** childs;
int child_count;
char* name;
enum node_type type;
char* val;
char* data_type_str;
struct data_type* data_type;
struct function* fun;
};
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_str = 0;
node->data_type = 0;
node->fun = 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;
|
| ︙ | ︙ | |||
347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
struct function** functions;
int function_count;
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) {
| > | 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
struct function** functions;
int function_count;
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) {
|
| ︙ | ︙ | |||
392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
add_data_type("u8");
add_data_type("u16");
add_data_type("u32");
add_data_type("u64");
add_data_type("f32");
add_data_type("f64");
}
void annotate_data_type(struct data_type** data_type, char* name) {
if(name == 0) { /* nullpointer == void */
*data_type = &data_types[0];
return;
}
| > > > > > > > > > > > > > > > > > > > > > > > > | 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 |
add_data_type("u8");
add_data_type("u16");
add_data_type("u32");
add_data_type("u64");
add_data_type("f32");
add_data_type("f64");
}
struct data_type* get_data_type(char* name) {
for(int i = 0; i < data_type_count; ++i) {
if(strcmp(name, data_types[i].name) == 0)
return &data_types[i];
}
return 0;
}
struct function* get_function(char* name, struct data_type** params, int param_count) {
for(int i = 0; i < function_count; ++i) {
struct function* fun = functions[i];
if(strcmp(fun->name, name) == 0 && fun->param_count == param_count) {
for(int j = 0; j < param_count; ++j) {
if(fun->params[j].data_type != params[j])
goto tryagain;
}
return fun;
}
tryagain: ;
}
return 0;
}
void annotate_data_type(struct data_type** data_type, char* name) {
if(name == 0) { /* nullpointer == void */
*data_type = &data_types[0];
return;
}
|
| ︙ | ︙ | |||
416 417 418 419 420 421 422 423 424 425 426 427 428 |
void annotate_function_data_types(struct function* fun) {
annotate_data_type(&fun->return_type, fun->return_type_str);
for(int i = 0; i < fun->param_count; ++i) {
annotate_data_type(&fun->params[i].data_type, fun->params[i].data_type_str);
}
}
void annotate_data_types() {
for(int i = 0; i < function_count; ++i) {
printf("%s\n", functions[i]->name);
annotate_function_data_types(functions[i]);
}
| > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > | > | 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 |
void annotate_function_data_types(struct function* fun) {
annotate_data_type(&fun->return_type, fun->return_type_str);
for(int i = 0; i < fun->param_count; ++i) {
annotate_data_type(&fun->params[i].data_type, fun->params[i].data_type_str);
}
}
void annotate_node_data_types(struct node* node) {
for(int i = 0; i < node->child_count; ++i) {
annotate_node_data_types(node->childs[i]);
}
switch(node->type) {
case NODE_LITERAL_INT:
node->data_type = get_data_type("i32"); /* temporary */
break;
case NODE_FUNCALL: {
int size = sizeof(struct data_type*) * node->child_count;
struct data_type** params = malloc(size);
for(int i = 0; i < node->child_count; ++i) {
params[i] = node->childs[i]->data_type;
}
struct function* fun = get_function(node->name, params, node->child_count);
if(!fun) {
printf("error, function %s(", node->name);
for(int i = 0; i < node->child_count; ++i) {
printf("%s", params[i]->name);
if(i < node->child_count-1)
printf(", ");
}
printf(") not found but used\n");
exit(-1);
}
node->data_type = fun->return_type;
break;
}
case NODE_FUNBODY:
break;
default:
printf("error, not yet annotating %i\n", node->type);
exit(-1);
}
}
void annotate_data_types() {
for(int i = 0; i < function_count; ++i) {
printf("%s\n", functions[i]->name);
annotate_function_data_types(functions[i]);
}
for(int i = 0; i < function_count; ++i) {
printf("%s\n", functions[i]->name);
annotate_node_data_types(functions[i]->body);
}
}
/******************************************************************************/
/* */
/* MAIN */
/* */
/******************************************************************************/
|
| ︙ | ︙ | |||
510 511 512 513 514 515 516 |
parse();
init_data_types();
annotate_data_types();
print_functions();
printf("-- %d\n", function_count);
return 0;
}
| > > > > > > > > > > | 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
parse();
init_data_types();
annotate_data_types();
print_functions();
printf("-- %d\n", function_count);
return 0;
}
/* TODO:
- have parameter types and return types be part of function signature
*/
/* NOTES:
- value types can be stack or heap allocated behind the scenes,
having the compiler insert malloc/free style syscalls where needed.
*/
|
Changes to bootstrap/test.rcs.
|
| | > | > | 1 2 3 4 5 6 7 8 9 10 11 | fun add(a:i32, b:i32):i32 end fun print(a:i32, b:i32, c:i16) end fun main() print(10,20,add(15,15)) end |