diff --git a/typechecking.cpp b/typechecking.cpp index e464e70..d9b8cb3 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -311,26 +311,6 @@ _rewrite_into_const(Ast *node, U64 ast_size, Value value){ } #if 0 - -function Operand -require_const_int(Ast_Expr *expr, B32 ast_can_be_null){ - Operand op = resolve_expr(expr); - - if(expr == 0 && ast_can_be_null) - return op; - else if(expr == 0) - compiler_error(expr->pos, "This field cannot be null"); - - if(!op.is_const) - compiler_error(expr->pos, "Expected a const value"); - if(!is_int(op.type)) - compiler_error(expr->pos, "Expected a constant integer got instead %s", docname(op.type)); - - return op; -} - - - function Operand resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_resolve){ if(!ast) return {}; // @todo: add option for better error prevention @@ -339,63 +319,8 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res switch(ast->kind){ - CASE(UNARY, Unary){ - Operand value = resolve_expr(node->expr); - switch(node->op){ - case TK_Pointer:{ - if(value.type->kind == TYPE_POINTER){return operand_lvalue(value.type->base);} - else if(value.type->kind == TYPE_TYPE){ - Ast_Resolved_Type *type = type_pointer(value.type_val); - sym_type(type, node); - return operand_type(type); - } - else{ compiler_error(node->pos, "Dereferencing expression %s that is not a [Pointer] or [Type]", type_names[value.type->kind]); return {}; } - }break; - case TK_Dereference:{ - return operand_lvalue(type_pointer(value.type)); - }break; - default:{ - Operand op = resolve_expr(node->expr); - eval_unary(node->pos, node->op, &op); - if(op.is_const){ - rewrite_into_const(node, Ast_Unary, op.value); - return operand_const_rvalue(op.value); - } - return operand_rvalue(op.value.type); - }break; - } - - BREAK(); - } - CASE(BINARY, Binary){ Operand result = {}; - if(node->op == TK_ColonAssign){ - // @note: This is actually a statement so it doesn't need to return Operand - assert(is_flag_set(node->flags, AST_STMT)); - assert(node->left->kind == AST_IDENT); - - Operand right = resolve_expr(node->right); - make_sure_value_is_compatible_with_type(node->pos, &right, 0, TYPE_CAN_BE_NULL); - assert(right.type); - - auto atom = (Ast_Atom *)node->left; - sym_var(atom->intern_val, right, node, INSERT_INTO_SCOPE); - } - //----------------------------------------------------------------------------- - - //----------------------------------------------------------------------------- - else if(token_is_assign(node->op)){ - assert(is_flag_set(node->flags, AST_STMT)); - Operand left = resolve_expr(node->left); - if(!left.is_lvalue) compiler_error(node->pos, "Assigning to rvalue"); - Operand right = resolve_expr(node->right); - - right.value = convert_untyped_to_typed(node->pos, right.value, left.type); - if(left.type != right.type) compiler_error(node->pos, "Can't assign value when left is %s and right is %s", docname(left.type), docname(right.type)); - } - //----------------------------------------------------------------------------- - //----------------------------------------------------------------------------- else if(node->op == TK_Dot){ B32 required_to_be_const = false; @@ -482,24 +407,6 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res invalid_codepath; } } - } - //----------------------------------------------------------------------------- - - //----------------------------------------------------------------------------- - - //----------------------------------------------------------------------------- - - //----------------------------------------------------------------------------- - - return result; - BREAK(); - } - - invalid_default_case; - } - - invalid_return; -} #endif diff --git a/typechecking.h b/typechecking.h index 2d6bedc..800e793 100644 --- a/typechecking.h +++ b/typechecking.h @@ -16,127 +16,6 @@ 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, Resolve_Flag flags); -#if 0 -//----------------------------------------------------------------------------- -// Symbol constructors and utils -//----------------------------------------------------------------------------- -function void -sym_insert(Sym *sym){ - U64 hash = hash_string(sym->name.s); - Sym *is_sym = (Sym *)map_get(&pctx->syms, hash); - if(is_sym) compiler_error(sym->ast->pos, "Symbol with name: [%s] defined multiple times", sym->name.s.str); - if(pctx->scope > 0) pctx->local_syms.add(sym); - map_insert(&pctx->syms, hash, sym); -} - -function Sym * -sym_get(Intern_String name){ - Sym *result = (Sym *)map_get(&pctx->syms, hash_string(name.s)); - return result; -} - -function S64 -scope_open(){ - S64 local_sym_count = pctx->local_syms.len; - pctx->scope++; - return local_sym_count; -} - -function void -scope_close(S64 local_sym_count){ - pctx->scope--; - assert(pctx->scope >= 0); - for(S64 i = local_sym_count; i < pctx->local_syms.len; i++){ - Sym *it = pctx->local_syms.data[i]; - void *removed = map_remove(&pctx->syms, hash_string(it->name.s)); - assert(removed); - } - pctx->local_syms.len = local_sym_count; -} - -function void -sym_associate(Ast *ast, Sym *sym){ - assert(ast); - assert(sym); - map_insert(&pctx->resolved, ast, sym); -} - -function Sym * -sym_new(Sym_Kind kind, Intern_String name, Ast *ast, B32 associate = true){ - Sym *result = exp_alloc_type(pctx->perm, Sym, AF_ZeroMemory); - result->name = name; - result->kind = kind; - result->ast = ast; - if(associate) sym_associate(ast, result); - return result; -} - -function Sym * -sym_new_resolved(Sym_Kind kind, Intern_String name, Value value, Ast *ast, B32 associate = true){ - Sym *result = sym_new(kind, name, ast, associate); - result->state = SYM_RESOLVED; - result->value = value; - return result; -} - -const B32 INSERT_INTO_SCOPE = true; - -function Sym * -sym_var(Intern_String name, Ast_Resolved_Type *type, Ast *ast, B32 insert_into_scope = false){ - Value value; - value.type = type; - Sym *sym = sym_new_resolved(SYM_VAR, name, value, ast); - if(insert_into_scope) sym_insert(sym); - return sym; -} - -function Sym * -sym_var(Intern_String name, Operand op, Ast *ast, B32 insert_into_scope = false){ - Sym *sym = sym_new_resolved(SYM_VAR, name, op.value, ast); - if(insert_into_scope) sym_insert(sym); - return sym; -} - -function Sym * -sym_const(Intern_String name, Operand op, Ast *ast, B32 insert_into_scope = false){ - Sym *sym = sym_new_resolved(SYM_CONST, name, op.value, ast); - if(insert_into_scope) sym_insert(sym); - return sym; -} - -function Sym * -resolved_get(Ast *ast){ - Sym *result = (Sym *)map_get(&pctx->resolved, ast); - assert(result); - return result; -} - -function Ast_Resolved_Type * -resolved_type_get(Ast_Expr *ast){ - Sym *result = resolved_get(ast); - assert(result->type == type_type); - assert(result->type); - return result->type_val; -} - -function Sym * -sym_type(Ast_Resolved_Type *type, Ast *ast, Intern_String name = {}, B32 associate = true){ - Value value; - value.type = type_type; - value.type_val = type; - Sym *result = sym_new_resolved(SYM_CONST, name, value, ast, associate); - return result; -} - -function Sym * -sym_insert(Sym_Kind kind, Intern_String name, Value value, Ast *ast){ - Sym *sym = sym_new_resolved(kind, name, value, ast); - sym_insert(sym); - return sym; -} - -#endif - //----------------------------------------------------------------------------- // Operands //-----------------------------------------------------------------------------