VarDecl and ConstDecl split; error, warning, et al. now global

This commit is contained in:
Ginger Bill
2016-09-17 11:39:08 +01:00
parent eb424bb315
commit 67694c0df0
13 changed files with 1840 additions and 670 deletions
+66 -69
View File
@@ -237,8 +237,6 @@ struct Checker {
gbArray(Type *) proc_stack;
b32 in_defer; // TODO(bill): Actually handle correctly
ErrorCollector error_collector;
};
gb_global Scope *universal_scope = NULL;
@@ -635,18 +633,23 @@ b32 add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) {
if (insert_entity) {
Entity *up = insert_entity->using_parent;
if (up != NULL) {
error(&c->error_collector, entity->token,
error(entity->token,
"Redeclararation of `%.*s` in this scope through `using`\n"
"\tat %.*s(%td:%td)",
LIT(entity->token.string),
LIT(up->token.pos.file), up->token.pos.line, up->token.pos.column);
return false;
} else {
error(&c->error_collector, entity->token,
TokenPos pos = insert_entity->token.pos;
if (token_pos_are_equal(pos, entity->token.pos)) {
// NOTE(bill): Error should have been handled already
return false;
}
error(entity->token,
"Redeclararation of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(entity->token.string),
LIT(entity->token.pos.file), entity->token.pos.line, entity->token.pos.column);
LIT(pos.file), pos.line, pos.column);
return false;
}
}
@@ -797,7 +800,7 @@ Type *const curr_procedure(Checker *c) {
void add_curr_ast_file(Checker *c, AstFile *file) {
TokenPos zero_pos = {};
c->error_collector.prev = zero_pos;
global_error_collector.prev = zero_pos;
c->curr_ast_file = file;
}
@@ -924,64 +927,60 @@ void check_parsed_files(Checker *c) {
// NOTE(bill): ignore
case_end;
case_ast_node(cd, ConstDecl, decl);
gb_for_array(i, cd->values) {
AstNode *name = cd->names[i];
AstNode *value = cd->values[i];
ExactValue v = {ExactValue_Invalid};
Entity *e = make_entity_constant(c->allocator, file_scope, name->Ident, NULL, v);
DeclInfo *di = make_declaration_info(c->allocator, file_scope);
di->type_expr = cd->type;
di->init_expr = value;
add_file_entity(c, file_scope, name, e, di);
}
isize lhs_count = gb_array_count(cd->names);
isize rhs_count = gb_array_count(cd->values);
if (rhs_count == 0 && cd->type == NULL) {
error(ast_node_token(decl), "Missing type or initial expression");
} else if (lhs_count < rhs_count) {
error(ast_node_token(decl), "Extra initial expression");
}
case_end;
case_ast_node(vd, VarDecl, decl);
switch (vd->kind) {
case Declaration_Immutable: {
gb_for_array(i, vd->values) {
AstNode *name = vd->names[i];
AstNode *value = vd->values[i];
ExactValue v = {ExactValue_Invalid};
Entity *e = make_entity_constant(c->allocator, file_scope, name->Ident, NULL, v);
DeclInfo *di = make_declaration_info(c->allocator, file_scope);
di->type_expr = vd->type;
di->init_expr = value;
add_file_entity(c, file_scope, name, e, di);
isize entity_count = gb_array_count(vd->names);
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
DeclInfo *di = NULL;
if (gb_array_count(vd->values) > 0) {
di = make_declaration_info(gb_heap_allocator(), file_scope);
di->entities = entities;
di->entity_count = entity_count;
di->type_expr = vd->type;
di->init_expr = vd->values[0]; // TODO(bill): Is this correct?
}
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
AstNode *value = NULL;
if (i < gb_array_count(vd->values)) {
value = vd->values[i];
}
Entity *e = make_entity_variable(c->allocator, file_scope, name->Ident, NULL);
entities[entity_index++] = e;
DeclInfo *d = di;
if (d == NULL) {
AstNode *init_expr = value;
d = make_declaration_info(gb_heap_allocator(), file_scope);
d->type_expr = vd->type;
d->init_expr = init_expr;
d->var_decl_tags = vd->tags;
}
isize lhs_count = gb_array_count(vd->names);
isize rhs_count = gb_array_count(vd->values);
if (rhs_count == 0 && vd->type == NULL) {
error(&c->error_collector, ast_node_token(decl), "Missing type or initial expression");
} else if (lhs_count < rhs_count) {
error(&c->error_collector, ast_node_token(decl), "Extra initial expression");
}
} break;
case Declaration_Mutable: {
isize entity_count = gb_array_count(vd->names);
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
DeclInfo *di = NULL;
if (gb_array_count(vd->values) > 0) {
di = make_declaration_info(gb_heap_allocator(), file_scope);
di->entities = entities;
di->entity_count = entity_count;
di->type_expr = vd->type;
di->init_expr = vd->values[0]; // TODO(bill): Is this correct?
}
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
AstNode *value = NULL;
if (i < gb_array_count(vd->values)) {
value = vd->values[i];
}
Entity *e = make_entity_variable(c->allocator, file_scope, name->Ident, NULL);
entities[entity_index++] = e;
DeclInfo *d = di;
if (d == NULL) {
AstNode *init_expr = value;
d = make_declaration_info(gb_heap_allocator(), file_scope);
d->type_expr = vd->type;
d->init_expr = init_expr;
d->var_decl_tags = vd->tags;
}
add_file_entity(c, file_scope, name, e, d);
}
} break;
add_file_entity(c, file_scope, name, e, d);
}
case_end;
@@ -1003,7 +1002,7 @@ void check_parsed_files(Checker *c) {
case_end;
default:
error(&c->error_collector, ast_node_token(decl), "Only declarations are allowed at file scope");
error(ast_node_token(decl), "Only declarations are allowed at file scope");
break;
}
}
@@ -1046,13 +1045,11 @@ void check_parsed_files(Checker *c) {
continue;
}
// NOTE(bill): Do not add other imported entities
if (e->kind != Entity_ImportName) {
if (is_entity_exported(e)) {
add_entity(c, file_scope, NULL, e);
if (!id->is_load) {
HashKey key = hash_string(e->token.string);
map_set(&file_scope->implicit, key, e);
}
if (is_entity_exported(e)) {
add_entity(c, file_scope, NULL, e);
if (!id->is_load) { // `#import`ed entities don't get exported
HashKey key = hash_string(e->token.string);
map_set(&file_scope->implicit, key, e);
}
}
}
+3
View File
@@ -67,6 +67,9 @@ struct Entity {
};
b32 is_entity_exported(Entity *e) {
if (e->kind == Entity_ImportName) {
return false;
}
// TODO(bill): Do I really want non-exported entities?
// if (e->token.string.len >= 1 &&
// e->token.string.text[0] == '_') {
+200 -212
View File
File diff suppressed because it is too large Load Diff
+161 -151
View File
@@ -203,7 +203,7 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) {
gbString str = expr_to_string(op_b.expr);
defer (gb_string_free(str));
error(&c->error_collector, ast_node_token(op_b.expr), "Cannot assign to `%s`", str);
error(ast_node_token(op_b.expr), "Cannot assign to `%s`", str);
} break;
}
@@ -225,7 +225,7 @@ Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String contex
defer (gb_string_free(expr_str));
// TODO(bill): is this a good enough error message?
error(&c->error_collector, ast_node_token(operand->expr),
error(ast_node_token(operand->expr),
"Cannot assign builtin procedure `%s` in %.*s",
expr_str,
LIT(context_name));
@@ -244,7 +244,7 @@ Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String contex
Type *t = operand->type;
if (is_type_untyped(t)) {
if (t == t_invalid) {
error(&c->error_collector, e->token, "Use of untyped thing in %.*s", LIT(context_name));
error(e->token, "Use of untyped thing in %.*s", LIT(context_name));
e->type = t_invalid;
return NULL;
}
@@ -285,6 +285,12 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
}
isize rhs_count = gb_array_count(operands);
gb_for_array(i, operands) {
if (operands[i].mode == Addressing_Invalid) {
rhs_count--;
}
}
isize max = gb_min(lhs_count, rhs_count);
for (isize i = 0; i < max; i++) {
@@ -292,7 +298,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
}
if (rhs_count > 0 && lhs_count != rhs_count) {
error(&c->error_collector, lhs[0]->token, "Assignment count mismatch `%td` := `%td`", lhs_count, rhs_count);
error(lhs[0]->token, "Assignment count mismatch `%td` := `%td`", lhs_count, rhs_count);
}
}
@@ -307,7 +313,7 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
if (operand->mode != Addressing_Constant) {
// TODO(bill): better error
error(&c->error_collector, ast_node_token(operand->expr),
error(ast_node_token(operand->expr),
"`%.*s` is not a constant", LIT(ast_node_token(operand->expr).string));
if (e->type == NULL)
e->type = t_invalid;
@@ -343,7 +349,7 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_e
if (!is_type_constant_type(t)) {
gbString str = type_to_string(t);
defer (gb_string_free(str));
error(&c->error_collector, ast_node_token(type_expr),
error(ast_node_token(type_expr),
"Invalid constant type `%s`", str);
e->type = t_invalid;
return;
@@ -411,13 +417,13 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != NULL) {
error(&c->error_collector, e->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
error(e->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
break;
}
}
}
} else {
error(&c->error_collector, e->token, "`using` can only be applied to variables of type struct or raw_union");
error(e->token, "`using` can only be applied to variables of type struct or raw_union");
break;
}
}
@@ -429,7 +435,7 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
check_stmt_list(c, bs->stmts, 0);
if (type->Proc.result_count > 0) {
if (!check_is_terminating(body)) {
error(&c->error_collector, bs->close, "Missing return statement at the end of the procedure");
error(bs->close, "Missing return statement at the end of the procedure");
}
}
pop_procedure(c);
@@ -501,20 +507,20 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
gbString str = type_to_string(proc_type);
defer (gb_string_free(str));
error(&c->error_collector, e->token,
error(e->token,
"Procedure type of `main` was expected to be `proc()`, got %s", str);
}
}
}
if (is_inline && is_no_inline) {
error(&c->error_collector, ast_node_token(pd->type),
error(ast_node_token(pd->type),
"You cannot apply both `inline` and `no_inline` to a procedure");
}
if (pd->body != NULL) {
if (is_foreign) {
error(&c->error_collector, ast_node_token(pd->body),
error(ast_node_token(pd->body),
"A procedure tagged as `#foreign` cannot have a body");
}
@@ -543,7 +549,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
Type *this_type = get_base_type(e->type);
Type *other_type = get_base_type(f->type);
if (!are_signatures_similar_enough(this_type, other_type)) {
error(&c->error_collector, ast_node_token(d->proc_decl),
error(ast_node_token(d->proc_decl),
"Redeclaration of #foreign procedure `%.*s` with different type signatures\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
@@ -662,118 +668,115 @@ void check_entity_decl(Checker *c, Entity *e, DeclInfo *d, Type *named_type, Cyc
void check_var_decl_node(Checker *c, AstNode *node) {
void check_var_decl(Checker *c, AstNode *node) {
ast_node(vd, VarDecl, node);
isize entity_count = gb_array_count(vd->names);
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
switch (vd->kind) {
case Declaration_Mutable: {
Entity **new_entities = gb_alloc_array(c->allocator, Entity *, entity_count);
isize new_entity_count = 0;
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
Entity *entity = NULL;
Token token = name->Ident;
if (name->kind == AstNode_Ident) {
String str = token.string;
Entity *found = NULL;
// NOTE(bill): Ignore assignments to `_`
b32 can_be_ignored = are_strings_equal(str, make_string("_"));
if (!can_be_ignored) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL);
if (!can_be_ignored) {
new_entities[new_entity_count++] = entity;
}
add_entity_definition(&c->info, name, entity);
} else {
TokenPos pos = found->token.pos;
error(&c->error_collector, token,
"Redeclaration of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(str), LIT(pos.file), pos.line, pos.column);
entity = found;
}
} else {
error(&c->error_collector, token, "A variable declaration must be an identifier");
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
Entity *entity = NULL;
Token token = name->Ident;
if (name->kind == AstNode_Ident) {
String str = token.string;
Entity *found = NULL;
// NOTE(bill): Ignore assignments to `_`
b32 can_be_ignored = are_strings_equal(str, make_string("_"));
if (!can_be_ignored) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (entity == NULL)
entity = make_entity_dummy_variable(c->allocator, c->global_scope, token);
entities[entity_index++] = entity;
}
Type *init_type = NULL;
if (vd->type) {
init_type = check_type(c, vd->type, NULL);
if (init_type == NULL)
init_type = t_invalid;
}
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
GB_ASSERT(e != NULL);
if (e->Variable.visited) {
e->type = t_invalid;
continue;
}
e->Variable.visited = true;
if (e->type == NULL)
e->type = init_type;
}
check_init_variables(c, entities, entity_count, vd->values, make_string("variable declaration"));
gb_for_array(i, vd->names) {
add_entity(c, c->context.scope, vd->names[i], new_entities[i]);
}
} break;
case Declaration_Immutable: {
gb_for_array(i, vd->values) {
AstNode *name = vd->names[i];
AstNode *value = vd->values[i];
GB_ASSERT(name->kind == AstNode_Ident);
ExactValue v = {ExactValue_Invalid};
String str = name->Ident.string;
Entity *found = current_scope_lookup_entity(c->context.scope, str);
if (found == NULL) {
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->Ident, NULL, v);
entities[entity_index++] = e;
check_const_decl(c, e, vd->type, value);
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL);
add_entity_definition(&c->info, name, entity);
} else {
entities[entity_index++] = found;
TokenPos pos = found->token.pos;
error(token,
"Redeclaration of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(str), LIT(pos.file), pos.line, pos.column);
entity = found;
}
} else {
error(token, "A variable declaration must be an identifier");
}
if (entity == NULL)
entity = make_entity_dummy_variable(c->allocator, c->global_scope, token);
entities[entity_index++] = entity;
}
isize lhs_count = gb_array_count(vd->names);
isize rhs_count = gb_array_count(vd->values);
Type *init_type = NULL;
if (vd->type) {
init_type = check_type(c, vd->type, NULL);
if (init_type == NULL)
init_type = t_invalid;
}
// TODO(bill): Better error messages or is this good enough?
if (rhs_count == 0 && vd->type == NULL) {
error(&c->error_collector, ast_node_token(node), "Missing type or initial expression");
} else if (lhs_count < rhs_count) {
error(&c->error_collector, ast_node_token(node), "Extra initial expression");
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
GB_ASSERT(e != NULL);
if (e->Variable.visited) {
e->type = t_invalid;
continue;
}
e->Variable.visited = true;
gb_for_array(i, vd->names) {
if (e->type == NULL)
e->type = init_type;
}
check_init_variables(c, entities, entity_count, vd->values, make_string("variable declaration"));
gb_for_array(i, vd->names) {
if (entities[i] != NULL) {
add_entity(c, c->context.scope, vd->names[i], entities[i]);
}
} break;
}
default:
error(&c->error_collector, ast_node_token(node), "Unknown variable declaration kind. Probably an invalid AST.");
return;
}
void check_const_decl(Checker *c, AstNode *node) {
ast_node(vd, ConstDecl, node);
isize entity_count = gb_array_count(vd->names);
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
gb_for_array(i, vd->values) {
AstNode *name = vd->names[i];
AstNode *value = vd->values[i];
GB_ASSERT(name->kind == AstNode_Ident);
ExactValue v = {ExactValue_Invalid};
String str = name->Ident.string;
Entity *found = current_scope_lookup_entity(c->context.scope, str);
if (found == NULL) {
Entity *e = make_entity_constant(c->allocator, c->context.scope, name->Ident, NULL, v);
entities[entity_index++] = e;
check_const_decl(c, e, vd->type, value);
} else {
entities[entity_index++] = found;
}
}
isize lhs_count = gb_array_count(vd->names);
isize rhs_count = gb_array_count(vd->values);
// TODO(bill): Better error messages or is this good enough?
if (rhs_count == 0 && vd->type == NULL) {
error(ast_node_token(node), "Missing type or initial expression");
} else if (lhs_count < rhs_count) {
error(ast_node_token(node), "Extra initial expression");
}
gb_for_array(i, vd->names) {
add_entity(c, c->context.scope, vd->names[i], entities[i]);
}
}
void check_stmt(Checker *c, AstNode *node, u32 flags) {
u32 mod_flags = flags & (~Stmt_FallthroughAllowed);
switch (node->kind) {
@@ -786,7 +789,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
ExprKind kind = check_expr_base(c, &operand, es->expr);
switch (operand.mode) {
case Addressing_Type:
error(&c->error_collector, ast_node_token(node), "Is not an expression");
error(ast_node_token(node), "Is not an expression");
break;
case Addressing_NoValue:
return;
@@ -800,14 +803,14 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
return;
}
error(&c->error_collector, ast_node_token(node), "Expression is not used: `%s`", expr_str);
error(ast_node_token(node), "Expression is not used: `%s`", expr_str);
} break;
}
case_end;
case_ast_node(ts, TagStmt, node);
// TODO(bill): Tag Statements
error(&c->error_collector, ast_node_token(node), "Tag statements are not supported yet");
error(ast_node_token(node), "Tag statements are not supported yet");
check_stmt(c, ts->stmt, flags);
case_end;
@@ -823,7 +826,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
op.string.len = 1;
break;
default:
error(&c->error_collector, ids->op, "Unknown inc/dec operation %.*s", LIT(ids->op.string));
error(ids->op, "Unknown inc/dec operation %.*s", LIT(ids->op.string));
return;
}
@@ -832,7 +835,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
if (operand.mode == Addressing_Invalid)
return;
if (!is_type_numeric(operand.type)) {
error(&c->error_collector, ids->op, "Non numeric type");
error(ids->op, "Non numeric type");
return;
}
@@ -855,7 +858,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
case Token_Eq: {
// a, b, c = 1, 2, 3; // Multisided
if (gb_array_count(as->lhs) == 0) {
error(&c->error_collector, as->op, "Missing lhs in assignment statement");
error(as->op, "Missing lhs in assignment statement");
return;
}
@@ -888,7 +891,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
check_assignment_variable(c, &operands[i], lhs);
}
if (lhs_count != rhs_count) {
error(&c->error_collector, ast_node_token(as->lhs[0]), "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count);
error(ast_node_token(as->lhs[0]), "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count);
}
} break;
@@ -896,11 +899,11 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
// a += 1; // Single-sided
Token op = as->op;
if (gb_array_count(as->lhs) != 1 || gb_array_count(as->rhs) != 1) {
error(&c->error_collector, op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
error(op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
return;
}
if (!gb_is_between(op.kind, Token__AssignOpBegin+1, Token__AssignOpEnd-1)) {
error(&c->error_collector, op, "Unknown Assignment operation `%.*s`", LIT(op.string));
error(op, "Unknown Assignment operation `%.*s`", LIT(op.string));
return;
}
// TODO(bill): Check if valid assignment operator
@@ -938,7 +941,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
check_expr(c, &operand, is->cond);
if (operand.mode != Addressing_Invalid &&
!is_type_boolean(operand.type)) {
error(&c->error_collector, ast_node_token(is->cond),
error(ast_node_token(is->cond),
"Non-boolean condition in `if` statement");
}
@@ -951,7 +954,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
check_stmt(c, is->else_stmt, mod_flags);
break;
default:
error(&c->error_collector, ast_node_token(is->else_stmt),
error(ast_node_token(is->else_stmt),
"Invalid `else` statement in `if` statement");
break;
}
@@ -962,23 +965,28 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
GB_ASSERT(gb_array_count(c->proc_stack) > 0);
if (c->in_defer) {
error(&c->error_collector, rs->token, "You cannot `return` within a defer statement");
error(rs->token, "You cannot `return` within a defer statement");
// TODO(bill): Should I break here?
break;
}
Type *proc_type = c->proc_stack[gb_array_count(c->proc_stack)-1];
isize result_count = 0;
if (proc_type->Proc.results)
if (proc_type->Proc.results) {
result_count = proc_type->Proc.results->Tuple.variable_count;
}
if (result_count != gb_array_count(rs->results)) {
error(&c->error_collector, rs->token, "Expected %td return %s, got %td",
error(rs->token, "Expected %td return %s, got %td",
result_count,
(result_count != 1 ? "values" : "value"),
gb_array_count(rs->results));
} else if (result_count > 0) {
auto *tuple = &proc_type->Proc.results->Tuple;
check_init_variables(c, tuple->variables, tuple->variable_count,
Entity **variables = NULL;
if (proc_type->Proc.results != NULL) {
auto *tuple = &proc_type->Proc.results->Tuple;
variables = tuple->variables;
}
check_init_variables(c, variables, result_count,
rs->results, make_string("return statement"));
}
case_end;
@@ -995,7 +1003,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
check_expr(c, &operand, fs->cond);
if (operand.mode != Addressing_Invalid &&
!is_type_boolean(operand.type)) {
error(&c->error_collector, ast_node_token(fs->cond),
error(ast_node_token(fs->cond),
"Non-boolean condition in `for` statement");
}
}
@@ -1040,13 +1048,13 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
default_stmt = stmt;
}
} else {
error(&c->error_collector, ast_node_token(stmt), "Invalid AST - expected case clause");
error(ast_node_token(stmt), "Invalid AST - expected case clause");
}
if (default_stmt != NULL) {
if (first_default != NULL) {
TokenPos pos = ast_node_token(first_default).pos;
error(&c->error_collector, ast_node_token(stmt),
error(ast_node_token(stmt),
"multiple `default` clauses\n"
"\tfirst at %.*s(%td:%td)", LIT(pos.file), pos.line, pos.column);
} else {
@@ -1114,8 +1122,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
if (are_types_identical(y.type, tap.type)) {
TokenPos pos = tap.token.pos;
gbString expr_str = expr_to_string(y.expr);
error(&c->error_collector,
ast_node_token(y.expr),
error(ast_node_token(y.expr),
"Duplicate case `%s`\n"
"\tprevious case at %.*s(%td:%td)",
expr_str,
@@ -1158,7 +1165,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
if (!is_type_pointer(x.type) || !is_type_union(type_deref(x.type))) {
gbString str = type_to_string(x.type);
defer (gb_string_free(str));
error(&c->error_collector, ast_node_token(x.expr),
error(ast_node_token(x.expr),
"Expected a pointer to a union for this type match expression, got `%s`", str);
break;
}
@@ -1177,13 +1184,13 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
default_stmt = stmt;
}
} else {
error(&c->error_collector, ast_node_token(stmt), "Invalid AST - expected case clause");
error(ast_node_token(stmt), "Invalid AST - expected case clause");
}
if (default_stmt != NULL) {
if (first_default != NULL) {
TokenPos pos = ast_node_token(first_default).pos;
error(&c->error_collector, ast_node_token(stmt),
error(ast_node_token(stmt),
"multiple `default` clauses\n"
"\tfirst at %.*s(%td:%td)", LIT(pos.file), pos.line, pos.column);
} else {
@@ -1226,7 +1233,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
if (!tag_type_found) {
gbString type_str = type_to_string(y.type);
defer (gb_string_free(type_str));
error(&c->error_collector, ast_node_token(y.expr),
error(ast_node_token(y.expr),
"Unknown tag type, got `%s`", type_str);
continue;
}
@@ -1237,8 +1244,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
if (found) {
TokenPos pos = cc->token.pos;
gbString expr_str = expr_to_string(y.expr);
error(&c->error_collector,
ast_node_token(y.expr),
error(ast_node_token(y.expr),
"Duplicate type case `%s`\n"
"\tprevious type case at %.*s(%td:%td)",
expr_str,
@@ -1266,7 +1272,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
case_ast_node(ds, DeferStmt, node);
if (is_ast_node_decl(ds->stmt)) {
error(&c->error_collector, ds->token, "You cannot defer a declaration");
error(ds->token, "You cannot defer a declaration");
} else {
b32 out_in_defer = c->in_defer;
c->in_defer = true;
@@ -1280,18 +1286,18 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
switch (token.kind) {
case Token_break:
if ((flags & Stmt_BreakAllowed) == 0)
error(&c->error_collector, token, "`break` only allowed in `for` or `match` statements");
error(token, "`break` only allowed in `for` or `match` statements");
break;
case Token_continue:
if ((flags & Stmt_ContinueAllowed) == 0)
error(&c->error_collector, token, "`continue` only allowed in `for` statements");
error(token, "`continue` only allowed in `for` statements");
break;
case Token_fallthrough:
if ((flags & Stmt_FallthroughAllowed) == 0)
error(&c->error_collector, token, "`fallthrough` statement in illegal position");
error(token, "`fallthrough` statement in illegal position");
break;
default:
error(&c->error_collector, token, "Invalid AST: Branch Statement `%.*s`", LIT(token.string));
error(token, "Invalid AST: Branch Statement `%.*s`", LIT(token.string));
break;
}
case_end;
@@ -1315,7 +1321,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
}
if (e == NULL) {
error(&c->error_collector, us->token, "`using` applied to an unknown entity");
error(us->token, "`using` applied to an unknown entity");
return;
}
@@ -1330,7 +1336,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
Entity *f = t->Record.other_fields[i];
Entity *found = scope_insert_entity(c->context.scope, f);
if (found != NULL) {
error(&c->error_collector, us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
error(us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
return;
}
f->using_parent = e;
@@ -1340,7 +1346,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
Entity *f = t->Record.fields[i];
Entity *found = scope_insert_entity(c->context.scope, f);
if (found != NULL) {
error(&c->error_collector, us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
error(us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
return;
}
f->using_parent = e;
@@ -1349,7 +1355,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
Entity *f = t->Record.other_fields[i];
Entity *found = scope_insert_entity(c->context.scope, f);
if (found != NULL) {
error(&c->error_collector, us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
error(us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
return;
}
f->using_parent = e;
@@ -1363,7 +1369,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
Entity *decl = scope->elements.entries[i].value;
Entity *found = scope_insert_entity(c->context.scope, decl);
if (found != NULL) {
error(&c->error_collector, us->token,
error(us->token,
"Namespace collision while `using` `%s` of: %.*s\n"
"\tat %.*s(%td:%td)\n"
"\tat %.*s(%td:%td)",
@@ -1377,12 +1383,12 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
} break;
case Entity_Constant:
error(&c->error_collector, us->token, "`using` cannot be applied to a constant");
error(us->token, "`using` cannot be applied to a constant");
break;
case Entity_Procedure:
case Entity_Builtin:
error(&c->error_collector, us->token, "`using` cannot be applied to a procedure");
error(us->token, "`using` cannot be applied to a procedure");
break;
case Entity_Variable: {
@@ -1399,13 +1405,13 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
}
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != NULL) {
error(&c->error_collector, us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(prev->token.string));
error(us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(prev->token.string));
return;
}
}
}
} else {
error(&c->error_collector, us->token, "`using` can only be applied to variables of type struct or raw_union");
error(us->token, "`using` can only be applied to variables of type struct or raw_union");
return;
}
} break;
@@ -1417,9 +1423,9 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
case_ast_node(vd, VarDecl, us->node);
if (gb_array_count(vd->names) > 1 && vd->type != NULL) {
error(&c->error_collector, us->token, "`using` can only be applied to one variable of the same type");
error(us->token, "`using` can only be applied to one variable of the same type");
}
check_var_decl_node(c, us->node);
check_var_decl(c, us->node);
gb_for_array(name_index, vd->names) {
AstNode *item = vd->names[name_index];
@@ -1436,13 +1442,13 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != NULL) {
error(&c->error_collector, us->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
error(us->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
return;
}
}
}
} else {
error(&c->error_collector, us->token, "`using` can only be applied to variables of type struct or raw_union");
error(us->token, "`using` can only be applied to variables of type struct or raw_union");
return;
}
}
@@ -1450,7 +1456,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
default:
error(&c->error_collector, us->token, "Invalid AST: Using Statement");
error(us->token, "Invalid AST: Using Statement");
break;
}
case_end;
@@ -1461,7 +1467,11 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
case_ast_node(vd, VarDecl, node);
check_var_decl_node(c, node);
check_var_decl(c, node);
case_end;
case_ast_node(cd, ConstDecl, node);
check_const_decl(c, node);
case_end;
case_ast_node(pd, ProcDecl, node);
+1 -9
View File
@@ -7,17 +7,9 @@ struct ssaGen {
};
b32 ssa_gen_init(ssaGen *s, Checker *c) {
if (c->error_collector.count != 0)
if (global_error_collector.count != 0)
return false;
gb_for_array(i, c->parser->files) {
AstFile *f = &c->parser->files[i];
if (f->error_collector.count != 0)
return false;
if (f->tokenizer.error_count != 0)
return false;
}
isize tc = c->parser->total_token_count;
if (tc < 2) {
return false;
+68 -70
View File
@@ -3053,82 +3053,80 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
case_end;
case_ast_node(vd, VarDecl, node);
if (vd->kind == Declaration_Mutable) {
if (gb_array_count(vd->names) == gb_array_count(vd->values)) { // 1:1 assigment
gbArray(ssaAddr) lvals;
gbArray(ssaValue *) inits;
gb_array_init_reserve(lvals, gb_heap_allocator(), gb_array_count(vd->names));
gb_array_init_reserve(inits, gb_heap_allocator(), gb_array_count(vd->names));
defer (gb_array_free(lvals));
defer (gb_array_free(inits));
if (gb_array_count(vd->names) == gb_array_count(vd->values)) { // 1:1 assigment
gbArray(ssaAddr) lvals;
gbArray(ssaValue *) inits;
gb_array_init_reserve(lvals, gb_heap_allocator(), gb_array_count(vd->names));
gb_array_init_reserve(inits, gb_heap_allocator(), gb_array_count(vd->names));
defer (gb_array_free(lvals));
defer (gb_array_free(inits));
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
ssaAddr lval = ssa_make_addr(NULL, NULL);
if (!ssa_is_blank_ident(name)) {
ssa_add_local_for_identifier(proc, name, false);
lval = ssa_build_addr(proc, name);
GB_ASSERT(lval.addr != NULL);
}
gb_array_append(lvals, lval);
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
ssaAddr lval = ssa_make_addr(NULL, NULL);
if (!ssa_is_blank_ident(name)) {
ssa_add_local_for_identifier(proc, name, false);
lval = ssa_build_addr(proc, name);
GB_ASSERT(lval.addr != NULL);
}
gb_for_array(i, vd->values) {
ssaValue *init = ssa_build_expr(proc, vd->values[i]);
gb_array_append(lvals, lval);
}
gb_for_array(i, vd->values) {
ssaValue *init = ssa_build_expr(proc, vd->values[i]);
gb_array_append(inits, init);
}
gb_for_array(i, inits) {
ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_addr_type(lvals[i]));
ssa_lvalue_store(proc, lvals[i], v);
}
} else if (gb_array_count(vd->values) == 0) { // declared and zero-initialized
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (!ssa_is_blank_ident(name)) {
ssa_add_local_for_identifier(proc, name, true);
}
}
} else { // Tuple(s)
gbArray(ssaAddr) lvals;
gbArray(ssaValue *) inits;
gb_array_init_reserve(lvals, gb_heap_allocator(), gb_array_count(vd->names));
gb_array_init_reserve(inits, gb_heap_allocator(), gb_array_count(vd->names));
defer (gb_array_free(lvals));
defer (gb_array_free(inits));
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
ssaAddr lval = ssa_make_addr(NULL, NULL);
if (!ssa_is_blank_ident(name)) {
ssa_add_local_for_identifier(proc, name, false);
lval = ssa_build_addr(proc, name);
}
gb_array_append(lvals, lval);
}
gb_for_array(i, vd->values) {
ssaValue *init = ssa_build_expr(proc, vd->values[i]);
Type *t = ssa_type(init);
if (t->kind == Type_Tuple) {
for (isize i = 0; i < t->Tuple.variable_count; i++) {
Entity *e = t->Tuple.variables[i];
ssaValue *v = ssa_emit_struct_ev(proc, init, i, e->type);
gb_array_append(inits, v);
}
} else {
gb_array_append(inits, init);
}
}
gb_for_array(i, inits) {
ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_addr_type(lvals[i]));
ssa_lvalue_store(proc, lvals[i], v);
}
} else if (gb_array_count(vd->values) == 0) { // declared and zero-initialized
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (!ssa_is_blank_ident(name)) {
ssa_add_local_for_identifier(proc, name, true);
}
}
} else { // Tuple(s)
gbArray(ssaAddr) lvals;
gbArray(ssaValue *) inits;
gb_array_init_reserve(lvals, gb_heap_allocator(), gb_array_count(vd->names));
gb_array_init_reserve(inits, gb_heap_allocator(), gb_array_count(vd->names));
defer (gb_array_free(lvals));
defer (gb_array_free(inits));
gb_for_array(i, vd->names) {
AstNode *name = vd->names[i];
ssaAddr lval = ssa_make_addr(NULL, NULL);
if (!ssa_is_blank_ident(name)) {
ssa_add_local_for_identifier(proc, name, false);
lval = ssa_build_addr(proc, name);
}
gb_array_append(lvals, lval);
}
gb_for_array(i, vd->values) {
ssaValue *init = ssa_build_expr(proc, vd->values[i]);
Type *t = ssa_type(init);
if (t->kind == Type_Tuple) {
for (isize i = 0; i < t->Tuple.variable_count; i++) {
Entity *e = t->Tuple.variables[i];
ssaValue *v = ssa_emit_struct_ev(proc, init, i, e->type);
gb_array_append(inits, v);
}
} else {
gb_array_append(inits, init);
}
}
gb_for_array(i, inits) {
ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_addr_type(lvals[i]));
ssa_lvalue_store(proc, lvals[i], v);
}
gb_for_array(i, inits) {
ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_addr_type(lvals[i]));
ssa_lvalue_store(proc, lvals[i], v);
}
}
case_end;
+115 -140
View File
@@ -34,8 +34,6 @@ struct AstFile {
isize scope_level;
Scope * scope; // NOTE(bill): Created in checker
ErrorCollector error_collector;
// TODO(bill): Error recovery
// NOTE(bill): Error recovery
#define PARSER_MAX_FIX_COUNT 6
@@ -59,13 +57,6 @@ struct Parser {
isize total_token_count;
};
enum DeclKind {
Declaration_Invalid,
Declaration_Mutable,
Declaration_Immutable,
Declaration_Count,
};
enum ProcTag : u64 {
ProcTag_bounds_check = GB_BIT(0),
ProcTag_no_bounds_check = GB_BIT(1),
@@ -227,20 +218,25 @@ AST_NODE_KIND(_StmtEnd, "", struct{}) \
AST_NODE_KIND(_DeclBegin, "", struct{}) \
AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
AST_NODE_KIND(VarDecl, "variable declaration", struct { \
DeclKind kind; \
u64 tags; \
b32 is_using; \
AstNodeArray names; \
AstNode *type; \
AstNodeArray values; \
}) \
}) \
AST_NODE_KIND(ConstDecl, "constant declaration", struct { \
u64 tags; \
AstNodeArray names; \
AstNode *type; \
AstNodeArray values; \
}) \
AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
AstNode *name; \
AstNode *type; \
AstNode *body; \
u64 tags; \
String foreign_name; \
}) \
}) \
AST_NODE_KIND(TypeDecl, "type declaration", struct { Token token; AstNode *name, *type; }) \
AST_NODE_KIND(ImportDecl, "import declaration", struct { \
Token token, relpath; \
@@ -419,6 +415,8 @@ Token ast_node_token(AstNode *node) {
return node->BadDecl.begin;
case AstNode_VarDecl:
return ast_node_token(node->VarDecl.names[0]);
case AstNode_ConstDecl:
return ast_node_token(node->ConstDecl.names[0]);
case AstNode_ProcDecl:
return node->ProcDecl.name->Ident;
case AstNode_TypeDecl:
@@ -458,26 +456,6 @@ HashKey hash_token(Token t) {
return hash_string(t.string);
}
#define ast_file_err(f, token, fmt, ...) ast_file_err_(f, __FUNCTION__, token, fmt, ##__VA_ARGS__)
void ast_file_err_(AstFile *file, char *function, Token token, char *fmt, ...) {
// NOTE(bill): Duplicate error, skip it
if (!token_pos_are_equal(file->error_collector.prev, token.pos)) {
va_list va;
file->error_collector.prev = token.pos;
#if 0
gb_printf_err("%s()\n", function);
#endif
va_start(va, fmt);
gb_printf_err("%.*s(%td:%td) Syntax error: %s\n",
LIT(token.pos.file), token.pos.line, token.pos.column,
gb_bprintf_va(fmt, va));
va_end(va);
}
file->error_collector.count++;
}
// NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
gb_inline AstNode *make_node(AstFile *f, AstNodeKind kind) {
@@ -525,11 +503,11 @@ gb_inline AstNode *make_binary_expr(AstFile *f, Token op, AstNode *left, AstNode
AstNode *result = make_node(f, AstNode_BinaryExpr);
if (left == NULL) {
ast_file_err(f, op, "No lhs expression for binary expression `%.*s`", LIT(op.string));
syntax_error(op, "No lhs expression for binary expression `%.*s`", LIT(op.string));
left = make_bad_expr(f, op, op);
}
if (right == NULL) {
ast_file_err(f, op, "No rhs expression for binary expression `%.*s`", LIT(op.string));
syntax_error(op, "No rhs expression for binary expression `%.*s`", LIT(op.string));
right = make_bad_expr(f, op, op);
}
@@ -795,15 +773,22 @@ gb_inline AstNode *make_bad_decl(AstFile *f, Token begin, Token end) {
return result;
}
gb_inline AstNode *make_var_decl(AstFile *f, DeclKind kind, AstNodeArray names, AstNode *type, AstNodeArray values) {
gb_inline AstNode *make_var_decl(AstFile *f, AstNodeArray names, AstNode *type, AstNodeArray values) {
AstNode *result = make_node(f, AstNode_VarDecl);
result->VarDecl.kind = kind;
result->VarDecl.names = names;
result->VarDecl.type = type;
result->VarDecl.values = values;
return result;
}
gb_inline AstNode *make_const_decl(AstFile *f, AstNodeArray names, AstNode *type, AstNodeArray values) {
AstNode *result = make_node(f, AstNode_ConstDecl);
result->ConstDecl.names = names;
result->ConstDecl.type = type;
result->ConstDecl.values = values;
return result;
}
gb_inline AstNode *make_field(AstFile *f, AstNodeArray names, AstNode *type, b32 is_using) {
AstNode *result = make_node(f, AstNode_Field);
result->Field.names = names;
@@ -918,7 +903,7 @@ gb_inline b32 next_token(AstFile *f) {
f->cursor++;
return true;
} else {
ast_file_err(f, f->cursor[0], "Token is EOF");
syntax_error(f->cursor[0], "Token is EOF");
return false;
}
}
@@ -926,7 +911,7 @@ gb_inline b32 next_token(AstFile *f) {
gb_inline Token expect_token(AstFile *f, TokenKind kind) {
Token prev = f->cursor[0];
if (prev.kind != kind) {
ast_file_err(f, f->cursor[0], "Expected `%.*s`, got `%.*s`",
syntax_error(f->cursor[0], "Expected `%.*s`, got `%.*s`",
LIT(token_strings[kind]),
LIT(token_strings[prev.kind]));
}
@@ -937,7 +922,7 @@ gb_inline Token expect_token(AstFile *f, TokenKind kind) {
gb_inline Token expect_operator(AstFile *f) {
Token prev = f->cursor[0];
if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
ast_file_err(f, f->cursor[0], "Expected an operator, got `%.*s`",
syntax_error(f->cursor[0], "Expected an operator, got `%.*s`",
LIT(token_strings[prev.kind]));
}
next_token(f);
@@ -947,7 +932,7 @@ gb_inline Token expect_operator(AstFile *f) {
gb_inline Token expect_keyword(AstFile *f) {
Token prev = f->cursor[0];
if (!gb_is_between(prev.kind, Token__KeywordBegin+1, Token__KeywordEnd-1)) {
ast_file_err(f, f->cursor[0], "Expected a keyword, got `%.*s`",
syntax_error(f->cursor[0], "Expected a keyword, got `%.*s`",
LIT(token_strings[prev.kind]));
}
next_token(f);
@@ -1027,7 +1012,7 @@ b32 expect_semicolon_after_stmt(AstFile *f, AstNode *s) {
if (f->cursor[0].pos.line == f->cursor[-1].pos.line) {
if (f->cursor[0].kind != Token_CloseBrace) {
// CLEANUP(bill): Semicolon handling in parser
ast_file_err(f, f->cursor[0],
syntax_error(f->cursor[0],
"Expected `;` after %.*s, got `%.*s`",
LIT(ast_node_strings[s->kind]), LIT(token_strings[f->cursor[0].kind]));
return false;
@@ -1126,7 +1111,7 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags = 0);
void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) {
if (*tags & tag) {
ast_file_err(f, ast_node_token(tag_expr), "Procedure tag already used: %.*s", LIT(tag_name));
syntax_error(ast_node_token(tag_expr), "Procedure tag already used: %.*s", LIT(tag_name));
}
*tags |= tag;
}
@@ -1200,7 +1185,7 @@ void parse_proc_tags(AstFile *f, u64 *tags, String *foreign_name) {
*foreign_name = f->cursor[0].string;
// TODO(bill): Check if valid string
if (!is_foreign_name_valid(*foreign_name)) {
ast_file_err(f, ast_node_token(tag_expr), "Invalid alternative foreign procedure name");
syntax_error(ast_node_token(tag_expr), "Invalid alternative foreign procedure name");
}
next_token(f);
@@ -1216,22 +1201,22 @@ void parse_proc_tags(AstFile *f, u64 *tags, String *foreign_name) {
ELSE_IF_ADD_TAG(fastcall)
// ELSE_IF_ADD_TAG(cdecl)
else {
ast_file_err(f, ast_node_token(tag_expr), "Unknown procedure tag");
syntax_error(ast_node_token(tag_expr), "Unknown procedure tag");
}
#undef ELSE_IF_ADD_TAG
}
if ((*tags & ProcTag_inline) && (*tags & ProcTag_no_inline)) {
ast_file_err(f, f->cursor[0], "You cannot apply both #inline and #no_inline to a procedure");
syntax_error(f->cursor[0], "You cannot apply both #inline and #no_inline to a procedure");
}
if ((*tags & ProcTag_bounds_check) && (*tags & ProcTag_no_bounds_check)) {
ast_file_err(f, f->cursor[0], "You cannot apply both #bounds_check and #no_bounds_check to a procedure");
syntax_error(f->cursor[0], "You cannot apply both #bounds_check and #no_bounds_check to a procedure");
}
if (((*tags & ProcTag_bounds_check) || (*tags & ProcTag_no_bounds_check)) && (*tags & ProcTag_foreign)) {
ast_file_err(f, f->cursor[0], "You cannot apply both #bounds_check or #no_bounds_check to a procedure without a body");
syntax_error(f->cursor[0], "You cannot apply both #bounds_check or #no_bounds_check to a procedure without a body");
}
}
@@ -1272,7 +1257,7 @@ AstNode *parse_operand(AstFile *f, b32 lhs) {
Token *s = &f->cursor[0];
if (gb_utf8_strnlen(s->string.text, s->string.len) != 1) {
ast_file_err(f, *s, "Invalid rune literal %.*s", LIT(s->string));
syntax_error(*s, "Invalid rune literal %.*s", LIT(s->string));
}
s->kind = Token_Rune; // NOTE(bill): Change it
} else {
@@ -1308,7 +1293,7 @@ AstNode *parse_operand(AstFile *f, b32 lhs) {
String foreign_name = {};
parse_proc_tags(f, &tags, &foreign_name);
if (tags & ProcTag_foreign) {
ast_file_err(f, f->cursor[0], "#foreign cannot be applied to procedure literals");
syntax_error(f->cursor[0], "#foreign cannot be applied to procedure literals");
}
if (f->cursor[0].kind != Token_OpenBrace) {
@@ -1335,7 +1320,7 @@ AstNode *parse_operand(AstFile *f, b32 lhs) {
}
Token begin = f->cursor[0];
ast_file_err(f, begin, "Expected an operand");
syntax_error(begin, "Expected an operand");
fix_advance_to_next_stmt(f);
return make_bad_expr(f, begin, f->cursor[0]);
}
@@ -1365,7 +1350,7 @@ AstNode *parse_call_expr(AstFile *f, AstNode *operand) {
f->cursor[0].kind != Token_EOF &&
ellipsis.pos.line == 0) {
if (f->cursor[0].kind == Token_Comma)
ast_file_err(f, f->cursor[0], "Expected an expression not a ,");
syntax_error(f->cursor[0], "Expected an expression not a ,");
if (f->cursor[0].kind == Token_Ellipsis) {
ellipsis = f->cursor[0];
@@ -1425,7 +1410,7 @@ AstNode *parse_atom_expr(AstFile *f, b32 lhs) {
operand = make_selector_expr(f, token, operand, parse_identifier(f));
break;
default: {
ast_file_err(f, f->cursor[0], "Expected a selector");
syntax_error(f->cursor[0], "Expected a selector");
next_token(f);
operand = make_selector_expr(f, f->cursor[0], operand, NULL);
} break;
@@ -1467,11 +1452,11 @@ AstNode *parse_atom_expr(AstFile *f, b32 lhs) {
if (colon_count == 2) {
triple_indexed = true;
if (indices[1] == NULL) {
ast_file_err(f, colons[0], "Second index is required in a triple indexed slice");
syntax_error(colons[0], "Second index is required in a triple indexed slice");
indices[1] = make_bad_expr(f, colons[0], colons[1]);
}
if (indices[2] == NULL) {
ast_file_err(f, colons[1], "Third index is required in a triple indexed slice");
syntax_error(colons[1], "Third index is required in a triple indexed slice");
indices[2] = make_bad_expr(f, colons[1], close);
}
}
@@ -1558,7 +1543,7 @@ AstNode *parse_binary_expr(AstFile *f, b32 lhs, i32 prec_in) {
default:
right = parse_binary_expr(f, false, prec+1);
if (!right) {
ast_file_err(f, op, "Expected expression on the right hand side of the binary operator");
syntax_error(op, "Expected expression on the right hand side of the binary operator");
}
break;
}
@@ -1622,13 +1607,13 @@ AstNode *parse_simple_stmt(AstFile *f) {
case Token_CmpOrEq:
{
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use a simple statement in the file scope");
syntax_error(f->cursor[0], "You cannot use a simple statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
next_token(f);
AstNodeArray rhs = parse_rhs_expr_list(f);
if (gb_array_count(rhs) == 0) {
ast_file_err(f, token, "No right-hand side in assignment statement.");
syntax_error(token, "No right-hand side in assignment statement.");
return make_bad_stmt(f, token, f->cursor[0]);
}
return make_assign_stmt(f, token, lhs, rhs);
@@ -1639,7 +1624,7 @@ AstNode *parse_simple_stmt(AstFile *f) {
}
if (lhs_count > 1) {
ast_file_err(f, token, "Expected 1 expression");
syntax_error(token, "Expected 1 expression");
return make_bad_stmt(f, token, f->cursor[0]);
}
@@ -1648,7 +1633,7 @@ AstNode *parse_simple_stmt(AstFile *f) {
case Token_Increment:
case Token_Decrement:
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use a simple statement in the file scope");
syntax_error(f->cursor[0], "You cannot use a simple statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
statement = make_inc_dec_stmt(f, token, lhs[0]);
@@ -1663,7 +1648,7 @@ AstNode *parse_simple_stmt(AstFile *f) {
AstNode *parse_block_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use a block statement in the file scope");
syntax_error(f->cursor[0], "You cannot use a block statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
AstNode *block_stmt = parse_body(f);
@@ -1677,7 +1662,7 @@ AstNode *convert_stmt_to_expr(AstFile *f, AstNode *statement, String kind) {
if (statement->kind == AstNode_ExprStmt)
return statement->ExprStmt.expr;
ast_file_err(f, f->cursor[0], "Expected `%.*s`, found a simple statement.", LIT(kind));
syntax_error(f->cursor[0], "Expected `%.*s`, found a simple statement.", LIT(kind));
return make_bad_expr(f, f->cursor[0], f->cursor[1]);
}
@@ -1710,7 +1695,7 @@ AstNode *parse_type(AstFile *f) {
AstNode *type = parse_type_attempt(f);
if (type == NULL) {
Token token = f->cursor[0];
ast_file_err(f, token, "Expected a type");
syntax_error(token, "Expected a type");
next_token(f);
return make_bad_expr(f, token, f->cursor[0]);
}
@@ -1738,11 +1723,11 @@ AstNode *parse_field_decl(AstFile *f) {
AstNodeArray names = parse_lhs_expr_list(f);
if (gb_array_count(names) == 0) {
ast_file_err(f, f->cursor[0], "Empty field declaration");
syntax_error(f->cursor[0], "Empty field declaration");
}
if (gb_array_count(names) > 1 && is_using) {
ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
syntax_error(f->cursor[0], "Cannot apply `using` to more than one of the same type");
is_using = false;
}
@@ -1755,11 +1740,11 @@ AstNode *parse_field_decl(AstFile *f) {
next_token(f);
type = parse_type_attempt(f);
if (type == NULL) {
ast_file_err(f, f->cursor[0], "variadic parameter is missing a type after `..`");
syntax_error(f->cursor[0], "variadic parameter is missing a type after `..`");
type = make_bad_expr(f, ellipsis, f->cursor[0]);
} else {
if (gb_array_count(names) > 1) {
ast_file_err(f, f->cursor[0], "mutliple variadic parameters, only `..`");
syntax_error(f->cursor[0], "mutliple variadic parameters, only `..`");
} else {
type = make_ellipsis(f, ellipsis, type);
}
@@ -1769,7 +1754,7 @@ AstNode *parse_field_decl(AstFile *f) {
}
if (type == NULL) {
ast_file_err(f, f->cursor[0], "Expected a type for this field declaration");
syntax_error(f->cursor[0], "Expected a type for this field declaration");
}
AstNode *field = make_field(f, names, type, is_using);
@@ -1804,15 +1789,15 @@ AstNodeArray parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allow
}
AstNodeArray names = parse_lhs_expr_list(f);
if (gb_array_count(names) == 0) {
ast_file_err(f, f->cursor[0], "Empty field declaration");
syntax_error(f->cursor[0], "Empty field declaration");
}
if (!using_allowed && is_using) {
ast_file_err(f, f->cursor[0], "Cannot apply `using` to members of a union");
syntax_error(f->cursor[0], "Cannot apply `using` to members of a union");
is_using = false;
}
if (gb_array_count(names) > 1 && is_using) {
ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
syntax_error(f->cursor[0], "Cannot apply `using` to more than one of the same type");
}
AstNode *decl = NULL;
@@ -1821,11 +1806,11 @@ AstNodeArray parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allow
decl = parse_decl(f, names);
if (decl->kind == AstNode_ProcDecl) {
ast_file_err(f, f->cursor[0], "Procedure declarations are not allowed within a structure");
syntax_error(f->cursor[0], "Procedure declarations are not allowed within a structure");
decl = make_bad_decl(f, ast_node_token(names[0]), f->cursor[0]);
}
} else {
ast_file_err(f, f->cursor[0], "Illegal structure field");
syntax_error(f->cursor[0], "Illegal structure field");
decl = make_bad_decl(f, ast_node_token(names[0]), f->cursor[0]);
}
@@ -1835,13 +1820,9 @@ AstNodeArray parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allow
gb_array_append(decls, decl);
if (decl->kind == AstNode_VarDecl) {
decl->VarDecl.is_using = is_using && using_allowed;
if (decl->VarDecl.kind == Declaration_Mutable) {
if (gb_array_count(decl->VarDecl.values) > 0) {
ast_file_err(f, f->cursor[0], "Default variable assignments within a structure will be ignored (at the moment)");
}
if (gb_array_count(decl->VarDecl.values) > 0) {
syntax_error(f->cursor[0], "Default variable assignments within a structure will be ignored (at the moment)");
}
} else {
decl_count += 1;
}
@@ -1925,12 +1906,12 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
} else if (are_strings_equal(tag.string, make_string("ordered"))) {
is_ordered = true;
} else {
ast_file_err(f, tag, "Expected a `#packed` or `#ordered` tag");
syntax_error(tag, "Expected a `#packed` or `#ordered` tag");
}
}
if (is_packed && is_ordered) {
ast_file_err(f, token, "`#ordered` is not needed with `#packed` which implies ordering");
syntax_error(token, "`#ordered` is not needed with `#packed` which implies ordering");
}
Token open = expect_token(f, Token_OpenBrace);
@@ -2034,8 +2015,8 @@ AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
break;
// fallthrough
default:
ast_file_err(f, f->cursor[0],
"Expected a type after `%.*s`, got `%.*s`", LIT(f->cursor[-1].string), LIT(f->cursor[0].string));
syntax_error(f->cursor[0],
"Expected a type or identifier after `%.*s`, got `%.*s`", LIT(f->cursor[-1].string), LIT(f->cursor[0].string));
break;
}
@@ -2109,7 +2090,7 @@ AstNode *parse_proc_decl(AstFile *f, Token proc_token, AstNode *name) {
if (f->cursor[0].kind == Token_OpenBrace) {
if ((tags & ProcTag_foreign) != 0) {
ast_file_err(f, f->cursor[0], "A procedure tagged as `#foreign` cannot have a body");
syntax_error(f->cursor[0], "A procedure tagged as `#foreign` cannot have a body");
}
body = parse_body(f);
}
@@ -2127,7 +2108,7 @@ AstNode *parse_decl(AstFile *f, AstNodeArray names) {
String n = name->Ident.string;
// NOTE(bill): Check for reserved identifiers
if (are_strings_equal(n, make_string("context"))) {
ast_file_err(f, ast_node_token(name), "`context` is a reserved identifier");
syntax_error(ast_node_token(name), "`context` is a reserved identifier");
break;
}
}
@@ -2138,15 +2119,16 @@ AstNode *parse_decl(AstFile *f, AstNodeArray names) {
type = parse_identifier_or_type(f);
}
} else if (f->cursor[0].kind != Token_Eq && f->cursor[0].kind != Token_Semicolon) {
ast_file_err(f, f->cursor[0], "Expected type separator `:` or `=`");
syntax_error(f->cursor[0], "Expected type separator `:` or `=`");
}
DeclKind declaration_kind = Declaration_Mutable;
b32 is_mutable = true;
if (f->cursor[0].kind == Token_Eq ||
f->cursor[0].kind == Token_Colon) {
if (f->cursor[0].kind == Token_Colon)
declaration_kind = Declaration_Immutable;
if (f->cursor[0].kind == Token_Colon) {
is_mutable = false;
}
next_token(f);
if (f->cursor[0].kind == Token_type ||
@@ -2159,71 +2141,67 @@ AstNode *parse_decl(AstFile *f, AstNodeArray names) {
next_token(f);
}
if (gb_array_count(names) != 1) {
ast_file_err(f, ast_node_token(names[0]), "You can only declare one type at a time");
syntax_error(ast_node_token(names[0]), "You can only declare one type at a time");
return make_bad_decl(f, names[0]->Ident, token);
}
if (type != NULL) {
ast_file_err(f, f->cursor[-1], "Expected either `type` or nothing between : and :");
syntax_error(f->cursor[-1], "Expected either `type` or nothing between : and :");
// NOTE(bill): Do not fail though
}
AstNode *type = parse_type(f);
return make_type_decl(f, token, names[0], type);
} else if (f->cursor[0].kind == Token_proc &&
declaration_kind == Declaration_Immutable) {
is_mutable == false) {
// NOTE(bill): Procedure declarations
Token proc_token = f->cursor[0];
AstNode *name = names[0];
if (gb_array_count(names) != 1) {
ast_file_err(f, proc_token, "You can only declare one procedure at a time");
syntax_error(proc_token, "You can only declare one procedure at a time");
return make_bad_decl(f, name->Ident, proc_token);
}
AstNode *proc_decl = parse_proc_decl(f, proc_token, name);
return proc_decl;
return parse_proc_decl(f, proc_token, name);
} else {
values = parse_rhs_expr_list(f);
if (gb_array_count(values) > gb_array_count(names)) {
ast_file_err(f, f->cursor[0], "Too many values on the right hand side of the declaration");
} else if (gb_array_count(values) < gb_array_count(names) &&
declaration_kind == Declaration_Immutable) {
ast_file_err(f, f->cursor[0], "All constant declarations must be defined");
syntax_error(f->cursor[0], "Too many values on the right hand side of the declaration");
} else if (gb_array_count(values) < gb_array_count(names) && !is_mutable) {
syntax_error(f->cursor[0], "All constant declarations must be defined");
} else if (gb_array_count(values) == 0) {
ast_file_err(f, f->cursor[0], "Expected an expression for this declaration");
syntax_error(f->cursor[0], "Expected an expression for this declaration");
}
}
}
if (declaration_kind == Declaration_Mutable) {
if (is_mutable) {
if (type == NULL && gb_array_count(values) == 0) {
ast_file_err(f, f->cursor[0], "Missing variable type or initialization");
return make_bad_decl(f, f->cursor[0], f->cursor[0]);
}
} else if (declaration_kind == Declaration_Immutable) {
if (type == NULL && gb_array_count(values) == 0 && gb_array_count(names) > 0) {
ast_file_err(f, f->cursor[0], "Missing constant value");
syntax_error(f->cursor[0], "Missing variable type or initialization");
return make_bad_decl(f, f->cursor[0], f->cursor[0]);
}
} else {
Token begin = f->cursor[0];
ast_file_err(f, begin, "Unknown type of variable declaration");
fix_advance_to_next_stmt(f);
return make_bad_decl(f, begin, f->cursor[0]);
if (type == NULL && gb_array_count(values) == 0 && gb_array_count(names) > 0) {
syntax_error(f->cursor[0], "Missing constant value");
return make_bad_decl(f, f->cursor[0], f->cursor[0]);
}
}
if (values == NULL) {
values = make_ast_node_array(f);
}
return make_var_decl(f, declaration_kind, names, type, values);
if (is_mutable) {
return make_var_decl(f, names, type, values);
}
return make_const_decl(f, names, type, values);
}
AstNode *parse_if_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use an if statement in the file scope");
syntax_error(f->cursor[0], "You cannot use an if statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
@@ -2252,7 +2230,7 @@ AstNode *parse_if_stmt(AstFile *f) {
f->expr_level = prev_level;
if (cond == NULL) {
ast_file_err(f, f->cursor[0], "Expected condition for if statement");
syntax_error(f->cursor[0], "Expected condition for if statement");
}
body = parse_block_stmt(f);
@@ -2266,7 +2244,7 @@ AstNode *parse_if_stmt(AstFile *f) {
else_stmt = parse_block_stmt(f);
break;
default:
ast_file_err(f, f->cursor[0], "Expected if statement block statement");
syntax_error(f->cursor[0], "Expected if statement block statement");
else_stmt = make_bad_stmt(f, f->cursor[0], f->cursor[1]);
break;
}
@@ -2277,7 +2255,7 @@ AstNode *parse_if_stmt(AstFile *f) {
AstNode *parse_return_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use a return statement in the file scope");
syntax_error(f->cursor[0], "You cannot use a return statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
@@ -2297,7 +2275,7 @@ AstNode *parse_return_stmt(AstFile *f) {
AstNode *parse_for_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use a for statement in the file scope");
syntax_error(f->cursor[0], "You cannot use a for statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
@@ -2314,7 +2292,7 @@ AstNode *parse_for_stmt(AstFile *f) {
if (f->cursor[0].kind != Token_Semicolon) {
cond = parse_simple_stmt(f);
if (is_ast_node_complex_stmt(cond)) {
ast_file_err(f, f->cursor[0],
syntax_error(f->cursor[0],
"You are not allowed that type of statement in a for statement, it is too complex!");
}
}
@@ -2371,7 +2349,7 @@ AstNode *parse_type_case_clause(AstFile *f) {
AstNode *parse_match_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use a match statement in the file scope");
syntax_error(f->cursor[0], "You cannot use a match statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
@@ -2444,7 +2422,7 @@ AstNode *parse_match_stmt(AstFile *f) {
AstNode *parse_defer_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
ast_file_err(f, f->cursor[0], "You cannot use a defer statement in the file scope");
syntax_error(f->cursor[0], "You cannot use a defer statement in the file scope");
return make_bad_stmt(f, f->cursor[0], f->cursor[0]);
}
@@ -2452,13 +2430,13 @@ AstNode *parse_defer_stmt(AstFile *f) {
AstNode *statement = parse_stmt(f);
switch (statement->kind) {
case AstNode_EmptyStmt:
ast_file_err(f, token, "Empty statement after defer (e.g. `;`)");
syntax_error(token, "Empty statement after defer (e.g. `;`)");
break;
case AstNode_DeferStmt:
ast_file_err(f, token, "You cannot defer a defer statement");
syntax_error(token, "You cannot defer a defer statement");
break;
case AstNode_ReturnStmt:
ast_file_err(f, token, "You cannot a return statement");
syntax_error(token, "You cannot a return statement");
break;
}
@@ -2554,14 +2532,12 @@ AstNode *parse_stmt(AstFile *f) {
}
} break;
case AstNode_VarDecl:
if (node->VarDecl.kind == Declaration_Mutable) {
valid = true;
}
valid = true;
break;
}
if (!valid) {
ast_file_err(f, token, "Illegal use of `using` statement.");
syntax_error(token, "Illegal use of `using` statement.");
return make_bad_stmt(f, token, f->cursor[0]);
}
@@ -2577,7 +2553,7 @@ AstNode *parse_stmt(AstFile *f) {
f->is_global_scope = true;
return make_empty_stmt(f, f->cursor[0]);
}
ast_file_err(f, token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope.");
syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope.");
return make_bad_decl(f, token, f->cursor[0]);
} else if (are_strings_equal(tag, make_string("import"))) {
// TODO(bill): better error messages
@@ -2589,7 +2565,7 @@ AstNode *parse_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
return make_import_decl(f, s->TagStmt.token, file_path, import_name, false);
}
ast_file_err(f, token, "You cannot use #import within a procedure. This must be done at the file scope.");
syntax_error(token, "You cannot use #import within a procedure. This must be done at the file scope.");
return make_bad_decl(f, token, file_path);
} else if (are_strings_equal(tag, make_string("load"))) {
// TODO(bill): better error messages
@@ -2600,24 +2576,23 @@ AstNode *parse_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
return make_import_decl(f, s->TagStmt.token, file_path, import_name, true);
}
ast_file_err(f, token, "You cannot use #load within a procedure. This must be done at the file scope.");
syntax_error(token, "You cannot use #load within a procedure. This must be done at the file scope.");
return make_bad_decl(f, token, file_path);
} else if (are_strings_equal(tag, make_string("foreign_system_library"))) {
Token file_path = expect_token(f, Token_String);
if (f->curr_proc == NULL) {
return make_foreign_system_library(f, s->TagStmt.token, file_path);
}
ast_file_err(f, token, "You cannot use #foreign_system_library within a procedure. This must be done at the file scope.");
syntax_error(token, "You cannot use #foreign_system_library within a procedure. This must be done at the file scope.");
return make_bad_decl(f, token, file_path);
} else if (are_strings_equal(tag, make_string("thread_local"))) {
AstNode *var_decl = parse_simple_stmt(f);
if (var_decl->kind != AstNode_VarDecl ||
var_decl->VarDecl.kind != Declaration_Mutable) {
ast_file_err(f, token, "#thread_local may only be applied to variable declarations");
if (var_decl->kind != AstNode_VarDecl) {
syntax_error(token, "#thread_local may only be applied to variable declarations");
return make_bad_decl(f, token, ast_node_token(var_decl));
}
if (f->curr_proc != NULL) {
ast_file_err(f, token, "#thread_local is only allowed at the file scope.");
syntax_error(token, "#thread_local is only allowed at the file scope.");
return make_bad_decl(f, token, ast_node_token(var_decl));
}
var_decl->VarDecl.tags |= VarDeclTag_thread_local;
@@ -2626,14 +2601,14 @@ AstNode *parse_stmt(AstFile *f) {
s = parse_stmt(f);
s->stmt_state_flags |= StmtStateFlag_bounds_check;
if ((s->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) {
ast_file_err(f, token, "#bounds_check and #no_bounds_check cannot be applied together");
syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
}
return s;
} else if (are_strings_equal(tag, make_string("no_bounds_check"))) {
s = parse_stmt(f);
s->stmt_state_flags |= StmtStateFlag_no_bounds_check;
if ((s->stmt_state_flags & StmtStateFlag_bounds_check) != 0) {
ast_file_err(f, token, "#bounds_check and #no_bounds_check cannot be applied together");
syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
}
return s;
}
@@ -2651,7 +2626,7 @@ AstNode *parse_stmt(AstFile *f) {
return s;
}
ast_file_err(f, token,
syntax_error(token,
"Expected a statement, got `%.*s`",
LIT(token_strings[token.kind]));
fix_advance_to_next_stmt(f);
@@ -2837,14 +2812,14 @@ void parse_file(Parser *p, AstFile *f) {
node->kind != AstNode_BadStmt &&
node->kind != AstNode_EmptyStmt) {
// NOTE(bill): Sanity check
ast_file_err(f, ast_node_token(node), "Only declarations are allowed at file scope");
syntax_error(ast_node_token(node), "Only declarations are allowed at file scope");
} else {
if (node->kind == AstNode_ImportDecl) {
auto *id = &node->ImportDecl;
String file_str = id->relpath.string;
if (!is_import_path_valid(file_str)) {
ast_file_err(f, ast_node_token(node), "Invalid `load` path");
syntax_error(ast_node_token(node), "Invalid `load` path");
continue;
}
@@ -2868,7 +2843,7 @@ void parse_file(Parser *p, AstFile *f) {
String file_str = id->filepath.string;
if (!is_import_path_valid(file_str)) {
ast_file_err(f, ast_node_token(node), "Invalid `foreign_system_library` path");
syntax_error(ast_node_token(node), "Invalid `foreign_system_library` path");
continue;
}
+11 -4
View File
@@ -142,11 +142,18 @@ void print_ast(AstNode *node, isize indent) {
case AstNode_VarDecl:
print_indent(indent);
if (node->VarDecl.kind == Declaration_Mutable) {
gb_printf("(decl:var,mutable)\n");
} else if (node->VarDecl.kind == Declaration_Immutable) {
gb_printf("(decl:var,immutable)\n");
gb_printf("(decl:var)\n");
gb_for_array(i, node->VarDecl.names) {
print_ast(node->VarDecl.names[i], indent+1);
}
print_ast(node->VarDecl.type, indent+1);
gb_for_array(i, node->VarDecl.values) {
print_ast(node->VarDecl.values[i], indent+1);
}
break;
case AstNode_ConstDecl:
print_indent(indent);
gb_printf("(decl:const)\n");
gb_for_array(i, node->VarDecl.names) {
print_ast(node->VarDecl.names[i], indent+1);
}
+40 -13
View File
@@ -155,31 +155,58 @@ Token empty_token = {Token_Invalid};
struct ErrorCollector {
TokenPos prev;
i64 count;
i64 warning_count;
};
gb_no_inline void error(ErrorCollector *ec, Token token, char *fmt, ...) {
ec->count++;
// NOTE(bill): Duplicate error, skip it
if (!token_pos_are_equal(ec->prev, token.pos)) {
ec->prev = token.pos;
gb_global ErrorCollector global_error_collector;
void warning(Token token, char *fmt, ...) {
global_error_collector.warning_count++;
// NOTE(bill): Duplicate error, skip it
if (!token_pos_are_equal(global_error_collector.prev, token.pos)) {
va_list va;
global_error_collector.prev = token.pos;
va_start(va, fmt);
gb_printf_err("%.*s(%td:%td) Warning: %s\n",
LIT(token.pos.file), token.pos.line, token.pos.column,
gb_bprintf_va(fmt, va));
va_end(va);
}
}
void error(Token token, char *fmt, ...) {
global_error_collector.count++;
// NOTE(bill): Duplicate error, skip it
if (!token_pos_are_equal(global_error_collector.prev, token.pos)) {
va_list va;
global_error_collector.prev = token.pos;
va_start(va, fmt);
gb_printf_err("%.*s(%td:%td) %s\n",
LIT(token.pos.file), token.pos.line, token.pos.column,
gb_bprintf_va(fmt, va));
va_end(va);
}
}
gb_no_inline void warning(Token token, char *fmt, ...) {
va_list va;
va_start(va, fmt);
gb_printf_err("%.*s(%td:%td) Warning: %s\n",
LIT(token.pos.file), token.pos.line, token.pos.column,
gb_bprintf_va(fmt, va));
va_end(va);
void syntax_error(Token token, char *fmt, ...) {
global_error_collector.count++;
// NOTE(bill): Duplicate error, skip it
if (!token_pos_are_equal(global_error_collector.prev, token.pos)) {
va_list va;
global_error_collector.prev = token.pos;
va_start(va, fmt);
gb_printf_err("%.*s(%td:%td) Syntax Error: %s\n",
LIT(token.pos.file), token.pos.line, token.pos.column,
gb_bprintf_va(fmt, va));
va_end(va);
}
}