mirror of
https://github.com/Ed94/metadesk.git
synced 2026-06-12 23:51:37 -07:00
[examples] generate map functions
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:776
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:775
|
||||
TypeInfoMember Circle_members[2] = {
|
||||
{"r", 1, -1, &F32_type_info},
|
||||
{"pos", 3, -1, &V2F32_type_info},
|
||||
@@ -14,14 +14,14 @@ TypeInfoMember RoundedPolygon_members[3] = {
|
||||
{"p", 1, 1, &V2F32_type_info},
|
||||
};
|
||||
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:815
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:814
|
||||
TypeInfoEnumerant Shape_members[3] = {
|
||||
{"Circle", 6, 1},
|
||||
{"Segment", 7, 2},
|
||||
{"Polygon", 7, 3},
|
||||
};
|
||||
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:851
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:850
|
||||
TypeInfo U32_type_info = {TypeKind_Basic, "U32", 3, 4, 0, 0};
|
||||
TypeInfo F32_type_info = {TypeKind_Basic, "F32", 3, 4, 0, 0};
|
||||
TypeInfo V2F32_type_info = {TypeKind_Basic, "V2F32", 5, 8, 0, 0};
|
||||
@@ -30,3 +30,33 @@ TypeInfo RoundedSegment_type_info = {TypeKind_Struct, "RoundedSegment", 14, 3, R
|
||||
TypeInfo RoundedPolygon_type_info = {TypeKind_Struct, "RoundedPolygon", 14, 3, RoundedPolygon_members, 0};
|
||||
TypeInfo Shape_type_info = {TypeKind_Enum, "Shape", 5, 3, Shape_members, &U32_type_info};
|
||||
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:910
|
||||
TypeInfo*
|
||||
type_info_from_shape(Shape v)
|
||||
{
|
||||
TypeInfo* result;
|
||||
memset(&result, 0, sizeof(result));
|
||||
switch (v)
|
||||
{
|
||||
case Shape_Circle:
|
||||
{
|
||||
result = &Circle_type_info;
|
||||
}break;
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
U32
|
||||
max_slot_from_shape(Shape v)
|
||||
{
|
||||
U32 result;
|
||||
memset(&result, 0, sizeof(result));
|
||||
switch (v)
|
||||
{
|
||||
case Shape_Polygon:
|
||||
{
|
||||
result = 12;
|
||||
}break;
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ Shape_Polygon = 3,
|
||||
TypeInfo* type_info_from_shape(Shape v);
|
||||
U32 max_slot_from_shape(Shape v);
|
||||
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:760
|
||||
// generated by W:/metadesk/examples/type_metadata/type_metadata.c:759
|
||||
extern TypeInfo U32_type_info;
|
||||
extern TypeInfo F32_type_info;
|
||||
extern TypeInfo V2F32_type_info;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
//~ setup base types //////////////////////////////////////////////////////////
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
typedef uint32_t U32;
|
||||
typedef float F32;
|
||||
typedef struct V2F32 V2F32;
|
||||
|
||||
@@ -733,8 +733,7 @@ gen_function_declarations_from_maps(FILE *out)
|
||||
{
|
||||
if (map->types_are_good)
|
||||
{
|
||||
MD_Node *node = map->node;
|
||||
|
||||
// TODO deduplicate
|
||||
MD_String8 in_type = map->in->node->string;
|
||||
MD_String8 out_type = {0};
|
||||
if (map->out_is_type_info_ptr)
|
||||
@@ -747,7 +746,7 @@ gen_function_declarations_from_maps(FILE *out)
|
||||
}
|
||||
|
||||
fprintf(out, "%.*s %.*s(%.*s v);\n",
|
||||
MD_S8VArg(out_type), MD_S8VArg(node->string), MD_S8VArg(in_type));
|
||||
MD_S8VArg(out_type), MD_S8VArg(map->node->string), MD_S8VArg(in_type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -903,6 +902,68 @@ gen_type_info_definitions_from_types(FILE *out)
|
||||
MD_ReleaseScratch(scratch);
|
||||
}
|
||||
|
||||
void
|
||||
gen_function_definitions_from_maps(FILE *out)
|
||||
{
|
||||
MD_ArenaTemp scratch = MD_GetScratch(0, 0);
|
||||
|
||||
MD_PrintGenNoteCComment(out);
|
||||
|
||||
for (GEN_MapInfo *map = first_map;
|
||||
map != 0;
|
||||
map = map->next)
|
||||
{
|
||||
if (map->types_are_good)
|
||||
{
|
||||
// TODO deduplicate
|
||||
MD_String8 in_type = map->in->node->string;
|
||||
MD_String8 out_type = {0};
|
||||
if (map->out_is_type_info_ptr)
|
||||
{
|
||||
out_type = MD_S8Lit("TypeInfo*");
|
||||
}
|
||||
else
|
||||
{
|
||||
out_type = map->out->node->string;
|
||||
}
|
||||
|
||||
fprintf(out, "%.*s\n", MD_S8VArg(out_type));
|
||||
fprintf(out, "%.*s(%.*s v)\n", MD_S8VArg(map->node->string), MD_S8VArg(in_type));
|
||||
fprintf(out, "{\n");
|
||||
// TODO implement default
|
||||
// TODO implement auto
|
||||
fprintf(out, "%.*s result;\n", MD_S8VArg(out_type));
|
||||
fprintf(out, "memset(&result, 0, sizeof(result));\n");
|
||||
fprintf(out, "switch (v)\n");
|
||||
fprintf(out, "{\n");
|
||||
for (GEN_MapCase *map_case = map->first_case;
|
||||
map_case != 0;
|
||||
map_case = map_case->next)
|
||||
{
|
||||
MD_String8 in_expr = map_case->in_enumerant->node->string;
|
||||
MD_String8 out_expr = map_case->out->string;
|
||||
MD_String8 val_expr = out_expr;
|
||||
if (map->out_is_type_info_ptr)
|
||||
{
|
||||
val_expr = MD_S8Fmt(scratch.arena, "&%.*s_type_info", MD_S8VArg(out_expr));
|
||||
}
|
||||
|
||||
fprintf(out, "case %.*s_%.*s:\n", MD_S8VArg(in_type), MD_S8VArg(in_expr));
|
||||
fprintf(out, "{\n");
|
||||
fprintf(out, "result = %.*s;\n", MD_S8VArg(val_expr));
|
||||
fprintf(out, "}break;\n");
|
||||
}
|
||||
fprintf(out, "}\n");
|
||||
fprintf(out, "return(result);\n");
|
||||
fprintf(out, "}\n");
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "\n");
|
||||
|
||||
MD_ReleaseScratch(scratch);
|
||||
}
|
||||
|
||||
|
||||
//~ main //////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -957,6 +1018,7 @@ main(int argc, char **argv)
|
||||
gen_equip_map_in_out_types();
|
||||
gen_equip_map_cases();
|
||||
gen_check_duplicate_cases();
|
||||
// TODO check 'complete'
|
||||
|
||||
// generate header file
|
||||
{
|
||||
@@ -978,8 +1040,7 @@ main(int argc, char **argv)
|
||||
gen_struct_member_tables_from_types(c);
|
||||
gen_enum_member_tables_from_types(c);
|
||||
gen_type_info_definitions_from_types(c);
|
||||
|
||||
// TODO generate function definitions
|
||||
gen_function_definitions_from_maps(c);
|
||||
|
||||
// close output file
|
||||
fclose(c);
|
||||
@@ -1029,5 +1090,15 @@ main(int argc, char **argv)
|
||||
{
|
||||
MD_Node *node = map->node;
|
||||
printf("%.*s: map\n", MD_S8VArg(node->string));
|
||||
|
||||
// print case list
|
||||
for (GEN_MapCase *map_case = map->first_case;
|
||||
map_case != 0;
|
||||
map_case = map_case->next)
|
||||
{
|
||||
printf(" %.*s -> %.*s\n",
|
||||
MD_S8VArg(map_case->in_enumerant->node->string),
|
||||
MD_S8VArg(map_case->out->string));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +113,7 @@ void gen_type_info_declarations_from_types(FILE *out);
|
||||
void gen_struct_member_tables_from_types(FILE *out);
|
||||
void gen_enum_member_tables_from_types(FILE *out);
|
||||
void gen_type_info_definitions_from_types(FILE *out);
|
||||
void gen_function_definitions_from_maps(FILE *out);
|
||||
|
||||
|
||||
#endif //TYPE_METADATA_H
|
||||
|
||||
Reference in New Issue
Block a user