Fix not top level ast's getting into ordered list. Delete parent nodes
This commit is contained in:
39
ast.cpp
39
ast.cpp
@@ -47,6 +47,7 @@ enum{
|
|||||||
AST_ATOM = bit_flag(7),
|
AST_ATOM = bit_flag(7),
|
||||||
AST_FOREIGN = bit_flag(8),
|
AST_FOREIGN = bit_flag(8),
|
||||||
AST_DECL = bit_flag(9),
|
AST_DECL = bit_flag(9),
|
||||||
|
AST_PACKAGE_LEVEL = bit_flag(10),
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ast{
|
struct Ast{
|
||||||
@@ -54,7 +55,6 @@ struct Ast{
|
|||||||
Token *pos;
|
Token *pos;
|
||||||
|
|
||||||
Ast_Kind kind;
|
Ast_Kind kind;
|
||||||
Ast *parent;
|
|
||||||
Ast_Scope *parent_scope;
|
Ast_Scope *parent_scope;
|
||||||
Ast_Flag flags;
|
Ast_Flag flags;
|
||||||
};
|
};
|
||||||
@@ -281,8 +281,6 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
|||||||
result->op = op->kind;
|
result->op = op->kind;
|
||||||
result->left = left;
|
result->left = left;
|
||||||
result->right = right;
|
result->right = right;
|
||||||
result->left->parent = result;
|
|
||||||
if(result->right) result->right->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,8 +289,6 @@ ast_call(Token *pos, Ast_Expr *name, Array<Ast_Call_Item *> exprs){
|
|||||||
AST_NEW(Call, CALL, pos, AST_EXPR);
|
AST_NEW(Call, CALL, pos, AST_EXPR);
|
||||||
result->name = name;
|
result->name = name;
|
||||||
result->exprs = exprs.tight_copy(pctx->perm);
|
result->exprs = exprs.tight_copy(pctx->perm);
|
||||||
if(result->name) result->name->parent = result;
|
|
||||||
For(result->exprs) it->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,9 +298,6 @@ ast_call_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *item){
|
|||||||
result->name = name;
|
result->name = name;
|
||||||
result->index = index;
|
result->index = index;
|
||||||
result->item = item;
|
result->item = item;
|
||||||
if(result->name) result->name->parent = result;
|
|
||||||
if(result->index) result->index->parent = result;
|
|
||||||
item->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,8 +307,6 @@ ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){
|
|||||||
result->flags = AST_EXPR;
|
result->flags = AST_EXPR;
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
result->typespec = typespec;
|
result->typespec = typespec;
|
||||||
expr->parent = result;
|
|
||||||
typespec->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,7 +316,6 @@ ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
|
|||||||
result->flags = AST_EXPR;
|
result->flags = AST_EXPR;
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
result->op = op;
|
result->op = op;
|
||||||
expr->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,8 +325,6 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
|||||||
result->flags = AST_EXPR;
|
result->flags = AST_EXPR;
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
result->index = index;
|
result->index = index;
|
||||||
expr->parent = result;
|
|
||||||
index->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -350,9 +338,6 @@ ast_lambda(Token *pos, Array<Ast_Decl *> params, B32 has_var_args, Ast_Expr *ret
|
|||||||
result->has_var_args = has_var_args;
|
result->has_var_args = has_var_args;
|
||||||
if(!ret) result->ret = ast_ident(result->pos, intern_void);
|
if(!ret) result->ret = ast_ident(result->pos, intern_void);
|
||||||
|
|
||||||
if(result->scope) result->scope->parent = result;
|
|
||||||
result->ret->parent = result;
|
|
||||||
For(result->args) it->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,7 +345,6 @@ function Ast_If *
|
|||||||
ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
||||||
AST_NEW(If, IF, pos, AST_STMT);
|
AST_NEW(If, IF, pos, AST_STMT);
|
||||||
result->ifs = ifs.tight_copy(pctx->perm);
|
result->ifs = ifs.tight_copy(pctx->perm);
|
||||||
For(result->ifs) it->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,10 +355,6 @@ ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Scope *s
|
|||||||
result->cond = cond;
|
result->cond = cond;
|
||||||
result->iter = iter;
|
result->iter = iter;
|
||||||
result->scope = scope;
|
result->scope = scope;
|
||||||
if(result->init) result->init->parent = result;
|
|
||||||
if(result->cond) result->cond->parent = result;
|
|
||||||
if(result->iter) result->iter->parent = result;
|
|
||||||
result->scope->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,7 +370,6 @@ ast_return(Token *pos, Ast_Expr *expr){
|
|||||||
if(expr){
|
if(expr){
|
||||||
assert(is_flag_set(expr->flags, AST_EXPR));
|
assert(is_flag_set(expr->flags, AST_EXPR));
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
result->expr->parent = result;
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -401,11 +380,8 @@ ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Scope *scope){
|
|||||||
result->scope = scope;
|
result->scope = scope;
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
result->init = (Ast_Binary *)init;
|
result->init = (Ast_Binary *)init;
|
||||||
if(result->scope) result->scope->parent = result;
|
|
||||||
if(result->expr) result->expr->parent = result;
|
|
||||||
if(result->init) {
|
if(result->init) {
|
||||||
assert(init->kind == AST_VAR);
|
assert(init->kind == AST_VAR);
|
||||||
result->init->parent = result;
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -414,7 +390,6 @@ function Ast_Array *
|
|||||||
ast_array(Token *pos, Ast_Expr *expr){
|
ast_array(Token *pos, Ast_Expr *expr){
|
||||||
AST_NEW(Array, ARRAY, pos, AST_EXPR);
|
AST_NEW(Array, ARRAY, pos, AST_EXPR);
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
if(result->expr) result->expr->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,9 +405,6 @@ function void
|
|||||||
finalize_decl_scope(Ast_Scope *scope){
|
finalize_decl_scope(Ast_Scope *scope){
|
||||||
scope->decls = scope->decls.tight_copy(pctx->perm);
|
scope->decls = scope->decls.tight_copy(pctx->perm);
|
||||||
pctx->currently_parsed_scope = scope->parent_scope;
|
pctx->currently_parsed_scope = scope->parent_scope;
|
||||||
For(scope->decls){
|
|
||||||
it->parent = scope;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ast_Scope *
|
function Ast_Scope *
|
||||||
@@ -448,16 +420,12 @@ function void
|
|||||||
finalize_stmt_scope(Ast_Scope *scope){
|
finalize_stmt_scope(Ast_Scope *scope){
|
||||||
scope->stmts = scope->stmts.tight_copy(pctx->perm);
|
scope->stmts = scope->stmts.tight_copy(pctx->perm);
|
||||||
pctx->currently_parsed_scope = scope->parent_scope;
|
pctx->currently_parsed_scope = scope->parent_scope;
|
||||||
For(scope->stmts){
|
|
||||||
it->parent = scope;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ast_Decl *
|
function Ast_Decl *
|
||||||
ast_struct(Token *pos, Ast_Scope *scope){
|
ast_struct(Token *pos, Ast_Scope *scope){
|
||||||
AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE);
|
AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE);
|
||||||
result->scope = scope;
|
result->scope = scope;
|
||||||
result->scope->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -466,8 +434,6 @@ ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope){
|
|||||||
AST_NEW(Decl, ENUM, pos, AST_DECL | AST_AGGREGATE);
|
AST_NEW(Decl, ENUM, pos, AST_DECL | AST_AGGREGATE);
|
||||||
result->scope = scope;
|
result->scope = scope;
|
||||||
result->typespec = typespec;
|
result->typespec = typespec;
|
||||||
result->scope->parent = result;
|
|
||||||
if(result->typespec) result->typespec->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,8 +443,6 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
|
|||||||
result->name = name;
|
result->name = name;
|
||||||
result->typespec = typespec;
|
result->typespec = typespec;
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
if(result->typespec) result->typespec->parent = result;
|
|
||||||
if(result->expr) result->expr->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,7 +459,6 @@ ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
|
|||||||
AST_NEW(Decl, CONST, pos, AST_DECL);
|
AST_NEW(Decl, CONST, pos, AST_DECL);
|
||||||
result->expr = expr;
|
result->expr = expr;
|
||||||
result->name = name;
|
result->name = name;
|
||||||
if(result->expr) result->expr->parent = result;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -568,6 +568,7 @@ compile_files(Array<String> filename){
|
|||||||
Ast_Decl *decl = parse_decl(true);
|
Ast_Decl *decl = parse_decl(true);
|
||||||
if(!decl) break;
|
if(!decl) break;
|
||||||
|
|
||||||
|
set_flag(decl->flags, AST_PACKAGE_LEVEL);
|
||||||
if(decl->kind == AST_STRUCT){
|
if(decl->kind == AST_STRUCT){
|
||||||
decl->type = type_type;
|
decl->type = type_type;
|
||||||
decl->type_val = type_incomplete(decl);
|
decl->type_val = type_incomplete(decl);
|
||||||
|
|||||||
@@ -177,7 +177,6 @@ struct Parse_Ctx:Lexer{
|
|||||||
Map type_map;
|
Map type_map;
|
||||||
|
|
||||||
Ast_Scope *currently_parsed_scope;
|
Ast_Scope *currently_parsed_scope;
|
||||||
Array<Ast_Scope *> scopes;
|
|
||||||
Ast_Package *resolving_package;
|
Ast_Package *resolving_package;
|
||||||
Array<Ast_Decl *> ordered_decls;
|
Array<Ast_Decl *> ordered_decls;
|
||||||
|
|
||||||
@@ -222,7 +221,6 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator)
|
|||||||
ctx->perm = perm_allocator;
|
ctx->perm = perm_allocator;
|
||||||
ctx->heap = heap_allocator;
|
ctx->heap = heap_allocator;
|
||||||
ctx->gen = {ctx->perm};
|
ctx->gen = {ctx->perm};
|
||||||
ctx->scopes = {ctx->heap};
|
|
||||||
ctx->ordered_decls = {ctx->heap};
|
ctx->ordered_decls = {ctx->heap};
|
||||||
ctx->type_map = {ctx->heap};
|
ctx->type_map = {ctx->heap};
|
||||||
bigint_allocator = ctx->perm;
|
bigint_allocator = ctx->perm;
|
||||||
|
|||||||
@@ -15,5 +15,5 @@ val := CONSTANT_VAL
|
|||||||
DEPENDENCE :: CONSTANT_VAL
|
DEPENDENCE :: CONSTANT_VAL
|
||||||
CONSTANT_VAL :: 10
|
CONSTANT_VAL :: 10
|
||||||
|
|
||||||
thing: a_type = 10
|
global_thing: a_type = 10
|
||||||
|
|
||||||
|
|||||||
@@ -627,7 +627,7 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
case AST_LAMBDA:
|
case AST_LAMBDA:
|
||||||
case AST_VAR:
|
case AST_VAR:
|
||||||
CASE(CONST, Decl){
|
CASE(CONST, Decl){
|
||||||
resolve_decl(node, IS_NOT_PACKAGE_GLOBAL);
|
resolve_decl(node);
|
||||||
insert_into_scope(node->parent_scope, node);
|
insert_into_scope(node->parent_scope, node);
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
@@ -645,25 +645,21 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
resolve_expr(node->init, AST_CAN_BE_NULL);
|
||||||
resolve_expr(node->init, AST_CAN_BE_NULL);
|
resolve_and_require_bool("Conditional in a for loop condition", node->cond, AST_CAN_BE_NULL);
|
||||||
resolve_and_require_bool("Conditional in a for loop condition", node->cond, AST_CAN_BE_NULL);
|
resolve_expr(node->iter, AST_CAN_BE_NULL);
|
||||||
resolve_expr(node->iter, AST_CAN_BE_NULL);
|
For(node->scope->stmts)
|
||||||
For(node->scope->stmts)
|
resolve_stmt(it, ret);
|
||||||
resolve_stmt(it, ret);
|
|
||||||
}
|
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
|
|
||||||
CASE(IF, If){
|
CASE(IF, If){
|
||||||
For(node->ifs){
|
For(node->ifs){
|
||||||
resolve_stmt(it->init, ret);
|
resolve_stmt(it->init, ret);
|
||||||
{
|
// @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL
|
||||||
// @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL
|
resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL);
|
||||||
resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL);
|
For_It(it->scope->stmts, jt)
|
||||||
For_It(it->scope->stmts, jt)
|
resolve_stmt(jt, ret);
|
||||||
resolve_stmt(jt, ret);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
@@ -924,7 +920,7 @@ resolve_expr(Ast_Expr *ast, B32 flags){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
resolve_decl(Ast_Decl *ast, B32 flags){
|
resolve_decl(Ast_Decl *ast){
|
||||||
if(ast->state == DECL_RESOLVED){
|
if(ast->state == DECL_RESOLVED){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -973,7 +969,8 @@ resolve_decl(Ast_Decl *ast, B32 flags){
|
|||||||
|
|
||||||
result = operand_lambda(lambda_type);
|
result = operand_lambda(lambda_type);
|
||||||
}
|
}
|
||||||
else if(is_flag_set(lambda->parent->flags, AST_FOREIGN)){
|
else if(is_flag_set(lambda->flags, AST_FOREIGN)){
|
||||||
|
// @todo: Add foreign
|
||||||
result = operand_lambda(lambda_type);
|
result = operand_lambda(lambda_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1029,7 +1026,7 @@ resolve_decl(Ast_Decl *ast, B32 flags){
|
|||||||
}
|
}
|
||||||
ast->state = DECL_RESOLVED;
|
ast->state = DECL_RESOLVED;
|
||||||
|
|
||||||
if(!is_flag_set(flags, IS_NOT_PACKAGE_GLOBAL))
|
if(is_flag_set(ast->flags, AST_PACKAGE_LEVEL))
|
||||||
pctx->ordered_decls.add(ast);
|
pctx->ordered_decls.add(ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ struct Operand{
|
|||||||
|
|
||||||
enum{AST_CANT_BE_NULL = 0, AST_CAN_BE_NULL = 1, IS_NOT_PACKAGE_GLOBAL=2, RESOLVE_TYPESPEC_COMPLETE = bit_flag(3)};
|
enum{AST_CANT_BE_NULL = 0, AST_CAN_BE_NULL = 1, IS_NOT_PACKAGE_GLOBAL=2, RESOLVE_TYPESPEC_COMPLETE = bit_flag(3)};
|
||||||
function Operand resolve_expr(Ast_Expr *ast, B32 flags);
|
function Operand resolve_expr(Ast_Expr *ast, B32 flags);
|
||||||
function void resolve_decl(Ast_Decl *ast, B32 flags = 0);
|
function void resolve_decl(Ast_Decl *ast);
|
||||||
function Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name);
|
function Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name);
|
||||||
function Ast_Resolved_Type *resolve_typespec(Ast_Expr *ast, B32 ast_can_be_null);
|
function Ast_Resolved_Type *resolve_typespec(Ast_Expr *ast, B32 ast_can_be_null);
|
||||||
#if 0
|
#if 0
|
||||||
|
|||||||
Reference in New Issue
Block a user