Add string as struct, Fix void pointer indexing

compound for String is not working but maybe that's fine
This commit is contained in:
Krzosa Karol
2023-02-09 20:17:48 +01:00
parent 5e8b3739af
commit d41da94c80
6 changed files with 32 additions and 45 deletions

View File

@@ -226,18 +226,6 @@ gen_value(Token *pos, Value a){
gen("0x%llx", pointer_value); gen("0x%llx", pointer_value);
} }
}break; }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: { CASE_BOOL: {
a.bool_val ? gen("true"):gen("false"); a.bool_val ? gen("true"):gen("false");
}break; }break;
@@ -248,7 +236,20 @@ gen_value(Token *pos, Value a){
case TYPE_TYPE: { case TYPE_TYPE: {
gen("%d", a.type_val->type_id); gen("%d", a.type_val->type_id);
}break; }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; return result;
} }
@@ -383,7 +384,7 @@ gen_expr(Ast_Expr *ast){
gen("("); gen("(");
gen_expr(node->expr); gen_expr(node->expr);
if(node->index_original_type == pctx->type_string || node->index_original_type == pctx->untyped_string){ 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)){ else if(is_slice(node->index_original_type)){
gen(".data"); gen(".data");
@@ -833,12 +834,6 @@ compile_to_c_code(){
#define BufferSize(x) (sizeof(x)/sizeof((x)[0])) #define BufferSize(x) (sizeof(x)/sizeof((x)[0]))
typedef struct String{
uint8_t *str;
int64_t len;
}String;
)"); )");
// Generate struct forward decls // Generate struct forward decls

View File

@@ -95,7 +95,7 @@ pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
// Init types // Init types
pctx->type__void = {TYPE_VOID}; 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__bool = {TYPE_BOOL, sizeof(bool), __alignof(bool)};
pctx->type__type = {TYPE_TYPE, sizeof(S64), __alignof(S64)}; 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_any; // Needs to be inited at runtime
pctx->type_type = &pctx->type__type; 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_bool = &pctx->type__bool;
pctx->type_f32 = &pctx->type__f32; pctx->type_f32 = &pctx->type__f32;
@@ -298,6 +298,10 @@ Any :: struct
data: *void data: *void
type: Type type: Type
String :: struct
data: *U8
len: S64
Type_Info_Kind :: enum Type_Info_Kind :: enum
S64 // FIRST_NUMERIC S64 // FIRST_NUMERIC
S32 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, "F32"_s, pctx->type_f32);
insert_builtin_type_into_scope(module, "void"_s, pctx->type_void); 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, "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); 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)); Ast_Decl *any_decl = search_for_single_decl(module, pctx->intern("Any"_s));
assert(any_decl->type == pctx->type_type); assert(any_decl->type == pctx->type_type);
pctx->type_any = any_decl->type_val; 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); Ast_Module *module = add_module(0, pctx->intern(filename), true);

View File

@@ -23,12 +23,7 @@
Fix backlog Fix backlog
- [ ] Fix invalid error message: AlmostLinearToSRGB :: (a: Color);; return {sqrtf(a.r), sqrtf(a.g), sqrtf(a.b), a.a} - [ ] 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 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 - [ ] Test and bulletproof any, slices
- [ ] Need tests that make sure stuff errors out - [ ] 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 - [ ] 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} result := String{data, filesize}
- [ ] Fix tuple being able to colapse into a single variable - [ ] Fix tuple being able to colapse into a single variable
- [ ] Fix Found multiple definitions of not showing multiple definitions in the error message - [ ] 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? - [ ] 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 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 ? - [ ] Way to import and force evaluate #import_lazy #import ?
Redesign: Redesign:
- [ ] Redesign Type map to use List and reduce wasting space - [ ] 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! - [ ] Casting syntax
- [ ] Probably need to move all the global data into the context if we want to use this as library or not?
- [ ] Casting
In the future In the future

View File

@@ -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){ if(left.type == pctx->type_pointer_to_char){
node->resolved_type = pctx->type_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)) { else if(is_string(left.type)) {
// @warning: not sure about this context thing here. // @warning: not sure about this context thing here.

View File

@@ -4,7 +4,6 @@ get_name_of_type(Ast_Type *type){
switch(type->kind){ switch(type->kind){
case TYPE_VOID: return "void"; case TYPE_VOID: return "void";
case TYPE_BOOL: return "Bool"; case TYPE_BOOL: return "Bool";
case TYPE_STRING: return "String";
case TYPE_CHAR: return "char"; case TYPE_CHAR: return "char";
case TYPE_F32: return "F32"; case TYPE_F32: return "F32";
case TYPE_F64: return "F64"; 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_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(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_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_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_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;} force_inline B32 is_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT;}

View File

@@ -8,6 +8,6 @@
#define CASE_INT case TYPE_UNTYPED_INT: CASE_SINT: CASE_UINT #define CASE_INT case TYPE_UNTYPED_INT: CASE_SINT: CASE_UINT
#define CASE_BOOL case TYPE_UNTYPED_BOOL: case TYPE_BOOL #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_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 CASE_UNTYPED case TYPE_UNTYPED_INT: case TYPE_UNTYPED_BOOL: case TYPE_UNTYPED_FLOAT: case TYPE_UNTYPED_STRING
#define ARRAY_SIZE_SLICE (-1) #define ARRAY_SIZE_SLICE (-1)