From 83130b130bc837870986f027b6602ce01aff63e8 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 12 Jun 2022 00:15:19 +0200 Subject: [PATCH] First draft for adding c string type --- G.globals.kl | 2 ++ ccodegen.cpp | 7 ++++++- main.cpp | 3 ++- typechecking.cpp | 8 +++++++- types.h | 10 ++++++++-- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/G.globals.kl b/G.globals.kl index 79e7ce5..8a5db57 100644 --- a/G.globals.kl +++ b/G.globals.kl @@ -28,6 +28,8 @@ imp_array := [5]S64{1,2} imp_array_c: [5]S64 = {[0] = 1, [2] = 2, [1] = 0} // @todo this should be illegal // without_size: []S64 = {} // @todo: this should be slice, converting from array should be implicit +string: char = "string" + //----------------------------------------------------------------------------- // Pointers //----------------------------------------------------------------------------- diff --git a/ccodegen.cpp b/ccodegen.cpp index 8bd8d97..642a332 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -90,7 +90,12 @@ gen_value(Value a){ const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10); gen("%s", string); }break; - CASE_STRING: gen("LIT(\"%s\")", a.intern_val.str); break; + case TYPE_CHAR: + gen("\"%s\"", a.intern_val.str); + break; + case TYPE_STRING: case TYPE_UNTYPED_STRING: + gen("LIT(\"%s\")", a.intern_val.str); + break; CASE_BOOL: a.bool_val ? gen("true"):gen("false"); break; CASE_FLOAT: gen("%f", a.f64_val); break; default: result = false; diff --git a/main.cpp b/main.cpp index 15f62ab..1253861 100644 --- a/main.cpp +++ b/main.cpp @@ -77,7 +77,7 @@ Expr: @todo -[ ] - Remodel compound from call to {} +[ ] - Should compound resolution use an algorithm to reorder compounds to initialize all fields in order [ ] - Switch [ ] - Add c string [ ] - Some way to take slice of data @@ -106,6 +106,7 @@ Expr: @donzo [x] - Scope +[x] - Remodel compound from call to {} [x] - Field access rewrite [-] - Constants embeded in structs should be able to refer to other constants in that namespace without prefix [-] - Order independent constants in structs diff --git a/typechecking.cpp b/typechecking.cpp index c29f0a1..0b3a743 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -372,6 +372,7 @@ insert_builtin_types_into_package(Ast_Package *p){ insert_type_into_package(p, "void"_s , type_void); insert_type_into_package(p, "Bool"_s , type_bool); insert_type_into_package(p, "String"_s, type_string); + insert_type_into_package(p, "char"_s, type_char); insert_type_into_package(p, "S8"_s, type_s8); insert_type_into_package(p, "S16"_s, type_s16); insert_type_into_package(p, "S32"_s, type_s32); @@ -614,7 +615,6 @@ resolve_compound_array(Ast_Call *node, Ast_Type *type){ function void resolve_compound_struct(Ast_Call *node, Ast_Type *type){ - // type->agg.members S64 default_counter = 0; For(node->exprs){ if(it->index) @@ -646,6 +646,8 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){ Operand item = resolve_expr(it->item, AST_CANT_BE_NULL); make_sure_value_is_compatible_with_type(it->pos, &item, item_type, TYPE_AND_EXPR_REQUIRED); } + + For(type->agg.members) it.visited = false; } function Operand @@ -709,6 +711,10 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ } CASE(VALUE, Atom){ + // @todo: More propagation of typed values to untyped values + if(compound_context && is_string(node->value.type) && is_string(compound_context)){ + node->value.type = compound_context; + } return operand_const_rvalue(node->value); BREAK(); } diff --git a/types.h b/types.h index 12994e6..8b92661 100644 --- a/types.h +++ b/types.h @@ -22,6 +22,7 @@ enum Ast_Type_Kind{ TYPE_POINTER, TYPE_BOOL, // LAST_NUMERIC TYPE_STRING, + TYPE_CHAR, TYPE_VOID, TYPE_ARRAY, TYPE_LAMBDA, @@ -45,7 +46,7 @@ enum Ast_Type_Kind{ #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 +#define CASE_STRING case TYPE_UNTYPED_STRING: case TYPE_STRING: case TYPE_CHAR #define CASE_UNTYPED case TYPE_UNTYPED_INT: case TYPE_UNTYPED_BOOL: case TYPE_UNTYPED_FLOAT: case TYPE_UNTYPED_STRING const char *type_names[] = { @@ -70,6 +71,7 @@ const char *type_names[] = { "[Float64]", "[Bool]", "[String]", + "[char]", "[void]", "[Pointer]", "[Array]", @@ -139,6 +141,7 @@ docname(Ast_Type *type){ case TYPE_F64: return "[Float64]"; case TYPE_BOOL: return "[Bool]"; case TYPE_STRING: return "[String]"; + case TYPE_CHAR: return "[char]"; case TYPE_VOID: return "[void]"; case TYPE_POINTER: return "[Pointer]"; case TYPE_ARRAY: return "[Array]"; @@ -158,6 +161,7 @@ name(Ast_Type *type){ 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"; case TYPE_S8: return "S8"; @@ -180,6 +184,7 @@ const SizeU pointer_size = sizeof(SizeU); const SizeU pointer_align = __alignof(SizeU); global Ast_Type type__void = {TYPE_VOID}; global Ast_Type type__string = {TYPE_STRING, sizeof(String), __alignof(String)}; +global Ast_Type type__char = {TYPE_CHAR, sizeof(char), __alignof(char)}; global Ast_Type type__bool = {TYPE_BOOL, sizeof(bool), __alignof(bool)}; global Ast_Type type__type = {TYPE_TYPE}; @@ -201,6 +206,7 @@ global Ast_Type type__untyped_int = {TYPE_UNTYPED_INT, sizeof(S64), __alignof(S6 global Ast_Type type__untyped_string = {TYPE_UNTYPED_STRING, sizeof(String), __alignof(String)}; global Ast_Type type__untyped_float = {TYPE_UNTYPED_FLOAT, sizeof(double), __alignof(double)}; +global Ast_Type *type_char = &type__char; global Ast_Type *type_type = &type__type; global Ast_Type *type_void = &type__void; global Ast_Type *type_string = &type__string; @@ -232,7 +238,7 @@ force_inline B32 is_lambda(Ast_Type *a){return a->kind == TYPE_LAMBDA;} force_inline B32 is_array(Ast_Type *a){return a->kind == TYPE_ARRAY;} 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_string(Ast_Type *a){return a->kind == TYPE_STRING || a->kind == TYPE_UNTYPED_STRING;} +force_inline B32 is_string(Ast_Type *a){return a->kind == TYPE_STRING || a->kind == TYPE_UNTYPED_STRING || a->kind == TYPE_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;}