diff options
| author | Ginger Bill <bill@gingerbill.org> | 2017-06-06 23:54:33 +0100 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2017-06-06 23:54:33 +0100 |
| commit | f60c772c11154d16c12d30a7f19c2c2e07068642 (patch) | |
| tree | a17b75c7801371f45b57d4d7893f6cf042a60d05 /src | |
| parent | 107740ca5e89b43171f37cb4d75c8b1dc2d53fc7 (diff) | |
Make `rune` a basic type and not an alias; Remove `byte`
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_expr.c | 26 | ||||
| -rw-r--r-- | src/checker.c | 53 | ||||
| -rw-r--r-- | src/ir.c | 4 | ||||
| -rw-r--r-- | src/ir_print.c | 2 | ||||
| -rw-r--r-- | src/tokenizer.c | 6 | ||||
| -rw-r--r-- | src/types.c | 33 |
6 files changed, 81 insertions, 43 deletions
diff --git a/src/check_expr.c b/src/check_expr.c index 667ae48fd..75d38f4fc 100644 --- a/src/check_expr.c +++ b/src/check_expr.c @@ -156,7 +156,7 @@ i64 check_distance_between_types(Checker *c, Operand *operand, Type *type) { if (is_type_typed(dst) && src->kind == Type_Basic) { switch (src->Basic.kind) { case Basic_UntypedInteger: - if (is_type_integer(dst)) { + if (is_type_integer(dst) || is_type_rune(dst)) { return 1; } break; @@ -2102,7 +2102,7 @@ bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type return in_value.kind == ExactValue_Bool; } else if (is_type_string(type)) { return in_value.kind == ExactValue_String; - } else if (is_type_integer(type)) { + } else if (is_type_integer(type) || is_type_rune(type)) { ExactValue v = exact_value_to_integer(in_value); if (v.kind != ExactValue_Integer) { return false; @@ -2127,6 +2127,7 @@ bool check_representable_as_constant(Checker *c, ExactValue in_value, Type *type i128 imax = i128_shl(I128_ONE, s-1ll); switch (type->Basic.kind) { + case Basic_rune: case Basic_i8: case Basic_i16: case Basic_i32: @@ -2212,7 +2213,15 @@ void check_is_expressible(Checker *c, Operand *o, Type *type) { if (!is_type_integer(o->type) && is_type_integer(type)) { error_node(o->expr, "`%s` truncated to `%s`", a, b); } else { - error_node(o->expr, "`%s = %lld` overflows `%s`", a, i128_to_i64(o->value.value_integer), b); + char buf[127] = {0}; + String str = {0}; + i128 i = o->value.value_integer; + if (is_type_unsigned(o->type)) { + str = u128_to_string(*cast(u128 *)&i, buf, gb_size_of(buf)); + } else { + str = i128_to_string(i, buf, gb_size_of(buf)); + } + error_node(o->expr, "`%s = %.*s` overflows `%s`", a, str, b); } } else { error_node(o->expr, "Cannot convert `%s` to `%s`", a, b); @@ -2623,6 +2632,13 @@ bool check_is_castable_to(Checker *c, Operand *operand, Type *y) { } } + if (is_type_integer(src) && is_type_rune(dst)) { + return true; + } + if (is_type_rune(src) && is_type_integer(dst)) { + return true; + } + if (is_type_complex(src) && is_type_complex(dst)) { return true; } @@ -4349,7 +4365,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id } break; case BuiltinProc_slice_to_bytes: { - // slice_to_bytes :: proc(a: []T) -> []byte + // slice_to_bytes :: proc(a: []T) -> []u8 Type *slice_type = base_type(operand->type); if (!is_type_slice(slice_type)) { gbString type_str = type_to_string(operand->type); @@ -4358,7 +4374,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id return false; } - operand->type = t_byte_slice; + operand->type = t_u8_slice; operand->mode = Addressing_Value; } break; diff --git a/src/checker.c b/src/checker.c index 8aafccb73..b416a6871 100644 --- a/src/checker.c +++ b/src/checker.c @@ -658,13 +658,12 @@ void init_universal_scope(void) { add_global_entity(make_entity_type_name(a, NULL, make_token_ident(basic_types[i].Basic.name), &basic_types[i])); } #if 1 - for (isize i = 0; i < gb_count_of(basic_type_aliases); i++) { - add_global_entity(make_entity_type_name(a, NULL, make_token_ident(basic_type_aliases[i].Basic.name), &basic_type_aliases[i])); - } + // for (isize i = 0; i < gb_count_of(basic_type_aliases); i++) { + // add_global_entity(make_entity_type_name(a, NULL, make_token_ident(basic_type_aliases[i].Basic.name), &basic_type_aliases[i])); + // } #else { t_byte = add_global_type_alias(a, str_lit("byte"), &basic_types[Basic_u8]); - t_rune = add_global_type_alias(a, str_lit("rune"), &basic_types[Basic_i32]); } #endif @@ -700,7 +699,7 @@ void init_universal_scope(void) { t_i64_ptr = make_type_pointer(a, t_i64); t_i128_ptr = make_type_pointer(a, t_i128); t_f64_ptr = make_type_pointer(a, t_f64); - t_byte_slice = make_type_slice(a, t_byte); + t_u8_slice = make_type_slice(a, t_u8); t_string_slice = make_type_slice(a, t_string); } @@ -1204,33 +1203,35 @@ void init_preload(Checker *c) { - if (record->variant_count != 22) { + if (record->variant_count != 23) { compiler_error("Invalid `TypeInfo` layout"); } t_type_info_named = record->variants[ 1]->type; t_type_info_integer = record->variants[ 2]->type; - t_type_info_float = record->variants[ 3]->type; - t_type_info_complex = record->variants[ 4]->type; - t_type_info_string = record->variants[ 5]->type; - t_type_info_boolean = record->variants[ 6]->type; - t_type_info_any = record->variants[ 7]->type; - t_type_info_pointer = record->variants[ 8]->type; - t_type_info_atomic = record->variants[ 9]->type; - t_type_info_procedure = record->variants[10]->type; - t_type_info_array = record->variants[11]->type; - t_type_info_dynamic_array = record->variants[12]->type; - t_type_info_slice = record->variants[13]->type; - t_type_info_vector = record->variants[14]->type; - t_type_info_tuple = record->variants[15]->type; - t_type_info_struct = record->variants[16]->type; - t_type_info_raw_union = record->variants[17]->type; - t_type_info_union = record->variants[18]->type; - t_type_info_enum = record->variants[19]->type; - t_type_info_map = record->variants[20]->type; - t_type_info_bit_field = record->variants[21]->type; + t_type_info_rune = record->variants[ 3]->type; + t_type_info_float = record->variants[ 4]->type; + t_type_info_complex = record->variants[ 5]->type; + t_type_info_string = record->variants[ 6]->type; + t_type_info_boolean = record->variants[ 7]->type; + t_type_info_any = record->variants[ 8]->type; + t_type_info_pointer = record->variants[ 9]->type; + t_type_info_atomic = record->variants[10]->type; + t_type_info_procedure = record->variants[11]->type; + t_type_info_array = record->variants[12]->type; + t_type_info_dynamic_array = record->variants[13]->type; + t_type_info_slice = record->variants[14]->type; + t_type_info_vector = record->variants[15]->type; + t_type_info_tuple = record->variants[16]->type; + t_type_info_struct = record->variants[17]->type; + t_type_info_raw_union = record->variants[18]->type; + t_type_info_union = record->variants[19]->type; + t_type_info_enum = record->variants[20]->type; + t_type_info_map = record->variants[21]->type; + t_type_info_bit_field = record->variants[22]->type; t_type_info_named_ptr = make_type_pointer(c->allocator, t_type_info_named); t_type_info_integer_ptr = make_type_pointer(c->allocator, t_type_info_integer); + t_type_info_rune_ptr = make_type_pointer(c->allocator, t_type_info_rune); t_type_info_float_ptr = make_type_pointer(c->allocator, t_type_info_float); t_type_info_complex_ptr = make_type_pointer(c->allocator, t_type_info_complex); t_type_info_string_ptr = make_type_pointer(c->allocator, t_type_info_string); @@ -2118,12 +2119,14 @@ void check_parsed_files(Checker *c) { } } + /* for (isize i = 0; i < gb_count_of(basic_type_aliases)-1; i++) { Type *t = &basic_type_aliases[i]; if (t->Basic.size > 0) { add_type_info_type(c, t); } } + */ #endif @@ -7526,6 +7526,10 @@ void ir_gen_tree(irGen *s) { ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), is_signed); } break; + case Basic_rune: + tag = ir_emit_conv(proc, ti_ptr, t_type_info_rune_ptr); + break; + // case Basic_f16: case Basic_f32: case Basic_f64: diff --git a/src/ir_print.c b/src/ir_print.c index d66a99c23..c7dec10cb 100644 --- a/src/ir_print.c +++ b/src/ir_print.c @@ -193,6 +193,8 @@ void ir_print_type(irFileBuffer *f, irModule *m, Type *t) { case Basic_i128: ir_fprintf(f, "i128"); return; case Basic_u128: ir_fprintf(f, "i128"); return; + case Basic_rune: ir_fprintf(f, "i32"); return; + // case Basic_f16: ir_fprintf(f, "half"); return; case Basic_f32: ir_fprintf(f, "float"); return; case Basic_f64: ir_fprintf(f, "double"); return; diff --git a/src/tokenizer.c b/src/tokenizer.c index 868ed93cc..459fca163 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -890,9 +890,9 @@ Token tokenizer_get_token(Tokenizer *t) { case '}': token.kind = Token_CloseBrace; break; case '\\': token.kind = Token_BackSlash; break; - case '≠': token.kind = Token_NotEq; break; - case '≤': token.kind = Token_LtEq; break; - case '≥': token.kind = Token_GtEq; break; + case 0x2260: token.kind = Token_NotEq; break; // '≠' + case 0x2264: token.kind = Token_LtEq; break; // '≤' + case 0x2265: token.kind = Token_GtEq; break; // '≥' case '%': token.kind = token_kind_dub_eq(t, '%', Token_Mod, Token_ModEq, Token_ModMod, Token_ModModEq); break; diff --git a/src/types.c b/src/types.c index 1364191d8..c98046a22 100644 --- a/src/types.c +++ b/src/types.c @@ -14,6 +14,8 @@ typedef enum BasicKind { Basic_i128, Basic_u128, + Basic_rune, + // Basic_f16, Basic_f32, Basic_f64, @@ -39,7 +41,6 @@ typedef enum BasicKind { Basic_COUNT, Basic_byte = Basic_u8, - Basic_rune = Basic_i32, } BasicKind; typedef enum BasicFlag { @@ -54,8 +55,8 @@ typedef enum BasicFlag { BasicFlag_Untyped = GB_BIT(8), BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Complex, - BasicFlag_Ordered = BasicFlag_Integer | BasicFlag_Float | BasicFlag_String | BasicFlag_Pointer, - BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_Pointer | BasicFlag_String | BasicFlag_Rune, + BasicFlag_Ordered = BasicFlag_Integer | BasicFlag_Float | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune, + BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune, } BasicFlag; typedef struct BasicType { @@ -235,6 +236,7 @@ gb_global Type basic_types[] = { {Type_Basic, {Basic_i128, BasicFlag_Integer, 16, STR_LIT("i128")}}, {Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, 16, STR_LIT("u128")}}, + {Type_Basic, {Basic_rune, BasicFlag_Integer | BasicFlag_Rune, 4, STR_LIT("rune")}}, // {Type_Basic, {Basic_f16, BasicFlag_Float, 2, STR_LIT("f16")}}, {Type_Basic, {Basic_f32, BasicFlag_Float, 4, STR_LIT("f32")}}, @@ -260,10 +262,10 @@ gb_global Type basic_types[] = { {Type_Basic, {Basic_UntypedNil, BasicFlag_Untyped, 0, STR_LIT("untyped nil")}}, }; -gb_global Type basic_type_aliases[] = { - {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("byte")}}, - {Type_Basic, {Basic_rune, BasicFlag_Integer, 4, STR_LIT("rune")}}, -}; +// gb_global Type basic_type_aliases[] = { +// // {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("byte")}}, +// // {Type_Basic, {Basic_rune, BasicFlag_Integer, 4, STR_LIT("rune")}}, +// }; gb_global Type *t_invalid = &basic_types[Basic_Invalid]; gb_global Type *t_bool = &basic_types[Basic_bool]; @@ -278,6 +280,8 @@ gb_global Type *t_u64 = &basic_types[Basic_u64]; gb_global Type *t_i128 = &basic_types[Basic_i128]; gb_global Type *t_u128 = &basic_types[Basic_u128]; +gb_global Type *t_rune = &basic_types[Basic_rune]; + // gb_global Type *t_f16 = &basic_types[Basic_f16]; gb_global Type *t_f32 = &basic_types[Basic_f32]; gb_global Type *t_f64 = &basic_types[Basic_f64]; @@ -301,15 +305,13 @@ gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString]; gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune]; gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil]; -gb_global Type *t_byte = &basic_type_aliases[0]; -gb_global Type *t_rune = &basic_type_aliases[1]; gb_global Type *t_u8_ptr = NULL; gb_global Type *t_int_ptr = NULL; gb_global Type *t_i64_ptr = NULL; gb_global Type *t_i128_ptr = NULL; gb_global Type *t_f64_ptr = NULL; -gb_global Type *t_byte_slice = NULL; +gb_global Type *t_u8_slice = NULL; gb_global Type *t_string_slice = NULL; @@ -323,6 +325,7 @@ gb_global Type *t_type_info_enum_value_ptr = NULL; gb_global Type *t_type_info_named = NULL; gb_global Type *t_type_info_integer = NULL; +gb_global Type *t_type_info_rune = NULL; gb_global Type *t_type_info_float = NULL; gb_global Type *t_type_info_complex = NULL; gb_global Type *t_type_info_any = NULL; @@ -345,6 +348,7 @@ gb_global Type *t_type_info_bit_field = NULL; gb_global Type *t_type_info_named_ptr = NULL; gb_global Type *t_type_info_integer_ptr = NULL; +gb_global Type *t_type_info_rune_ptr = NULL; gb_global Type *t_type_info_float_ptr = NULL; gb_global Type *t_type_info_complex_ptr = NULL; gb_global Type *t_type_info_quaternion_ptr = NULL; @@ -646,6 +650,13 @@ bool is_type_unsigned(Type *t) { } return false; } +bool is_type_rune(Type *t) { + t = core_type(t); + if (t->kind == Type_Basic) { + return (t->Basic.flags & BasicFlag_Rune) != 0; + } + return false; +} bool is_type_numeric(Type *t) { t = core_type(t); if (t->kind == Type_Basic) { @@ -932,6 +943,8 @@ bool is_type_comparable(Type *t) { case Basic_UntypedNil: case Basic_any: return false; + case Basic_rune: + return true; } return true; case Type_Pointer: |