Iterator + start to add core to core codegen

This commit is contained in:
Krzosa Karol
2023-03-31 17:38:32 +02:00
parent 1839279235
commit 277404fe95
6 changed files with 198 additions and 61 deletions

View File

@@ -4,64 +4,42 @@ Ast_Decl *get_or_instantiate_polymorph_type(Ast_Decl *poly, Array<Ast_Call_Item
return 0;
}
/* @todo
for (Ast_Iter iter = iterate_depth_first(ast); iter.ast; next(&iter)) {
Ast *ast = iter.ast;
switch(ast->kind) {
case AST_CALL: {
} break;
case AST_CALL_END: {
} break;
}
}
*/
const unsigned AST_NODE_END = 128;
struct Ast_Iter {
Array<Ast *> stack;
Ast *ast;
Ast_Kind kind;
bool skip_children;
uint32_t visit_id;
uint32_t di;
};
uint32_t Ast_Iter_VisitIDGen;
Ast_Iter iterate_depth_first(Allocator *a, Ast *ast) {
assert(ast);
Ast_Iter result = {};
result.stack = {a};
result.stack.add(ast);
result.ast = ast;
result.kind = ast->kind;
result.visit_id = ++Ast_Iter_VisitIDGen;
void next(Ast_Iter * iter);
next(&result);
return result;
}
void next(Ast_Iter *iter) {
if (iter->stack.len <= 0) {
iter->ast = 0;
iter->kind = AST_NONE;
iter->stack.dealloc();
return;
}
Ast *ast = iter->stack.pop();
assert(ast != 0);
iter->di += 1;
iter->ast = ast;
iter->kind = ast->kind;
if (ast->visit_id == iter->visit_id) {
iter->kind = (Ast_Kind)((unsigned)iter->kind + 128);
}
Ast *ast = iter->ast;
ast->visit_id = iter->visit_id;
if (iter->skip_children) {
iter->skip_children = false;
goto end_of_switch;
}
switch (iter->kind) {
case AST_SCOPE: {
Ast_Scope *node = (Ast_Scope *)ast;
iter->stack.add(node);
For(node->stmts) iter->stack.add(it);
For(node->decls) iter->stack.add(it);
} break;
case AST_MODULE: invalid_codepath; break;
@@ -77,12 +55,15 @@ void next(Ast_Iter *iter) {
iter->stack.add(node);
iter->stack.add(node->index);
iter->stack.add(node->expr);
assert(node->index);
assert(node->expr);
} break;
case AST_UNARY: {
Ast_Unary *node = (Ast_Unary *)ast;
iter->stack.add(node);
iter->stack.add(node->expr);
assert(node->expr);
} break;
case AST_BINARY: {
@@ -90,14 +71,23 @@ void next(Ast_Iter *iter) {
iter->stack.add(node);
iter->stack.add(node->right);
iter->stack.add(node->left);
assert(node->right);
assert(node->left);
} break;
case AST_CALL_ITEM: {
Ast_Call_Item *node = (Ast_Call_Item *)ast;
iter->stack.add(node);
iter->stack.add(node->item);
if (node->call_flags & CALL_INDEX) iter->stack.add(node->index);
else if (node->call_flags & CALL_NAME) iter->stack.add(node->name);
assert(node->item);
if (node->call_flags & CALL_INDEX) {
iter->stack.add(node->index);
assert(node->index);
}
else if (node->call_flags & CALL_NAME) {
iter->stack.add(node->name);
assert(node->name);
}
} break;
case AST_COMPOUND:
@@ -105,7 +95,7 @@ void next(Ast_Iter *iter) {
Ast_Call *node = (Ast_Call *)ast;
iter->stack.add(node);
For(node->exprs) iter->stack.add(it);
iter->stack.add(node->name);
if (node->name) iter->stack.add(node->name);
} break;
case AST_TYPE_OF:
@@ -117,20 +107,23 @@ void next(Ast_Iter *iter) {
Ast_Builtin *node = (Ast_Builtin *)ast;
iter->stack.add(node);
iter->stack.add(node->expr);
assert(node->expr);
} break;
case AST_SWITCH: {
Ast_Switch *node = (Ast_Switch *)ast;
iter->stack.add(node);
iter->stack.add(node->default_scope);
if (node->default_scope) iter->stack.add(node->default_scope);
For(node->cases) iter->stack.add(it);
iter->stack.add(node->value);
assert(node->value);
} break;
case AST_SWITCH_CASE: {
Ast_Switch_Case *node = (Ast_Switch_Case *)ast;
iter->stack.add(node);
iter->stack.add(node->scope);
assert(node->scope);
For(node->labels) iter->stack.add(it);
} break;
@@ -138,6 +131,7 @@ void next(Ast_Iter *iter) {
Ast_Var_Unpack *node = (Ast_Var_Unpack *)ast;
iter->stack.add(node);
iter->stack.add(node->expr);
assert(node->expr);
For(node->vars) iter->stack.add(it);
} break;
@@ -168,13 +162,14 @@ void next(Ast_Iter *iter) {
case AST_ARRAY: {
Ast_Array *node = (Ast_Array *)ast;
iter->stack.add(node);
iter->stack.add(node->expr);
if (node->expr) iter->stack.add(node->expr);
} break;
case AST_FOR: {
Ast_For *node = (Ast_For *)ast;
iter->stack.add(node);
iter->stack.add(node->scope);
assert(node->scope);
if (node->iter) iter->stack.add(node->iter);
if (node->cond) iter->stack.add(node->cond);
if (node->init) iter->stack.add(node->init);
@@ -190,7 +185,8 @@ void next(Ast_Iter *iter) {
Ast_If_Node *node = (Ast_If_Node *)ast;
iter->stack.add(node);
iter->stack.add(node->scope);
iter->stack.add(node->expr);
assert(node->scope);
if (node->expr) iter->stack.add(node->expr);
if (node->init) iter->stack.add(node->init);
} break;
@@ -204,10 +200,27 @@ void next(Ast_Iter *iter) {
Ast_Lambda *node = (Ast_Lambda *)ast;
iter->stack.add(node);
iter->stack.add(node->scope);
assert(node->scope);
For(node->ret) iter->stack.add(it);
For(node->args) iter->stack.add(it);
} break;
}
end_of_switch:
if (iter->stack.len <= 0) {
iter->ast = 0;
iter->kind = AST_NONE;
iter->stack.dealloc();
return;
}
iter->ast = iter->stack.pop();
assert(iter->ast != 0);
iter->di += 1;
iter->kind = iter->ast->kind;
if (iter->ast->visit_id == iter->visit_id) {
iter->kind = (Ast_Kind)((unsigned)iter->kind + AST_NODE_END);
}
}
// We are not copying module and file Ast's