From d41da94c80a88859850e1023f42f2cd21fecdf6c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 9 Feb 2023 20:17:48 +0100 Subject: [PATCH] Add string as struct, Fix void pointer indexing compound for String is not working but maybe that's fine --- core_codegen_c_language.cpp | 35 +++++++++++++++-------------------- core_compiler.cpp | 14 +++++++++++--- core_main.cpp | 20 +------------------- core_typechecking.cpp | 3 +++ core_types.cpp | 3 +-- core_types.h | 2 +- 6 files changed, 32 insertions(+), 45 deletions(-) diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 480f97d..3020026 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -226,18 +226,6 @@ gen_value(Token *pos, Value a){ gen("0x%llx", pointer_value); } }break; - case TYPE_STRING:{ - int length = 0; - gen("(%QString){(uint8_t *)\"", pctx->symbol_prefix); - for(int i = 0; i < a.intern_val.len; i++){ - if(a.intern_val.str[i] == '\n'){length += 2; gen("\\n");} - else if(a.intern_val.str[i] == '\r'){length += 2; gen("\\r");} - else{length += 1; gen("%c", a.intern_val.str[i]);} - - } - gen("\", %d}", length); - - }break; CASE_BOOL: { a.bool_val ? gen("true"):gen("false"); }break; @@ -248,7 +236,20 @@ gen_value(Token *pos, Value a){ case TYPE_TYPE: { gen("%d", a.type_val->type_id); }break; - default: result = false; + default: { + if (is_string(type)) { + int length = 0; + gen("(%QString){(uint8_t *)\"", pctx->symbol_prefix); + for(int i = 0; i < a.intern_val.len; i++){ + if(a.intern_val.str[i] == '\n'){length += 2; gen("\\n");} + else if(a.intern_val.str[i] == '\r'){length += 2; gen("\\r");} + else{length += 1; gen("%c", a.intern_val.str[i]);} + + } + gen("\", %d}", length); + } + else result = false; + } } return result; } @@ -383,7 +384,7 @@ gen_expr(Ast_Expr *ast){ gen("("); gen_expr(node->expr); if(node->index_original_type == pctx->type_string || node->index_original_type == pctx->untyped_string){ - gen(".str"); + gen(".data"); } else if(is_slice(node->index_original_type)){ gen(".data"); @@ -833,12 +834,6 @@ compile_to_c_code(){ #define BufferSize(x) (sizeof(x)/sizeof((x)[0])) -typedef struct String{ - uint8_t *str; - int64_t len; -}String; - - )"); // Generate struct forward decls diff --git a/core_compiler.cpp b/core_compiler.cpp index e663d98..308c10b 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -95,7 +95,7 @@ pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1}; // Init types pctx->type__void = {TYPE_VOID}; - pctx->type__string = {TYPE_STRING, sizeof(String), __alignof(String)}; + // pctx->type__string = {TYPE_STRING, sizeof(String), __alignof(String)}; pctx->type__bool = {TYPE_BOOL, sizeof(bool), __alignof(bool)}; pctx->type__type = {TYPE_TYPE, sizeof(S64), __alignof(S64)}; @@ -127,7 +127,7 @@ pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1}; //pctx->type_any; // Needs to be inited at runtime pctx->type_type = &pctx->type__type; - pctx->type_string = &pctx->type__string; + // pctx->type_string = &pctx->type__string; pctx->type_bool = &pctx->type__bool; pctx->type_f32 = &pctx->type__f32; @@ -298,6 +298,10 @@ Any :: struct data: *void type: Type +String :: struct + data: *U8 + len: S64 + Type_Info_Kind :: enum S64 // FIRST_NUMERIC S32 @@ -369,7 +373,7 @@ GetTypeInfo :: (type: Type): *Type_Info insert_builtin_type_into_scope(module, "F32"_s, pctx->type_f32); insert_builtin_type_into_scope(module, "void"_s, pctx->type_void); insert_builtin_type_into_scope(module, "Bool"_s, pctx->type_bool); - insert_builtin_type_into_scope(module, "String"_s, pctx->type_string); + // insert_builtin_type_into_scope(module, "String"_s, pctx->type_string); insert_builtin_type_into_scope(module, "Type"_s, pctx->type_type); } @@ -411,6 +415,10 @@ GetTypeInfo :: (type: Type): *Type_Info Ast_Decl *any_decl = search_for_single_decl(module, pctx->intern("Any"_s)); assert(any_decl->type == pctx->type_type); pctx->type_any = any_decl->type_val; + + Ast_Decl *string_decl = search_for_single_decl(module, pctx->intern("String"_s)); + assert(string_decl->type == pctx->type_type); + pctx->type_string = string_decl->type_val; } Ast_Module *module = add_module(0, pctx->intern(filename), true); diff --git a/core_main.cpp b/core_main.cpp index 87e09d6..da3a321 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -23,12 +23,7 @@ Fix backlog - [ ] Fix invalid error message: AlmostLinearToSRGB :: (a: Color);; return {sqrtf(a.r), sqrtf(a.g), sqrtf(a.b), a.a} -- [ ] Fix untyped literal going to codegen stage, example in arms_race - [ ] Fix and decide what to do when initializing global variable using not constants C:/AProgramming/cparse/compiler/modules/Language.core:180:28: error: initializer element is not a compile-time constant -- [ ] Fix adressing void is possible, mayp make it possible to address void - using bytes - * a.void + 10 - * a.void[10] - [ ] Test and bulletproof any, slices - [ ] Need tests that make sure stuff errors out - [ ] String declaration in Language.core this way we can compound create it and access fields and stuff @@ -36,14 +31,7 @@ Fix backlog result := String{data, filesize} - [ ] Fix tuple being able to colapse into a single variable - [ ] Fix Found multiple definitions of not showing multiple definitions in the error message -- [ ] Fix being able to cast -1 to *void - [ ] How to cast from U64 to pointer? -- [ ] Fix returning literlas using multiple arguments ``` -C:/AProgramming/cparse/compiler/examples/arms_race/arms_race.core:141:35: error: cannot take the address of an rvalue of type 'int' - MemoryCopy(&S10_S11_S44_139.m0, &(0x0), sizeof(S10_S11_S44_139.m0)); - ^ ~~~ -C:/AProgramming/cparse/compiler/examples/arms_race/arms_race.core:142:35: error: cannot take the address of an rvalue of type 'unsigned long long' - MemoryCopy(&S10_S11_S44_139.m1, &(32ULL), sizeof(S10_S11_S44_139.m1)); ``` Future features @@ -59,17 +47,11 @@ Future features ### -- [ ] Maybe wait to implement WASM / bytecode emitter before doing this? - Probably need some UInt Int types and those should be default, target - decides the size of these - - [ ] Way to import and force evaluate #import_lazy #import ? Redesign: - [ ] Redesign Type map to use List and reduce wasting space -- [ ] Redesign lexing to minimize memory usage, we got rid of heap but in a naive way! -- [ ] Probably need to move all the global data into the context if we want to use this as library or not? -- [ ] Casting +- [ ] Casting syntax In the future diff --git a/core_typechecking.cpp b/core_typechecking.cpp index c041a53..e749adc 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1190,6 +1190,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str if(left.type == pctx->type_pointer_to_char){ node->resolved_type = pctx->type_char; } + if (left.type == pctx->type_pointer_to_void){ + compiler_error(node->pos, "Trying to index a void pointer is invalid"); + } else if(is_string(left.type)) { // @warning: not sure about this context thing here. diff --git a/core_types.cpp b/core_types.cpp index 06be3cf..dc5b370 100644 --- a/core_types.cpp +++ b/core_types.cpp @@ -4,7 +4,6 @@ get_name_of_type(Ast_Type *type){ switch(type->kind){ case TYPE_VOID: return "void"; case TYPE_BOOL: return "Bool"; - case TYPE_STRING: return "String"; case TYPE_CHAR: return "char"; case TYPE_F32: return "F32"; case TYPE_F64: return "F64"; @@ -37,7 +36,7 @@ force_inline B32 is_enum(Ast_Type *a){return a->kind == TYPE_ENUM;} force_inline B32 is_pointer(Ast_Type *a){return a->kind == TYPE_POINTER;} force_inline B32 is_void(Ast_Type *a){return a->kind == TYPE_VOID;} force_inline B32 is_void_pointer(Ast_Type *a){return a == pctx->type_pointer_to_void;} -force_inline B32 is_string(Ast_Type *a){return a->kind == TYPE_STRING || a->kind == TYPE_UNTYPED_STRING || a == pctx->type_pointer_to_char;} +force_inline B32 is_string(Ast_Type *a){return a == pctx->type_string || a->kind == TYPE_UNTYPED_STRING || a == pctx->type_pointer_to_char;} force_inline B32 is_untyped_int(Ast_Type *a){return a->kind == TYPE_UNTYPED_INT;} force_inline B32 is_typed_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8);} force_inline B32 is_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT;} diff --git a/core_types.h b/core_types.h index b3a7cc1..4f561fa 100644 --- a/core_types.h +++ b/core_types.h @@ -8,6 +8,6 @@ #define CASE_INT case TYPE_UNTYPED_INT: CASE_SINT: CASE_UINT #define CASE_BOOL case TYPE_UNTYPED_BOOL: case TYPE_BOOL #define CASE_FLOAT case TYPE_UNTYPED_FLOAT: case TYPE_F32: case TYPE_F64 -#define CASE_STRING case TYPE_UNTYPED_STRING: case TYPE_STRING: case TYPE_POINTER +#define CASE_STRING case TYPE_UNTYPED_STRING: case TYPE_STRUCT: case TYPE_POINTER #define CASE_UNTYPED case TYPE_UNTYPED_INT: case TYPE_UNTYPED_BOOL: case TYPE_UNTYPED_FLOAT: case TYPE_UNTYPED_STRING #define ARRAY_SIZE_SLICE (-1)