Cleanup, FLAG32

This commit is contained in:
Krzosa Karol
2022-06-10 18:07:03 +02:00
parent ab663d0b01
commit 4149300bb9
3 changed files with 22 additions and 46 deletions

View File

@@ -17,4 +17,4 @@ CONSTANT_VAL :: 10
global_thing: a_type = 10 global_thing: a_type = 10
arena: order2.Arena // arena: order2.Arena

View File

@@ -513,14 +513,20 @@ _search_for_decl(Ast_Scope *scope, Intern_String name){
return 0; return 0;
} }
FLAG32(Search_Flag) {
SEARCH_ONLY_CURRENT_SCOPE = bit_flag(1),
SEARCH_ALSO_FOR_PACKAGE = bit_flag(2),
};
function Ast_Decl * function Ast_Decl *
search_for_decl(Ast_Scope *scope, Intern_String name){ search_for_decl(Ast_Scope *scope, Intern_String name, Search_Flag flags = 0){
Ast_Decl *result = 0; Ast_Decl *result = 0;
for(Ast_Scope *it = scope; it; it=it->parent_scope){ for(Ast_Scope *it = scope; it; it=it->parent_scope){
result = _search_for_decl(it, name); result = _search_for_decl(it, name);
if(result) break; if(result) break;
if(is_flag_set(flags, SEARCH_ONLY_CURRENT_SCOPE)) break;
} }
if(!result){ if(!result && is_flag_set(flags, SEARCH_ALSO_FOR_PACKAGE)){
For(pctx->packages){ For(pctx->packages){
if(name == it->name){ if(name == it->name){
result = (Ast_Decl *)it; result = (Ast_Decl *)it;
@@ -570,7 +576,7 @@ insert_builtin_types_into_package(Ast_Package *p){
} }
function Ast_Resolved_Type * function Ast_Resolved_Type *
resolve_typespec(Ast_Expr *ast, B32 flags){ resolve_typespec(Ast_Expr *ast, Resolve_Flag flags){
if(!ast && is_flag_set(flags, AST_CAN_BE_NULL)) if(!ast && is_flag_set(flags, AST_CAN_BE_NULL))
return 0; return 0;
@@ -583,7 +589,7 @@ resolve_typespec(Ast_Expr *ast, B32 flags){
} }
function Operand function Operand
resolve_and_require_bool(const char *error, Ast_Expr *expr, B32 flags){ resolve_and_require_bool(const char *error, Ast_Expr *expr, Resolve_Flag flags){
if(!expr){ if(!expr){
if(flags == AST_CAN_BE_NULL) if(flags == AST_CAN_BE_NULL)
return {}; return {};
@@ -599,7 +605,7 @@ resolve_and_require_bool(const char *error, Ast_Expr *expr, B32 flags){
} }
function Operand function Operand
require_const_int(Ast_Expr *expr, B32 flags){ require_const_int(Ast_Expr *expr, Resolve_Flag flags){
Operand op = resolve_expr(expr, flags); Operand op = resolve_expr(expr, flags);
if(expr == 0 && flags) if(expr == 0 && flags)
@@ -726,7 +732,7 @@ resolve_cast(Ast_Cast *node){
} }
function Operand function Operand
resolve_expr(Ast_Expr *ast, B32 flags){ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){
if(!ast && is_flag_set(flags, AST_CAN_BE_NULL)) return {}; if(!ast && is_flag_set(flags, AST_CAN_BE_NULL)) return {};
assert(is_flag_set(ast->flags, AST_EXPR)); assert(is_flag_set(ast->flags, AST_EXPR));
assert(ast->parent_scope->kind == AST_SCOPE || ast->parent_scope->kind == AST_PACKAGE); assert(ast->parent_scope->kind == AST_SCOPE || ast->parent_scope->kind == AST_PACKAGE);
@@ -740,7 +746,6 @@ resolve_expr(Ast_Expr *ast, B32 flags){
// Typespec array [32]int // Typespec array [32]int
CASE(ARRAY, Array){ CASE(ARRAY, Array){
// @todo: Arrays of inferred size []
Operand type = resolve_expr(node->base, AST_CANT_BE_NULL); Operand type = resolve_expr(node->base, AST_CANT_BE_NULL);
Operand expr = require_const_int(node->expr, AST_CAN_BE_NULL); Operand expr = require_const_int(node->expr, AST_CAN_BE_NULL);
if(type.type != type_type) compiler_error(node->pos, "Prefix array operator is only allowed on types"); if(type.type != type_type) compiler_error(node->pos, "Prefix array operator is only allowed on types");
@@ -923,13 +928,8 @@ resolve_expr(Ast_Expr *ast, B32 flags){
invalid_return; invalid_return;
} }
// @todo: I hate this, package is a scope so it cant
// be a DECL, I don't personally like this meme
function void function void
resolve_decl(Ast_Decl *ast){ resolve_decl(Ast_Decl *ast){
if(ast->kind == AST_PACKAGE){
return;
}
if(ast->state == DECL_RESOLVED){ if(ast->state == DECL_RESOLVED){
return; return;
} }

View File

@@ -5,42 +5,18 @@ struct Operand{
U8 is_lvalue: 1; U8 is_lvalue: 1;
}; };
enum{AST_CANT_BE_NULL = 0, AST_CAN_BE_NULL = 1, IS_NOT_PACKAGE_GLOBAL=2, RESOLVE_TYPESPEC_COMPLETE = bit_flag(3)}; FLAG32(Resolve_Flag){
function Operand resolve_expr(Ast_Expr *ast, B32 flags); AST_CANT_BE_NULL = bit_flag(0),
AST_CAN_BE_NULL = bit_flag(1),
RESOLVE_TYPESPEC_COMPLETE = bit_flag(2),
};
function Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags);
function void resolve_decl(Ast_Decl *ast); 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, Resolve_Flag flags);
#if 0 #if 0
//-----------------------------------------------------------------------------
// Symbols
//-----------------------------------------------------------------------------
enum Sym_Kind{
SYM_NONE,
SYM_CONST,
SYM_VAR,
};
enum Sym_State{
SYM_NOT_RESOLVED,
SYM_RESOLVING,
SYM_RESOLVED,
};
struct Sym{
Intern_String name;
Sym_Kind kind;
Sym_State state;
Ast *ast;
INLINE_VALUE_FIELDS;
};
function Ast_Resolved_Type *resolve_typespec(Ast_Expr *ast, B32 ast_can_be_null = AST_CANT_BE_NULL);
function Sym *resolve_name(Token *pos, Intern_String name);
function Operand resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *compound_required_type = 0, Sym *const_sym = 0);
function Operand resolve_binding(Ast *ast, Sym *sym = 0);
global Ast_Named empty_decl = {};
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Symbol constructors and utils // Symbol constructors and utils
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------