Skip to content

Instantly share code, notes, and snippets.

@ArnaudValensi
Created May 15, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArnaudValensi/38f048ed9444f3a0f7ca54013a52d78a to your computer and use it in GitHub Desktop.
Save ArnaudValensi/38f048ed9444f3a0f7ca54013a52d78a to your computer and use it in GitHub Desktop.
Debug variables and types
// Pretty print the values of a variable recursively.
debug_variable :: (t: $T, prefix: string = "") {
print("%0%0\n", prefix, formatStruct(
t,
use_newlines_if_long_form=true,
use_long_form_if_more_than_this_many_members=1
));
}
// Pretty print the type of a variable recursively like this:
//
// name: Source_Code_Location {
// fully_pathed_filename: int
// other_struct: MyStruct {
// var1: int
// var2: int
// }
// }
//
debug_variable_type :: (name: string, type: $T, indent_size := 4) {
ti := type_info(T);
debug_type(name, ti, indent=0, indent_size=indent_size);
}
// Like `debug_variable` but take a type instead of a variable.
debug_type :: (name: string, ti: *Type_Info, indent: int = 0, indent_size: int = 4) {
if ti.type == .STRUCT {
ti_struct: *Type_Info_Struct = cast(*Type_Info_Struct) ti;
for 1..indent print(" ");
print("%0: %0 {\n", name, ti_struct.name);
for member: ti_struct.members {
debug_type(member.name, member.type, indent + indent_size, indent_size);
}
for 1..indent print(" ");
print("}\n");
} else {
type: string = ---;
if ti.type == .INTEGER {
tii : *Type_Info_Integer = xx ti;
if tii.runtime_size == 1 && tii.signed {
type = "s8";
} else if tii.runtime_size == 2 && tii.signed {
type = "s16";
} else if tii.runtime_size == 4 && tii.signed {
type = "s32";
} else if tii.runtime_size == 8 && tii.signed {
type = "s64";
} else if tii.runtime_size == 1 && !tii.signed {
type = "u8";
} else if tii.runtime_size == 2 && !tii.signed {
type = "u16";
} else if tii.runtime_size == 4 && !tii.signed {
type = "u32";
} else if tii.runtime_size == 8 && !tii.signed {
type = "u64";
} else {
panic("Unknown integer type");
}
} else if ti.type == .FLOAT {
tif : *Type_Info_Float = xx ti;
if tif.runtime_size == 4 {
type = "float32";
} else if tif.runtime_size == 8 {
type = "float64";
} else {
panic("Unknown float type");
}
} else {
type = tprint("%", ti.type);
}
for 1..indent print(" ");
print("%: %\n", name, type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment