First draft for adding c string type

This commit is contained in:
Krzosa Karol
2022-06-12 00:15:19 +02:00
parent 6a612cf1b4
commit 83130b130b
5 changed files with 25 additions and 5 deletions

View File

@@ -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 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 // without_size: []S64 = {} // @todo: this should be slice, converting from array should be implicit
string: char = "string"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Pointers // Pointers
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@@ -90,7 +90,12 @@ gen_value(Value a){
const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10); const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10);
gen("%s", string); gen("%s", string);
}break; }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_BOOL: a.bool_val ? gen("true"):gen("false"); break;
CASE_FLOAT: gen("%f", a.f64_val); break; CASE_FLOAT: gen("%f", a.f64_val); break;
default: result = false; default: result = false;

View File

@@ -77,7 +77,7 @@ Expr:
@todo @todo
[ ] - Remodel compound from call to {} [ ] - Should compound resolution use an algorithm to reorder compounds to initialize all fields in order
[ ] - Switch [ ] - Switch
[ ] - Add c string [ ] - Add c string
[ ] - Some way to take slice of data [ ] - Some way to take slice of data
@@ -106,6 +106,7 @@ Expr:
@donzo @donzo
[x] - Scope [x] - Scope
[x] - Remodel compound from call to {}
[x] - Field access rewrite [x] - Field access rewrite
[-] - Constants embeded in structs should be able to refer to other constants in that namespace without prefix [-] - Constants embeded in structs should be able to refer to other constants in that namespace without prefix
[-] - Order independent constants in structs [-] - Order independent constants in structs

View File

@@ -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, "void"_s , type_void);
insert_type_into_package(p, "Bool"_s , type_bool); insert_type_into_package(p, "Bool"_s , type_bool);
insert_type_into_package(p, "String"_s, type_string); 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, "S8"_s, type_s8);
insert_type_into_package(p, "S16"_s, type_s16); insert_type_into_package(p, "S16"_s, type_s16);
insert_type_into_package(p, "S32"_s, type_s32); insert_type_into_package(p, "S32"_s, type_s32);
@@ -614,7 +615,6 @@ resolve_compound_array(Ast_Call *node, Ast_Type *type){
function void function void
resolve_compound_struct(Ast_Call *node, Ast_Type *type){ resolve_compound_struct(Ast_Call *node, Ast_Type *type){
// type->agg.members
S64 default_counter = 0; S64 default_counter = 0;
For(node->exprs){ For(node->exprs){
if(it->index) 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); 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); 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 function Operand
@@ -709,6 +711,10 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
} }
CASE(VALUE, Atom){ 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); return operand_const_rvalue(node->value);
BREAK(); BREAK();
} }

10
types.h
View File

@@ -22,6 +22,7 @@ enum Ast_Type_Kind{
TYPE_POINTER, TYPE_POINTER,
TYPE_BOOL, // LAST_NUMERIC TYPE_BOOL, // LAST_NUMERIC
TYPE_STRING, TYPE_STRING,
TYPE_CHAR,
TYPE_VOID, TYPE_VOID,
TYPE_ARRAY, TYPE_ARRAY,
TYPE_LAMBDA, TYPE_LAMBDA,
@@ -45,7 +46,7 @@ enum Ast_Type_Kind{
#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 #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 #define CASE_UNTYPED case TYPE_UNTYPED_INT: case TYPE_UNTYPED_BOOL: case TYPE_UNTYPED_FLOAT: case TYPE_UNTYPED_STRING
const char *type_names[] = { const char *type_names[] = {
@@ -70,6 +71,7 @@ const char *type_names[] = {
"[Float64]", "[Float64]",
"[Bool]", "[Bool]",
"[String]", "[String]",
"[char]",
"[void]", "[void]",
"[Pointer]", "[Pointer]",
"[Array]", "[Array]",
@@ -139,6 +141,7 @@ docname(Ast_Type *type){
case TYPE_F64: return "[Float64]"; case TYPE_F64: return "[Float64]";
case TYPE_BOOL: return "[Bool]"; case TYPE_BOOL: return "[Bool]";
case TYPE_STRING: return "[String]"; case TYPE_STRING: return "[String]";
case TYPE_CHAR: return "[char]";
case TYPE_VOID: return "[void]"; case TYPE_VOID: return "[void]";
case TYPE_POINTER: return "[Pointer]"; case TYPE_POINTER: return "[Pointer]";
case TYPE_ARRAY: return "[Array]"; case TYPE_ARRAY: return "[Array]";
@@ -158,6 +161,7 @@ name(Ast_Type *type){
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_STRING: return "String";
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";
case TYPE_S8: return "S8"; case TYPE_S8: return "S8";
@@ -180,6 +184,7 @@ const SizeU pointer_size = sizeof(SizeU);
const SizeU pointer_align = __alignof(SizeU); const SizeU pointer_align = __alignof(SizeU);
global Ast_Type type__void = {TYPE_VOID}; global Ast_Type type__void = {TYPE_VOID};
global Ast_Type type__string = {TYPE_STRING, sizeof(String), __alignof(String)}; 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__bool = {TYPE_BOOL, sizeof(bool), __alignof(bool)};
global Ast_Type type__type = {TYPE_TYPE}; 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_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__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_type = &type__type;
global Ast_Type *type_void = &type__void; global Ast_Type *type_void = &type__void;
global Ast_Type *type_string = &type__string; 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_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_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_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_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;}