mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
Massive Refactor and If statements
This commit is contained in:
+8
-3
@@ -1,6 +1,11 @@
|
|||||||
int main() {
|
int main() {
|
||||||
float a = 0.5;
|
int x = 15;
|
||||||
float b = 1.5;
|
int y = 4;
|
||||||
int c = a < b;
|
x = x & (~y);
|
||||||
|
if (x > 0) {
|
||||||
|
x = 123;
|
||||||
|
} else {
|
||||||
|
x = 321;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,17 @@
|
|||||||
define void @main() {
|
define void @main() {
|
||||||
entry:
|
entry:
|
||||||
|
%0 = alloca i64, align 8 ; x
|
||||||
|
store i64 zeroinitializer, i64* %0
|
||||||
|
store i64 15, i64* %0
|
||||||
|
%1 = load i64, i64* %0
|
||||||
|
%2 = icmp sgt i64 %1, 0
|
||||||
|
br i1 %2, label %if-then, label %if-else
|
||||||
|
if-then:
|
||||||
|
store i64 123, i64* %0
|
||||||
|
br label %if-end
|
||||||
|
if-else:
|
||||||
|
store i64 321, i64* %0
|
||||||
|
br label %if-end
|
||||||
|
if-end:
|
||||||
ret void
|
ret void
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,8 @@
|
|||||||
main :: proc() {
|
main :: proc() {
|
||||||
|
x : int = 15;
|
||||||
|
if x > 0 {
|
||||||
|
x = 123;
|
||||||
|
} else {
|
||||||
|
x = 321;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ rem del "..\examples\test.bc"
|
|||||||
call ..\bin\odin.exe ..\examples/test.odin
|
call ..\bin\odin.exe ..\examples/test.odin
|
||||||
call lli ..\examples/test.ll
|
call lli ..\examples/test.ll
|
||||||
rem call opt -mem2reg ..\examples/test.ll > ..\examples/test.bc
|
rem call opt -mem2reg ..\examples/test.ll > ..\examples/test.bc
|
||||||
rem call llvm-dis ..\examples/test.bc -o..\examples/test.ll
|
rem call llvm-dis ..\examples/test.bc -o ..\examples/test.ll
|
||||||
rem call clang ..\examples/test.c -O1 -S -emit-llvm -o ..\examples/test-c.ll
|
call clang ..\examples/test.c -O0 -S -emit-llvm -o ..\examples/test-c.ll
|
||||||
|
|||||||
+30
-32
@@ -63,7 +63,8 @@ b32 has_init(DeclInfo *d) {
|
|||||||
if (d->init_expr != NULL)
|
if (d->init_expr != NULL)
|
||||||
return true;
|
return true;
|
||||||
if (d->proc_decl != NULL) {
|
if (d->proc_decl != NULL) {
|
||||||
if (d->proc_decl->proc_decl.body != NULL)
|
ast_node(pd, ProcDecl, d->proc_decl);
|
||||||
|
if (pd->body != NULL)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -466,7 +467,7 @@ void add_entity_use(CheckerInfo *i, AstNode *identifier, Entity *entity) {
|
|||||||
|
|
||||||
|
|
||||||
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclInfo *d) {
|
void add_file_entity(Checker *c, AstNode *identifier, Entity *e, DeclInfo *d) {
|
||||||
GB_ASSERT(are_strings_equal(identifier->ident.token.string, e->token.string));
|
GB_ASSERT(are_strings_equal(identifier->Ident.token.string, e->token.string));
|
||||||
|
|
||||||
add_entity(c, c->global_scope, identifier, e);
|
add_entity(c, c->global_scope, identifier, e);
|
||||||
map_set(&c->info.entities, hash_pointer(e), d);
|
map_set(&c->info.entities, hash_pointer(e), d);
|
||||||
@@ -537,20 +538,18 @@ void check_parsed_files(Checker *c) {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
switch (decl->kind) {
|
switch (decl->kind) {
|
||||||
case AstNode_BadDecl:
|
case_ast_node(bd, BadDecl, decl);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_VarDecl: {
|
|
||||||
auto *vd = &decl->var_decl;
|
|
||||||
|
|
||||||
|
case_ast_node(vd, VarDecl, decl);
|
||||||
switch (vd->kind) {
|
switch (vd->kind) {
|
||||||
case Declaration_Immutable: {
|
case Declaration_Immutable: {
|
||||||
for (AstNode *name = vd->name_list, *value = vd->value_list;
|
for (AstNode *name = vd->name_list, *value = vd->value_list;
|
||||||
name != NULL && value != NULL;
|
name != NULL && value != NULL;
|
||||||
name = name->next, value = value->next) {
|
name = name->next, value = value->next) {
|
||||||
GB_ASSERT(name->kind == AstNode_Ident);
|
ast_node(n, Ident, name);
|
||||||
ExactValue v = {ExactValue_Invalid};
|
ExactValue v = {ExactValue_Invalid};
|
||||||
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->ident.token, NULL, v);
|
Entity *e = make_entity_constant(c->allocator, c->context.scope, n->token, NULL, v);
|
||||||
DeclInfo *di = make_declaration_info(c->allocator, c->global_scope);
|
DeclInfo *di = make_declaration_info(c->allocator, c->global_scope);
|
||||||
di->type_expr = vd->type;
|
di->type_expr = vd->type;
|
||||||
di->init_expr = value;
|
di->init_expr = value;
|
||||||
@@ -582,7 +581,8 @@ void check_parsed_files(Checker *c) {
|
|||||||
|
|
||||||
AstNode *value = vd->value_list;
|
AstNode *value = vd->value_list;
|
||||||
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
||||||
Entity *e = make_entity_variable(c->allocator, c->global_scope, name->ident.token, NULL);
|
ast_node(n, Ident, name);
|
||||||
|
Entity *e = make_entity_variable(c->allocator, c->global_scope, n->token, NULL);
|
||||||
entities[entity_index++] = e;
|
entities[entity_index++] = e;
|
||||||
|
|
||||||
DeclInfo *d = di;
|
DeclInfo *d = di;
|
||||||
@@ -600,41 +600,39 @@ void check_parsed_files(Checker *c) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
case_end;
|
||||||
|
|
||||||
|
case_ast_node(td, TypeDecl, decl);
|
||||||
} break;
|
ast_node(n, Ident, td->name);
|
||||||
|
Entity *e = make_entity_type_name(c->allocator, c->global_scope, n->token, NULL);
|
||||||
case AstNode_TypeDecl: {
|
|
||||||
AstNode *identifier = decl->type_decl.name;
|
|
||||||
Entity *e = make_entity_type_name(c->allocator, c->global_scope, identifier->ident.token, NULL);
|
|
||||||
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
||||||
d->type_expr = decl->type_decl.type;
|
d->type_expr = td->type;
|
||||||
add_file_entity(c, identifier, e, d);
|
add_file_entity(c, td->name, e, d);
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_AliasDecl: {
|
case_ast_node(ad, AliasDecl, decl);
|
||||||
AstNode *identifier = decl->alias_decl.name;
|
ast_node(n, Ident, ad->name);
|
||||||
Entity *e = make_entity_alias_name(c->allocator, c->global_scope, identifier->ident.token, NULL);
|
Entity *e = make_entity_alias_name(c->allocator, c->global_scope, n->token, NULL);
|
||||||
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
||||||
d->type_expr = decl->alias_decl.type;
|
d->type_expr = ad->type;
|
||||||
add_file_entity(c, identifier, e, d);
|
add_file_entity(c, ad->name, e, d);
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ProcDecl: {
|
case_ast_node(pd, ProcDecl, decl);
|
||||||
AstNode *identifier = decl->proc_decl.name;
|
ast_node(n, Ident, pd->name);
|
||||||
Token token = identifier->ident.token;
|
Token token = n->token;
|
||||||
Entity *e = make_entity_procedure(c->allocator, c->global_scope, token, NULL);
|
Entity *e = make_entity_procedure(c->allocator, c->global_scope, token, NULL);
|
||||||
add_entity(c, c->global_scope, identifier, e);
|
add_entity(c, c->global_scope, pd->name, e);
|
||||||
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
DeclInfo *d = make_declaration_info(c->allocator, e->parent);
|
||||||
d->proc_decl = decl;
|
d->proc_decl = decl;
|
||||||
map_set(&c->info.entities, hash_pointer(e), d);
|
map_set(&c->info.entities, hash_pointer(e), d);
|
||||||
e->order = gb_array_count(c->info.entities.entries);
|
e->order = gb_array_count(c->info.entities.entries);
|
||||||
|
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ImportDecl:
|
case_ast_node(id, ImportDecl, decl);
|
||||||
// NOTE(bill): ignore
|
// NOTE(bill): ignore
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
error(&c->error_collector, ast_node_token(decl), "Only declarations are allowed at file scope");
|
error(&c->error_collector, ast_node_token(decl), "Only declarations are allowed at file scope");
|
||||||
|
|||||||
+220
-191
@@ -16,7 +16,7 @@ void check_proc_body (Checker *c, Token token, DeclInfo *decl, Type
|
|||||||
void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
||||||
GB_ASSERT(node->kind == AstNode_StructType);
|
GB_ASSERT(node->kind == AstNode_StructType);
|
||||||
GB_ASSERT(struct_type->kind == Type_Structure);
|
GB_ASSERT(struct_type->kind == Type_Structure);
|
||||||
auto *st = &node->struct_type;
|
ast_node(st, StructType, node);
|
||||||
if (st->field_count == 0) {
|
if (st->field_count == 0) {
|
||||||
error(&c->error_collector, ast_node_token(node), "Empty struct{} definition");
|
error(&c->error_collector, ast_node_token(node), "Empty struct{} definition");
|
||||||
return;
|
return;
|
||||||
@@ -28,7 +28,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
|||||||
|
|
||||||
isize field_count = 0;
|
isize field_count = 0;
|
||||||
for (AstNode *field = st->field_list; field != NULL; field = field->next) {
|
for (AstNode *field = st->field_list; field != NULL; field = field->next) {
|
||||||
for (AstNode *name = field->field.name_list; name != NULL; name = name->next) {
|
for (AstNode *name = field->Field.name_list; name != NULL; name = name->next) {
|
||||||
GB_ASSERT(name->kind == AstNode_Ident);
|
GB_ASSERT(name->kind == AstNode_Ident);
|
||||||
field_count++;
|
field_count++;
|
||||||
}
|
}
|
||||||
@@ -37,10 +37,11 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
|||||||
Entity **fields = gb_alloc_array(c->allocator, Entity *, st->field_count);
|
Entity **fields = gb_alloc_array(c->allocator, Entity *, st->field_count);
|
||||||
isize field_index = 0;
|
isize field_index = 0;
|
||||||
for (AstNode *field = st->field_list; field != NULL; field = field->next) {
|
for (AstNode *field = st->field_list; field != NULL; field = field->next) {
|
||||||
Type *type = check_type(c, field->field.type);
|
ast_node(f, Field, field);
|
||||||
for (AstNode *name = field->field.name_list; name != NULL; name = name->next) {
|
Type *type = check_type(c, f->type);
|
||||||
GB_ASSERT(name->kind == AstNode_Ident);
|
for (AstNode *name = f->name_list; name != NULL; name = name->next) {
|
||||||
Token name_token = name->ident.token;
|
ast_node(i, Ident, name);
|
||||||
|
Token name_token = i->token;
|
||||||
// TODO(bill): is the curr_scope correct?
|
// TODO(bill): is the curr_scope correct?
|
||||||
Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type);
|
Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type);
|
||||||
u64 key = hash_string(name_token.string);
|
u64 key = hash_string(name_token.string);
|
||||||
@@ -67,13 +68,14 @@ Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize fiel
|
|||||||
Entity **variables = gb_alloc_array(c->allocator, Entity *, field_count);
|
Entity **variables = gb_alloc_array(c->allocator, Entity *, field_count);
|
||||||
isize variable_index = 0;
|
isize variable_index = 0;
|
||||||
for (AstNode *field = field_list; field != NULL; field = field->next) {
|
for (AstNode *field = field_list; field != NULL; field = field->next) {
|
||||||
GB_ASSERT(field->kind == AstNode_Field);
|
ast_node(f, Field, field);
|
||||||
AstNode *type_expr = field->field.type;
|
AstNode *type_expr = f->type;
|
||||||
if (type_expr) {
|
if (type_expr) {
|
||||||
Type *type = check_type(c, type_expr);
|
Type *type = check_type(c, type_expr);
|
||||||
for (AstNode *name = field->field.name_list; name != NULL; name = name->next) {
|
for (AstNode *name = f->name_list; name != NULL; name = name->next) {
|
||||||
if (name->kind == AstNode_Ident) {
|
if (name->kind == AstNode_Ident) {
|
||||||
Entity *param = make_entity_param(c->allocator, scope, name->ident.token, type);
|
ast_node(i, Ident, name);
|
||||||
|
Entity *param = make_entity_param(c->allocator, scope, i->token, type);
|
||||||
add_entity(c, scope, name, param);
|
add_entity(c, scope, name, param);
|
||||||
variables[variable_index++] = param;
|
variables[variable_index++] = param;
|
||||||
} else {
|
} else {
|
||||||
@@ -117,19 +119,21 @@ Type *check_get_results(Checker *c, Scope *scope, AstNode *list, isize list_coun
|
|||||||
|
|
||||||
|
|
||||||
void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
|
void check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node) {
|
||||||
isize param_count = proc_type_node->proc_type.param_count;
|
ast_node(pt, ProcType, proc_type_node);
|
||||||
isize result_count = proc_type_node->proc_type.result_count;
|
|
||||||
|
isize param_count = pt->param_count;
|
||||||
|
isize result_count = pt->result_count;
|
||||||
|
|
||||||
// gb_printf("%td -> %td\n", param_count, result_count);
|
// gb_printf("%td -> %td\n", param_count, result_count);
|
||||||
|
|
||||||
Type *params = check_get_params(c, c->context.scope, proc_type_node->proc_type.param_list, param_count);
|
Type *params = check_get_params(c, c->context.scope, pt->param_list, param_count);
|
||||||
Type *results = check_get_results(c, c->context.scope, proc_type_node->proc_type.result_list, result_count);
|
Type *results = check_get_results(c, c->context.scope, pt->result_list, result_count);
|
||||||
|
|
||||||
type->procedure.scope = c->context.scope;
|
type->procedure.scope = c->context.scope;
|
||||||
type->procedure.params = params;
|
type->procedure.params = params;
|
||||||
type->procedure.param_count = proc_type_node->proc_type.param_count;
|
type->procedure.param_count = pt->param_count;
|
||||||
type->procedure.results = results;
|
type->procedure.results = results;
|
||||||
type->procedure.result_count = proc_type_node->proc_type.result_count;
|
type->procedure.result_count = pt->result_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -137,10 +141,11 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
|
|||||||
GB_ASSERT(n->kind == AstNode_Ident);
|
GB_ASSERT(n->kind == AstNode_Ident);
|
||||||
o->mode = Addressing_Invalid;
|
o->mode = Addressing_Invalid;
|
||||||
o->expr = n;
|
o->expr = n;
|
||||||
Entity *e = scope_lookup_entity(c->context.scope, n->ident.token.string);
|
ast_node(i, Ident, n);
|
||||||
|
Entity *e = scope_lookup_entity(c->context.scope, i->token.string);
|
||||||
if (e == NULL) {
|
if (e == NULL) {
|
||||||
error(&c->error_collector, n->ident.token,
|
error(&c->error_collector, i->token,
|
||||||
"Undeclared type or identifier `%.*s`", LIT(n->ident.token.string));
|
"Undeclared type or identifier `%.*s`", LIT(i->token.string));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
add_entity_use(&c->info, n, e);
|
add_entity_use(&c->info, n, e);
|
||||||
@@ -155,7 +160,7 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (e->type == NULL) {
|
if (e->type == NULL) {
|
||||||
GB_PANIC("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(n->ident.token.string));
|
GB_PANIC("Compiler error: How did this happen? type: %s; identifier: %.*s\n", type_to_string(e->type), LIT(i->token.string));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,7 +235,7 @@ Type *check_type_expr_extra(Checker *c, AstNode *e, Type *named_type) {
|
|||||||
defer (gb_string_free(err_str));
|
defer (gb_string_free(err_str));
|
||||||
|
|
||||||
switch (e->kind) {
|
switch (e->kind) {
|
||||||
case AstNode_Ident: {
|
case_ast_node(i, Ident, e);
|
||||||
Operand o = {};
|
Operand o = {};
|
||||||
check_identifier(c, &o, e, named_type);
|
check_identifier(c, &o, e, named_type);
|
||||||
switch (o.mode) {
|
switch (o.mode) {
|
||||||
@@ -252,46 +257,48 @@ Type *check_type_expr_extra(Checker *c, AstNode *e, Type *named_type) {
|
|||||||
error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
|
error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ParenExpr:
|
case_ast_node(pe, ParenExpr, e);
|
||||||
return check_type(c, e->paren_expr.expr, named_type);
|
return check_type(c, pe->expr, named_type);
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_ArrayType:
|
|
||||||
if (e->array_type.count != NULL) {
|
case_ast_node(at, ArrayType, e);
|
||||||
|
if (at->count != NULL) {
|
||||||
Type *t = make_type_array(c->allocator,
|
Type *t = make_type_array(c->allocator,
|
||||||
check_type(c, e->array_type.elem),
|
check_type(c, at->elem),
|
||||||
check_array_count(c, e->array_type.count));
|
check_array_count(c, at->count));
|
||||||
set_base_type(named_type, t);
|
set_base_type(named_type, t);
|
||||||
return t;
|
return t;
|
||||||
} else {
|
} else {
|
||||||
Type *t = make_type_slice(c->allocator, check_type(c, e->array_type.elem));
|
Type *t = make_type_slice(c->allocator, check_type(c, at->elem));
|
||||||
set_base_type(named_type, t);
|
set_base_type(named_type, t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_StructType: {
|
case_ast_node(st, StructType, e);
|
||||||
Type *t = make_type_structure(c->allocator);
|
Type *t = make_type_structure(c->allocator);
|
||||||
set_base_type(named_type, t);
|
set_base_type(named_type, t);
|
||||||
check_struct_type(c, t, e);
|
check_struct_type(c, t, e);
|
||||||
return t;
|
return t;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_PointerType: {
|
case_ast_node(pt, PointerType, e);
|
||||||
Type *t = make_type_pointer(c->allocator, check_type(c, e->pointer_type.type));
|
Type *t = make_type_pointer(c->allocator, check_type(c, pt->type));
|
||||||
set_base_type(named_type, t);
|
set_base_type(named_type, t);
|
||||||
return t;
|
return t;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ProcType: {
|
case_ast_node(pt, ProcType, e);
|
||||||
Type *t = alloc_type(c->allocator, Type_Procedure);
|
Type *t = alloc_type(c->allocator, Type_Procedure);
|
||||||
set_base_type(named_type, t);
|
set_base_type(named_type, t);
|
||||||
check_open_scope(c, e);
|
check_open_scope(c, e);
|
||||||
check_procedure_type(c, t, e);
|
check_procedure_type(c, t, e);
|
||||||
check_close_scope(c);
|
check_close_scope(c);
|
||||||
return t;
|
return t;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
err_str = expr_to_string(e);
|
err_str = expr_to_string(e);
|
||||||
@@ -312,7 +319,7 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
|
|||||||
defer (gb_string_free(err_str));
|
defer (gb_string_free(err_str));
|
||||||
|
|
||||||
switch (e->kind) {
|
switch (e->kind) {
|
||||||
case AstNode_Ident: {
|
case_ast_node(i, Ident, e);
|
||||||
Operand operand = {};
|
Operand operand = {};
|
||||||
check_identifier(c, &operand, e, named_type);
|
check_identifier(c, &operand, e, named_type);
|
||||||
switch (operand.mode) {
|
switch (operand.mode) {
|
||||||
@@ -334,9 +341,9 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
|
|||||||
error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
|
error(&c->error_collector, ast_node_token(e), "`%s` used as a type when not a type", err_str);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_SelectorExpr: {
|
case_ast_node(se, SelectorExpr, e);
|
||||||
Operand o = {};
|
Operand o = {};
|
||||||
check_selector(c, &o, e);
|
check_selector(c, &o, e);
|
||||||
|
|
||||||
@@ -344,43 +351,44 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
|
|||||||
set_base_type(type, o.type);
|
set_base_type(type, o.type);
|
||||||
return o.type;
|
return o.type;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ParenExpr:
|
case_ast_node(pe, ParenExpr, e);
|
||||||
return check_type(c, e->paren_expr.expr, named_type);
|
return check_type(c, pe->expr, named_type);
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_ArrayType: {
|
case_ast_node(at, ArrayType, e);
|
||||||
if (e->array_type.count != NULL) {
|
if (at->count != NULL) {
|
||||||
type = make_type_array(c->allocator,
|
type = make_type_array(c->allocator,
|
||||||
check_type(c, e->array_type.elem),
|
check_type(c, at->elem),
|
||||||
check_array_count(c, e->array_type.count));
|
check_array_count(c, at->count));
|
||||||
set_base_type(named_type, type);
|
set_base_type(named_type, type);
|
||||||
} else {
|
} else {
|
||||||
type = make_type_slice(c->allocator, check_type(c, e->array_type.elem));
|
type = make_type_slice(c->allocator, check_type(c, at->elem));
|
||||||
set_base_type(named_type, type);
|
set_base_type(named_type, type);
|
||||||
}
|
}
|
||||||
goto end;
|
goto end;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_StructType: {
|
case_ast_node(st, StructType, e);
|
||||||
type = make_type_structure(c->allocator);
|
type = make_type_structure(c->allocator);
|
||||||
set_base_type(named_type, type);
|
set_base_type(named_type, type);
|
||||||
check_struct_type(c, type, e);
|
check_struct_type(c, type, e);
|
||||||
goto end;
|
goto end;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_PointerType: {
|
case_ast_node(pt, PointerType, e);
|
||||||
type = make_type_pointer(c->allocator, check_type(c, e->pointer_type.type));
|
type = make_type_pointer(c->allocator, check_type(c, pt->type));
|
||||||
set_base_type(named_type, type);
|
set_base_type(named_type, type);
|
||||||
goto end;
|
goto end;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ProcType: {
|
case_ast_node(pt, ProcType, e);
|
||||||
type = alloc_type(c->allocator, Type_Procedure);
|
type = alloc_type(c->allocator, Type_Procedure);
|
||||||
set_base_type(named_type, type);
|
set_base_type(named_type, type);
|
||||||
check_procedure_type(c, type, e);
|
check_procedure_type(c, type, e);
|
||||||
goto end;
|
goto end;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
err_str = expr_to_string(e);
|
err_str = expr_to_string(e);
|
||||||
@@ -581,7 +589,8 @@ void check_is_expressible(Checker *c, Operand *o, Type *type) {
|
|||||||
void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
|
void check_unary_expr(Checker *c, Operand *o, Token op, AstNode *node) {
|
||||||
if (op.kind == Token_Pointer) { // Pointer address
|
if (op.kind == Token_Pointer) { // Pointer address
|
||||||
if (o->mode != Addressing_Variable) {
|
if (o->mode != Addressing_Variable) {
|
||||||
gbString str = expr_to_string(node->unary_expr.expr);
|
ast_node(ue, UnaryExpr, node);
|
||||||
|
gbString str = expr_to_string(ue->expr);
|
||||||
defer (gb_string_free(str));
|
defer (gb_string_free(str));
|
||||||
error(&c->error_collector, op, "Cannot take the pointer address of `%s`", str);
|
error(&c->error_collector, op, "Cannot take the pointer address of `%s`", str);
|
||||||
o->mode = Addressing_Invalid;
|
o->mode = Addressing_Invalid;
|
||||||
@@ -672,8 +681,10 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
|
|||||||
gbString err_str = NULL;
|
gbString err_str = NULL;
|
||||||
defer (gb_string_free(err_str));
|
defer (gb_string_free(err_str));
|
||||||
|
|
||||||
check_expr(c, x, node->binary_expr.left);
|
ast_node(be, BinaryExpr, node);
|
||||||
check_expr(c, y, node->binary_expr.right);
|
|
||||||
|
check_expr(c, x, be->left);
|
||||||
|
check_expr(c, y, be->right);
|
||||||
if (x->mode == Addressing_Invalid) return;
|
if (x->mode == Addressing_Invalid) return;
|
||||||
if (y->mode == Addressing_Invalid) {
|
if (y->mode == Addressing_Invalid) {
|
||||||
x->mode = Addressing_Invalid;
|
x->mode = Addressing_Invalid;
|
||||||
@@ -689,7 +700,7 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Token op = node->binary_expr.op;
|
Token op = be->op;
|
||||||
if (token_is_comparison(op)) {
|
if (token_is_comparison(op)) {
|
||||||
check_comparison(c, x, y, op);
|
check_comparison(c, x, y, op);
|
||||||
return;
|
return;
|
||||||
@@ -770,19 +781,21 @@ void update_expr_type(Checker *c, AstNode *e, Type *type) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
switch (e->kind) {
|
switch (e->kind) {
|
||||||
case AstNode_UnaryExpr:
|
case_ast_node(ue, UnaryExpr, e);
|
||||||
if (found->value.kind != ExactValue_Invalid)
|
if (found->value.kind != ExactValue_Invalid)
|
||||||
break;
|
break;
|
||||||
update_expr_type(c, e->unary_expr.expr, type);
|
update_expr_type(c, ue->expr, type);
|
||||||
break;
|
break;
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_BinaryExpr:
|
case_ast_node(be, BinaryExpr, e);
|
||||||
if (found->value.kind != ExactValue_Invalid)
|
if (found->value.kind != ExactValue_Invalid)
|
||||||
break;
|
break;
|
||||||
if (!token_is_comparison(e->binary_expr.op)) {
|
if (!token_is_comparison(be->op)) {
|
||||||
update_expr_type(c, e->binary_expr.left, type);
|
update_expr_type(c, be->left, type);
|
||||||
update_expr_type(c, e->binary_expr.right, type);
|
update_expr_type(c, be->right, type);
|
||||||
}
|
}
|
||||||
|
case_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_type_untyped(type)) {
|
if (is_type_untyped(type)) {
|
||||||
@@ -946,7 +959,8 @@ Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
|
|||||||
if (type->kind == Type_Pointer)
|
if (type->kind == Type_Pointer)
|
||||||
type = get_base_type(type->pointer.element);
|
type = get_base_type(type->pointer.element);
|
||||||
|
|
||||||
String field_str = field_node->ident.token.string;
|
ast_node(i, Ident, field_node);
|
||||||
|
String field_str = i->token.string;
|
||||||
switch (type->kind) {
|
switch (type->kind) {
|
||||||
case Type_Structure:
|
case Type_Structure:
|
||||||
for (isize i = 0; i < type->structure.field_count; i++) {
|
for (isize i = 0; i < type->structure.field_count; i++) {
|
||||||
@@ -970,8 +984,9 @@ Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
|
|||||||
void check_selector(Checker *c, Operand *operand, AstNode *node) {
|
void check_selector(Checker *c, Operand *operand, AstNode *node) {
|
||||||
GB_ASSERT(node->kind == AstNode_SelectorExpr);
|
GB_ASSERT(node->kind == AstNode_SelectorExpr);
|
||||||
|
|
||||||
AstNode *op_expr = node->selector_expr.expr;
|
ast_node(se, SelectorExpr, node);
|
||||||
AstNode *selector = node->selector_expr.selector;
|
AstNode *op_expr = se->expr;
|
||||||
|
AstNode *selector = se->selector;
|
||||||
if (selector) {
|
if (selector) {
|
||||||
Entity *entity = lookup_field(operand->type, selector);
|
Entity *entity = lookup_field(operand->type, selector);
|
||||||
if (entity == NULL) {
|
if (entity == NULL) {
|
||||||
@@ -999,7 +1014,7 @@ void check_selector(Checker *c, Operand *operand, AstNode *node) {
|
|||||||
|
|
||||||
b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
|
b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id) {
|
||||||
GB_ASSERT(call->kind == AstNode_CallExpr);
|
GB_ASSERT(call->kind == AstNode_CallExpr);
|
||||||
auto *ce = &call->call_expr;
|
ast_node(ce, CallExpr, call);
|
||||||
BuiltinProcedure *bp = &builtin_procedures[id];
|
BuiltinProcedure *bp = &builtin_procedures[id];
|
||||||
{
|
{
|
||||||
char *err = NULL;
|
char *err = NULL;
|
||||||
@@ -1008,8 +1023,9 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
|||||||
if (ce->arg_list_count > bp->arg_count && !bp->variadic)
|
if (ce->arg_list_count > bp->arg_count && !bp->variadic)
|
||||||
err = "Too many";
|
err = "Too many";
|
||||||
if (err) {
|
if (err) {
|
||||||
|
ast_node(proc, Ident, ce->proc);
|
||||||
error(&c->error_collector, ce->close, "`%s` arguments for `%.*s`, expected %td, got %td",
|
error(&c->error_collector, ce->close, "`%s` arguments for `%.*s`, expected %td, got %td",
|
||||||
err, LIT(call->call_expr.proc->ident.token.string),
|
err, LIT(proc->token.string),
|
||||||
bp->arg_count, ce->arg_list_count);
|
bp->arg_count, ce->arg_list_count);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1093,9 +1109,10 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
|||||||
isize index = 0;
|
isize index = 0;
|
||||||
Entity *entity = lookup_field(type, field_arg, &index);
|
Entity *entity = lookup_field(type, field_arg, &index);
|
||||||
if (entity == NULL) {
|
if (entity == NULL) {
|
||||||
|
ast_node(arg, Ident, field_arg);
|
||||||
gbString type_str = type_to_string(type);
|
gbString type_str = type_to_string(type);
|
||||||
error(&c->error_collector, ast_node_token(ce->arg_list),
|
error(&c->error_collector, ast_node_token(ce->arg_list),
|
||||||
"`%s` has no field named `%.*s`", type_str, LIT(field_arg->ident.token.string));
|
"`%s` has no field named `%.*s`", type_str, LIT(arg->token.string));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1112,7 +1129,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
|||||||
error(&c->error_collector, ast_node_token(arg), "`%s` is not a selector expression", str);
|
error(&c->error_collector, ast_node_token(arg), "`%s` is not a selector expression", str);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto *s = &arg->selector_expr;
|
ast_node(s, SelectorExpr, arg);
|
||||||
|
|
||||||
check_expr(c, operand, s->expr);
|
check_expr(c, operand, s->expr);
|
||||||
if (operand->mode == Addressing_Invalid)
|
if (operand->mode == Addressing_Invalid)
|
||||||
@@ -1128,9 +1145,10 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
|||||||
isize index = 0;
|
isize index = 0;
|
||||||
Entity *entity = lookup_field(type, s->selector, &index);
|
Entity *entity = lookup_field(type, s->selector, &index);
|
||||||
if (entity == NULL) {
|
if (entity == NULL) {
|
||||||
|
ast_node(i, Ident, s->selector);
|
||||||
gbString type_str = type_to_string(type);
|
gbString type_str = type_to_string(type);
|
||||||
error(&c->error_collector, ast_node_token(arg),
|
error(&c->error_collector, ast_node_token(arg),
|
||||||
"`%s` has no field named `%.*s`", type_str, LIT(s->selector->ident.token.string));
|
"`%s` has no field named `%.*s`", type_str, LIT(i->token.string));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1267,7 +1285,7 @@ b32 check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id)
|
|||||||
void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
|
void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode *call) {
|
||||||
GB_ASSERT(call->kind == AstNode_CallExpr);
|
GB_ASSERT(call->kind == AstNode_CallExpr);
|
||||||
GB_ASSERT(proc_type->kind == Type_Procedure);
|
GB_ASSERT(proc_type->kind == Type_Procedure);
|
||||||
auto *ce = &call->call_expr;
|
ast_node(ce, CallExpr, call);
|
||||||
isize error_code = 0;
|
isize error_code = 0;
|
||||||
isize param_index = 0;
|
isize param_index = 0;
|
||||||
isize param_count = 0;
|
isize param_count = 0;
|
||||||
@@ -1341,7 +1359,7 @@ void check_call_arguments(Checker *c, Operand *operand, Type *proc_type, AstNode
|
|||||||
|
|
||||||
ExpressionKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
|
ExpressionKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
|
||||||
GB_ASSERT(call->kind == AstNode_CallExpr);
|
GB_ASSERT(call->kind == AstNode_CallExpr);
|
||||||
auto *ce = &call->call_expr;
|
ast_node(ce, CallExpr, call);
|
||||||
check_expr_or_type(c, operand, ce->proc);
|
check_expr_or_type(c, operand, ce->proc);
|
||||||
|
|
||||||
if (operand->mode == Addressing_Invalid) {
|
if (operand->mode == Addressing_Invalid) {
|
||||||
@@ -1492,16 +1510,17 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
o->type = &basic_types[Basic_Invalid];
|
o->type = &basic_types[Basic_Invalid];
|
||||||
|
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
case AstNode_BadExpr:
|
case_ast_node(be, BadExpr, node)
|
||||||
goto error;
|
goto error;
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_Ident:
|
case_ast_node(i, Ident, node);
|
||||||
check_identifier(c, o, node, type_hint);
|
check_identifier(c, o, node, type_hint);
|
||||||
break;
|
case_end;
|
||||||
case AstNode_BasicLit: {
|
|
||||||
|
case_ast_node(bl, BasicLit, node);
|
||||||
BasicKind basic_kind = Basic_Invalid;
|
BasicKind basic_kind = Basic_Invalid;
|
||||||
Token lit = node->basic_lit;
|
switch (bl->kind) {
|
||||||
switch (lit.kind) {
|
|
||||||
case Token_Integer: basic_kind = Basic_UntypedInteger; break;
|
case Token_Integer: basic_kind = Basic_UntypedInteger; break;
|
||||||
case Token_Float: basic_kind = Basic_UntypedFloat; break;
|
case Token_Float: basic_kind = Basic_UntypedFloat; break;
|
||||||
case Token_String: basic_kind = Basic_UntypedString; break;
|
case Token_String: basic_kind = Basic_UntypedString; break;
|
||||||
@@ -1510,14 +1529,14 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
}
|
}
|
||||||
o->mode = Addressing_Constant;
|
o->mode = Addressing_Constant;
|
||||||
o->type = &basic_types[basic_kind];
|
o->type = &basic_types[basic_kind];
|
||||||
o->value = make_exact_value_from_basic_literal(lit);
|
o->value = make_exact_value_from_basic_literal(*bl);
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ProcLit: {
|
case_ast_node(pl, ProcLit, node);
|
||||||
Scope *origin_curr_scope = c->context.scope;
|
Scope *origin_curr_scope = c->context.scope;
|
||||||
Type *proc_type = check_type(c, node->proc_lit.type);
|
Type *proc_type = check_type(c, pl->type);
|
||||||
if (proc_type != NULL) {
|
if (proc_type != NULL) {
|
||||||
check_proc_body(c, empty_token, c->context.decl, proc_type, node->proc_lit.body);
|
check_proc_body(c, empty_token, c->context.decl, proc_type, pl->body);
|
||||||
o->mode = Addressing_Value;
|
o->mode = Addressing_Value;
|
||||||
o->type = proc_type;
|
o->type = proc_type;
|
||||||
} else {
|
} else {
|
||||||
@@ -1526,10 +1545,9 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
gb_string_free(str);
|
gb_string_free(str);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_CompoundLit: {
|
case_ast_node(cl, CompoundLit, node);
|
||||||
auto *cl = &node->compound_lit;
|
|
||||||
Type *type = type_hint;
|
Type *type = type_hint;
|
||||||
if (cl->type != NULL) {
|
if (cl->type != NULL) {
|
||||||
type = check_type(c, cl->type);
|
type = check_type(c, cl->type);
|
||||||
@@ -1562,7 +1580,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
check_assignment(c, o, field->type, make_string("structure literal"));
|
check_assignment(c, o, field->type, make_string("structure literal"));
|
||||||
}
|
}
|
||||||
if (cl->elem_count < field_count) {
|
if (cl->elem_count < field_count) {
|
||||||
error(&c->error_collector, node->compound_lit.close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elem_count);
|
error(&c->error_collector, cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elem_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1610,43 +1628,48 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
|
|
||||||
o->mode = Addressing_Value;
|
o->mode = Addressing_Value;
|
||||||
o->type = type;
|
o->type = type;
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ParenExpr:
|
case_ast_node(pe, ParenExpr, node);
|
||||||
kind = check_expr_base(c, o, node->paren_expr.expr, type_hint);
|
kind = check_expr_base(c, o, pe->expr, type_hint);
|
||||||
o->expr = node;
|
o->expr = node;
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_TagExpr:
|
|
||||||
|
case_ast_node(te, TagExpr, node);
|
||||||
// TODO(bill): Tag expressions
|
// TODO(bill): Tag expressions
|
||||||
error(&c->error_collector, ast_node_token(node), "Tag expressions are not supported yet");
|
error(&c->error_collector, ast_node_token(node), "Tag expressions are not supported yet");
|
||||||
kind = check_expr_base(c, o, node->tag_expr.expr, type_hint);
|
kind = check_expr_base(c, o, te->expr, type_hint);
|
||||||
o->expr = node;
|
o->expr = node;
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_UnaryExpr:
|
|
||||||
check_expr(c, o, node->unary_expr.expr);
|
case_ast_node(ue, UnaryExpr, node);
|
||||||
|
check_expr(c, o, ue->expr);
|
||||||
if (o->mode == Addressing_Invalid)
|
if (o->mode == Addressing_Invalid)
|
||||||
goto error;
|
goto error;
|
||||||
check_unary_expr(c, o, node->unary_expr.op, node);
|
check_unary_expr(c, o, ue->op, node);
|
||||||
if (o->mode == Addressing_Invalid)
|
if (o->mode == Addressing_Invalid)
|
||||||
goto error;
|
goto error;
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_BinaryExpr:
|
|
||||||
|
case_ast_node(be, BinaryExpr, node);
|
||||||
check_binary_expr(c, o, node);
|
check_binary_expr(c, o, node);
|
||||||
if (o->mode == Addressing_Invalid)
|
if (o->mode == Addressing_Invalid)
|
||||||
goto error;
|
goto error;
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_SelectorExpr:
|
|
||||||
check_expr_base(c, o, node->selector_expr.expr);
|
case_ast_node(se, SelectorExpr, node);
|
||||||
|
check_expr_base(c, o, se->expr);
|
||||||
check_selector(c, o, node);
|
check_selector(c, o, node);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_IndexExpr: {
|
|
||||||
check_expr(c, o, node->index_expr.expr);
|
case_ast_node(ie, IndexExpr, node);
|
||||||
|
check_expr(c, o, ie->expr);
|
||||||
if (o->mode == Addressing_Invalid)
|
if (o->mode == Addressing_Invalid)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
@@ -1693,19 +1716,19 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node->index_expr.index == NULL) {
|
if (ie->index == NULL) {
|
||||||
gbString str = expr_to_string(o->expr);
|
gbString str = expr_to_string(o->expr);
|
||||||
error(&c->error_collector, ast_node_token(o->expr), "Missing index for `%s`", str);
|
error(&c->error_collector, ast_node_token(o->expr), "Missing index for `%s`", str);
|
||||||
gb_string_free(str);
|
gb_string_free(str);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
check_index_value(c, node->index_expr.index, max_count, NULL);
|
check_index_value(c, ie->index, max_count, NULL);
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_SliceExpr: {
|
|
||||||
auto *se = &node->slice_expr;
|
case_ast_node(se, SliceExpr, node);
|
||||||
check_expr(c, o, se->expr);
|
check_expr(c, o, se->expr);
|
||||||
if (o->mode == Addressing_Invalid)
|
if (o->mode == Addressing_Invalid)
|
||||||
goto error;
|
goto error;
|
||||||
@@ -1784,21 +1807,24 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_CastExpr: {
|
|
||||||
Type *cast_type = check_type(c, node->cast_expr.type);
|
case_ast_node(ce, CastExpr, node);
|
||||||
check_expr_or_type(c, o, node->cast_expr.expr);
|
Type *cast_type = check_type(c, ce->type);
|
||||||
|
check_expr_or_type(c, o, ce->expr);
|
||||||
if (o->mode != Addressing_Invalid)
|
if (o->mode != Addressing_Invalid)
|
||||||
check_cast_expr(c, o, cast_type);
|
check_cast_expr(c, o, cast_type);
|
||||||
|
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_CallExpr:
|
|
||||||
|
case_ast_node(ce, CallExpr, node);
|
||||||
return check_call_expr(c, o, node);
|
return check_call_expr(c, o, node);
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_DerefExpr:
|
case_ast_node(de, DerefExpr, node);
|
||||||
check_expr_or_type(c, o, node->deref_expr.expr);
|
check_expr_or_type(c, o, de->expr);
|
||||||
if (o->mode == Addressing_Invalid) {
|
if (o->mode == Addressing_Invalid) {
|
||||||
goto error;
|
goto error;
|
||||||
} else {
|
} else {
|
||||||
@@ -1813,7 +1839,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ProcType:
|
case AstNode_ProcType:
|
||||||
case AstNode_PointerType:
|
case AstNode_PointerType:
|
||||||
@@ -1925,12 +1951,12 @@ gbString write_expr_to_string(gbString str, AstNode *node);
|
|||||||
gbString write_field_list_to_string(gbString str, AstNode *field_list, char *sep) {
|
gbString write_field_list_to_string(gbString str, AstNode *field_list, char *sep) {
|
||||||
isize i = 0;
|
isize i = 0;
|
||||||
for (AstNode *field = field_list; field != NULL; field = field->next) {
|
for (AstNode *field = field_list; field != NULL; field = field->next) {
|
||||||
GB_ASSERT(field->kind == AstNode_Field);
|
ast_node(f, Field, field);
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
str = gb_string_appendc(str, sep);
|
str = gb_string_appendc(str, sep);
|
||||||
|
|
||||||
isize j = 0;
|
isize j = 0;
|
||||||
for (AstNode *name = field->field.name_list; name != NULL; name = name->next) {
|
for (AstNode *name = f->name_list; name != NULL; name = name->next) {
|
||||||
if (j > 0)
|
if (j > 0)
|
||||||
str = gb_string_appendc(str, ", ");
|
str = gb_string_appendc(str, ", ");
|
||||||
str = write_expr_to_string(str, name);
|
str = write_expr_to_string(str, name);
|
||||||
@@ -1938,7 +1964,7 @@ gbString write_field_list_to_string(gbString str, AstNode *field_list, char *sep
|
|||||||
}
|
}
|
||||||
|
|
||||||
str = gb_string_appendc(str, ": ");
|
str = gb_string_appendc(str, ": ");
|
||||||
str = write_expr_to_string(str, field->field.type);
|
str = write_expr_to_string(str, f->type);
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@@ -1959,115 +1985,118 @@ gbString write_expr_to_string(gbString str, AstNode *node) {
|
|||||||
str = gb_string_appendc(str, "(bad expression)");
|
str = gb_string_appendc(str, "(bad expression)");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_Ident:
|
case_ast_node(i, Ident, node);
|
||||||
str = string_append_token(str, node->ident.token);
|
str = string_append_token(str, i->token);
|
||||||
break;
|
case_end;
|
||||||
case AstNode_BasicLit:
|
|
||||||
str = string_append_token(str, node->basic_lit);
|
case_ast_node(bl, BasicLit, node);
|
||||||
break;
|
str = string_append_token(str, *bl);
|
||||||
case AstNode_ProcLit:
|
case_end;
|
||||||
str = write_expr_to_string(str, node->proc_lit.type);
|
|
||||||
break;
|
case_ast_node(pl, ProcLit, node);
|
||||||
case AstNode_CompoundLit:
|
str = write_expr_to_string(str, pl->type);
|
||||||
|
case_end;
|
||||||
|
|
||||||
|
case_ast_node(cl, CompoundLit, node);
|
||||||
str = gb_string_appendc(str, "(");
|
str = gb_string_appendc(str, "(");
|
||||||
str = write_expr_to_string(str, node->compound_lit.type);
|
str = write_expr_to_string(str, cl->type);
|
||||||
str = gb_string_appendc(str, " literal)");
|
str = gb_string_appendc(str, " literal)");
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_TagExpr:
|
case_ast_node(te, TagExpr, node);
|
||||||
str = gb_string_appendc(str, "#");
|
str = gb_string_appendc(str, "#");
|
||||||
str = string_append_token(str, node->tag_expr.name);
|
str = string_append_token(str, te->name);
|
||||||
str = write_expr_to_string(str, node->tag_expr.expr);
|
str = write_expr_to_string(str, te->expr);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_UnaryExpr:
|
case_ast_node(ue, UnaryExpr, node);
|
||||||
str = string_append_token(str, node->unary_expr.op);
|
str = string_append_token(str, ue->op);
|
||||||
str = write_expr_to_string(str, node->unary_expr.expr);
|
str = write_expr_to_string(str, ue->expr);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_BinaryExpr:
|
case_ast_node(be, BinaryExpr, node);
|
||||||
str = write_expr_to_string(str, node->binary_expr.left);
|
str = write_expr_to_string(str, be->left);
|
||||||
str = gb_string_appendc(str, " ");
|
str = gb_string_appendc(str, " ");
|
||||||
str = string_append_token(str, node->binary_expr.op);
|
str = string_append_token(str, be->op);
|
||||||
str = gb_string_appendc(str, " ");
|
str = gb_string_appendc(str, " ");
|
||||||
str = write_expr_to_string(str, node->binary_expr.right);
|
str = write_expr_to_string(str, be->right);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ParenExpr:
|
case_ast_node(pe, ParenExpr, node);
|
||||||
str = gb_string_appendc(str, "(");
|
str = gb_string_appendc(str, "(");
|
||||||
str = write_expr_to_string(str, node->paren_expr.expr);
|
str = write_expr_to_string(str, pe->expr);
|
||||||
str = gb_string_appendc(str, ")");
|
str = gb_string_appendc(str, ")");
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_SelectorExpr:
|
case_ast_node(se, SelectorExpr, node);
|
||||||
str = write_expr_to_string(str, node->selector_expr.expr);
|
str = write_expr_to_string(str, se->expr);
|
||||||
str = gb_string_appendc(str, ".");
|
str = gb_string_appendc(str, ".");
|
||||||
str = write_expr_to_string(str, node->selector_expr.selector);
|
str = write_expr_to_string(str, se->selector);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_IndexExpr:
|
case_ast_node(ie, IndexExpr, node);
|
||||||
str = write_expr_to_string(str, node->index_expr.expr);
|
str = write_expr_to_string(str, ie->expr);
|
||||||
str = gb_string_appendc(str, "[");
|
str = gb_string_appendc(str, "[");
|
||||||
str = write_expr_to_string(str, node->index_expr.index);
|
str = write_expr_to_string(str, ie->index);
|
||||||
str = gb_string_appendc(str, "]");
|
str = gb_string_appendc(str, "]");
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_SliceExpr:
|
case_ast_node(se, SliceExpr, node);
|
||||||
str = write_expr_to_string(str, node->slice_expr.expr);
|
str = write_expr_to_string(str, se->expr);
|
||||||
str = gb_string_appendc(str, "[");
|
str = gb_string_appendc(str, "[");
|
||||||
str = write_expr_to_string(str, node->slice_expr.low);
|
str = write_expr_to_string(str, se->low);
|
||||||
str = gb_string_appendc(str, ":");
|
str = gb_string_appendc(str, ":");
|
||||||
str = write_expr_to_string(str, node->slice_expr.high);
|
str = write_expr_to_string(str, se->high);
|
||||||
if (node->slice_expr.triple_indexed) {
|
if (se->triple_indexed) {
|
||||||
str = gb_string_appendc(str, ":");
|
str = gb_string_appendc(str, ":");
|
||||||
str = write_expr_to_string(str, node->slice_expr.max);
|
str = write_expr_to_string(str, se->max);
|
||||||
}
|
}
|
||||||
str = gb_string_appendc(str, "]");
|
str = gb_string_appendc(str, "]");
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
|
case_ast_node(ce, CastExpr, node);
|
||||||
case AstNode_CastExpr:
|
|
||||||
str = gb_string_appendc(str, "cast(");
|
str = gb_string_appendc(str, "cast(");
|
||||||
str = write_expr_to_string(str, node->cast_expr.type);
|
str = write_expr_to_string(str, ce->type);
|
||||||
str = gb_string_appendc(str, ")");
|
str = gb_string_appendc(str, ")");
|
||||||
str = write_expr_to_string(str, node->cast_expr.expr);
|
str = write_expr_to_string(str, ce->expr);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_PointerType:
|
case_ast_node(pt, PointerType, node);
|
||||||
str = gb_string_appendc(str, "^");
|
str = gb_string_appendc(str, "^");
|
||||||
str = write_expr_to_string(str, node->pointer_type.type);
|
str = write_expr_to_string(str, pt->type);
|
||||||
break;
|
case_end;
|
||||||
case AstNode_ArrayType:
|
|
||||||
|
case_ast_node(at, ArrayType, node);
|
||||||
str = gb_string_appendc(str, "[");
|
str = gb_string_appendc(str, "[");
|
||||||
str = write_expr_to_string(str, node->array_type.count);
|
str = write_expr_to_string(str, at->count);
|
||||||
str = gb_string_appendc(str, "]");
|
str = gb_string_appendc(str, "]");
|
||||||
str = write_expr_to_string(str, node->array_type.elem);
|
str = write_expr_to_string(str, at->elem);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
|
case_ast_node(ce, CallExpr, node);
|
||||||
case AstNode_CallExpr: {
|
str = write_expr_to_string(str, ce->proc);
|
||||||
str = write_expr_to_string(str, node->call_expr.proc);
|
|
||||||
str = gb_string_appendc(str, "(");
|
str = gb_string_appendc(str, "(");
|
||||||
isize i = 0;
|
isize i = 0;
|
||||||
for (AstNode *arg = node->call_expr.arg_list; arg != NULL; arg = arg->next) {
|
for (AstNode *arg = ce->arg_list; arg != NULL; arg = arg->next) {
|
||||||
if (i > 0) gb_string_appendc(str, ", ");
|
if (i > 0) gb_string_appendc(str, ", ");
|
||||||
str = write_expr_to_string(str, arg);
|
str = write_expr_to_string(str, arg);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
str = gb_string_appendc(str, ")");
|
str = gb_string_appendc(str, ")");
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ProcType:
|
case_ast_node(pt, ProcType, node);
|
||||||
str = gb_string_appendc(str, "proc(");
|
str = gb_string_appendc(str, "proc(");
|
||||||
str = write_field_list_to_string(str, node->proc_type.param_list, ", ");
|
str = write_field_list_to_string(str, pt->param_list, ", ");
|
||||||
str = gb_string_appendc(str, ")");
|
str = gb_string_appendc(str, ")");
|
||||||
|
case_end;
|
||||||
|
|
||||||
break;
|
case_ast_node(st, StructType, node);
|
||||||
case AstNode_StructType:
|
|
||||||
str = gb_string_appendc(str, "struct{");
|
str = gb_string_appendc(str, "struct{");
|
||||||
|
str = write_field_list_to_string(str, st->field_list, ", ");
|
||||||
str = gb_string_appendc(str, "}");
|
str = gb_string_appendc(str, "}");
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+127
-123
@@ -39,29 +39,32 @@ b32 check_is_terminating_list(Checker *c, AstNode *list) {
|
|||||||
// TODO(bill): Warn/err against code after `return` that it won't be executed
|
// TODO(bill): Warn/err against code after `return` that it won't be executed
|
||||||
b32 check_is_terminating(Checker *c, AstNode *node) {
|
b32 check_is_terminating(Checker *c, AstNode *node) {
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
case AstNode_ReturnStmt:
|
case_ast_node(rs, ReturnStmt, node);
|
||||||
return true;
|
return true;
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_BlockStmt:
|
case_ast_node(bs, BlockStmt, node);
|
||||||
return check_is_terminating_list(c, node->block_stmt.list);
|
return check_is_terminating_list(c, bs->list);
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_ExprStmt:
|
case_ast_node(es, ExprStmt, node);
|
||||||
return check_is_terminating(c, node->expr_stmt.expr);
|
return check_is_terminating(c, es->expr);
|
||||||
|
case_end;
|
||||||
|
|
||||||
case AstNode_IfStmt:
|
case_ast_node(is, IfStmt, node);
|
||||||
if (node->if_stmt.else_stmt != NULL) {
|
if (is->else_stmt != NULL) {
|
||||||
if (check_is_terminating(c, node->if_stmt.body) &&
|
if (check_is_terminating(c, is->body) &&
|
||||||
check_is_terminating(c, node->if_stmt.else_stmt)) {
|
check_is_terminating(c, is->else_stmt)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ForStmt:
|
case_ast_node(fs, ForStmt, node);
|
||||||
if (node->for_stmt.cond == NULL) {
|
if (fs->cond == NULL) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
case_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -170,19 +173,22 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
|
|||||||
AstNode *node = unparen_expr(lhs);
|
AstNode *node = unparen_expr(lhs);
|
||||||
|
|
||||||
// NOTE(bill): Ignore assignments to `_`
|
// NOTE(bill): Ignore assignments to `_`
|
||||||
if (node->kind == AstNode_Ident &&
|
if (node->kind == AstNode_Ident) {
|
||||||
are_strings_equal(node->ident.token.string, make_string("_"))) {
|
ast_node(i, Ident, node);
|
||||||
add_entity_definition(&c->info, node, NULL);
|
if (are_strings_equal(i->token.string, make_string("_"))) {
|
||||||
check_assignment(c, op_a, NULL, make_string("assignment to `_` identifier"));
|
add_entity_definition(&c->info, node, NULL);
|
||||||
if (op_a->mode == Addressing_Invalid)
|
check_assignment(c, op_a, NULL, make_string("assignment to `_` identifier"));
|
||||||
return NULL;
|
if (op_a->mode == Addressing_Invalid)
|
||||||
return op_a->type;
|
return NULL;
|
||||||
}
|
return op_a->type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Entity *e = NULL;
|
Entity *e = NULL;
|
||||||
b32 used = false;
|
b32 used = false;
|
||||||
if (node->kind == AstNode_Ident) {
|
if (node->kind == AstNode_Ident) {
|
||||||
e = scope_lookup_entity(c->context.scope, node->ident.token.string);
|
ast_node(i, Ident, node);
|
||||||
|
e = scope_lookup_entity(c->context.scope, i->token.string);
|
||||||
if (e != NULL && e->kind == Entity_Variable) {
|
if (e != NULL && e->kind == Entity_Variable) {
|
||||||
used = e->variable.used; // TODO(bill): Make backup just in case
|
used = e->variable.used; // TODO(bill): Make backup just in case
|
||||||
}
|
}
|
||||||
@@ -207,7 +213,8 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
|
|||||||
if (op_b.expr->kind == AstNode_SelectorExpr) {
|
if (op_b.expr->kind == AstNode_SelectorExpr) {
|
||||||
// NOTE(bill): Extra error checks
|
// NOTE(bill): Extra error checks
|
||||||
Operand op_c = {Addressing_Invalid};
|
Operand op_c = {Addressing_Invalid};
|
||||||
check_expr(c, &op_c, op_b.expr->selector_expr.expr);
|
ast_node(se, SelectorExpr, op_b.expr);
|
||||||
|
check_expr(c, &op_c, se->expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
gbString str = expr_to_string(op_b.expr);
|
gbString str = expr_to_string(op_b.expr);
|
||||||
@@ -379,10 +386,11 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
|
|||||||
c->context.decl = decl;
|
c->context.decl = decl;
|
||||||
|
|
||||||
push_procedure(c, type);
|
push_procedure(c, type);
|
||||||
check_stmt_list(c, body->block_stmt.list, 0);
|
ast_node(bs, BlockStmt, body);
|
||||||
|
check_stmt_list(c, bs->list, 0);
|
||||||
if (type->procedure.result_count > 0) {
|
if (type->procedure.result_count > 0) {
|
||||||
if (!check_is_terminating(c, body)) {
|
if (!check_is_terminating(c, body)) {
|
||||||
error(&c->error_collector, body->block_stmt.close, "Missing return statement at the end of the procedure");
|
error(&c->error_collector, bs->close, "Missing return statement at the end of the procedure");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pop_procedure(c);
|
pop_procedure(c);
|
||||||
@@ -395,7 +403,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
|
|||||||
|
|
||||||
Type *proc_type = make_type_procedure(c->allocator, e->parent, NULL, 0, NULL, 0);
|
Type *proc_type = make_type_procedure(c->allocator, e->parent, NULL, 0, NULL, 0);
|
||||||
e->type = proc_type;
|
e->type = proc_type;
|
||||||
auto *pd = &d->proc_decl->proc_decl;
|
ast_node(pd, ProcDecl, d->proc_decl);
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
Scope *original_curr_scope = c->context.scope;
|
Scope *original_curr_scope = c->context.scope;
|
||||||
@@ -409,7 +417,8 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
|
|||||||
for (AstNode *tag = pd->tag_list; tag != NULL; tag = tag->next) {
|
for (AstNode *tag = pd->tag_list; tag != NULL; tag = tag->next) {
|
||||||
GB_ASSERT(tag->kind == AstNode_TagExpr);
|
GB_ASSERT(tag->kind == AstNode_TagExpr);
|
||||||
|
|
||||||
String tag_name = tag->tag_expr.name.string;
|
ast_node(te, TagExpr, tag);
|
||||||
|
String tag_name = te->name.string;
|
||||||
if (are_strings_equal(tag_name, make_string("foreign"))) {
|
if (are_strings_equal(tag_name, make_string("foreign"))) {
|
||||||
is_foreign = true;
|
is_foreign = true;
|
||||||
} else if (are_strings_equal(tag_name, make_string("inline"))) {
|
} else if (are_strings_equal(tag_name, make_string("inline"))) {
|
||||||
@@ -516,13 +525,13 @@ void check_entity_decl(Checker *c, Entity *e, DeclInfo *d, Type *named_type) {
|
|||||||
|
|
||||||
void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
case AstNode_EmptyStmt: break;
|
case_ast_node(_, EmptyStmt, node); case_end;
|
||||||
case AstNode_BadStmt: break;
|
case_ast_node(_, BadStmt, node); case_end;
|
||||||
case AstNode_BadDecl: break;
|
case_ast_node(_, BadDecl, node); case_end;
|
||||||
|
|
||||||
case AstNode_ExprStmt: {
|
case_ast_node(es, ExprStmt, node)
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
ExpressionKind kind = check_expr_base(c, &operand, node->expr_stmt.expr);
|
ExpressionKind kind = check_expr_base(c, &operand, es->expr);
|
||||||
switch (operand.mode) {
|
switch (operand.mode) {
|
||||||
case Addressing_Type:
|
case Addressing_Type:
|
||||||
error(&c->error_collector, ast_node_token(node), "Is not an expression");
|
error(&c->error_collector, ast_node_token(node), "Is not an expression");
|
||||||
@@ -533,19 +542,18 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
error(&c->error_collector, ast_node_token(node), "Expression is not used");
|
error(&c->error_collector, ast_node_token(node), "Expression is not used");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_TagStmt:
|
case_ast_node(ts, TagStmt, node);
|
||||||
// TODO(bill): Tag Statements
|
// TODO(bill): Tag Statements
|
||||||
error(&c->error_collector, ast_node_token(node), "Tag statements are not supported yet");
|
error(&c->error_collector, ast_node_token(node), "Tag statements are not supported yet");
|
||||||
check_stmt(c, node->tag_stmt.stmt, flags);
|
check_stmt(c, ts->stmt, flags);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_IncDecStmt: {
|
case_ast_node(ids, IncDecStmt, node);
|
||||||
Token op = {};
|
Token op = {};
|
||||||
auto *s = &node->inc_dec_stmt;
|
op = ids->op;
|
||||||
op = s->op;
|
switch (ids->op.kind) {
|
||||||
switch (s->op.kind) {
|
|
||||||
case Token_Increment:
|
case Token_Increment:
|
||||||
op.kind = Token_Add;
|
op.kind = Token_Add;
|
||||||
op.string.len = 1;
|
op.string.len = 1;
|
||||||
@@ -555,44 +563,45 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
op.string.len = 1;
|
op.string.len = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
error(&c->error_collector, s->op, "Unknown inc/dec operation %.*s", LIT(s->op.string));
|
error(&c->error_collector, ids->op, "Unknown inc/dec operation %.*s", LIT(ids->op.string));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
check_expr(c, &operand, s->expr);
|
check_expr(c, &operand, ids->expr);
|
||||||
if (operand.mode == Addressing_Invalid)
|
if (operand.mode == Addressing_Invalid)
|
||||||
return;
|
return;
|
||||||
if (!is_type_numeric(operand.type)) {
|
if (!is_type_numeric(operand.type)) {
|
||||||
error(&c->error_collector, s->op, "Non numeric type");
|
error(&c->error_collector, ids->op, "Non numeric type");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AstNode basic_lit = {AstNode_BasicLit};
|
AstNode basic_lit = {AstNode_BasicLit};
|
||||||
basic_lit.basic_lit = s->op;
|
ast_node(bl, BasicLit, &basic_lit);
|
||||||
basic_lit.basic_lit.kind = Token_Integer;
|
*bl = ids->op;
|
||||||
basic_lit.basic_lit.string = make_string("1");
|
bl->kind = Token_Integer;
|
||||||
|
bl->string = make_string("1");
|
||||||
|
|
||||||
AstNode be = {AstNode_BinaryExpr};
|
AstNode binary_expr = {AstNode_BinaryExpr};
|
||||||
be.binary_expr.op = op;
|
ast_node(be, BinaryExpr, &binary_expr);
|
||||||
be.binary_expr.left = s->expr;;
|
be->op = op;
|
||||||
be.binary_expr.right = &basic_lit;
|
be->left = ids->expr;
|
||||||
check_binary_expr(c, &operand, &be);
|
be->right = &basic_lit;
|
||||||
|
check_binary_expr(c, &operand, &binary_expr);
|
||||||
|
case_end;
|
||||||
|
|
||||||
} break;
|
case_ast_node(as, AssignStmt, node);
|
||||||
|
switch (as->op.kind) {
|
||||||
case AstNode_AssignStmt:
|
|
||||||
switch (node->assign_stmt.op.kind) {
|
|
||||||
case Token_Eq: {
|
case Token_Eq: {
|
||||||
// a, b, c = 1, 2, 3; // Multisided
|
// a, b, c = 1, 2, 3; // Multisided
|
||||||
if (node->assign_stmt.lhs_count == 0) {
|
if (as->lhs_count == 0) {
|
||||||
error(&c->error_collector, node->assign_stmt.op, "Missing lhs in assignment statement");
|
error(&c->error_collector, as->op, "Missing lhs in assignment statement");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Operand operand = {};
|
Operand operand = {};
|
||||||
AstNode *lhs = node->assign_stmt.lhs_list;
|
AstNode *lhs = as->lhs_list;
|
||||||
AstNode *rhs = node->assign_stmt.rhs_list;
|
AstNode *rhs = as->rhs_list;
|
||||||
isize i = 0;
|
isize i = 0;
|
||||||
for (;
|
for (;
|
||||||
lhs != NULL && rhs != NULL;
|
lhs != NULL && rhs != NULL;
|
||||||
@@ -615,7 +624,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < node->assign_stmt.lhs_count && i < node->assign_stmt.rhs_count) {
|
if (i < as->lhs_count && i < as->rhs_count) {
|
||||||
if (lhs == NULL)
|
if (lhs == NULL)
|
||||||
error(&c->error_collector, ast_node_token(lhs), "Too few values on the right hand side of the declaration");
|
error(&c->error_collector, ast_node_token(lhs), "Too few values on the right hand side of the declaration");
|
||||||
} else if (rhs != NULL) {
|
} else if (rhs != NULL) {
|
||||||
@@ -625,9 +634,8 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
|
|
||||||
default: {
|
default: {
|
||||||
// a += 1; // Single-sided
|
// a += 1; // Single-sided
|
||||||
Token op = node->assign_stmt.op;
|
Token op = as->op;
|
||||||
if (node->assign_stmt.lhs_count != 1 ||
|
if (as->lhs_count != 1 || as->rhs_count != 1) {
|
||||||
node->assign_stmt.rhs_count != 1) {
|
|
||||||
error(&c->error_collector, op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
|
error(&c->error_collector, op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -637,61 +645,60 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
}
|
}
|
||||||
// TODO(bill): Check if valid assignment operator
|
// TODO(bill): Check if valid assignment operator
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
AstNode be = {AstNode_BinaryExpr};
|
AstNode binary_expr = {AstNode_BinaryExpr};
|
||||||
be.binary_expr.op = op;
|
ast_node(be, BinaryExpr, &binary_expr);
|
||||||
|
be->op = op;
|
||||||
// NOTE(bill): Only use the first one will be used
|
// NOTE(bill): Only use the first one will be used
|
||||||
be.binary_expr.left = node->assign_stmt.lhs_list;
|
be->left = as->lhs_list;
|
||||||
be.binary_expr.right = node->assign_stmt.rhs_list;
|
be->right = as->rhs_list;
|
||||||
|
|
||||||
check_binary_expr(c, &operand, &be);
|
check_binary_expr(c, &operand, &binary_expr);
|
||||||
if (operand.mode == Addressing_Invalid)
|
if (operand.mode == Addressing_Invalid)
|
||||||
return;
|
return;
|
||||||
// NOTE(bill): Only use the first one will be used
|
// NOTE(bill): Only use the first one will be used
|
||||||
check_assignment_variable(c, &operand, node->assign_stmt.lhs_list);
|
check_assignment_variable(c, &operand, as->lhs_list);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_BlockStmt:
|
case_ast_node(bs, BlockStmt, node);
|
||||||
check_open_scope(c, node);
|
check_open_scope(c, node);
|
||||||
check_stmt_list(c, node->block_stmt.list, flags);
|
check_stmt_list(c, bs->list, flags);
|
||||||
check_close_scope(c);
|
check_close_scope(c);
|
||||||
break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_IfStmt: {
|
case_ast_node(is, IfStmt, node);
|
||||||
check_open_scope(c, node);
|
check_open_scope(c, node);
|
||||||
defer (check_close_scope(c));
|
defer (check_close_scope(c));
|
||||||
auto *is = &node->if_stmt;
|
|
||||||
|
|
||||||
if (is->init != NULL)
|
if (is->init != NULL)
|
||||||
check_stmt(c, is->init, 0);
|
check_stmt(c, is->init, 0);
|
||||||
|
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
check_expr(c, &operand, node->if_stmt.cond);
|
check_expr(c, &operand, is->cond);
|
||||||
if (operand.mode != Addressing_Invalid &&
|
if (operand.mode != Addressing_Invalid &&
|
||||||
!is_type_boolean(operand.type)) {
|
!is_type_boolean(operand.type)) {
|
||||||
error(&c->error_collector, ast_node_token(node->if_stmt.cond),
|
error(&c->error_collector, ast_node_token(is->cond),
|
||||||
"Non-boolean condition in `if` statement");
|
"Non-boolean condition in `if` statement");
|
||||||
}
|
}
|
||||||
|
|
||||||
check_stmt(c, node->if_stmt.body, flags);
|
check_stmt(c, is->body, flags);
|
||||||
|
|
||||||
if (node->if_stmt.else_stmt) {
|
if (is->else_stmt) {
|
||||||
switch (node->if_stmt.else_stmt->kind) {
|
switch (is->else_stmt->kind) {
|
||||||
case AstNode_IfStmt:
|
case AstNode_IfStmt:
|
||||||
case AstNode_BlockStmt:
|
case AstNode_BlockStmt:
|
||||||
check_stmt(c, node->if_stmt.else_stmt, flags);
|
check_stmt(c, is->else_stmt, flags);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
error(&c->error_collector, ast_node_token(node->if_stmt.else_stmt),
|
error(&c->error_collector, ast_node_token(is->else_stmt),
|
||||||
"Invalid `else` statement in `if` statement");
|
"Invalid `else` statement in `if` statement");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ReturnStmt: {
|
case_ast_node(rs, ReturnStmt, node);
|
||||||
auto *rs = &node->return_stmt;
|
|
||||||
GB_ASSERT(gb_array_count(c->procedure_stack) > 0);
|
GB_ASSERT(gb_array_count(c->procedure_stack) > 0);
|
||||||
|
|
||||||
if (c->in_defer) {
|
if (c->in_defer) {
|
||||||
@@ -714,31 +721,30 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
check_init_variables(c, tuple->variables, tuple->variable_count,
|
check_init_variables(c, tuple->variables, tuple->variable_count,
|
||||||
rs->result_list, rs->result_count, make_string("return statement"));
|
rs->result_list, rs->result_count, make_string("return statement"));
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ForStmt: {
|
case_ast_node(fs, ForStmt, node);
|
||||||
flags |= Statement_BreakAllowed | Statement_ContinueAllowed;
|
flags |= Statement_BreakAllowed | Statement_ContinueAllowed;
|
||||||
check_open_scope(c, node);
|
check_open_scope(c, node);
|
||||||
defer (check_close_scope(c));
|
defer (check_close_scope(c));
|
||||||
|
|
||||||
if (node->for_stmt.init != NULL)
|
if (fs->init != NULL)
|
||||||
check_stmt(c, node->for_stmt.init, 0);
|
check_stmt(c, fs->init, 0);
|
||||||
if (node->for_stmt.cond) {
|
if (fs->cond) {
|
||||||
Operand operand = {Addressing_Invalid};
|
Operand operand = {Addressing_Invalid};
|
||||||
check_expr(c, &operand, node->for_stmt.cond);
|
check_expr(c, &operand, fs->cond);
|
||||||
if (operand.mode != Addressing_Invalid &&
|
if (operand.mode != Addressing_Invalid &&
|
||||||
!is_type_boolean(operand.type)) {
|
!is_type_boolean(operand.type)) {
|
||||||
error(&c->error_collector, ast_node_token(node->for_stmt.cond),
|
error(&c->error_collector, ast_node_token(fs->cond),
|
||||||
"Non-boolean condition in `for` statement");
|
"Non-boolean condition in `for` statement");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node->for_stmt.end != NULL)
|
if (fs->end != NULL)
|
||||||
check_stmt(c, node->for_stmt.end, 0);
|
check_stmt(c, fs->end, 0);
|
||||||
check_stmt(c, node->for_stmt.body, flags);
|
check_stmt(c, fs->body, flags);
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_DeferStmt: {
|
case_ast_node(ds, DeferStmt, node);
|
||||||
auto *ds = &node->defer_stmt;
|
|
||||||
if (is_ast_node_decl(ds->stmt)) {
|
if (is_ast_node_decl(ds->stmt)) {
|
||||||
error(&c->error_collector, ds->token, "You cannot defer a declaration");
|
error(&c->error_collector, ds->token, "You cannot defer a declaration");
|
||||||
} else {
|
} else {
|
||||||
@@ -747,10 +753,10 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
check_stmt(c, ds->stmt, 0);
|
check_stmt(c, ds->stmt, 0);
|
||||||
c->in_defer = out_in_defer;
|
c->in_defer = out_in_defer;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_BranchStmt: {
|
case_ast_node(bs, BranchStmt, node);
|
||||||
Token token = node->branch_stmt.token;
|
Token token = bs->token;
|
||||||
switch (token.kind) {
|
switch (token.kind) {
|
||||||
case Token_break:
|
case Token_break:
|
||||||
if ((flags & Statement_BreakAllowed) == 0)
|
if ((flags & Statement_BreakAllowed) == 0)
|
||||||
@@ -764,12 +770,11 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
error(&c->error_collector, token, "Invalid AST: Branch Statement `%.*s`", LIT(token.string));
|
error(&c->error_collector, token, "Invalid AST: Branch Statement `%.*s`", LIT(token.string));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
|
|
||||||
// Declarations
|
// Declarations
|
||||||
case AstNode_VarDecl: {
|
case_ast_node(vd, VarDecl, node);
|
||||||
auto *vd = &node->var_decl;
|
|
||||||
isize entity_count = vd->name_count;
|
isize entity_count = vd->name_count;
|
||||||
isize entity_index = 0;
|
isize entity_index = 0;
|
||||||
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
|
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
|
||||||
@@ -780,7 +785,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
|
|
||||||
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
||||||
Entity *entity = NULL;
|
Entity *entity = NULL;
|
||||||
Token token = name->ident.token;
|
Token token = name->Ident.token;
|
||||||
if (name->kind == AstNode_Ident) {
|
if (name->kind == AstNode_Ident) {
|
||||||
String str = token.string;
|
String str = token.string;
|
||||||
Entity *found = NULL;
|
Entity *found = NULL;
|
||||||
@@ -841,7 +846,8 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
name = name->next, value = value->next) {
|
name = name->next, value = value->next) {
|
||||||
GB_ASSERT(name->kind == AstNode_Ident);
|
GB_ASSERT(name->kind == AstNode_Ident);
|
||||||
ExactValue v = {ExactValue_Invalid};
|
ExactValue v = {ExactValue_Invalid};
|
||||||
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->ident.token, NULL, v);
|
ast_node(i, Ident, name);
|
||||||
|
Entity *e = make_entity_constant(c->allocator, c->context.scope, i->token, NULL, v);
|
||||||
entities[entity_index++] = e;
|
entities[entity_index++] = e;
|
||||||
check_const_decl(c, e, vd->type, value);
|
check_const_decl(c, e, vd->type, value);
|
||||||
}
|
}
|
||||||
@@ -866,11 +872,11 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
error(&c->error_collector, ast_node_token(node), "Unknown variable declaration kind. Probably an invalid AST.");
|
error(&c->error_collector, ast_node_token(node), "Unknown variable declaration kind. Probably an invalid AST.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_ProcDecl: {
|
case_ast_node(pd, ProcDecl, node);
|
||||||
auto *pd = &node->proc_decl;
|
ast_node(name, Ident, pd->name);
|
||||||
Entity *e = make_entity_procedure(c->allocator, c->context.scope, pd->name->ident.token, NULL);
|
Entity *e = make_entity_procedure(c->allocator, c->context.scope, name->token, NULL);
|
||||||
add_entity(c, c->context.scope, pd->name, e);
|
add_entity(c, c->context.scope, pd->name, e);
|
||||||
|
|
||||||
DeclInfo decl = {};
|
DeclInfo decl = {};
|
||||||
@@ -878,22 +884,20 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
|||||||
decl.proc_decl = node;
|
decl.proc_decl = node;
|
||||||
check_proc_decl(c, e, &decl, false);
|
check_proc_decl(c, e, &decl, false);
|
||||||
destroy_declaration_info(&decl);
|
destroy_declaration_info(&decl);
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_TypeDecl: {
|
case_ast_node(td, TypeDecl, node);
|
||||||
auto *td = &node->type_decl;
|
ast_node(name, Ident, td->name);
|
||||||
AstNode *name = td->name;
|
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name->token, NULL);
|
||||||
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name->ident.token, NULL);
|
add_entity(c, c->context.scope, td->name, e);
|
||||||
add_entity(c, c->context.scope, name, e);
|
|
||||||
check_type_decl(c, e, td->type, NULL);
|
check_type_decl(c, e, td->type, NULL);
|
||||||
} break;
|
case_end;
|
||||||
|
|
||||||
case AstNode_AliasDecl: {
|
case_ast_node(ad, AliasDecl, node);
|
||||||
auto *ad = &node->alias_decl;
|
ast_node(name, Ident, ad->name);
|
||||||
AstNode *name = ad->name;
|
Entity *e = make_entity_alias_name(c->allocator, c->context.scope, name->token, NULL);
|
||||||
Entity *e = make_entity_alias_name(c->allocator, c->context.scope, name->ident.token, NULL);
|
add_entity(c, c->context.scope, ad->name, e);
|
||||||
add_entity(c, c->context.scope, name, e);
|
|
||||||
check_alias_decl(c, e, ad->type, NULL);
|
check_alias_decl(c, e, ad->type, NULL);
|
||||||
} break;
|
case_end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-26
@@ -215,24 +215,24 @@ void ssa_print_value(gbFile *f, ssaModule *m, ssaValue *value, Type *type_hint)
|
|||||||
ssa_print_encoded_global(f, value->global.entity->token.string);
|
ssa_print_encoded_global(f, value->global.entity->token.string);
|
||||||
break;
|
break;
|
||||||
case ssaValue_Procedure:
|
case ssaValue_Procedure:
|
||||||
ssa_print_encoded_global(f, value->procedure.entity->token.string);
|
ssa_print_encoded_global(f, value->proc.entity->token.string);
|
||||||
break;
|
break;
|
||||||
case ssaValue_Constant: {
|
case ssaValue_Constant: {
|
||||||
ssa_print_exact_value(f, m, value->constant.value, type_hint);
|
ssa_print_exact_value(f, m, value->constant.value, type_hint);
|
||||||
} break;
|
} break;
|
||||||
case ssaValue_Instruction:
|
case ssaValue_Instr:
|
||||||
ssa_fprintf(f, "%%%d", value->id);
|
ssa_fprintf(f, "%%%d", value->id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) {
|
||||||
GB_ASSERT(value->kind == ssaValue_Instruction);
|
GB_ASSERT(value->kind == ssaValue_Instr);
|
||||||
ssaInstruction *instr = &value->instruction;
|
ssaInstr *instr = &value->instr;
|
||||||
|
|
||||||
ssa_fprintf(f, "\t");
|
ssa_fprintf(f, "\t");
|
||||||
switch (instr->kind) {
|
switch (instr->kind) {
|
||||||
case ssaInstruction_Local: {
|
case ssaInstr_Local: {
|
||||||
Type *type = instr->local.entity->type;
|
Type *type = instr->local.entity->type;
|
||||||
ssa_fprintf(f, "%%%d = alloca ", value->id);
|
ssa_fprintf(f, "%%%d = alloca ", value->id);
|
||||||
ssa_print_type(f, m->sizes, type);
|
ssa_print_type(f, m->sizes, type);
|
||||||
@@ -246,7 +246,7 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
|||||||
ssa_fprintf(f, "* %%%d\n", value->id);
|
ssa_fprintf(f, "* %%%d\n", value->id);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ssaInstruction_Store: {
|
case ssaInstr_Store: {
|
||||||
Type *type = ssa_value_type(instr->store.address);
|
Type *type = ssa_value_type(instr->store.address);
|
||||||
ssa_fprintf(f, "store ");
|
ssa_fprintf(f, "store ");
|
||||||
ssa_print_type(f, m->sizes, type);
|
ssa_print_type(f, m->sizes, type);
|
||||||
@@ -259,7 +259,7 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
|||||||
ssa_fprintf(f, "\n");
|
ssa_fprintf(f, "\n");
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ssaInstruction_Load: {
|
case ssaInstr_Load: {
|
||||||
Type *type = instr->load.type;
|
Type *type = instr->load.type;
|
||||||
ssa_fprintf(f, "%%%d = load ", value->id);
|
ssa_fprintf(f, "%%%d = load ", value->id);
|
||||||
ssa_print_type(f, m->sizes, type);
|
ssa_print_type(f, m->sizes, type);
|
||||||
@@ -270,8 +270,7 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
|||||||
ssa_fprintf(f, "\n");
|
ssa_fprintf(f, "\n");
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ssaInstruction_GetElementPtr: {
|
case ssaInstr_GetElementPtr: {
|
||||||
Type *rt = instr->get_element_ptr.result_type;
|
|
||||||
Type *et = instr->get_element_ptr.element_type;
|
Type *et = instr->get_element_ptr.element_type;
|
||||||
Type *t_int = &basic_types[Basic_int];
|
Type *t_int = &basic_types[Basic_int];
|
||||||
ssa_fprintf(f, "%%%d = getelementptr ", value->id);
|
ssa_fprintf(f, "%%%d = getelementptr ", value->id);
|
||||||
@@ -283,22 +282,35 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
|||||||
ssa_print_type(f, m->sizes, et);
|
ssa_print_type(f, m->sizes, et);
|
||||||
ssa_fprintf(f, "* ");
|
ssa_fprintf(f, "* ");
|
||||||
ssa_print_value(f, m, instr->get_element_ptr.address, et);
|
ssa_print_value(f, m, instr->get_element_ptr.address, et);
|
||||||
ssa_fprintf(f, ", ");
|
for (isize i = 0; i < instr->get_element_ptr.index_count; i++) {
|
||||||
ssa_print_type(f, m->sizes, t_int);
|
|
||||||
ssa_fprintf(f, " ");
|
|
||||||
ssa_print_value(f, m, instr->get_element_ptr.indices[0], t_int);
|
|
||||||
if (instr->get_element_ptr.index_count == 2) {
|
|
||||||
ssa_fprintf(f, ", ");
|
ssa_fprintf(f, ", ");
|
||||||
ssa_print_type(f, m->sizes, t_int);
|
ssa_print_type(f, m->sizes, t_int);
|
||||||
ssa_fprintf(f, " ");
|
ssa_fprintf(f, " ");
|
||||||
ssa_print_value(f, m, instr->get_element_ptr.indices[1], t_int);
|
ssa_print_value(f, m, instr->get_element_ptr.indices[i], t_int);
|
||||||
}
|
}
|
||||||
ssa_fprintf(f, "\n");
|
ssa_fprintf(f, "\n");
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
|
case ssaInstr_Br: {
|
||||||
|
ssa_fprintf(f, "br ");
|
||||||
|
if (instr->br.cond != NULL) {
|
||||||
|
Type *t_bool = &basic_types[Basic_bool];
|
||||||
|
ssa_print_type(f, m->sizes, t_bool);
|
||||||
|
ssa_fprintf(f, " ");
|
||||||
|
ssa_print_value(f, m, instr->br.cond, t_bool);
|
||||||
|
ssa_fprintf(f, ", ", instr->br.cond->id);
|
||||||
|
}
|
||||||
|
ssa_fprintf(f, "label ");
|
||||||
|
ssa_print_encoded_local(f, instr->br.true_block->label);
|
||||||
|
if (instr->br.false_block != NULL) {
|
||||||
|
ssa_fprintf(f, ", label ");
|
||||||
|
ssa_print_encoded_local(f, instr->br.false_block->label);
|
||||||
|
}
|
||||||
|
ssa_fprintf(f, "\n");
|
||||||
|
} break;
|
||||||
|
|
||||||
case ssaInstruction_BinaryOp: {
|
case ssaInstr_BinaryOp: {
|
||||||
auto *bo = &value->instruction.binary_op;
|
auto *bo = &value->instr.binary_op;
|
||||||
Type *type = ssa_value_type(bo->left);
|
Type *type = ssa_value_type(bo->left);
|
||||||
|
|
||||||
ssa_fprintf(f, "%%%d = ", value->id);
|
ssa_fprintf(f, "%%%d = ", value->id);
|
||||||
@@ -319,9 +331,9 @@ void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
|||||||
if (bo->op.kind != Token_CmpEq &&
|
if (bo->op.kind != Token_CmpEq &&
|
||||||
bo->op.kind != Token_NotEq) {
|
bo->op.kind != Token_NotEq) {
|
||||||
if (is_type_unsigned(type)) {
|
if (is_type_unsigned(type)) {
|
||||||
ssa_fprintf(f, "s");
|
|
||||||
} else {
|
|
||||||
ssa_fprintf(f, "u");
|
ssa_fprintf(f, "u");
|
||||||
|
} else {
|
||||||
|
ssa_fprintf(f, "s");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (bo->op.kind) {
|
switch (bo->op.kind) {
|
||||||
@@ -401,7 +413,7 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
|||||||
} break;
|
} break;
|
||||||
|
|
||||||
case ssaValue_Procedure: {
|
case ssaValue_Procedure: {
|
||||||
ssaProcedure *proc = &v->procedure;
|
ssaProcedure *proc = &v->proc;
|
||||||
if (proc->body == NULL) {
|
if (proc->body == NULL) {
|
||||||
ssa_fprintf(f, "declare ");
|
ssa_fprintf(f, "declare ");
|
||||||
} else {
|
} else {
|
||||||
@@ -438,12 +450,12 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
|||||||
ssa_fprintf(f, "\n");
|
ssa_fprintf(f, "\n");
|
||||||
} else {
|
} else {
|
||||||
ssa_fprintf(f, "{\n");
|
ssa_fprintf(f, "{\n");
|
||||||
gb_for_array(i, proc->blocks) {
|
gb_for_array(i, proc->blocks.entries) {
|
||||||
ssaBlock *block = &proc->blocks[i]->block;
|
ssaBlock *block = &proc->blocks.entries[i].value->block;
|
||||||
ssa_fprintf(f, "%.*s:\n", LIT(block->label));
|
ssa_fprintf(f, "%.*s:\n", LIT(block->label));
|
||||||
gb_for_array(j, block->instructions) {
|
gb_for_array(j, block->instrs) {
|
||||||
ssaValue *value = block->instructions[j];
|
ssaValue *value = block->instrs[j];
|
||||||
ssa_print_instruction(f, m, value);
|
ssa_print_instr(f, m, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+396
-273
File diff suppressed because it is too large
Load Diff
+319
-387
File diff suppressed because it is too large
Load Diff
+64
-64
@@ -12,24 +12,24 @@ void print_ast(AstNode *node, isize indent) {
|
|||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
case AstNode_BasicLit:
|
case AstNode_BasicLit:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->basic_lit);
|
print_token(node->BasicLit);
|
||||||
break;
|
break;
|
||||||
case AstNode_Ident:
|
case AstNode_Ident:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->ident.token);
|
print_token(node->Ident.token);
|
||||||
break;
|
break;
|
||||||
case AstNode_ProcLit:
|
case AstNode_ProcLit:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(proc lit)\n");
|
gb_printf("(proc lit)\n");
|
||||||
print_ast(node->proc_lit.type, indent+1);
|
print_ast(node->ProcLit.type, indent+1);
|
||||||
print_ast(node->proc_lit.body, indent+1);
|
print_ast(node->ProcLit.body, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_CompoundLit:
|
case AstNode_CompoundLit:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(compound lit)\n");
|
gb_printf("(compound lit)\n");
|
||||||
print_ast(node->compound_lit.type, indent+1);
|
print_ast(node->CompoundLit.type, indent+1);
|
||||||
print_ast(node->compound_lit.elem_list, indent+1);
|
print_ast(node->CompoundLit.elem_list, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
@@ -37,168 +37,168 @@ void print_ast(AstNode *node, isize indent) {
|
|||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(tag)\n");
|
gb_printf("(tag)\n");
|
||||||
print_indent(indent+1);
|
print_indent(indent+1);
|
||||||
print_token(node->tag_expr.name);
|
print_token(node->TagExpr.name);
|
||||||
print_ast(node->tag_expr.expr, indent+1);
|
print_ast(node->TagExpr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_UnaryExpr:
|
case AstNode_UnaryExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->unary_expr.op);
|
print_token(node->UnaryExpr.op);
|
||||||
print_ast(node->unary_expr.expr, indent+1);
|
print_ast(node->UnaryExpr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_BinaryExpr:
|
case AstNode_BinaryExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->binary_expr.op);
|
print_token(node->BinaryExpr.op);
|
||||||
print_ast(node->binary_expr.left, indent+1);
|
print_ast(node->BinaryExpr.left, indent+1);
|
||||||
print_ast(node->binary_expr.right, indent+1);
|
print_ast(node->BinaryExpr.right, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_CallExpr:
|
case AstNode_CallExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(call)\n");
|
gb_printf("(call)\n");
|
||||||
print_ast(node->call_expr.proc, indent+1);
|
print_ast(node->CallExpr.proc, indent+1);
|
||||||
print_ast(node->call_expr.arg_list, indent+1);
|
print_ast(node->CallExpr.arg_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_SelectorExpr:
|
case AstNode_SelectorExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf(".\n");
|
gb_printf(".\n");
|
||||||
print_ast(node->selector_expr.expr, indent+1);
|
print_ast(node->SelectorExpr.expr, indent+1);
|
||||||
print_ast(node->selector_expr.selector, indent+1);
|
print_ast(node->SelectorExpr.selector, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_IndexExpr:
|
case AstNode_IndexExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("([])\n");
|
gb_printf("([])\n");
|
||||||
print_ast(node->index_expr.expr, indent+1);
|
print_ast(node->IndexExpr.expr, indent+1);
|
||||||
print_ast(node->index_expr.index, indent+1);
|
print_ast(node->IndexExpr.index, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_CastExpr:
|
case AstNode_CastExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(cast)\n");
|
gb_printf("(cast)\n");
|
||||||
print_ast(node->cast_expr.type, indent+1);
|
print_ast(node->CastExpr.type, indent+1);
|
||||||
print_ast(node->cast_expr.expr, indent+1);
|
print_ast(node->CastExpr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_DerefExpr:
|
case AstNode_DerefExpr:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(deref)\n");
|
gb_printf("(deref)\n");
|
||||||
print_ast(node->deref_expr.expr, indent+1);
|
print_ast(node->DerefExpr.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_ExprStmt:
|
case AstNode_ExprStmt:
|
||||||
print_ast(node->expr_stmt.expr, indent);
|
print_ast(node->ExprStmt.expr, indent);
|
||||||
break;
|
break;
|
||||||
case AstNode_IncDecStmt:
|
case AstNode_IncDecStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->inc_dec_stmt.op);
|
print_token(node->IncDecStmt.op);
|
||||||
print_ast(node->inc_dec_stmt.expr, indent+1);
|
print_ast(node->IncDecStmt.expr, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_AssignStmt:
|
case AstNode_AssignStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->assign_stmt.op);
|
print_token(node->AssignStmt.op);
|
||||||
print_ast(node->assign_stmt.lhs_list, indent+1);
|
print_ast(node->AssignStmt.lhs_list, indent+1);
|
||||||
print_ast(node->assign_stmt.rhs_list, indent+1);
|
print_ast(node->AssignStmt.rhs_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_BlockStmt:
|
case AstNode_BlockStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(block)\n");
|
gb_printf("(block)\n");
|
||||||
print_ast(node->block_stmt.list, indent+1);
|
print_ast(node->BlockStmt.list, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_IfStmt:
|
case AstNode_IfStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(if)\n");
|
gb_printf("(if)\n");
|
||||||
print_ast(node->if_stmt.cond, indent+1);
|
print_ast(node->IfStmt.cond, indent+1);
|
||||||
print_ast(node->if_stmt.body, indent+1);
|
print_ast(node->IfStmt.body, indent+1);
|
||||||
if (node->if_stmt.else_stmt) {
|
if (node->IfStmt.else_stmt) {
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(else)\n");
|
gb_printf("(else)\n");
|
||||||
print_ast(node->if_stmt.else_stmt, indent+1);
|
print_ast(node->IfStmt.else_stmt, indent+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case AstNode_ReturnStmt:
|
case AstNode_ReturnStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(return)\n");
|
gb_printf("(return)\n");
|
||||||
print_ast(node->return_stmt.result_list, indent+1);
|
print_ast(node->ReturnStmt.result_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_ForStmt:
|
case AstNode_ForStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(for)\n");
|
gb_printf("(for)\n");
|
||||||
print_ast(node->for_stmt.init, indent+1);
|
print_ast(node->ForStmt.init, indent+1);
|
||||||
print_ast(node->for_stmt.cond, indent+1);
|
print_ast(node->ForStmt.cond, indent+1);
|
||||||
print_ast(node->for_stmt.end, indent+1);
|
print_ast(node->ForStmt.end, indent+1);
|
||||||
print_ast(node->for_stmt.body, indent+1);
|
print_ast(node->ForStmt.body, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_DeferStmt:
|
case AstNode_DeferStmt:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(defer)\n");
|
gb_printf("(defer)\n");
|
||||||
print_ast(node->defer_stmt.stmt, indent+1);
|
print_ast(node->DeferStmt.stmt, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_VarDecl:
|
case AstNode_VarDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
if (node->var_decl.kind == Declaration_Mutable)
|
if (node->VarDecl.kind == Declaration_Mutable)
|
||||||
gb_printf("(decl:var,mutable)\n");
|
gb_printf("(decl:var,mutable)\n");
|
||||||
else if (node->var_decl.kind == Declaration_Immutable)
|
else if (node->VarDecl.kind == Declaration_Immutable)
|
||||||
gb_printf("(decl:var,immutable)\n");
|
gb_printf("(decl:var,immutable)\n");
|
||||||
print_ast(node->var_decl.name_list, indent+1);
|
print_ast(node->VarDecl.name_list, indent+1);
|
||||||
print_ast(node->var_decl.type, indent+1);
|
print_ast(node->VarDecl.type, indent+1);
|
||||||
print_ast(node->var_decl.value_list, indent+1);
|
print_ast(node->VarDecl.value_list, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_ProcDecl:
|
case AstNode_ProcDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
if (node->proc_decl.kind == Declaration_Mutable)
|
if (node->ProcDecl.kind == Declaration_Mutable)
|
||||||
gb_printf("(decl:proc,mutable)\n");
|
gb_printf("(decl:proc,mutable)\n");
|
||||||
else if (node->proc_decl.kind == Declaration_Immutable)
|
else if (node->ProcDecl.kind == Declaration_Immutable)
|
||||||
gb_printf("(decl:proc,immutable)\n");
|
gb_printf("(decl:proc,immutable)\n");
|
||||||
print_ast(node->proc_decl.type, indent+1);
|
print_ast(node->ProcDecl.type, indent+1);
|
||||||
print_ast(node->proc_decl.body, indent+1);
|
print_ast(node->ProcDecl.body, indent+1);
|
||||||
print_ast(node->proc_decl.tag_list, indent+1);
|
print_ast(node->ProcDecl.tag_list, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_TypeDecl:
|
case AstNode_TypeDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(type)\n");
|
gb_printf("(type)\n");
|
||||||
print_ast(node->type_decl.name, indent+1);
|
print_ast(node->TypeDecl.name, indent+1);
|
||||||
print_ast(node->type_decl.type, indent+1);
|
print_ast(node->TypeDecl.type, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AstNode_AliasDecl:
|
case AstNode_AliasDecl:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(alias)\n");
|
gb_printf("(alias)\n");
|
||||||
print_ast(node->alias_decl.name, indent+1);
|
print_ast(node->AliasDecl.name, indent+1);
|
||||||
print_ast(node->alias_decl.type, indent+1);
|
print_ast(node->AliasDecl.type, indent+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case AstNode_ProcType:
|
case AstNode_ProcType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(type:proc)(%td -> %td)\n", node->proc_type.param_count, node->proc_type.result_count);
|
gb_printf("(type:proc)(%td -> %td)\n", node->ProcType.param_count, node->ProcType.result_count);
|
||||||
print_ast(node->proc_type.param_list, indent+1);
|
print_ast(node->ProcType.param_list, indent+1);
|
||||||
if (node->proc_type.result_list) {
|
if (node->ProcType.result_list) {
|
||||||
print_indent(indent+1);
|
print_indent(indent+1);
|
||||||
gb_printf("->\n");
|
gb_printf("->\n");
|
||||||
print_ast(node->proc_type.result_list, indent+1);
|
print_ast(node->ProcType.result_list, indent+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case AstNode_Field:
|
case AstNode_Field:
|
||||||
print_ast(node->field.name_list, indent);
|
print_ast(node->Field.name_list, indent);
|
||||||
print_ast(node->field.type, indent);
|
print_ast(node->Field.type, indent);
|
||||||
break;
|
break;
|
||||||
case AstNode_PointerType:
|
case AstNode_PointerType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
print_token(node->pointer_type.token);
|
print_token(node->PointerType.token);
|
||||||
print_ast(node->pointer_type.type, indent+1);
|
print_ast(node->PointerType.type, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_ArrayType:
|
case AstNode_ArrayType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("[]\n");
|
gb_printf("[]\n");
|
||||||
print_ast(node->array_type.count, indent+1);
|
print_ast(node->ArrayType.count, indent+1);
|
||||||
print_ast(node->array_type.elem, indent+1);
|
print_ast(node->ArrayType.elem, indent+1);
|
||||||
break;
|
break;
|
||||||
case AstNode_StructType:
|
case AstNode_StructType:
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
gb_printf("(struct)\n");
|
gb_printf("(struct)\n");
|
||||||
print_ast(node->struct_type.field_list, indent+1);
|
print_ast(node->StructType.field_list, indent+1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-1
@@ -705,7 +705,12 @@ Token tokenizer_get_token(Tokenizer *t) {
|
|||||||
case '&':
|
case '&':
|
||||||
token.kind = Token_And;
|
token.kind = Token_And;
|
||||||
if (t->curr_rune == '~') {
|
if (t->curr_rune == '~') {
|
||||||
token.kind = token_type_variant2(t, Token_AndNot, Token_AndNotEq);
|
token.kind = Token_AndNot;
|
||||||
|
advance_to_next_rune(t);
|
||||||
|
if (t->curr_rune == '=') {
|
||||||
|
token.kind = Token_AndNotEq;
|
||||||
|
advance_to_next_rune(t);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
token.kind = token_type_variant3(t, Token_And, Token_AndEq, '&', Token_CmpAnd);
|
token.kind = token_type_variant3(t, Token_And, Token_AndEq, '&', Token_CmpAnd);
|
||||||
if (t->curr_rune == '=') {
|
if (t->curr_rune == '=') {
|
||||||
|
|||||||
Reference in New Issue
Block a user