diff --git a/ccodegen.cpp b/ccodegen.cpp index 0a9f7ef..30d5324 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -64,6 +64,18 @@ gen_simple_decl(Ast_Resolved_Type *ast, Intern_String name){ } } +function void +gen_value(Value a){ + gen("%s", docname(a.type)); + switch(a.type->kind){ + CASE_INT: gen("%lld", bigint_as_signed(&a.big_int_val)); break; + CASE_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; + invalid_default_case; + } +} + function void gen_expr(Ast_Expr *ast){ switch(ast->kind){ @@ -73,11 +85,7 @@ gen_expr(Ast_Expr *ast){ } CASE(VALUE, Atom){ - if(is_int(node->type)) gen("%lld", bigint_as_signed(&node->big_int_val)); - else if(is_string(node->type)) gen("LIT(\"%s\")", node->intern_val.str); - else if(is_bool(node->type)) node->bool_val ? gen("true"):gen("false"); - else if(is_float(node->type)) gen("%f", node->f64_val); - else invalid_codepath; + gen_value(node->value); BREAK(); } @@ -311,16 +319,20 @@ gen_ast(Ast *ast){ } } else if(sym->type == untyped_float){ - gen("// constant F64 %s = %f;", node->name.str, sym->f64_val); + gen("// F64 %s = ", node->name.str); + gen_value(sym->value); } else if(sym->type == untyped_int){ - gen("// constant int %s = %lld;", node->name.str, bigint_as_signed(&sym->big_int_val)); + gen("// constant int %s = ", node->name.str); + gen_value(sym->value); } else if(sym->type == untyped_string){ - gen("// const String %s = LIT(\"%s\");", node->name.str, sym->intern_val.str); + gen("// const String %s = ", node->name.str); + gen_value(sym->value); } else if(sym->type == untyped_bool){ - gen("// const Bool %s = %d;", node->name.str, sym->bool_val); + gen("// const Bool %s = ", node->name.str); + gen_value(sym->value); } else if(sym->type == type_type){ if(sym->type_val->kind == TYPE_STRUCT){ diff --git a/enums.kl b/enums.kl index 5d9cb81..6cb6c78 100644 --- a/enums.kl +++ b/enums.kl @@ -1,6 +1,6 @@ Thing :: struct - len: int + len: S64 Constant_String :: "Test" Constant :: 10 diff --git a/globals.kl b/globals.kl index f0d2977..363d3da 100644 --- a/globals.kl +++ b/globals.kl @@ -1,44 +1,44 @@ //----------------------------------------------------------------------------- // Function types //----------------------------------------------------------------------------- -test_function :: (thing: int): *int +test_function :: (thing: S64): *S64 function_type: test_function const_function_alias :: test_function -// null_function: (t: int): *int = null +// null_function: (t: S64): *S64 = null //----------------------------------------------------------------------------- // Booleans //----------------------------------------------------------------------------- Boolean: Bool = true -value_of_Bool: int = cast(Boolean: int) +value_of_Bool: S64 = cast(Boolean: S64) //----------------------------------------------------------------------------- // Nulls //----------------------------------------------------------------------------- -// int_null: int = null +// int_null: S64 = null // str_null: String = null // Bool_null: Bool = null //----------------------------------------------------------------------------- // Compound expressions //----------------------------------------------------------------------------- -array1: [4]int = [4]int(1,2,3,4) -array2 := [32]int(1,2,3,4) -array3 := [32]int( +array1: [4]S64 = [4]S64(1,2,3,4) +array2 := [32]S64(1,2,3,4) +array3 := [32]S64( [0] = 0, [1] = 1, [2] = 2, [31] = 31, ) array_item := array1[0] -array_item_imp: int = array2[2] +array_item_imp: S64 = array2[2] //----------------------------------------------------------------------------- // Pointers //----------------------------------------------------------------------------- -pointer_decl : *int -variable_from_deref: int = *pointer_decl -pointer_from_var : *int = &variable_from_deref +pointer_decl : *S64 +variable_from_deref: S64 = *pointer_decl +pointer_from_var : *S64 = &variable_from_deref Boolean_pointer := &Boolean //----------------------------------------------------------------------------- @@ -50,9 +50,9 @@ implicit_str :: "Hello world" //----------------------------------------------------------------------------- // Pointers //----------------------------------------------------------------------------- -// pointer1: *int = 0 -// pointer2: *int = pointer1 -// pointer3: **int = 0 +// pointer1: *S64 = 0 +// pointer2: *S64 = pointer1 +// pointer3: **S64 = 0 //----------------------------------------------------------------------------- // String types @@ -61,7 +61,7 @@ string1 :: "Test" string2 :: string1 //----------------------------------------------------------------------------- -// Constant int variables +// Constant S64 variables //----------------------------------------------------------------------------- thing0 :: 10 thing1 :: thing0 + 11 diff --git a/lambdas.kl b/lambdas.kl index 055cd7b..f1f8275 100644 --- a/lambdas.kl +++ b/lambdas.kl @@ -1,8 +1,8 @@ -a_type :: int -pointer_type :: *int +a_type :: S64 +pointer_type :: *S64 // null_pointer: pointer_type = null -if_stmt :: (cond: int): type +if_stmt :: (cond: S64): type CONSTANT :: 10 thing := 10 if i := thing + cond, cond + CONSTANT @@ -17,11 +17,11 @@ for_stmt :: () for i := 0, i + 10, i+=1 pass -add_10 :: (size: int): int - add_20 :: (new_size: int): int +add_10 :: (size: S64): S64 + add_20 :: (new_size: S64): S64 return 20 - add :: (a: int, b: int = 10): int + add :: (a: S64, b: S64 = 10): S64 return a + b constant :: 20; result := constant + 10 @@ -38,13 +38,13 @@ add_10 :: (size: int): int return v4 -return_constant :: (): int +return_constant :: (): S64 constant :: 10 return constant -returning_void :: (insert: int) - val1: int = return_constant() - val2: int = add_10(val1) +returning_void :: (insert: S64) + val1: S64 = return_constant() + val2: S64 = add_10(val1) return diff --git a/main.cpp b/main.cpp index 9c82477..7d06ff6 100644 --- a/main.cpp +++ b/main.cpp @@ -120,6 +120,7 @@ int main(){ String result = {}; #if 0 +#endif result = compile_file("globals.kl"_s); printf("%s", result.str); result = compile_file("enums.kl"_s); @@ -130,7 +131,6 @@ int main(){ printf("%s", result.str); result = compile_file("lambdas.kl"_s); printf("%s", result.str); -#endif result = compile_file("new_types.kl"_s); printf("%s", result.str); diff --git a/new_ast.cpp b/new_ast.cpp index d567341..028ef75 100644 --- a/new_ast.cpp +++ b/new_ast.cpp @@ -141,7 +141,7 @@ Ast_Resolved_Type *type; \ union{ \ bool bool_val; \ F64 f64_val; \ - Intern_String intern_val;\ + Intern_String intern_val; \ BigInt big_int_val;\ Ast_Resolved_Type *type_val; \ }; diff --git a/new_types.kl b/new_types.kl index 51ec422..c381a56 100644 --- a/new_types.kl +++ b/new_types.kl @@ -5,7 +5,7 @@ unary_test :: () float_val :: 124.42 conversion: F64 = -+int_val float2 := -float_val - unsigned: int = -+-+-int_val + unsigned: S64 = -+-+-int_val not: Bool = !int_val notf := !float_val diff --git a/order1.kl b/order1.kl index 86b6518..fabae8c 100644 --- a/order1.kl +++ b/order1.kl @@ -2,7 +2,7 @@ other_func :: () a_val := recursive_lambda -recursive_lambda :: (thing: int) +recursive_lambda :: (thing: S64) in_val := recursive_lambda some_value := thing + const_in_lambda diff --git a/order2.kl b/order2.kl index e7556a2..23446d1 100644 --- a/order2.kl +++ b/order2.kl @@ -11,20 +11,20 @@ arena := Arena( cap = 1000, ) -// lambda_value := (val: int) // @todo +// lambda_value := (val: S64) // @todo // return Arena :: struct // arena: Arena next: *Arena - data: *int + data: *S64 len : S64 - cap : int + cap : S64 Sub :: struct - len: int + len: S64 Sub_Sub :: struct - len: int + len: S64 get_len :: (s: *Arena): S64 // @todo return s.next.len @@ -35,8 +35,8 @@ Arena :: struct string16: Str16 String16 :: struct - data: *Void - len : int + data: *void + len : S64 with_type: Arena = thing pointer := &with_type @@ -60,7 +60,7 @@ test_assignments :: () CONST :: 23 == 23 CONST_FLOAT :: 23.52 - j: *int + j: *S64 *j = 1 /* invalid 8 = 32 diff --git a/typecheck.cpp b/typecheck.cpp index f1b1c99..cfe5d3a 100644 --- a/typecheck.cpp +++ b/typecheck.cpp @@ -490,7 +490,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res Operand left = resolve_expr(node->expr); Operand index = resolve_expr(node->index); if(left.type->kind != TYPE_ARRAY && left.type->kind != TYPE_POINTER) parsing_error(node->pos, "Indexing variable that is not an [Array] or [Pointer], it's of type %s instead", docname(left.type)); - if(!is_int(index.type)) type_error(node->pos, type_int, index.type,"Trying to index the array with invalid type, expected int"); + if(!is_int(index.type)) type_error(node->pos, untyped_int, index.type,"Trying to index the array with invalid type, expected int"); return operand_lvalue(left.type->arr.base); BREAK(); } @@ -630,11 +630,8 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res CASE(CAST, Cast){ Operand expr = resolve_expr(node->expr); Ast_Resolved_Type *type = resolve_typespec(node->typespec); - - if(type == expr.type); - else if(expr.type == type_int && type == type_bool) type = type_bool; - else if(expr.type == type_bool && type == type_int) type = type_int; - else parsing_error(node->pos, "Failed to cast, incompatible types"); + unused(expr); + // @todo return operand_rvalue(type); BREAK(); } @@ -842,7 +839,7 @@ resolve_const(Ast_Expr *ast, Sym *sym){ value = bigint_as_signed(&op.big_int_val) + 1; } else{ - op.type = type_s64; + op.type = untyped_int; bigint_init_signed(&op.big_int_val, value++); } @@ -979,13 +976,13 @@ test_types(){ ctx.init(scratch, scratch); pctx = &ctx; - Ast_Resolved_Type *array_type1 = type_array(type_int, 32); - Ast_Resolved_Type *array_type2 = type_array(type_int, 32); - Ast_Resolved_Type *array_type3 = type_array(type_int, 48); + Ast_Resolved_Type *array_type1 = type_array(type_s64, 32); + Ast_Resolved_Type *array_type2 = type_array(type_s64, 32); + Ast_Resolved_Type *array_type3 = type_array(type_s64, 48); assert(array_type1 == array_type2); assert(array_type2 != array_type3); - Ast_Resolved_Type *pointer_type1 = type_pointer(type_int); - Ast_Resolved_Type *pointer_type2 = type_pointer(type_int); + Ast_Resolved_Type *pointer_type1 = type_pointer(type_s64); + Ast_Resolved_Type *pointer_type2 = type_pointer(type_s64); assert(pointer_type2 == pointer_type1); Ast_Resolved_Type *pointer_type3 = type_pointer(pointer_type1); Ast_Resolved_Type *pointer_type4 = type_pointer(pointer_type2); @@ -993,17 +990,17 @@ test_types(){ assert(pointer_type3 == pointer_type4); Array types = {scratch}; - types.add(type_array(type_int, 32)); + types.add(type_array(type_s64, 32)); Ast_Resolved_Type *func_type1 = type_lambda(0, types[0], types); Ast_Resolved_Type *func_type2 = type_lambda(0, types[0], types); assert(func_type1 == func_type2); Array types2 = {scratch}; { - types2.add(type_array(type_int, 32)); - types2.add(type_int); + types2.add(type_array(type_s64, 32)); + types2.add(type_s64); } - types.add(type_int); + types.add(type_s64); Ast_Resolved_Type *func_type3 = type_lambda(0, types[0], types); Ast_Resolved_Type *func_type4 = type_lambda(0, types[0], types2); assert(func_type1 != func_type3); diff --git a/typecheck.h b/typecheck.h index 9be1c58..5eb37e5 100644 --- a/typecheck.h +++ b/typecheck.h @@ -166,7 +166,6 @@ function void sym_insert_builtins(){ sym_insert_builtin_type("void"_s , type_void); sym_insert_builtin_type("Bool"_s , type_bool); - sym_insert_builtin_type("int"_s , type_int); sym_insert_builtin_type("String"_s, type_string); sym_insert_builtin_type("S8"_s, type_s8); sym_insert_builtin_type("S16"_s, type_s16); @@ -206,7 +205,7 @@ operand_type(Ast_Resolved_Type *type){ function Operand operand_int(BigInt big_int){ Operand result = {}; - result.type = type_int; + result.type = untyped_int; bigint_init_bigint(&result.big_int_val, &big_int); result.is_const = true; result.is_lvalue = false; @@ -332,8 +331,8 @@ type_lambda(Ast *ast, Ast_Resolved_Type *ret, Array args){ function Ast_Resolved_Type * type_enum(Ast_Enum *ast){ Ast_Resolved_Type *type = resolve_typespec(ast->typespec, AST_CAN_BE_NULL); - if(!type) { - type = type_int; + if(!type){ + type = untyped_int; } Ast_Resolved_Type *result = type_new(pctx->perm, TYPE_ENUM, type->size, type->align); diff --git a/types.h b/types.h index e4a6db7..7030f5d 100644 --- a/types.h +++ b/types.h @@ -1,7 +1,3 @@ -#define TYPE_INT_MIN S64MIN -#define TYPE_INT_MAX S64MAX -#define TYPE_UINT_MIN U64MIN -#define TYPE_UINT_MAX U64MAX //----------------------------------------------------------------------------- // Resolved Types //----------------------------------------------------------------------------- @@ -12,13 +8,11 @@ enum Ast_Resolved_Type_Kind{ TYPE_UNTYPED_BOOL, // FIRST_TYPED_NUMERIC, FIRST_NUMERIC TYPE_UNTYPED_INT, TYPE_UNTYPED_FLOAT, // LAST_TYPED_NUMERIC - TYPE_UNTYPED_STRING, // LAST_NUMERIC - TYPE_INT, // FIRST_NUMERIC - TYPE_S64, + TYPE_UNTYPED_STRING, + TYPE_S64, // FIRST_NUMERIC TYPE_S32, TYPE_S16, TYPE_S8 , - TYPE_UINT, TYPE_U64, TYPE_U32, TYPE_U16, @@ -42,7 +36,7 @@ enum Ast_Resolved_Type_Kind{ TYPE_UNTYPED_FIRST_NUMERIC = TYPE_UNTYPED_BOOL, TYPE_UNTYPED_LAST_NUMERIC = TYPE_UNTYPED_FLOAT, - TYPE_FIRST_NUMERIC = TYPE_INT, + TYPE_FIRST_NUMERIC = TYPE_S64, TYPE_LAST_NUMERIC = TYPE_BOOL, }; @@ -63,12 +57,10 @@ const char *type_names[] = { "[Untyped_Float]", "[Untyped_String]", - "[int]", "[S64]", "[S32]", "[S16]", "[S8]", - "[uint]", "[U64]", "[U32]", "[U16]", @@ -127,12 +119,10 @@ docname(Ast_Resolved_Type *type){ case TYPE_UNTYPED_INT: return "[Untyped_Int]"; case TYPE_UNTYPED_FLOAT: return "[Untyped_Float]"; case TYPE_UNTYPED_STRING: return "[Untyped_String]"; - case TYPE_INT: return "[int]"; case TYPE_S64: return "[S64]"; case TYPE_S32: return "[S32]"; case TYPE_S16: return "[S16]"; case TYPE_S8: return "[S8]"; - case TYPE_UINT: return "[uint]"; case TYPE_U64: return "[U64]"; case TYPE_U32: return "[U32]"; case TYPE_U16: return "[U16]"; @@ -158,11 +148,8 @@ function const char * name(Ast_Resolved_Type *type){ switch(type->kind){ case TYPE_VOID: return "void"; - case TYPE_INT: return "int"; - case TYPE_UINT: return "uint"; case TYPE_BOOL: return "Bool"; case TYPE_STRING: return "String"; - case TYPE_F32: return "F32"; case TYPE_F64: return "F64"; case TYPE_S8: return "S8"; @@ -191,13 +178,11 @@ global Ast_Resolved_Type type__type = {TYPE_TYPE}; global Ast_Resolved_Type type__f32 = {TYPE_F32, sizeof(F32), __alignof(F32)}; global Ast_Resolved_Type type__f64 = {TYPE_F64, sizeof(F64), __alignof(F64)}; -global Ast_Resolved_Type type__int = {TYPE_INT, sizeof(S64), __alignof(S64)}; global Ast_Resolved_Type type__s8 = {TYPE_S8, sizeof(S8), __alignof(S8)}; global Ast_Resolved_Type type__s16 = {TYPE_S16, sizeof(S16), __alignof(S16)}; global Ast_Resolved_Type type__s32 = {TYPE_S32, sizeof(S32), __alignof(S32)}; global Ast_Resolved_Type type__s64 = {TYPE_S64, sizeof(S64), __alignof(S64)}; -global Ast_Resolved_Type type__uint = {TYPE_UINT, sizeof(SizeU), __alignof(SizeU)}; global Ast_Resolved_Type type__u8 = {TYPE_U8, sizeof(U8), __alignof(U8)}; global Ast_Resolved_Type type__u16 = {TYPE_U16, sizeof(U16), __alignof(U16)}; global Ast_Resolved_Type type__u32 = {TYPE_U32, sizeof(U32), __alignof(U32)}; @@ -216,13 +201,11 @@ global Ast_Resolved_Type *type_bool = &type__bool; global Ast_Resolved_Type *type_f32 = &type__f32; global Ast_Resolved_Type *type_f64 = &type__f64; -global Ast_Resolved_Type *type_int = &type__int; global Ast_Resolved_Type *type_s8 = &type__s8 ; global Ast_Resolved_Type *type_s16 = &type__s16; global Ast_Resolved_Type *type_s32 = &type__s32; global Ast_Resolved_Type *type_s64 = &type__s64; -global Ast_Resolved_Type *type_uint = &type__uint; global Ast_Resolved_Type *type_u8 = &type__u8 ; global Ast_Resolved_Type *type_u16 = &type__u16; global Ast_Resolved_Type *type_u32 = &type__u32; @@ -242,10 +225,10 @@ force_inline B32 is_enum(Ast_Resolved_Type *a){return a->kind == TYPE_ENUM;} force_inline B32 is_pointer(Ast_Resolved_Type *a){return a->kind == TYPE_POINTER;} force_inline B32 is_string(Ast_Resolved_Type *a){return a->kind == TYPE_STRING || a->kind == TYPE_UNTYPED_STRING;} force_inline B32 is_untyped_int(Ast_Resolved_Type *a){return a->kind == TYPE_UNTYPED_INT;} -force_inline B32 is_typed_int(Ast_Resolved_Type *a){return (a->kind >= TYPE_INT && a->kind <= TYPE_U8);} -force_inline B32 is_int(Ast_Resolved_Type *a){return (a->kind >= TYPE_INT && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT;} -force_inline B32 is_signed_int(Ast_Resolved_Type *a){return a->kind >= TYPE_INT && a->kind <= TYPE_S8;} -force_inline B32 is_unsigned_int(Ast_Resolved_Type *a){return a->kind >= TYPE_UINT && a->kind <= TYPE_U8;} +force_inline B32 is_typed_int(Ast_Resolved_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8);} +force_inline B32 is_int(Ast_Resolved_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT;} +force_inline B32 is_signed_int(Ast_Resolved_Type *a){return a->kind >= TYPE_S64 && a->kind <= TYPE_S8;} +force_inline B32 is_unsigned_int(Ast_Resolved_Type *a){return a->kind >= TYPE_U64 && a->kind <= TYPE_U8;} force_inline B32 is_float(Ast_Resolved_Type *a){return a->kind == TYPE_F32 || a->kind == TYPE_F64 || a->kind == TYPE_UNTYPED_FLOAT;} force_inline B32 is_f32(Ast_Resolved_Type *a){return a->kind == TYPE_F32;} force_inline B32 is_f64(Ast_Resolved_Type *a){return a->kind == TYPE_F64;} @@ -264,13 +247,11 @@ int_type_max(Ast_Resolved_Type *type){ switch(type->kind){ case TYPE_BOOL: return 1; case TYPE_UNTYPED_BOOL: return 1; - case TYPE_UNTYPED_INT: return TYPE_INT_MAX; - case TYPE_INT: return TYPE_INT_MAX; + case TYPE_UNTYPED_INT: return S64MAX; case TYPE_S64: return S64MAX; case TYPE_S32: return S32MAX; case TYPE_S16: return S16MAX; case TYPE_S8: return S8MAX; - case TYPE_UINT: return TYPE_UINT_MAX; case TYPE_U64: return U64MAX; case TYPE_U32: return U32MAX; case TYPE_U16: return U16MAX;