Created
May 2, 2016 00:11
-
-
Save pervognsen/ed6074efc21e6d3400c157eb2bdf75f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { | |
output_file = CreateFileA("output.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); | |
Xeno_PrintType(&xeno_uint8); | |
Printf("\n"); | |
Xeno_PrintType(&xeno_uint32); | |
Printf("\n"); | |
Xeno_PrintType(&xeno_uint64); | |
Printf("\n"); | |
Xeno_Type pointer_uint8 = {XENO_POINTER, "pointer_uint8", sizeof(void *), 1, &xeno_uint8}; | |
Xeno_PrintType(&pointer_uint8); | |
Printf("\n"); | |
Xeno_Type pointer_pointer_uint8 = {XENO_POINTER, "pointer_pointer_uint8", sizeof(void *), 1, &pointer_uint8}; | |
Xeno_PrintType(&pointer_pointer_uint8); | |
Printf("\n"); | |
struct Person { | |
uint8_t *name; | |
uint8_t age; | |
}; | |
Xeno_Field person_fields[] = {{&pointer_uint8, "name", offsetof(Person, name)}, {&xeno_uint8, "age", offsetof(Person, age)}}; | |
Xeno_Type person_type = {XENO_AGGREGATE, "Person", sizeof(Person), 1}; | |
person_type.fields.count = 2; | |
person_type.fields.array = person_fields; | |
Xeno_PrintType(&person_type); | |
Printf("\n"); | |
uint8_t byte = 42; | |
Xeno_Data byte_data = {&byte, &xeno_uint8}; | |
Xeno_Print(byte_data); | |
Printf("\n"); | |
uint8_t bytes[3] = {12, 34, 56}; | |
Xeno_Type bytes_type = {XENO_UINT8, "bytes_type", sizeof(uint8_t), 3}; | |
Xeno_Data bytes_data = {bytes, &bytes_type}; | |
Xeno_Print(bytes_data); | |
Printf("\n"); | |
Person per = {(uint8_t *)0x12345678, 33}; | |
Xeno_Data per_data = {&per, &person_type}; | |
Xeno_Print(per_data); | |
Printf("\n"); | |
float f = 3.14f; | |
Xeno_Data f_data = {&f, &xeno_float}; | |
int32_t i; | |
Xeno_Data i_data = {&i, &xeno_int32}; | |
Xeno_Cast(i_data, f_data); | |
Printf("i=%d\n", i); | |
i = 123; | |
Xeno_Cast(f_data, i_data); | |
Printf("f=%f\n", f); | |
uint8_t b = 123; | |
uint8_t *p = &b; | |
Printf("%u\n", b); | |
Xeno_Data p_data = {&p, &xeno_uint8_pointer}; | |
Xeno_Data b_data; | |
Xeno_Dereference(&b_data, p_data); | |
Assert(b_data.type == &xeno_uint8); | |
*(uint8_t *)b_data.pointer = 42; | |
Printf("%u\n", b); | |
struct Trim { | |
float length; | |
}; | |
Xeno_Field trim_fields[] = {{&xeno_float, "length", offsetof(Trim, length)}}; | |
Xeno_Type trim_type = {XENO_AGGREGATE, "Trim", sizeof(Trim), 1}; | |
trim_type.fields.count = 1; | |
trim_type.fields.array = trim_fields; | |
struct Window { | |
uint16_t type; | |
float width; | |
float height; | |
Trim trims[4]; | |
}; | |
Xeno_Type window_trims_type = trim_type; | |
window_trims_type.count = 4; | |
Xeno_Field window_fields[] = {{&xeno_uint16, "type", offsetof(Window, type)}, | |
{&xeno_float, "width", offsetof(Window, width)}, | |
{&xeno_float, "height", offsetof(Window, height)}, | |
{&window_trims_type, "trims", offsetof(Window, trims)}}; | |
Xeno_Type window_type = {XENO_AGGREGATE, "Window", sizeof(Window), 1}; | |
window_type.fields.count = 4; | |
window_type.fields.array = window_fields; | |
struct Building { | |
char *title; | |
float width; | |
float height; | |
Window windows[6]; | |
}; | |
Xeno_Type building_windows_type = window_type; | |
building_windows_type.count = 6; | |
Xeno_Field building_fields[] = {{&xeno_uint8_pointer, "title", offsetof(Building, title)}, | |
{&xeno_float, "width", offsetof(Building, width)}, | |
{&xeno_float, "height", offsetof(Building, height)}, | |
{&building_windows_type, "windows", offsetof(Building, windows)}}; | |
Xeno_Type building_type = {XENO_AGGREGATE, "Building", sizeof(Building), 1}; | |
building_type.fields.count = 4; | |
building_type.fields.array = building_fields; | |
Building building = {0}; | |
Xeno_Data building_data = {&building, &building_type}; | |
Xeno_Print(building_data); | |
Printf("\n"); | |
Xeno_Data data = building_data; | |
Xeno_GetField(&data, data, Xeno_GetFieldIndex(data.type, "windows")); | |
Xeno_GetElement(&data, data, 3); | |
Xeno_Data height_data = data; | |
Xeno_GetField(&height_data, data, Xeno_GetFieldIndex(data.type, "height")); | |
Assert(height_data.type == &xeno_float); | |
*(float *)height_data.pointer = 3.14f; | |
Xeno_GetField(&data, data, Xeno_GetFieldIndex(data.type, "trims")); | |
Xeno_GetElement(&data, data, 2); | |
Xeno_GetField(&data, data, Xeno_GetFieldIndex(data.type, "length")); | |
Assert(data.type == &xeno_float); | |
*(float *)data.pointer = 1.23f; | |
Xeno_Print(building_data); | |
Printf("\n"); | |
union float_uint32 { | |
float f; | |
uint32_t u; | |
}; | |
Xeno_Field float_uint32_fields[] = {{&xeno_float, "f", offsetof(float_uint32, f)}, | |
{&xeno_uint32, "u", offsetof(float_uint32, u)}}; | |
Xeno_Type float_uint32_type = {XENO_AGGREGATE, "float_uint32", sizeof(float_uint32), 1}; | |
float_uint32_type.fields.count = 2; | |
float_uint32_type.fields.array = float_uint32_fields; | |
float_uint32 fu; | |
fu.f = 3.14f; | |
Xeno_Data fu_data = {&fu, &float_uint32_type}; | |
Xeno_Print(fu_data); | |
Printf("\n"); | |
fu.u += 10000; | |
Xeno_Print(fu_data); | |
Printf("\n"); | |
if (output_file != INVALID_HANDLE_VALUE) { | |
CloseHandle(output_file); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment