Enumerated arrays [Enum_Type]Elem_Type

This commit is contained in:
gingerBill
2019-12-27 12:51:02 +00:00
parent eea403d0ab
commit 10f0961184
11 changed files with 864 additions and 146 deletions
+80
View File
@@ -873,6 +873,49 @@ fmt_enum :: proc(fi: ^Info, v: any, verb: rune) {
}
stored_enum_value_to_string :: proc(enum_type: ^runtime.Type_Info, ev: runtime.Type_Info_Enum_Value, offset: int = 0) -> (string, bool) {
et := runtime.type_info_base(enum_type);
#partial switch e in et.variant {
case: return "", false;
case runtime.Type_Info_Enum:
get_str :: proc(i: $T, e: runtime.Type_Info_Enum) -> (string, bool) {
if reflect.is_string(e.base) {
for val, idx in e.values {
if v, ok := val.(T); ok && v == i {
return e.names[idx], true;
}
}
} else if len(e.values) == 0 {
return "", true;
} else {
for val, idx in e.values {
if v, ok := val.(T); ok && v == i {
return e.names[idx], true;
}
}
}
return "", false;
}
switch v in ev {
case rune: return get_str(v + auto_cast offset, e);
case i8: return get_str(v + auto_cast offset, e);
case i16: return get_str(v + auto_cast offset, e);
case i32: return get_str(v + auto_cast offset, e);
case i64: return get_str(v + auto_cast offset, e);
case int: return get_str(v + auto_cast offset, e);
case u8: return get_str(v + auto_cast offset, e);
case u16: return get_str(v + auto_cast offset, e);
case u32: return get_str(v + auto_cast offset, e);
case u64: return get_str(v + auto_cast offset, e);
case uint: return get_str(v + auto_cast offset, e);
case uintptr: return get_str(v + auto_cast offset, e);
}
}
return "", false;
}
enum_value_to_u64 :: proc(ev: runtime.Type_Info_Enum_Value) -> u64 {
switch i in ev {
@@ -892,6 +935,24 @@ enum_value_to_u64 :: proc(ev: runtime.Type_Info_Enum_Value) -> u64 {
return 0;
}
enum_value_to_i64 :: proc(ev: runtime.Type_Info_Enum_Value) -> i64 {
switch i in ev {
case rune: return i64(i);
case i8: return i64(i);
case i16: return i64(i);
case i32: return i64(i);
case i64: return i64(i);
case int: return i64(i);
case u8: return i64(i);
case u16: return i64(i);
case u32: return i64(i);
case u64: return i64(i);
case uint: return i64(i);
case uintptr: return i64(i);
}
return 0;
}
fmt_bit_set :: proc(fi: ^Info, v: any, name: string = "") {
is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
if ti == nil {
@@ -1241,6 +1302,25 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
fmt_arg(fi, any{rawptr(data), info.elem.id}, verb);
}
case runtime.Type_Info_Enumerated_Array:
strings.write_byte(fi.buf, '[');
defer strings.write_byte(fi.buf, ']');
for i in 0..<info.count {
if i > 0 do strings.write_string(fi.buf, ", ");
idx, ok := stored_enum_value_to_string(info.index, info.min_value, i);
if ok {
strings.write_byte(fi.buf, '.');
strings.write_string(fi.buf, idx);
} else {
strings.write_i64(fi.buf, enum_value_to_i64(info.min_value)+i64(i));
}
strings.write_string(fi.buf, " = ");
data := uintptr(v.data) + uintptr(i*info.elem_size);
fmt_arg(fi, any{rawptr(data), info.elem.id}, verb);
}
case runtime.Type_Info_Dynamic_Array:
if verb == 'p' {
slice := cast(^mem.Raw_Dynamic_Array)v.data;
+2
View File
@@ -20,6 +20,7 @@ Type_Kind :: enum {
Pointer,
Procedure,
Array,
Enumerated_Array,
Dynamic_Array,
Slice,
Tuple,
@@ -51,6 +52,7 @@ type_kind :: proc(T: typeid) -> Type_Kind {
case runtime.Type_Info_Pointer: return .Pointer;
case runtime.Type_Info_Procedure: return .Procedure;
case runtime.Type_Info_Array: return .Array;
case runtime.Type_Info_Enumerated_Array: return .Enumerated_Array;
case runtime.Type_Info_Dynamic_Array: return .Dynamic_Array;
case runtime.Type_Info_Slice: return .Slice;
case runtime.Type_Info_Tuple: return .Tuple;
+14
View File
@@ -82,6 +82,13 @@ are_types_identical :: proc(a, b: ^rt.Type_Info) -> bool {
if x.count != y.count do return false;
return are_types_identical(x.elem, y.elem);
case rt.Type_Info_Enumerated_Array:
y, ok := b.variant.(rt.Type_Info_Enumerated_Array);
if !ok do return false;
if x.count != y.count do return false;
return are_types_identical(x.index, y.index) &&
are_types_identical(x.elem, y.elem);
case rt.Type_Info_Dynamic_Array:
y, ok := b.variant.(rt.Type_Info_Dynamic_Array);
if !ok do return false;
@@ -397,6 +404,13 @@ write_type :: proc(buf: ^strings.Builder, ti: ^rt.Type_Info) {
write_i64(buf, i64(info.count), 10);
write_string(buf, "]");
write_type(buf, info.elem);
case rt.Type_Info_Enumerated_Array:
write_string(buf, "[");
write_type(buf, info.index);
write_string(buf, "]");
write_type(buf, info.elem);
case rt.Type_Info_Dynamic_Array:
write_string(buf, "[dynamic]");
write_type(buf, info.elem);
+9
View File
@@ -79,6 +79,14 @@ Type_Info_Array :: struct {
elem_size: int,
count: int,
};
Type_Info_Enumerated_Array :: struct {
elem: ^Type_Info,
index: ^Type_Info,
elem_size: int,
count: int,
min_value: Type_Info_Enum_Value,
max_value: Type_Info_Enum_Value,
};
Type_Info_Dynamic_Array :: struct {elem: ^Type_Info, elem_size: int};
Type_Info_Slice :: struct {elem: ^Type_Info, elem_size: int};
Type_Info_Tuple :: struct { // Only really used for procedures
@@ -156,6 +164,7 @@ Type_Info :: struct {
Type_Info_Pointer,
Type_Info_Procedure,
Type_Info_Array,
Type_Info_Enumerated_Array,
Type_Info_Dynamic_Array,
Type_Info_Slice,
Type_Info_Tuple,
+8
View File
@@ -172,6 +172,14 @@ print_type :: proc(fd: os.Handle, ti: ^Type_Info) {
print_u64(fd, u64(info.count));
os.write_byte(fd, ']');
print_type(fd, info.elem);
case Type_Info_Enumerated_Array:
os.write_byte(fd, '[');
print_type(fd, info.index);
os.write_byte(fd, ']');
print_type(fd, info.elem);
case Type_Info_Dynamic_Array:
os.write_string(fd, "[dynamic]");
print_type(fd, info.elem);