From 0fcdd02c5b198d1b1ad4849d3633e05c6658ace6 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 11 Jun 2022 23:19:41 +0200 Subject: [PATCH] Rewritten field access, lose functionality but I'm fine with it, it's simpler now, might need to support casting --- ccodegen.cpp | 9 ++++-- globals.kl | 63 ------------------------------------------ main.cpp | 4 +-- typechecking.cpp | 72 +++++++++++++++++++----------------------------- typechecking.h | 10 +++---- 5 files changed, 42 insertions(+), 116 deletions(-) delete mode 100644 globals.kl diff --git a/ccodegen.cpp b/ccodegen.cpp index 9276bcc..8bd8d97 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -184,7 +184,8 @@ gen_expr(Ast_Expr *ast){ CASE(BINARY, Binary){ if(node->op == TK_Dot){ gen_expr(node->left); - if(node->type->kind == TYPE_POINTER) gen("->"); + if(!node->type) gen("__"); + else if(node->type->kind == TYPE_POINTER) gen("->"); else gen("."); gen_expr(node->right); return; @@ -426,7 +427,11 @@ compile_files(Array filename){ // by default it's name of the file // but if you add [package name] then it's overwritten Token *token = token_get(); - String filename_without_ext = string_chop_last_period(token->file.s); + String filename_without_ext = {}; + if(string_find(token->file.s, "."_s, MatchFlag_None, &filename_without_ext.len)){ + filename_without_ext.str = token->file.str; + } else filename_without_ext = token->file.s; + it.name = pctx->intern(filename_without_ext); if(token_is(SAME_SCOPE) && token_is_keyword(keyword_package, 1)){ diff --git a/globals.kl b/globals.kl deleted file mode 100644 index cf43c89..0000000 --- a/globals.kl +++ /dev/null @@ -1,63 +0,0 @@ -//----------------------------------------------------------------------------- -// Function types -//----------------------------------------------------------------------------- -test_function :: (thing: S64): *S64 -function_type: test_function -const_function_alias :: test_function -// null_function: (t: S64): *S64 = null - -//----------------------------------------------------------------------------- -// Booleans -//----------------------------------------------------------------------------- -Boolean: Bool = true - -//----------------------------------------------------------------------------- -// Nulls -//----------------------------------------------------------------------------- -// int_null: S64 = null -// str_null: String = null -// Bool_null: Bool = null - -//----------------------------------------------------------------------------- -// Compound expressions -//----------------------------------------------------------------------------- -array1: [4]S64 = {1,2,3,4} -imp_array := [5]S64{1,2} -// imp_array_a := [5]S64{1,2,3,4,5,6} -// imp_array_b: [5]S64 = {1,2,3,4,5,6} -imp_array_c: [5]S64 = {[0] = 1, [2] = 2, [1] = 0} // @todo this should be illegal - -//----------------------------------------------------------------------------- -// Pointers -//----------------------------------------------------------------------------- -pointer_decl : *S64 -variable_from_deref: S64 = *pointer_decl -pointer_from_var : *S64 = &variable_from_deref -Boolean_pointer := &Boolean - -//----------------------------------------------------------------------------- -// Implicit type -//----------------------------------------------------------------------------- -implicit_int :: 10 -implicit_str :: "Hello world" - -//----------------------------------------------------------------------------- -// Pointers -//----------------------------------------------------------------------------- -// pointer1: *S64 = 0 -// pointer2: *S64 = pointer1 -// pointer3: **S64 = 0 - -//----------------------------------------------------------------------------- -// String types -//----------------------------------------------------------------------------- -string1 :: "Test" -string2 :: string1 - -//----------------------------------------------------------------------------- -// Constant S64 variables -//----------------------------------------------------------------------------- -thing0 :: 10 -thing1 :: thing0 + 11 -thing2 :: thing1 + 20 -combin :: thing0 + thing1 + thing2 diff --git a/main.cpp b/main.cpp index 3bb49c0..ff90deb 100644 --- a/main.cpp +++ b/main.cpp @@ -207,8 +207,8 @@ int main(int argument_count, char **arguments){ // files.add("order1.kl"_s); // files.add("order2.kl"_s); // files.add("new_types.kl"_s); - // files.add("enums.kl"_s); - files.add("globals.kl"_s); + files.add("enums.kl"_s); + // files.add("G.globals.kl"_s); // files.add("euler.kl"_s); String result = compile_files(files); printf("%s", result.str); diff --git a/typechecking.cpp b/typechecking.cpp index 205c4e9..ff4e639 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -261,14 +261,14 @@ try_converting_untyped_to_typed(Operand *op){ } } -enum{ +FLAG32(Typecheck_Flag){ TYPE_AND_EXPR_REQUIRED = 0, TYPE_CAN_BE_NULL = 1, EXPR_CAN_BE_NULL = 2 }; function void -make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Type *type, U64 debug_flag){ +make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Type *type, Typecheck_Flag debug_flag){ if(type == expr->type){ assert(type); assert(expr->type); @@ -617,8 +617,6 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){ } -global Ast_Scope *field_access_scope; // @memes - function Operand resolve_field_access(Ast_Expr *node, Ast_Scope *context){ Ast_Binary *current = 0; @@ -629,37 +627,35 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){ node = current->left; } - Ast_Scope *new_context = 0; - if(!context && node->kind == AST_IDENT){ - auto ident = (Ast_Atom *)node; - Ast_Package *package = search_for_package(ident->intern_val); - if(package) { - new_context = package; - } + if(node->kind != AST_IDENT) + compiler_error(node->pos, "Required to be identifier in field access"); + + Ast_Atom *ident = (Ast_Atom *)node; + Ast_Scope *scope = context ? context : node->parent_scope; + Search_Flag flag = context ? SEARCH_ONLY_CURRENT_SCOPE : SEARCH_ALSO_FOR_PACKAGE; + Ast_Decl *decl = resolve_name(scope, node->pos, ident->intern_val, flag); + if(decl->kind == AST_PACKAGE){ + assert(next); + return resolve_field_access(next, (Ast_Scope *)decl); } - if(!new_context){ - field_access_scope = context; - Operand op = resolve_expr(node, AST_CANT_BE_NULL); + Ast_Type *type = decl->type; + if(type == type_type && is_enum(decl->type_val)) type = decl->type_val; + if(current) current->type = type; + if(is_pointer(type)) type = type->base; - Ast_Type *type = op.type; - if(op.type == type_type && is_enum(op.type_val)) type = op.type_val; - if(current) current->type = type; - if(is_pointer(type)) type = type->base; - type_complete(type); + type_complete(type); + if(next && !is_struct(type) && !is_enum(type)){ + compiler_error(node->pos, "Dot access"); + } - if(next && !is_struct(type) && !is_enum(type)){ - compiler_error(node->pos, "Dot access"); - } - - if(!next) - return op; - - new_context = ((Ast_Decl *)type->ast)->scope; + if(!next){ + Operand op = operand(decl); + return op; } assert(next); - return resolve_field_access(next, new_context); + return resolve_field_access(next, ((Ast_Decl *)type->ast)->scope); } function Operand @@ -671,24 +667,12 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ switch(ast->kind){ CASE(IDENT, Atom){ - Ast_Scope *scope = node->parent_scope; - Search_Flag flags = 0; - if(field_access_scope){ - set_flag(flags, SEARCH_ONLY_CURRENT_SCOPE); - scope = field_access_scope; - } - Ast_Decl *decl = resolve_name(scope, node->pos, node->intern_val, flags); - field_access_scope = 0; // @memes + node->resolved_decl = resolve_name(node->parent_scope, node->pos, node->intern_val); - node->resolved_decl = decl; - Operand result = operand(decl); - if(decl->kind != AST_VAR){ - if(decl->kind == AST_CONST){ - rewrite_into_const(node, Ast_Atom, decl->value); - } - result.is_const = 1; + Operand result = operand(node->resolved_decl); + if(node->resolved_decl->kind == AST_CONST){ + rewrite_into_const(node, Ast_Atom, node->resolved_decl->value); } - return result; BREAK(); } diff --git a/typechecking.h b/typechecking.h index 6b1837f..7201290 100644 --- a/typechecking.h +++ b/typechecking.h @@ -24,12 +24,12 @@ function Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_Stri // Operands //----------------------------------------------------------------------------- function Operand -operand(Ast_Decl *sym){ +operand(Ast_Decl *decl){ Operand result = {}; - result.type = sym->type; - result.is_const = sym->kind == AST_CONST ? true : false; - result.is_lvalue= sym->kind == AST_CONST ? false : true; // Cant assign to const values - result.value = sym->value; + result.type = decl->type; + result.is_const = decl->kind != AST_VAR ? true : false; + result.is_lvalue= decl->kind == AST_CONST ? false : true; // Cant assign to const values + result.value = decl->value; return result; }