enum_to_string

This commit is contained in:
Ginger Bill
2016-09-19 21:33:52 +01:00
parent bbc9739f5c
commit 9561dc33ce
17 changed files with 660 additions and 214 deletions
+60 -38
View File
@@ -1,5 +1,60 @@
#import "os.odin" as os
PRINT_BUF_SIZE :: 1<<12
fprint :: proc(f: ^os.File, args: ..any) {
data: [PRINT_BUF_SIZE]byte
buf := data[:0]
prev_string := false
for i := 0; i < args.count; i++ {
arg := args[i]
is_string := arg.data != null && type_info_is_string(arg.type_info)
if i > 0 && is_string && !prev_string {
print_space_to_buffer(^buf)
}
print_any_to_buffer(^buf, arg)
prev_string = is_string;
}
os.write(f, buf)
}
fprintln :: proc(f: ^os.File, args: ..any) {
data: [PRINT_BUF_SIZE]byte
buf := data[:0]
for i := 0; i < args.count; i++ {
if i > 0 {
append(^buf, #rune " ")
}
print_any_to_buffer(^buf, args[i])
}
print_nl_to_buffer(^buf)
os.write(f, buf)
}
fprintf :: proc(f: ^os.File, fmt: string, args: ..any) {
data: [PRINT_BUF_SIZE]byte
buf := data[:0]
printf_to_buffer(^buf, fmt, ..args)
os.write(f, buf)
}
print :: proc(args: ..any) {
fprint(os.stdout, ..args)
}
println :: proc(args: ..any) {
fprintln(os.stdout, ..args)
}
printf :: proc(fmt: string, args: ..any) {
fprintf(os.stdout, fmt, ..args)
}
print_byte_buffer :: proc(buf: ^[]byte, b: []byte) {
if buf.count < buf.capacity {
n := min(buf.capacity-buf.count, b.count)
@@ -68,7 +123,7 @@ print_nl_to_buffer :: proc(buf: ^[]byte) { print_rune_to_buffer(buf, #rune "\
print_int_to_buffer :: proc(buf: ^[]byte, i: int) {
print_int_base_to_buffer(buf, i, 10);
}
PRINT__NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"
__NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"
print_int_base_to_buffer :: proc(buffer: ^[]byte, i, base: int) {
buf: [65]byte
@@ -83,7 +138,7 @@ print_int_base_to_buffer :: proc(buffer: ^[]byte, i, base: int) {
len++
}
for i > 0 {
buf[len] = PRINT__NUM_TO_CHAR_TABLE[i % base]
buf[len] = __NUM_TO_CHAR_TABLE[i % base]
len++
i /= base
}
@@ -108,7 +163,7 @@ print_uint_base_to_buffer :: proc(buffer: ^[]byte, i, base: uint, min_width: int
len++
}
for i > 0 {
buf[len] = PRINT__NUM_TO_CHAR_TABLE[i % base]
buf[len] = __NUM_TO_CHAR_TABLE[i % base]
len++
i /= base
}
@@ -149,7 +204,7 @@ print__f64 :: proc(buffer: ^[]byte, f: f64, decimal_places: int) {
len++
}
for i > 0 {
buf[len] = PRINT__NUM_TO_CHAR_TABLE[i % 10]
buf[len] = __NUM_TO_CHAR_TABLE[i % 10]
len++
i /= 10
}
@@ -334,7 +389,6 @@ print_any_to_buffer :: proc(buf: ^[]byte, arg: any) {
case 2: i = (arg.data as ^i16)^ as int
case 4: i = (arg.data as ^i32)^ as int
case 8: i = (arg.data as ^i64)^ as int
case 16: i = (arg.data as ^i128)^ as int
}
}
print_int_to_buffer(buf, i)
@@ -346,7 +400,6 @@ print_any_to_buffer :: proc(buf: ^[]byte, arg: any) {
case 2: i = (arg.data as ^u16)^ as uint
case 4: i = (arg.data as ^u32)^ as uint
case 8: i = (arg.data as ^u64)^ as uint
case 16: i = (arg.data as ^u128)^ as uint
}
}
print_uint_to_buffer(buf, i)
@@ -488,7 +541,7 @@ type_info_is_string :: proc(info: ^Type_Info) -> bool {
}
print_to_buffer :: proc(buf: ^[]byte, fmt: string, args: ..any) {
printf_to_buffer :: proc(buf: ^[]byte, fmt: string, args: ..any) {
is_digit :: proc(r: rune) -> bool #inline {
return r >= #rune "0" && r <= #rune "9"
}
@@ -550,34 +603,3 @@ print_to_buffer :: proc(buf: ^[]byte, fmt: string, args: ..any) {
print_string_to_buffer(buf, fmt[prev:])
}
PRINT_BUF_SIZE :: 1<<12
print_to_file :: proc(f: ^os.File, fmt: string, args: ..any) {
data: [PRINT_BUF_SIZE]byte
buf := data[:0]
print_to_buffer(^buf, fmt, ..args)
os.write(f, buf)
}
println_to_file :: proc(f: ^os.File, fmt: string, args: ..any) {
data: [PRINT_BUF_SIZE]byte
buf := data[:0]
print_to_buffer(^buf, fmt, ..args)
print_nl_to_buffer(^buf)
os.write(f, buf)
}
print :: proc(fmt: string, args: ..any) {
print_to_file(os.get_standard_file(os.File_Standard.OUTPUT), fmt, ..args)
}
print_err :: proc(fmt: string, args: ..any) {
print_to_file(os.get_standard_file(os.File_Standard.ERROR), fmt, ..args)
}
println :: proc(fmt: string, args: ..any) {
println_to_file(os.get_standard_file(os.File_Standard.OUTPUT), fmt, ..args)
}
println_err :: proc(fmt: string, args: ..any) {
println_to_file(os.get_standard_file(os.File_Standard.ERROR), fmt, ..args)
}
+5 -4
View File
@@ -46,6 +46,11 @@ File_Standard :: type enum {
__std_files := __set_file_standards();
stdin := ^__std_files[File_Standard.INPUT]
stdout := ^__std_files[File_Standard.OUTPUT]
stderr := ^__std_files[File_Standard.ERROR]
__set_file_standards :: proc() -> [File_Standard.COUNT as int]File {
return [File_Standard.COUNT as int]File{
File{handle = win32.GetStdHandle(win32.STD_INPUT_HANDLE)},
@@ -54,10 +59,6 @@ __set_file_standards :: proc() -> [File_Standard.COUNT as int]File {
}
}
get_standard_file :: proc(std: File_Standard) -> ^File {
return ^__std_files[std]
}
read_entire_file :: proc(name: string) -> (string, bool) {
buf: [300]byte
+29 -8
View File
@@ -17,7 +17,6 @@ Type_Info :: union {
ordered: bool
}
Named: struct #ordered {
name: string
base: ^Type_Info
@@ -59,6 +58,8 @@ Type_Info :: union {
Raw_Union: Record
Enum: struct #ordered {
base: ^Type_Info
values: []i64
names: []string
}
}
@@ -170,7 +171,7 @@ __string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >
__assert :: proc(msg: string) {
fmt.print_err("%", msg)
fmt.fprintln(os.stderr, msg)
__debug_trap()
}
@@ -179,8 +180,8 @@ __bounds_check_error :: proc(file: string, line, column: int,
if 0 <= index && index < count {
return
}
fmt.println_err("%(%:%) Index % is out of bounds range [0, %)",
file, line, column, index, count)
fmt.fprintf(os.stderr, "%(%:%) Index % is out of bounds range [0, %)\n",
file, line, column, index, count)
__debug_trap()
}
@@ -189,8 +190,8 @@ __slice_expr_error :: proc(file: string, line, column: int,
if 0 <= low && low <= high && high <= max {
return
}
fmt.println_err("%(%:%) Invalid slice indices: [%:%:%]",
file, line, column, low, high, max)
fmt.fprintf(os.stderr, "%(%:%) Invalid slice indices: [%:%:%]\n",
file, line, column, low, high, max)
__debug_trap()
}
__substring_expr_error :: proc(file: string, line, column: int,
@@ -198,8 +199,8 @@ __substring_expr_error :: proc(file: string, line, column: int,
if 0 <= low && low <= high {
return
}
fmt.println_err("%(%:%) Invalid substring indices: [%:%:%]",
file, line, column, low, high)
fmt.fprintf(os.stderr, "%(%:%) Invalid substring indices: [%:%:%]\n",
file, line, column, low, high)
__debug_trap()
}
@@ -341,5 +342,25 @@ __default_allocator :: proc() -> Allocator {
}
__enum_to_string :: proc(info: ^Type_Info, value: i64) -> string {
for {
match type i : info {
case Type_Info.Named:
info = i.base
continue
}
break
}
match type ti : info {
case Type_Info.Enum:
fmt.println("Here: ", ti.values.count)
for i := 0; i < ti.values.count; i++ {
if ti.values[i] == value {
return ti.names[i]
}
}
}
return ""
}