Cleanup iterators
This commit is contained in:
3
base.cpp
3
base.cpp
@@ -422,6 +422,3 @@ operator!=(Intern_String a, Intern_String b){
|
||||
#define For_Linked_List(a) For_Linked_List_Named(a,it)
|
||||
#define For_Named(a,it) for(auto &it : (a))
|
||||
#define For(a) For_Named((a),it)
|
||||
|
||||
#define Iter_Named(list, it) for(auto it = iterate(list); should_we_continue(&it); advance(&it))
|
||||
#define Iter(list) Iter_Named(list, it)
|
||||
|
||||
@@ -330,6 +330,40 @@ struct List{
|
||||
List_Node<T> *first = 0;
|
||||
List_Node<T> *last = 0;
|
||||
List_Node<T> *first_free = 0;
|
||||
|
||||
struct Iter{
|
||||
T *item;
|
||||
List_Node<T> *node;
|
||||
int node_index;
|
||||
|
||||
T &operator*() { return *item; }
|
||||
|
||||
Iter &operator++() {
|
||||
if (node) {
|
||||
if (node_index + 1 >= node->len) {
|
||||
node = node->next;
|
||||
node_index = -1;
|
||||
item = 0;
|
||||
}
|
||||
|
||||
if (node) {
|
||||
node_index += 1;
|
||||
item = node->data + node_index;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
Iter begin() {
|
||||
Iter result = {};
|
||||
result.node = first;
|
||||
result.node_index = -1;
|
||||
return ++result;
|
||||
}
|
||||
|
||||
Iter end() { return{}; }
|
||||
friend bool operator!=(Iter &a, Iter &b) { return a.item != b.item; }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
@@ -540,42 +574,3 @@ T *merge(Allocator *arena, List<T> *list){
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
struct List_Iter{
|
||||
T *item;
|
||||
int index;
|
||||
List_Node<T> *node;
|
||||
int node_index;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
void advance(List_Iter<T> *iter){
|
||||
if(!iter->node) return;
|
||||
|
||||
if(iter->node_index + 1 >= iter->node->len){
|
||||
iter->node = iter->node->next;
|
||||
iter->node_index = -1;
|
||||
iter->item = 0;
|
||||
}
|
||||
|
||||
if(iter->node){
|
||||
iter->node_index += 1;
|
||||
iter->index += 1;
|
||||
iter->item = iter->node->data + iter->node_index;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
List_Iter<T> iterate(List<T> *list){
|
||||
List_Iter<T> result = {};
|
||||
result.node = list->first;
|
||||
result.index = result.node_index = -1;
|
||||
advance(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool should_we_continue(List_Iter<T> *iter){
|
||||
return iter->item != 0;
|
||||
}
|
||||
|
||||
@@ -679,9 +679,9 @@ gen_ast(Ast *ast){
|
||||
gen("%Q{", node->unique_name);
|
||||
global_indent++;
|
||||
is_inside_struct++;
|
||||
Iter(&node->scope->decls){
|
||||
For(node->scope->decls){
|
||||
genln("");
|
||||
gen_ast(it.item[0]);
|
||||
gen_ast(it);
|
||||
}
|
||||
|
||||
is_inside_struct--;
|
||||
@@ -694,10 +694,10 @@ gen_ast(Ast *ast){
|
||||
gen("/*enum %Q{", node->name);
|
||||
// @todo add typespec
|
||||
global_indent++;
|
||||
Iter(&node->scope->decls){
|
||||
genln("%Q", it.item[0]->name);
|
||||
For(node->scope->decls){
|
||||
genln("%Q", it->name);
|
||||
gen(" = ");
|
||||
gen_value(it.item[0]->pos, it.item[0]->value);
|
||||
gen_value(it->pos, it->value);
|
||||
gen(",");
|
||||
}
|
||||
global_indent--;
|
||||
@@ -836,20 +836,17 @@ typedef struct String{
|
||||
)");
|
||||
|
||||
// Generate struct forward decls
|
||||
Iter(&pctx->ordered_decls){
|
||||
auto i = it.item[0];
|
||||
if(i->kind == AST_STRUCT){
|
||||
genln("typedef struct %Q %Q;", i->unique_name, i->unique_name);
|
||||
For(pctx->ordered_decls){
|
||||
if(it->kind == AST_STRUCT){
|
||||
genln("typedef struct %Q %Q;", it->unique_name, it->unique_name);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate slice and tuple types
|
||||
Iter(&pctx->all_types){
|
||||
For_Named(pctx->all_types, type){
|
||||
Scratch_Arena *scratch = pctx->scratch;
|
||||
Scratch_Scope _scope(scratch);
|
||||
|
||||
Ast_Type *type = it.item[0];
|
||||
|
||||
if(type->kind == TYPE_SLICE){
|
||||
genln("typedef struct Slice%llu{", type->type_id);
|
||||
global_indent++;
|
||||
@@ -883,21 +880,21 @@ typedef struct String{
|
||||
Ast_Decl *win_main = 0;
|
||||
|
||||
// Generate lambda forward decls
|
||||
Iter(&pctx->ordered_decls){
|
||||
if(it.item[0]->kind == AST_LAMBDA){
|
||||
if(it.item[0]->name == intern_main){
|
||||
main = it.item[0];
|
||||
it.item[0]->unique_name = it.item[0]->name;
|
||||
For(pctx->ordered_decls){
|
||||
if(it->kind == AST_LAMBDA){
|
||||
if(it->name == intern_main){
|
||||
main = it;
|
||||
it->unique_name = it->name;
|
||||
}
|
||||
|
||||
if(it.item[0]->name == intern_win_main){
|
||||
win_main = it.item[0];
|
||||
it.item[0]->unique_name = it.item[0]->name;
|
||||
if(it->name == intern_win_main){
|
||||
win_main = it;
|
||||
it->unique_name = it->name;
|
||||
}
|
||||
|
||||
genln("");
|
||||
|
||||
gen_lambda(it.item[0]->unique_name, it.item[0]->lambda, false);
|
||||
gen_lambda(it->unique_name, it->lambda, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -918,8 +915,7 @@ typedef struct String{
|
||||
genln("int64_t type_infos_len = %d;", length(&pctx->all_types));
|
||||
genln("Type_Info *type_infos = (Type_Info[]){");
|
||||
global_indent++;
|
||||
Iter(&pctx->all_types){
|
||||
Ast_Type *t = it.item[0];
|
||||
For_Named(pctx->all_types, t){
|
||||
genln("{/*%Q*/.kind = %d, .size = %d, .align = %d, .is_unsigned = %s, .type = %d, ", typestring(t),
|
||||
(S32)t->kind, (S32)t->size, (S32)t->align, t->is_unsigned ? "true" : "false", t->type_id);
|
||||
switch(t->kind){
|
||||
@@ -965,11 +961,13 @@ typedef struct String{
|
||||
}
|
||||
|
||||
// Generate actual code
|
||||
Iter(&pctx->ordered_decls){
|
||||
if(it.index >= pctx->base_language_ordered_decl_len){
|
||||
int index = 0;
|
||||
For(pctx->ordered_decls){
|
||||
if(index >= pctx->base_language_ordered_decl_len){
|
||||
genln("");
|
||||
gen_ast(it.item[0]);
|
||||
gen_ast(it);
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -178,13 +178,12 @@ CORE_Static void
|
||||
parse_all_modules() {
|
||||
pctx->parsing_time_begin = os_time();
|
||||
|
||||
Iter_Named(&pctx->modules, mod_it) {
|
||||
Ast_Module *module = mod_it.item[0];
|
||||
For_Named(pctx->modules, module) {
|
||||
if (module->state != MODULE_REGISTERED)
|
||||
continue;
|
||||
|
||||
Iter(&module->all_loaded_files) {
|
||||
parse_file(*it.item);
|
||||
For(module->all_loaded_files) {
|
||||
parse_file(it);
|
||||
}
|
||||
|
||||
if (module != pctx->language_base_module) {
|
||||
@@ -220,8 +219,8 @@ add_module(Token *pos, Intern_String filename, B32 command_line_module) {
|
||||
// Find in module folder
|
||||
//
|
||||
else {
|
||||
Iter(&pctx->module_folders) {
|
||||
String path = string_fmt(scratch, "%Q/%Q", it.item[0], filename);
|
||||
For(pctx->module_folders) {
|
||||
String path = string_fmt(scratch, "%Q/%Q", it, filename);
|
||||
if (os_does_file_exist(path)) {
|
||||
absolute_file_path = path;
|
||||
absolute_base_folder = string_chop_last_slash(path);
|
||||
@@ -234,10 +233,10 @@ add_module(Token *pos, Intern_String filename, B32 command_line_module) {
|
||||
compiler_error(pos, "Couldn't find the module with name %Q", filename);
|
||||
}
|
||||
|
||||
Iter(&pctx->modules) {
|
||||
if (string_compare(it.item[0]->absolute_file_path, absolute_file_path)) {
|
||||
For(pctx->modules) {
|
||||
if (string_compare(it->absolute_file_path, absolute_file_path)) {
|
||||
log_trace("Returning registered module: %Q\n", absolute_file_path);
|
||||
return it.item[0];
|
||||
return it;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,10 +260,9 @@ resolve_everything_in_module(Ast_Module *module) {
|
||||
if (module->state == MODULE_RESOLVED)
|
||||
return;
|
||||
pctx->resolving_time_begin = os_time();
|
||||
Iter_Named(&module->all_loaded_files, file) {
|
||||
Iter(&file.item[0]->decls) {
|
||||
Ast_Decl *decl = it.item[0];
|
||||
resolve_name(file.item[0], decl->pos, decl->name);
|
||||
For_Named(module->all_loaded_files, file) {
|
||||
For_Named(file->decls, decl) {
|
||||
resolve_name(file, decl->pos, decl->name);
|
||||
|
||||
if (decl->kind == AST_STRUCT) {
|
||||
type_complete(decl->type_val);
|
||||
@@ -371,8 +369,8 @@ compile_file(Allocator *allocator, String filename, U32 compile_flags = COMPILE_
|
||||
F64 begin = os_time();
|
||||
String_Builder builder = {scratch};
|
||||
builder.addf("clang program.c -Wall -Wno-unused-function -Wno-parentheses-equality -g -o a" OS_EXE " ");
|
||||
Iter(&pctx->files_to_link) {
|
||||
builder.addf("-l%Q ", it.item[0]->intern_val);
|
||||
For(pctx->files_to_link) {
|
||||
builder.addf("-l%Q ", it->intern_val);
|
||||
}
|
||||
String compiler_call = string_flatten(scratch, &builder);
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
|
||||
/*
|
||||
@! Separate out the codegen stage cause that can change
|
||||
@! Change type of Stage allocator
|
||||
@! Look into String_Builders in Core_Ctx
|
||||
@! Look into stage allocator and perhaps
|
||||
use it more often to reduce scenarios
|
||||
@@ -34,8 +33,10 @@ struct Lex_Stream{
|
||||
|
||||
struct Core_Ctx{
|
||||
Allocator *heap;
|
||||
|
||||
Push_Arena perm_push_only;
|
||||
Push_Arena *perm; // Stores: AST, tokens, interns
|
||||
|
||||
Scratch_Arena *scratch;
|
||||
Scratch_Arena *stage_arena;
|
||||
|
||||
@@ -98,6 +99,7 @@ struct Core_Ctx{
|
||||
|
||||
Token same_scope_token;
|
||||
Token null_token;
|
||||
|
||||
/*#import meta
|
||||
for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};')
|
||||
for i in meta.interns: print(f'Intern_String intern_{i.lower()};')
|
||||
@@ -197,16 +199,12 @@ Ast_Operator_Info op_info_table[20];
|
||||
Ast_Type *untyped_int = &type__untyped_int;
|
||||
Ast_Type *untyped_float = &type__untyped_float;
|
||||
|
||||
|
||||
|
||||
|
||||
Intern_String intern(String string){
|
||||
assert(string.len > 0);
|
||||
return intern_string(&interns, string);
|
||||
}
|
||||
};
|
||||
|
||||
CORE_Static void init_type();
|
||||
CORE_Static String compile_to_c_code();
|
||||
CORE_Static Ast_Module *ast_module(Token *pos, Intern_String filename);
|
||||
CORE_Static void insert_builtin_types_into_scope(Ast_Scope *p);
|
||||
|
||||
@@ -474,6 +474,7 @@ struct Ast_Builtin: Ast_Expr{
|
||||
|
||||
// Problem: We are parsing out of order, in the middle of parsing a function
|
||||
// we can jump down a different function, we cant therfore use global map.
|
||||
// On top of that in the future we want a way to inject scopes, for convenience.
|
||||
// Each scope needs to have it's checked locals list. To lookup syms we need to
|
||||
// look into global scope and to the locals list.
|
||||
//
|
||||
@@ -551,10 +552,10 @@ struct Ast_Switch: Ast{
|
||||
*/
|
||||
|
||||
struct Ast_Scope: Ast{
|
||||
String debug_name; // Dont use
|
||||
String debug_name; // Only for debug purposes, dont depend on it
|
||||
List<Ast_Scope *> implicit_imports;
|
||||
List<Ast_Decl *> decls;
|
||||
Array<Ast *> stmts;
|
||||
Array<Ast *> stmts;
|
||||
|
||||
uint32_t visit_id;
|
||||
uint32_t scope_id;
|
||||
|
||||
@@ -740,8 +740,8 @@ parse_enum(Token *pos){
|
||||
CORE_Static void
|
||||
add_implicit_import(Ast_Scope *scope, Ast_Scope *to_add){
|
||||
B32 found = false;
|
||||
Iter(&scope->implicit_imports){
|
||||
if(it.item[0] == to_add){
|
||||
For(scope->implicit_imports){
|
||||
if(it == to_add){
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -757,8 +757,7 @@ CORE_Static Ast_File *
|
||||
register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32 global_implicit_load){
|
||||
Ast_File *file = 0;
|
||||
|
||||
Iter(&pctx->files){
|
||||
Ast_File *it_file = it.item[0];
|
||||
For_Named(pctx->files, it_file){
|
||||
if(string_compare(it_file->absolute_file_path, absolute_file_path)){
|
||||
if(module == it_file->module){
|
||||
log_trace("%Q :: Returning registered file: %Q\n", module->absolute_file_path, absolute_file_path);
|
||||
|
||||
@@ -445,8 +445,7 @@ inside_scope_search(Scope_Search *search, Ast_Scope *scope, int level){
|
||||
scope->visit_id = search->scope_visit_id;
|
||||
|
||||
// Search for declarations in current scope
|
||||
Iter(&scope->decls){
|
||||
Ast_Decl *decl = it.item[0];
|
||||
For_Named(scope->decls, decl){
|
||||
if(decl->name == search->name){
|
||||
search->results.add(decl);
|
||||
if(search->exit_on_find){
|
||||
@@ -459,9 +458,9 @@ inside_scope_search(Scope_Search *search, Ast_Scope *scope, int level){
|
||||
// We dont want to descent into foreign implicit imports
|
||||
// so we cap it with level and then we need to make sure that we
|
||||
// descent into module 1 level away and it's files but no more then that
|
||||
Iter(&scope->implicit_imports){
|
||||
if(level > 0 && it.item[0]->kind == AST_MODULE) continue;
|
||||
inside_scope_search(search, it.item[0], level + 1);
|
||||
For(scope->implicit_imports){
|
||||
if(level > 0 && it->kind == AST_MODULE) continue;
|
||||
inside_scope_search(search, it, level + 1);
|
||||
|
||||
if(search->exit_on_find && search->results.len){
|
||||
return;
|
||||
@@ -1709,8 +1708,7 @@ resolve_decl(Ast_Decl *ast){
|
||||
node->type_val = type_enum(node, type_of_enum);
|
||||
|
||||
S64 value = 1;
|
||||
Iter(&node->scope->decls){
|
||||
Ast_Decl *decl = it.item[0];
|
||||
For_Named(node->scope->decls, decl){
|
||||
Operand op = {};
|
||||
if(decl->expr){
|
||||
op = require_const_int(decl->expr, AST_CANT_BE_NULL);
|
||||
|
||||
@@ -232,8 +232,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node){
|
||||
Array<Ast_Resolved_Member> members = {scratch};
|
||||
type->kind = TYPE_COMPLETING;
|
||||
size_t members_size = 0;
|
||||
Iter(&node->scope->decls){
|
||||
Ast_Decl *decl = it.item[0];
|
||||
For_Named(node->scope->decls, decl){
|
||||
resolve_decl(decl);
|
||||
assert(decl->type->kind != TYPE_INCOMPLETE);
|
||||
assert(is_pow2(decl->type->align));
|
||||
@@ -271,11 +270,6 @@ type_complete(Ast_Type *type){
|
||||
add(pctx->perm, &pctx->ordered_decls, (Ast_Decl *)type->ast);
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
init_type(){
|
||||
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
typename_base(String_Builder *sb, Ast_Type *type){
|
||||
switch(type->kind){
|
||||
|
||||
Reference in New Issue
Block a user