diff --git a/ast.cpp b/ast.cpp index 873eda9..028d9df 100644 --- a/ast.cpp +++ b/ast.cpp @@ -47,6 +47,7 @@ enum{ AST_ATOM = bit_flag(7), AST_FOREIGN = bit_flag(8), AST_DECL = bit_flag(9), + AST_PACKAGE_LEVEL = bit_flag(10), }; struct Ast{ @@ -54,7 +55,6 @@ struct Ast{ Token *pos; Ast_Kind kind; - Ast *parent; Ast_Scope *parent_scope; Ast_Flag flags; }; @@ -281,8 +281,6 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){ result->op = op->kind; result->left = left; result->right = right; - result->left->parent = result; - if(result->right) result->right->parent = result; return result; } @@ -291,8 +289,6 @@ ast_call(Token *pos, Ast_Expr *name, Array exprs){ AST_NEW(Call, CALL, pos, AST_EXPR); result->name = name; result->exprs = exprs.tight_copy(pctx->perm); - if(result->name) result->name->parent = result; - For(result->exprs) it->parent = 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->index = index; result->item = item; - if(result->name) result->name->parent = result; - if(result->index) result->index->parent = result; - item->parent = result; return result; } @@ -314,8 +307,6 @@ ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){ result->flags = AST_EXPR; result->expr = expr; result->typespec = typespec; - expr->parent = result; - typespec->parent = result; return result; } @@ -325,7 +316,6 @@ ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){ result->flags = AST_EXPR; result->expr = expr; result->op = op; - expr->parent = result; return result; } @@ -335,8 +325,6 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){ result->flags = AST_EXPR; result->expr = expr; result->index = index; - expr->parent = result; - index->parent = result; return result; } @@ -350,9 +338,6 @@ ast_lambda(Token *pos, Array params, B32 has_var_args, Ast_Expr *ret result->has_var_args = has_var_args; 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; } @@ -360,7 +345,6 @@ function Ast_If * ast_if(Token *pos, Array ifs){ AST_NEW(If, IF, pos, AST_STMT); result->ifs = ifs.tight_copy(pctx->perm); - For(result->ifs) it->parent = 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->iter = iter; 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; } @@ -390,7 +370,6 @@ ast_return(Token *pos, Ast_Expr *expr){ if(expr){ assert(is_flag_set(expr->flags, AST_EXPR)); result->expr = expr; - result->expr->parent = 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->expr = expr; result->init = (Ast_Binary *)init; - if(result->scope) result->scope->parent = result; - if(result->expr) result->expr->parent = result; if(result->init) { assert(init->kind == AST_VAR); - result->init->parent = result; } return result; } @@ -414,7 +390,6 @@ function Ast_Array * ast_array(Token *pos, Ast_Expr *expr){ AST_NEW(Array, ARRAY, pos, AST_EXPR); result->expr = expr; - if(result->expr) result->expr->parent = result; return result; } @@ -430,9 +405,6 @@ function void finalize_decl_scope(Ast_Scope *scope){ scope->decls = scope->decls.tight_copy(pctx->perm); pctx->currently_parsed_scope = scope->parent_scope; - For(scope->decls){ - it->parent = scope; - } } function Ast_Scope * @@ -448,16 +420,12 @@ function void finalize_stmt_scope(Ast_Scope *scope){ scope->stmts = scope->stmts.tight_copy(pctx->perm); pctx->currently_parsed_scope = scope->parent_scope; - For(scope->stmts){ - it->parent = scope; - } } function Ast_Decl * ast_struct(Token *pos, Ast_Scope *scope){ AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE); result->scope = scope; - result->scope->parent = 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); result->scope = scope; result->typespec = typespec; - result->scope->parent = result; - if(result->typespec) result->typespec->parent = result; return result; } @@ -477,8 +443,6 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){ result->name = name; result->typespec = typespec; result->expr = expr; - if(result->typespec) result->typespec->parent = result; - if(result->expr) result->expr->parent = result; return result; } @@ -495,7 +459,6 @@ ast_const(Token *pos, Intern_String name, Ast_Expr *expr){ AST_NEW(Decl, CONST, pos, AST_DECL); result->expr = expr; result->name = name; - if(result->expr) result->expr->parent = result; return result; } diff --git a/ccodegen.cpp b/ccodegen.cpp index 47df6db..83aaf88 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -568,6 +568,7 @@ compile_files(Array filename){ Ast_Decl *decl = parse_decl(true); if(!decl) break; + set_flag(decl->flags, AST_PACKAGE_LEVEL); if(decl->kind == AST_STRUCT){ decl->type = type_type; decl->type_val = type_incomplete(decl); diff --git a/compiler.h b/compiler.h index 73b9412..293c405 100644 --- a/compiler.h +++ b/compiler.h @@ -177,7 +177,6 @@ struct Parse_Ctx:Lexer{ Map type_map; Ast_Scope *currently_parsed_scope; - Array scopes; Ast_Package *resolving_package; Array ordered_decls; @@ -222,7 +221,6 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator) ctx->perm = perm_allocator; ctx->heap = heap_allocator; ctx->gen = {ctx->perm}; - ctx->scopes = {ctx->heap}; ctx->ordered_decls = {ctx->heap}; ctx->type_map = {ctx->heap}; bigint_allocator = ctx->perm; diff --git a/order1.kl b/order1.kl index 179c512..82d4db4 100644 --- a/order1.kl +++ b/order1.kl @@ -15,5 +15,5 @@ val := CONSTANT_VAL DEPENDENCE :: CONSTANT_VAL CONSTANT_VAL :: 10 -thing: a_type = 10 +global_thing: a_type = 10 diff --git a/typechecking.cpp b/typechecking.cpp index 2501b51..994a3bc 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -627,7 +627,7 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ case AST_LAMBDA: case AST_VAR: CASE(CONST, Decl){ - resolve_decl(node, IS_NOT_PACKAGE_GLOBAL); + resolve_decl(node); insert_into_scope(node->parent_scope, node); BREAK(); } @@ -645,25 +645,21 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ } } - { - 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_expr(node->iter, AST_CAN_BE_NULL); - For(node->scope->stmts) - resolve_stmt(it, ret); - } + 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_expr(node->iter, AST_CAN_BE_NULL); + For(node->scope->stmts) + resolve_stmt(it, ret); BREAK(); } CASE(IF, If){ For(node->ifs){ resolve_stmt(it->init, ret); - { - // @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); - For_It(it->scope->stmts, jt) - resolve_stmt(jt, ret); - } + // @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); + For_It(it->scope->stmts, jt) + resolve_stmt(jt, ret); } BREAK(); } @@ -924,7 +920,7 @@ resolve_expr(Ast_Expr *ast, B32 flags){ } function void -resolve_decl(Ast_Decl *ast, B32 flags){ +resolve_decl(Ast_Decl *ast){ if(ast->state == DECL_RESOLVED){ return; } @@ -973,7 +969,8 @@ resolve_decl(Ast_Decl *ast, B32 flags){ 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); } @@ -1029,7 +1026,7 @@ resolve_decl(Ast_Decl *ast, B32 flags){ } 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); } diff --git a/typechecking.h b/typechecking.h index f020519..595fa5c 100644 --- a/typechecking.h +++ b/typechecking.h @@ -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)}; 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_Resolved_Type *resolve_typespec(Ast_Expr *ast, B32 ast_can_be_null); #if 0