Semicolons are required; when condition for certain file scope declarations; #import syntax change

This commit is contained in:
Ginger Bill
2016-12-01 22:44:00 +00:00
parent be8b9bda2f
commit 4bb45700a5
29 changed files with 3400 additions and 3244 deletions
+52 -73
View File
@@ -223,10 +223,10 @@ typedef struct CheckerContext {
#define MAP_NAME MapExprInfo
#include "../map.c"
typedef struct DelayedImport {
Scope * parent;
AstNodeImportDecl *decl;
} DelayedImport;
typedef struct DelayedDecl {
Scope * parent;
AstNode *decl;
} DelayedDecl;
// NOTE(bill): Symbol tables
@@ -253,7 +253,8 @@ typedef struct Checker {
BaseTypeSizes sizes;
Scope * global_scope;
Array(ProcedureInfo) procs; // NOTE(bill): Procedures to check
Array(DelayedImport) delayed_imports;
Array(DelayedDecl) delayed_imports;
Array(DelayedDecl) delayed_foreign_libraries;
gbArena arena;
@@ -427,7 +428,7 @@ void scope_lookup_parent_entity(Scope *scope, String name, Scope **scope_, Entit
}
if (e->scope != shared) {
// Do not return imported entities even #load ones
// Do not return imported entities even #include ones
continue;
}
@@ -616,6 +617,7 @@ void init_checker(Checker *c, Parser *parser, BaseTypeSizes sizes) {
array_init(&c->proc_stack, a);
array_init(&c->procs, a);
array_init(&c->delayed_imports, a);
array_init(&c->delayed_foreign_libraries, a);
// NOTE(bill): Is this big enough or too small?
isize item_size = gb_max3(gb_size_of(Entity), gb_size_of(Type), gb_size_of(Scope));
@@ -642,6 +644,7 @@ void destroy_checker(Checker *c) {
array_free(&c->proc_stack);
array_free(&c->procs);
array_free(&c->delayed_imports);
array_free(&c->delayed_foreign_libraries);
gb_arena_free(&c->arena);
}
@@ -1083,39 +1086,6 @@ void check_global_entities_by_kind(Checker *c, EntityKind kind) {
}
}
void check_global_collect_entities(Checker *c, Scope *parent_scope, AstNodeArray nodes, MapScope *file_scopes);
void check_global_when_stmt(Checker *c, Scope *parent_scope, AstNodeWhenStmt *ws, MapScope *file_scopes) {
Operand operand = {Addressing_Invalid};
check_expr(c, &operand, ws->cond);
if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
error_node(ws->cond, "Non-boolean condition in `when` statement");
}
if (operand.mode != Addressing_Constant) {
error_node(ws->cond, "Non-constant condition in `when` statement");
}
if (ws->body == NULL || ws->body->kind != AstNode_BlockStmt) {
error_node(ws->cond, "Invalid body for `when` statement");
} else {
if (operand.value.kind == ExactValue_Bool &&
operand.value.value_bool == true) {
ast_node(body, BlockStmt, ws->body);
check_global_collect_entities(c, parent_scope, body->stmts, file_scopes);
} else if (ws->else_stmt) {
switch (ws->else_stmt->kind) {
case AstNode_BlockStmt:
check_global_collect_entities(c, parent_scope, ws->else_stmt->BlockStmt.stmts, file_scopes);
break;
case AstNode_WhenStmt:
check_global_when_stmt(c, parent_scope, &ws->else_stmt->WhenStmt, file_scopes);
break;
default:
error_node(ws->else_stmt, "Invalid `else` statement in `when` statement");
break;
}
}
}
}
void check_global_collect_entities(Checker *c, Scope *parent_scope, AstNodeArray nodes, MapScope *file_scopes) {
for_array(decl_index, nodes) {
AstNode *decl = nodes.e[decl_index];
@@ -1126,16 +1096,13 @@ void check_global_collect_entities(Checker *c, Scope *parent_scope, AstNodeArray
switch (decl->kind) {
case_ast_node(bd, BadDecl, decl);
case_end;
case_ast_node(ws, WhenStmt, decl);
// Will be handled later
case_end;
case_ast_node(id, ImportDecl, decl);
if (!parent_scope->is_file) {
// NOTE(bill): _Should_ be caught by the parser
// TODO(bill): Better error handling if it isn't
continue;
}
DelayedImport di = {parent_scope, id};
DelayedDecl di = {parent_scope, decl};
array_add(&c->delayed_imports, di);
case_end;
case_ast_node(fl, ForeignLibrary, decl);
@@ -1145,24 +1112,8 @@ void check_global_collect_entities(Checker *c, Scope *parent_scope, AstNodeArray
continue;
}
String file_str = fl->filepath.string;
String base_dir = fl->base_dir;
if (!fl->is_system) {
gbAllocator a = heap_allocator(); // TODO(bill): Change this allocator
String rel_path = get_fullpath_relative(a, base_dir, file_str);
String import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(a, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
}
}
file_str = import_file;
}
try_add_foreign_library_path(c, file_str);
DelayedDecl di = {parent_scope, decl};
array_add(&c->delayed_foreign_libraries, di);
case_end;
case_ast_node(cd, ConstDecl, decl);
for_array(i, cd->values) {
@@ -1251,23 +1202,13 @@ void check_global_collect_entities(Checker *c, Scope *parent_scope, AstNodeArray
break;
}
}
// NOTE(bill): `when` stmts need to be handled after the other as the condition may refer to something
// declared after this stmt in source
for_array(decl_index, nodes) {
AstNode *decl = nodes.e[decl_index];
switch (decl->kind) {
case_ast_node(ws, WhenStmt, decl);
check_global_when_stmt(c, parent_scope, ws, file_scopes);
case_end;
}
}
}
void check_import_entities(Checker *c, MapScope *file_scopes) {
for_array(i, c->delayed_imports) {
AstNodeImportDecl *id = c->delayed_imports.e[i].decl;
Scope *parent_scope = c->delayed_imports.e[i].parent;
AstNode *decl = c->delayed_imports.e[i].decl;
ast_node(id, ImportDecl, decl);
HashKey key = hash_string(id->fullpath);
Scope **found = map_scope_get(file_scopes, key);
@@ -1286,6 +1227,19 @@ void check_import_entities(Checker *c, MapScope *file_scopes) {
continue;
}
if (id->cond != NULL) {
Operand operand = {Addressing_Invalid};
check_expr(c, &operand, id->cond);
if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) {
error_node(id->cond, "Non-constant boolean `when` condition");
continue;
}
if (operand.value.kind == ExactValue_Bool &&
!operand.value.value_bool) {
continue;
}
}
bool previously_added = false;
for_array(import_index, parent_scope->imported) {
Scope *prev = parent_scope->imported.e[import_index];
@@ -1366,6 +1320,31 @@ void check_import_entities(Checker *c, MapScope *file_scopes) {
}
}
}
for_array(i, c->delayed_foreign_libraries) {
Scope *parent_scope = c->delayed_foreign_libraries.e[i].parent;
AstNode *decl = c->delayed_foreign_libraries.e[i].decl;
ast_node(fl, ForeignLibrary, decl);
String file_str = fl->filepath.string;
String base_dir = fl->base_dir;
if (!fl->is_system) {
gbAllocator a = heap_allocator(); // TODO(bill): Change this allocator
String rel_path = get_fullpath_relative(a, base_dir, file_str);
String import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(a, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
}
}
file_str = import_file;
}
try_add_foreign_library_path(c, file_str);
}
}
+23 -26
View File
@@ -1,10 +1,10 @@
bool check_is_terminating(AstNode *node);
void check_stmt (Checker *c, AstNode *node, u32 flags);
void check_stmt_list (Checker *c, AstNodeArray stmts, u32 flags);
void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def, CycleChecker *cycle_checker);
void check_const_decl (Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr);
void check_proc_decl (Checker *c, Entity *e, DeclInfo *d);
void check_var_decl (Checker *c, Entity *e, Entity **entities, isize entity_count, AstNode *type_expr, AstNode *init_expr);
void check_stmt (Checker *c, AstNode *node, u32 flags);
void check_stmt_list (Checker *c, AstNodeArray stmts, u32 flags);
void check_type_decl (Checker *c, Entity *e, AstNode *type_expr, Type *def, CycleChecker *cycle_checker);
void check_const_decl (Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr);
void check_proc_decl (Checker *c, Entity *e, DeclInfo *d);
void check_var_decl (Checker *c, Entity *e, Entity **entities, isize entity_count, AstNode *type_expr, AstNode *init_expr);
// NOTE(bill): `content_name` is for debugging and error messages
Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String context_name) {
@@ -227,23 +227,21 @@ void check_init_constant(Checker *c, Entity *e, Operand *operand) {
if (operand->mode != Addressing_Constant) {
// TODO(bill): better error
error_node(operand->expr,
"`%.*s` is not a constant", LIT(ast_node_token(operand->expr).string));
error_node(operand->expr, "`%.*s` is not a constant", LIT(ast_node_token(operand->expr).string));
if (e->type == NULL) {
e->type = t_invalid;
}
return;
}
if (!is_type_constant_type(operand->type)) {
gbString type_str = type_to_string(operand->type);
error_node(operand->expr, "Invalid constant type: `%s`", type_str);
gb_string_free(type_str);
if (e->type == NULL) {
e->type = t_invalid;
}
return;
}
// if (!is_type_constant_type(operand->type)) {
// gbString type_str = type_to_string(operand->type);
// defer (gb_string_free(type_str));
// error_node(operand->expr,
// "Invalid constant type: `%s`", type_str);
// if (e->type == NULL) {
// e->type = t_invalid;
// }
// return;
// }
if (e->type == NULL) { // NOTE(bill): type inference
e->type = operand->type;
@@ -269,14 +267,13 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_e
if (type_expr) {
Type *t = check_type(c, type_expr);
// if (!is_type_constant_type(t)) {
// gbString str = type_to_string(t);
// defer (gb_string_free(str));
// error_node(type_expr),
// "Invalid constant type `%s`", str);
// e->type = t_invalid;
// return;
// }
if (!is_type_constant_type(t)) {
gbString str = type_to_string(t);
error_node(type_expr, "Invalid constant type `%s`", str);
gb_string_free(str);
e->type = t_invalid;
return;
}
e->type = t;
}
+2
View File
@@ -149,6 +149,7 @@ void check_local_collect_entities(Checker *c, AstNodeArray nodes, DelayedEntitie
gb_temp_arena_memory_end(tmp);
case_end;
#if 0
case_ast_node(pd, ProcDecl, node);
if (!ast_node_expect(pd->name, AstNode_Ident)) {
break;
@@ -163,6 +164,7 @@ void check_local_collect_entities(Checker *c, AstNodeArray nodes, DelayedEntitie
add_entity_and_decl_info(c, pd->name, e, d);
check_entity_decl(c, e, d, NULL, NULL);
case_end;
#endif
case_ast_node(td, TypeDecl, node);
if (!ast_node_expect(td->name, AstNode_Ident)) {
+29 -22
View File
@@ -111,6 +111,15 @@ bool check_is_terminating(AstNode *node) {
}
case_end;
case_ast_node(ws, WhenStmt, node);
if (ws->else_stmt != NULL) {
if (check_is_terminating(ws->body) &&
check_is_terminating(ws->else_stmt)) {
return true;
}
}
case_end;
case_ast_node(fs, ForStmt, node);
if (fs->cond == NULL && !check_has_break(fs->body, true)) {
return true;
@@ -283,30 +292,28 @@ typedef struct TypeAndToken {
void check_when_stmt(Checker *c, AstNodeWhenStmt *ws, u32 flags) {
Operand operand = {Addressing_Invalid};
check_expr(c, &operand, ws->cond);
if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
error_node(ws->cond, "Non-boolean condition in `when` statement");
}
if (operand.mode != Addressing_Constant) {
error_node(ws->cond, "Non-constant condition in `when` statement");
if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) {
error_node(ws->cond, "Non-constant boolean `when` condition");
return;
}
if (ws->body == NULL || ws->body->kind != AstNode_BlockStmt) {
error_node(ws->cond, "Invalid body for `when` statement");
} else {
if (operand.value.kind == ExactValue_Bool &&
operand.value.value_bool) {
check_stmt_list(c, ws->body->BlockStmt.stmts, flags);
} else if (ws->else_stmt) {
switch (ws->else_stmt->kind) {
case AstNode_BlockStmt:
check_stmt_list(c, ws->else_stmt->BlockStmt.stmts, flags);
break;
case AstNode_WhenStmt:
check_when_stmt(c, &ws->else_stmt->WhenStmt, flags);
break;
default:
error_node(ws->else_stmt, "Invalid `else` statement in `when` statement");
break;
}
return;
}
if (operand.value.kind == ExactValue_Bool &&
operand.value.value_bool) {
check_stmt_list(c, ws->body->BlockStmt.stmts, flags);
} else if (ws->else_stmt) {
switch (ws->else_stmt->kind) {
case AstNode_BlockStmt:
check_stmt_list(c, ws->else_stmt->BlockStmt.stmts, flags);
break;
case AstNode_WhenStmt:
check_when_stmt(c, &ws->else_stmt->WhenStmt, flags);
break;
default:
error_node(ws->else_stmt, "Invalid `else` statement in `when` statement");
break;
}
}
}
@@ -1085,7 +1092,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
case_ast_node(pd, ProcDecl, node);
// NOTE(bill): Handled elsewhere
#if 0
#if 1
// NOTE(bill): This must be handled here so it has access to the parent scope stuff
// e.g. using
Entity *e = make_entity_procedure(c->allocator, c->context.scope, pd->name->Ident, NULL);
+3 -4
View File
@@ -233,8 +233,8 @@ int main(int argc, char **argv) {
gbString lib_str = gb_string_make(heap_allocator(), "Kernel32.lib");
// defer (gb_string_free(lib_str));
char lib_str_buf[1024] = {0};
for_array(i, parser.foreign_libraries) {
String lib = parser.foreign_libraries.e[i];
for_array(i, checker.info.foreign_libraries) {
String lib = checker.info.foreign_libraries.e[i];
isize len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf),
" %.*s.lib", LIT(lib));
lib_str = gb_string_appendc(lib_str, lib_str_buf);
@@ -255,8 +255,7 @@ int main(int argc, char **argv) {
// timings_print_all(&timings);
if (run_output) {
win32_exec_command_line_app("odin run",
"%.*s.exe", cast(int)base_name_len, output_name);
win32_exec_command_line_app("odin run", "%.*s.exe", cast(int)base_name_len, output_name);
}
#endif
#endif
+122 -119
View File
@@ -56,7 +56,6 @@ typedef struct Parser {
Array(AstFile) files;
Array(ImportedFile) imports;
gbAtomic32 import_index;
Array(String) foreign_libraries;
isize total_token_count;
gbMutex mutex;
} Parser;
@@ -88,7 +87,8 @@ typedef enum StmtStateFlag {
AstNodeArray make_ast_node_array(AstFile *f) {
AstNodeArray a;
array_init(&a, gb_arena_allocator(&f->arena));
// array_init(&a, gb_arena_allocator(&f->arena));
array_init(&a, heap_allocator());
return a;
}
@@ -254,16 +254,17 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
}) \
AST_NODE_KIND(ImportDecl, "import declaration", struct { \
Token token, relpath; \
String os, arch; \
String fullpath; \
Token import_name; \
bool is_load; \
AstNode *cond; \
AstNode *note; \
}) \
AST_NODE_KIND(ForeignLibrary, "foreign library", struct { \
Token token, filepath; \
String base_dir; \
bool is_system; \
String base_dir; \
AstNode *cond; \
bool is_system; \
}) \
AST_NODE_KIND(_DeclEnd, "", i32) \
AST_NODE_KIND(_TypeBegin, "", i32) \
@@ -1005,22 +1006,22 @@ AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
}
AstNode *make_import_decl(AstFile *f, Token token, Token relpath, Token import_name,
String os, String arch,
AstNode *cond,
bool is_load) {
AstNode *result = make_node(f, AstNode_ImportDecl);
result->ImportDecl.token = token;
result->ImportDecl.relpath = relpath;
result->ImportDecl.import_name = import_name;
result->ImportDecl.os = os;
result->ImportDecl.arch = arch;
result->ImportDecl.cond = cond;
result->ImportDecl.is_load = is_load;
return result;
}
AstNode *make_foreign_library(AstFile *f, Token token, Token filepath, bool is_system) {
AstNode *make_foreign_library(AstFile *f, Token token, Token filepath, AstNode *cond, bool is_system) {
AstNode *result = make_node(f, AstNode_ForeignLibrary);
result->ForeignLibrary.token = token;
result->ForeignLibrary.filepath = filepath;
result->ForeignLibrary.cond = cond;
result->ForeignLibrary.is_system = is_system;
return result;
}
@@ -1156,19 +1157,30 @@ bool expect_semicolon_after_stmt(AstFile *f, AstNode *s) {
return true;
}
if (f->curr_token.pos.line != f->prev_token.pos.line) {
return true;
if (s != NULL) {
switch (s->kind) {
case AstNode_ProcDecl:
case AstNode_TypeDecl:
return true;
}
}
switch (f->curr_token.kind) {
case Token_EOF:
case Token_CloseBrace:
return true;
}
// if (f->curr_token.pos.line != f->prev_token.pos.line) {
// return true;
// }
syntax_error(f->curr_token,
"Expected `;` after %.*s, got `%.*s`",
LIT(ast_node_strings[s->kind]), LIT(token_strings[f->curr_token.kind]));
// switch (f->curr_token.kind) {
// case Token_EOF:
// case Token_CloseBrace:
// return true;
// }
if (s != NULL) {
syntax_error(f->prev_token, "Expected `;` after %.*s, got `%.*s`",
LIT(ast_node_strings[s->kind]), LIT(token_strings[f->prev_token.kind]));
} else {
syntax_error(f->prev_token, "Expected `;`");
}
fix_advance_to_next_stmt(f);
return false;
}
@@ -1197,12 +1209,6 @@ AstNode *parse_tag_expr(AstFile *f, AstNode *expression) {
return make_tag_expr(f, token, name, expression);
}
AstNode *parse_tag_stmt(AstFile *f, AstNode *statement) {
Token token = expect_token(f, Token_Hash);
Token name = expect_token(f, Token_Identifier);
return make_tag_stmt(f, token, name, statement);
}
AstNode *unparen_expr(AstNode *node) {
for (;;) {
if (node->kind != AstNode_ParenExpr)
@@ -2518,10 +2524,8 @@ AstNode *parse_return_stmt(AstFile *f) {
f->curr_token.pos.line == token.pos.line) {
results = parse_rhs_expr_list(f);
}
if (f->curr_token.kind != Token_CloseBrace) {
expect_semicolon_after_stmt(f, results.e[0]);
}
expect_semicolon_after_stmt(f, results.e[0]);
return make_return_stmt(f, token, results);
}
@@ -2751,11 +2755,11 @@ AstNode *parse_stmt(AstFile *f) {
// TODO(bill): other keywords
case Token_if: return parse_if_stmt(f);
case Token_when: return parse_when_stmt(f);
case Token_return: return parse_return_stmt(f);
case Token_for: return parse_for_stmt(f);
case Token_match: return parse_match_stmt(f);
case Token_defer: return parse_defer_stmt(f);
case Token_asm: return parse_asm_stmt(f);
case Token_return: return parse_return_stmt(f);
case Token_break:
case Token_continue:
@@ -2821,29 +2825,103 @@ AstNode *parse_stmt(AstFile *f) {
} break;
case Token_Hash: {
s = parse_tag_stmt(f, NULL);
String tag = s->TagStmt.name.string;
AstNode *s = NULL;
Token hash_token = expect_token(f, Token_Hash);
Token name = expect_token(f, Token_Identifier);
String tag = name.string;
if (str_eq(tag, str_lit("shared_global_scope"))) {
if (f->curr_proc == NULL) {
f->is_global_scope = true;
return make_empty_stmt(f, f->curr_token);
s = make_empty_stmt(f, f->curr_token);
} else {
syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope");
s = make_bad_decl(f, token, f->curr_token);
}
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->curr_token);
expect_semicolon_after_stmt(f, s);
return s;
} else if (str_eq(tag, str_lit("foreign_system_library"))) {
AstNode *cond = NULL;
Token file_path = expect_token(f, Token_String);
if (f->curr_proc == NULL) {
return make_foreign_library(f, s->TagStmt.token, file_path, true);
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
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);
if (f->curr_proc == NULL) {
s = make_foreign_library(f, hash_token, file_path, cond, true);
} else {
syntax_error(token, "You cannot use #foreign_system_library within a procedure. This must be done at the file scope");
s = make_bad_decl(f, token, file_path);
}
expect_semicolon_after_stmt(f, s);
return s;
} else if (str_eq(tag, str_lit("foreign_library"))) {
AstNode *cond = NULL;
Token file_path = expect_token(f, Token_String);
if (f->curr_proc == NULL) {
return make_foreign_library(f, s->TagStmt.token, file_path, false);
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
syntax_error(token, "You cannot use #foreign_library within a procedure. This must be done at the file scope");
return make_bad_decl(f, token, file_path);
if (f->curr_proc == NULL) {
s = make_foreign_library(f, hash_token, file_path, cond, false);
} else {
syntax_error(token, "You cannot use #foreign_library within a procedure. This must be done at the file scope");
s = make_bad_decl(f, token, file_path);
}
expect_semicolon_after_stmt(f, s);
return s;
} else if (str_eq(tag, str_lit("import"))) {
AstNode *cond = NULL;
Token import_name = {0};
switch (f->curr_token.kind) {
case Token_Period:
import_name = f->curr_token;
import_name.kind = Token_Identifier;
next_token(f);
break;
case Token_Identifier:
import_name = f->curr_token;
next_token(f);
break;
}
if (str_eq(import_name.string, str_lit("_"))) {
syntax_error(token, "Illegal import name: `_`");
}
Token file_path = expect_token_after(f, Token_String, "#import");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
if (f->curr_proc != NULL) {
syntax_error(token, "You cannot use #import within a procedure. This must be done at the file scope");
s = make_bad_decl(f, token, file_path);
} else {
s = make_import_decl(f, hash_token, file_path, import_name, cond, false);
expect_semicolon_after_stmt(f, s);
}
return s;
} else if (str_eq(tag, str_lit("include"))) {
AstNode *cond = NULL;
Token file_path = expect_token(f, Token_String);
Token import_name = file_path;
import_name.string = str_lit(".");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
if (f->curr_proc == NULL) {
s = make_import_decl(f, hash_token, file_path, import_name, cond, true);
} else {
syntax_error(token, "You cannot use #include within a procedure. This must be done at the file scope");
s = make_bad_decl(f, token, file_path);
}
expect_semicolon_after_stmt(f, s);
return s;
} else if (str_eq(tag, str_lit("thread_local"))) {
AstNode *var_decl = parse_simple_stmt(f);
if (var_decl->kind != AstNode_VarDecl) {
@@ -2870,60 +2948,9 @@ AstNode *parse_stmt(AstFile *f) {
syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
}
return s;
} else if (str_eq(tag, str_lit("import"))) {
String os = {0};
String arch = {0};
// if (tag.len > 6) {
// String sub = make_string(tag.text+6, tag.len-6);
// }
// TODO(bill): better error messages
Token import_name = {0};
Token file_path = expect_token_after(f, Token_String, "#import");
if (allow_token(f, Token_as)) {
// NOTE(bill): Custom import name
if (f->curr_token.kind == Token_Period) {
import_name = f->curr_token;
import_name.kind = Token_Identifier;
next_token(f);
} else {
import_name = expect_token_after(f, Token_Identifier, "`as` for import declaration");
}
if (str_eq(import_name.string, str_lit("_"))) {
syntax_error(token, "Illegal import name: `_`");
return make_bad_decl(f, token, f->curr_token);
}
}
if (f->curr_proc != NULL) {
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);
}
return make_import_decl(f, s->TagStmt.token, file_path, import_name, os, arch, false);
} else if (str_eq(tag, str_lit("load"))) {
String os = {0};
String arch = {0};
// TODO(bill): better error messages
Token file_path = expect_token(f, Token_String);
Token import_name = file_path;
import_name.string = str_lit(".");
if (f->curr_proc == NULL) {
return make_import_decl(f, s->TagStmt.token, file_path, import_name, os, arch, true);
}
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 {
}
s->TagStmt.stmt = parse_stmt(f); // TODO(bill): Find out why this doesn't work as an argument
return s;
return make_tag_stmt(f, hash_token, name, parse_stmt(f));
} break;
case Token_OpenBrace:
@@ -3016,7 +3043,6 @@ void destroy_ast_file(AstFile *f) {
bool init_parser(Parser *p) {
array_init(&p->files, heap_allocator());
array_init(&p->imports, heap_allocator());
array_init(&p->foreign_libraries, heap_allocator());
gb_mutex_init(&p->mutex);
return true;
}
@@ -3033,7 +3059,6 @@ void destroy_parser(Parser *p) {
#endif
array_free(&p->files);
array_free(&p->imports);
array_free(&p->foreign_libraries);
gb_mutex_destroy(&p->mutex);
}
@@ -3169,43 +3194,21 @@ String get_filepath_extension(String path) {
return make_string(path.text, dot);
}
void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, AstNodeArray decls);
void parse_setup_file_when_stmt(Parser *p, AstFile *f, String base_dir, AstNodeWhenStmt *ws) {
if (ws->body != NULL && ws->body->kind == AstNode_BlockStmt) {
parse_setup_file_decls(p, f, base_dir, ws->body->BlockStmt.stmts);
}
if (ws->else_stmt) {
switch (ws->else_stmt->kind) {
case AstNode_BlockStmt:
parse_setup_file_decls(p, f, base_dir, ws->else_stmt->BlockStmt.stmts);
break;
case AstNode_WhenStmt:
parse_setup_file_when_stmt(p, f, base_dir, &ws->else_stmt->WhenStmt);
break;
}
}
}
void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, AstNodeArray decls) {
for_array(i, decls) {
AstNode *node = decls.e[i];
if (!is_ast_node_decl(node) &&
!is_ast_node_when_stmt(node) &&
node->kind != AstNode_BadStmt &&
node->kind != AstNode_EmptyStmt) {
// NOTE(bill): Sanity check
syntax_error_node(node, "Only declarations are allowed at file scope %.*s", LIT(ast_node_strings[node->kind]));
} else if (node->kind == AstNode_WhenStmt) {
parse_setup_file_when_stmt(p, f, base_dir, &node->WhenStmt);
} else if (node->kind == AstNode_ImportDecl) {
AstNodeImportDecl *id = &node->ImportDecl;
String file_str = id->relpath.string;
if (!is_import_path_valid(file_str)) {
if (id->is_load) {
syntax_error_node(node, "Invalid #load path: `%.*s`", LIT(file_str));
syntax_error_node(node, "Invalid #include path: `%.*s`", LIT(file_str));
} else {
syntax_error_node(node, "Invalid #import path: `%.*s`", LIT(file_str));
}
+20 -13
View File
@@ -30,7 +30,6 @@ typedef struct ssaModule {
String layout;
// String triple;
MapEntity min_dep_map; // Key: Entity *
MapSsaValue values; // Key: Entity *
MapSsaValue members; // Key: String
@@ -192,6 +191,11 @@ struct ssaProcedure {
}) \
SSA_INSTR_KIND(Phi, struct { ssaValueArray edges; Type *type; }) \
SSA_INSTR_KIND(Unreachable, i32) \
SSA_INSTR_KIND(UnaryOp, struct { \
Type * type; \
TokenKind op; \
ssaValue *expr; \
}) \
SSA_INSTR_KIND(BinaryOp, struct { \
Type * type; \
TokenKind op; \
@@ -560,6 +564,8 @@ Type *ssa_instr_type(ssaInstr *instr) {
return instr->UnionTagPtr.type;
case ssaInstr_UnionTagValue:
return instr->UnionTagValue.type;
case ssaInstr_UnaryOp:
return instr->UnaryOp.type;
case ssaInstr_BinaryOp:
return instr->BinaryOp.type;
case ssaInstr_Conv:
@@ -887,6 +893,15 @@ ssaValue *ssa_make_instr_union_tag_value(ssaProcedure *p, ssaValue *address) {
return v;
}
ssaValue *ssa_make_instr_unary_op(ssaProcedure *p, TokenKind op, ssaValue *expr, Type *type) {
ssaValue *v = ssa_alloc_instr(p, ssaInstr_UnaryOp);
ssaInstr *i = &v->Instr;
i->UnaryOp.op = op;
i->UnaryOp.expr = expr;
i->UnaryOp.type = type;
return v;
}
ssaValue *ssa_make_instr_binary_op(ssaProcedure *p, TokenKind op, ssaValue *left, ssaValue *right, Type *type) {
ssaValue *v = ssa_alloc_instr(p, ssaInstr_BinaryOp);
@@ -2619,18 +2634,10 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
case Token_Add:
return ssa_build_expr(proc, ue->expr);
case Token_Sub: // NOTE(bill): -`x` == 0 - `x`
return ssa_emit_arith(proc, ue->op.kind, v_zero, ssa_build_expr(proc, ue->expr), tv->type);
case Token_Not: // Boolean not
case Token_Xor: { // Bitwise not
// NOTE(bill): "not" `x` == `x` "xor" `-1`
ssaValue *left = ssa_build_expr(proc, ue->expr);
ssaValue *right = ssa_add_module_constant(proc->module, tv->type, make_exact_value_integer(-1));
return ssa_emit_arith(proc, ue->op.kind,
left, right,
tv->type);
} break;
case Token_Not: // Boolean not
case Token_Xor: // Bitwise not
case Token_Sub: // Bitwise not
return ssa_emit(proc, ssa_make_instr_unary_op(proc, ue->op.kind, ssa_build_expr(proc, ue->expr), tv->type));
}
case_end;
+49 -1
View File
@@ -866,6 +866,55 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
ssa_fprintf(f, "unreachable\n");
} break;
case ssaInstr_UnaryOp: {
ssaInstrUnaryOp *uo = &value->Instr.UnaryOp;
Type *type = base_type(ssa_type(uo->expr));
Type *elem_type = type;
while (elem_type->kind == Type_Vector) {
elem_type = base_type(elem_type->Vector.elem);
}
ssa_fprintf(f, "%%%d = ", value->index);
switch (uo->op) {
case Token_Sub:
if (is_type_float(elem_type)) {
ssa_fprintf(f, "fsub");
} else {
ssa_fprintf(f, "sub");
}
break;
case Token_Xor:
case Token_Not:
GB_ASSERT(is_type_integer(type) || is_type_boolean(type));
ssa_fprintf(f, "xor");
break;
default:
GB_PANIC("Unknown unary operator");
break;
}
ssa_fprintf(f, " ");
ssa_print_type(f, m, type);
ssa_fprintf(f, " ");
switch (uo->op) {
case Token_Sub:
if (is_type_float(elem_type)) {
ssa_print_exact_value(f, m, make_exact_value_float(0), type);
} else {
ssa_fprintf(f, "0");
}
break;
case Token_Xor:
case Token_Not:
GB_ASSERT(is_type_integer(type) || is_type_boolean(type));
ssa_fprintf(f, "-1");
break;
}
ssa_fprintf(f, ", ");
ssa_print_value(f, m, uo->expr, type);
ssa_fprintf(f, "\n");
} break;
case ssaInstr_BinaryOp: {
ssaInstrBinaryOp *bo = &value->Instr.BinaryOp;
Type *type = base_type(ssa_type(bo->left));
@@ -971,7 +1020,6 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
ssa_fprintf(f, ", ");
ssa_print_value(f, m, bo->right, type);
ssa_fprintf(f, "\n");
} break;
case ssaInstr_Call: {
+2 -2
View File
@@ -83,8 +83,8 @@ TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \
\
TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_type, "type"), \
/* TOKEN_KIND(Token_import, "import"), */ \
/* TOKEN_KIND(Token_include, "include"), */ \
/* TOKEN_KIND(Token_import, "import"), */\
/* TOKEN_KIND(Token_include, "include"), */\
TOKEN_KIND(Token_proc, "proc"), \
TOKEN_KIND(Token_match, "match"), \
TOKEN_KIND(Token_break, "break"), \