Add c types, int is default type

This commit is contained in:
Krzosa Karol
2023-04-16 15:02:25 +02:00
parent a2662d1cd8
commit 7152a710cc
8 changed files with 125 additions and 37 deletions

View File

@@ -46,8 +46,8 @@ Add(&guys, {100, 100})
*/ */
Array :: struct($T: Type) Array :: struct($T: Type)
data: *T data: *T
len: S64 len: int
cap: S64 cap: int
Guy :: struct Guy :: struct
pos: Vector2 pos: Vector2

View File

@@ -48,10 +48,20 @@ get_ctype_name_for_type(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 (char *)prefixed_string_type.str; case TYPE_STRING: return (char *)prefixed_string_type.str;
case TYPE_CHAR: return "char";
case TYPE_F32: return "float"; case TYPE_F32: return "float";
case TYPE_F64: return "double"; case TYPE_F64: return "double";
case TYPE_CHAR: return "char";
case TYPE_UCHAR: return "unsigned char";
case TYPE_INT: return "int"; case TYPE_INT: return "int";
case TYPE_UINT: return "unsigned int";
case TYPE_LONG: return "long";
case TYPE_ULONG: return "unsigned long";
case TYPE_LLONG: return "long long";
case TYPE_ULLONG: return "unsigned long long";
case TYPE_SHORT: return "short";
case TYPE_USHORT: return "unsigned short";
case TYPE_S8: return "int8_t"; case TYPE_S8: return "int8_t";
case TYPE_S16: return "int16_t"; case TYPE_S16: return "int16_t";
case TYPE_S32: return "int32_t"; case TYPE_S32: return "int32_t";

View File

@@ -125,17 +125,34 @@ for i in meta.token_simple_expr:
pctx->type__untyped_float = {TYPE_UNTYPED_FLOAT, sizeof(double), __alignof(double)}; pctx->type__untyped_float = {TYPE_UNTYPED_FLOAT, sizeof(double), __alignof(double)};
pctx->type__char = {TYPE_CHAR, sizeof(char), __alignof(char)}; pctx->type__char = {TYPE_CHAR, sizeof(char), __alignof(char)};
pctx->type__uchar = {TYPE_UCHAR, sizeof(unsigned char), __alignof(unsigned char), true};
pctx->type__int = {TYPE_INT, sizeof(int), __alignof(int)}; pctx->type__int = {TYPE_INT, sizeof(int), __alignof(int)};
pctx->type__uint = {TYPE_UINT, sizeof(unsigned), __alignof(unsigned), true};
pctx->type__long = {TYPE_LONG, sizeof(long), __alignof(long)};
pctx->type__ulong = {TYPE_ULONG, sizeof(unsigned long), __alignof(unsigned long), true};
pctx->type__llong = {TYPE_LLONG, sizeof(long long), __alignof(long long)};
pctx->type__ullong = {TYPE_ULLONG, sizeof(unsigned long long), __alignof(unsigned long long), true};
pctx->type__short = {TYPE_SHORT, sizeof(short), __alignof(short)};
pctx->type__ushort = {TYPE_USHORT, sizeof(unsigned short), __alignof(unsigned short), true};
pctx->type_char = &pctx->type__char; pctx->type_char = &pctx->type__char;
pctx->type_uchar = &pctx->type__uchar;
pctx->type_int = &pctx->type__int; pctx->type_int = &pctx->type__int;
pctx->type_uint = &pctx->type__uint;
pctx->type_long = &pctx->type__long;
pctx->type_ulong = &pctx->type__ulong;
pctx->type_llong = &pctx->type__llong;
pctx->type_ullong = &pctx->type__ullong;
pctx->type_short = &pctx->type__short;
pctx->type_ushort = &pctx->type__ushort;
pctx->type_void = &pctx->type__void; pctx->type_void = &pctx->type__void;
// pctx->type__string = {TYPE_STRING, sizeof(String), __alignof(String)}; // pctx->type__string = {TYPE_STRING, sizeof(String), __alignof(String)};
// pctx->type_any; // Needs to be inited at runtime // pctx->type_any; // Needs to be inited at runtime
// pctx->type_string = &pctx->type__string;
pctx->type_type = &pctx->type__type; pctx->type_type = &pctx->type__type;
// 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;
@@ -373,12 +390,23 @@ GetTypeInfo :: (type: Type): *Type_Info
)"_s; )"_s;
{ {
insert_builtin_type_into_scope(module, "char"_s, pctx->type_char);
insert_builtin_type_into_scope(module, "uchar"_s, pctx->type_uchar);
insert_builtin_type_into_scope(module, "int"_s, pctx->type_int);
insert_builtin_type_into_scope(module, "uint"_s, pctx->type_uint);
insert_builtin_type_into_scope(module, "long"_s, pctx->type_long);
insert_builtin_type_into_scope(module, "ulong"_s, pctx->type_ulong);
insert_builtin_type_into_scope(module, "llong"_s, pctx->type_llong);
insert_builtin_type_into_scope(module, "ullong"_s, pctx->type_ullong);
insert_builtin_type_into_scope(module, "short"_s, pctx->type_short);
insert_builtin_type_into_scope(module, "ushort"_s, pctx->type_ushort);
insert_builtin_type_into_scope(module, "S64"_s, pctx->type_s64); insert_builtin_type_into_scope(module, "S64"_s, pctx->type_s64);
insert_builtin_type_into_scope(module, "S32"_s, pctx->type_s32); insert_builtin_type_into_scope(module, "S32"_s, pctx->type_s32);
insert_builtin_type_into_scope(module, "S16"_s, pctx->type_s16); insert_builtin_type_into_scope(module, "S16"_s, pctx->type_s16);
insert_builtin_type_into_scope(module, "S8"_s, pctx->type_s8); insert_builtin_type_into_scope(module, "S8"_s, pctx->type_s8);
insert_builtin_type_into_scope(module, "int"_s, pctx->type_int);
insert_builtin_type_into_scope(module, "char"_s, pctx->type_char);
insert_builtin_type_into_scope(module, "U64"_s, pctx->type_u64); insert_builtin_type_into_scope(module, "U64"_s, pctx->type_u64);
insert_builtin_type_into_scope(module, "U32"_s, pctx->type_u32); insert_builtin_type_into_scope(module, "U32"_s, pctx->type_u32);
insert_builtin_type_into_scope(module, "U16"_s, pctx->type_u16); insert_builtin_type_into_scope(module, "U16"_s, pctx->type_u16);

View File

@@ -154,40 +154,57 @@ print(f"Ast_Operator_Info op_info_table[{size}];")
Ast_Type type__untyped_float; Ast_Type type__untyped_float;
Ast_Type type__char; Ast_Type type__char;
Ast_Type type__uchar;
Ast_Type type__int; Ast_Type type__int;
Ast_Type type__uint;
Ast_Type type__long;
Ast_Type type__ulong;
Ast_Type type__llong;
Ast_Type type__ullong;
Ast_Type type__short;
Ast_Type type__ushort;
Ast_Type *type_char = &type__char; Ast_Type *type_char;
Ast_Type *type_int = &type__int; Ast_Type *type_uchar;
Ast_Type *type_void = &type__void; Ast_Type *type_int;
Ast_Type *type_uint;
Ast_Type *type_long;
Ast_Type *type_ulong;
Ast_Type *type_llong;
Ast_Type *type_ullong;
Ast_Type *type_short;
Ast_Type *type_ushort;
Ast_Type *type_void;
Ast_Type *type_pointer_to_char; Ast_Type *type_pointer_to_char;
Ast_Type *type_pointer_to_void; Ast_Type *type_pointer_to_void;
Ast_Type *type_any; // Needs to be inited at runtime Ast_Type *type_any; // Needs to be inited at runtime
Ast_Type *type_type = &type__type; Ast_Type *type_type;
Ast_Type *type_string = &type__string; Ast_Type *type_string;
Ast_Type *type_bool = &type__bool; Ast_Type *type_bool;
Ast_Type *type_f32 = &type__f32; Ast_Type *type_f32;
Ast_Type *type_f64 = &type__f64; Ast_Type *type_f64;
Ast_Type *type_s8 = &type__s8; Ast_Type *type_s8;
Ast_Type *type_s16 = &type__s16; Ast_Type *type_s16;
Ast_Type *type_s32 = &type__s32; Ast_Type *type_s32;
Ast_Type *type_s64 = &type__s64; Ast_Type *type_s64;
Ast_Type *type_u8 = &type__u8; Ast_Type *type_u8;
Ast_Type *type_u16 = &type__u16; Ast_Type *type_u16;
Ast_Type *type_u32 = &type__u32; Ast_Type *type_u32;
Ast_Type *type_u64 = &type__u64; Ast_Type *type_u64;
Ast_Type *untyped_string = &type__untyped_string; Ast_Type *untyped_string;
Ast_Type *untyped_bool = &type__untyped_bool; Ast_Type *untyped_bool;
Ast_Type *untyped_int = &type__untyped_int; Ast_Type *untyped_int;
Ast_Type *untyped_float = &type__untyped_float; Ast_Type *untyped_float;
Ast_Type type__vargs; Ast_Type type__vargs;
Ast_Type *type_vargs = &type__vargs; Ast_Type *type_vargs;
Intern_String intern(String string) { Intern_String intern(String string) {
assert(string.len > 0); assert(string.len > 0);

View File

@@ -184,16 +184,27 @@ struct Token {
enum Ast_Type_Kind { enum Ast_Type_Kind {
TYPE_NONE, TYPE_NONE,
TYPE_S64, // FIRST_NUMERIC TYPE_S64, // FIRST_NUMERIC is_int start
TYPE_S32, TYPE_S32,
TYPE_S16, TYPE_S16,
TYPE_S8, TYPE_S8,
TYPE_INT, TYPE_INT,
TYPE_UINT,
TYPE_LONG,
TYPE_ULONG,
TYPE_LLONG,
TYPE_ULLONG,
TYPE_SHORT,
TYPE_USHORT,
TYPE_CHAR, TYPE_CHAR,
TYPE_UCHAR,
TYPE_U64, TYPE_U64,
TYPE_U32, TYPE_U32,
TYPE_U16, TYPE_U16,
TYPE_U8, TYPE_U8, //is_int end
TYPE_F32, TYPE_F32,
TYPE_F64, TYPE_F64,
TYPE_POINTER, TYPE_POINTER,

View File

@@ -6,14 +6,26 @@
CORE_Static String CORE_Static String
core_type_to_string(Ast_Type *type) { core_type_to_string(Ast_Type *type) {
// @todo: use get_name_of_type instead of duplicating the typename dispatch table
switch (type->kind) { switch (type->kind) {
case TYPE_NONE: return "<NONE>"_s; break; case TYPE_NONE: return "<NONE>"_s; break;
case TYPE_S64: return "S64"_s; break; case TYPE_S64: return "S64"_s; break;
case TYPE_S32: return "S32"_s; break; case TYPE_S32: return "S32"_s; break;
case TYPE_S16: return "S16"_s; break; case TYPE_S16: return "S16"_s; break;
case TYPE_S8: return "S8"_s; break; case TYPE_S8: return "S8"_s; break;
case TYPE_INT: return "int"_s; break;
case TYPE_CHAR: return "char"_s; break; case TYPE_CHAR: return "char"_s;
case TYPE_UCHAR: return "uchar"_s;
case TYPE_INT: return "int"_s;
case TYPE_UINT: return "uint"_s;
case TYPE_LONG: return "long"_s;
case TYPE_ULONG: return "ulong"_s;
case TYPE_LLONG: return "llong"_s;
case TYPE_ULLONG: return "ullong"_s;
case TYPE_SHORT: return "short"_s;
case TYPE_USHORT: return "ushort"_s;
case TYPE_U64: return "U64"_s; break; case TYPE_U64: return "U64"_s; break;
case TYPE_U32: return "U32"_s; break; case TYPE_U32: return "U32"_s; break;
case TYPE_U16: return "U16"_s; break; case TYPE_U16: return "U16"_s; break;

View File

@@ -89,7 +89,7 @@ operand_rvalue(Ast_Type *type) {
CORE_Static Ast_Type * CORE_Static Ast_Type *
get_default_type_from_untyped(Ast_Type *type) { get_default_type_from_untyped(Ast_Type *type) {
switch (type->kind) { switch (type->kind) {
case TYPE_UNTYPED_INT: return pctx->type_s64; break; case TYPE_UNTYPED_INT: return pctx->type_int; break;
case TYPE_UNTYPED_BOOL: return pctx->type_bool; break; case TYPE_UNTYPED_BOOL: return pctx->type_bool; break;
case TYPE_UNTYPED_STRING: return pctx->type_string; break; case TYPE_UNTYPED_STRING: return pctx->type_string; break;
case TYPE_UNTYPED_FLOAT: return pctx->type_f32; break; case TYPE_UNTYPED_FLOAT: return pctx->type_f32; break;
@@ -1268,7 +1268,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
// @todo: type_architecture? // @todo: type_architecture?
// we only try to convert the index cause array can't be const // we only try to convert the index cause array can't be const
// right now // right now
try_propagating_resolved_type_to_untyped_literals(node->index, pctx->type_s64); try_propagating_resolved_type_to_untyped_literals(node->index, pctx->type_int);
try_propagating_resolved_type_to_untyped_literals(node->expr, left.type); try_propagating_resolved_type_to_untyped_literals(node->expr, left.type);
if (!left.type) if (!left.type)
@@ -1566,7 +1566,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope); Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL), 0, field_access_scope);
node->kind = AST_LENGTH_OF; node->kind = AST_LENGTH_OF;
node->resolved_type = pctx->type_s64; node->resolved_type = pctx->type_int;
if (is_array(name.type)) { if (is_array(name.type)) {
Value value = value_int(name.type->arr.size); Value value = value_int(name.type->arr.size);
rewrite_into_const(node, Ast_Builtin, value); rewrite_into_const(node, Ast_Builtin, value);
@@ -1578,7 +1578,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
return operand_const_rvalue(value); return operand_const_rvalue(value);
} }
else if (is_array(name.type) || is_slice(name.type) || is_string(name.type)) { else if (is_array(name.type) || is_slice(name.type) || is_string(name.type)) {
return operand_rvalue(pctx->type_s64); return operand_rvalue(pctx->type_int);
} }
else compiler_error(node->pos, "Can't get length of type %Q", typestring(name.type)); else compiler_error(node->pos, "Can't get length of type %Q", typestring(name.type));
} }

View File

@@ -4,11 +4,21 @@ 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_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";
case TYPE_CHAR: return "char";
case TYPE_UCHAR: return "uchar";
case TYPE_INT: return "int"; case TYPE_INT: return "int";
case TYPE_UINT: return "uint";
case TYPE_LONG: return "long";
case TYPE_ULONG: return "ulong";
case TYPE_LLONG: return "llong";
case TYPE_ULLONG: return "ullong";
case TYPE_SHORT: return "short";
case TYPE_USHORT: return "ushort";
case TYPE_S16: return "S16"; case TYPE_S16: return "S16";
case TYPE_S32: return "S32"; case TYPE_S32: return "S32";
case TYPE_S64: return "S64"; case TYPE_S64: return "S64";
@@ -210,7 +220,7 @@ type_lambda(Ast *ast, Array<Ast_Type *> return_vals, Array<Ast_Type *> args) {
CORE_Static Ast_Type * CORE_Static Ast_Type *
type_enum(Ast_Decl *ast, Ast_Type *type) { type_enum(Ast_Decl *ast, Ast_Type *type) {
if (!type) { if (!type) {
type = pctx->type_s64; type = pctx->type_int;
} }
Ast_Type *result = type_new(pctx->perm, TYPE_ENUM, type->size, type->align); Ast_Type *result = type_new(pctx->perm, TYPE_ENUM, type->size, type->align);