Skip to content

Instantly share code, notes, and snippets.

@sir-pinecone
Forked from gingerBill/augmented.c
Created July 19, 2017 06:02
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 sir-pinecone/115abd4a28046bae2873306e47f98914 to your computer and use it in GitHub Desktop.
Save sir-pinecone/115abd4a28046bae2873306e47f98914 to your computer and use it in GitHub Desktop.
Augmented C - 002
#ifndef AUGMENTED_C_GUARD
#define AUGMENTED_C_GUARD
#include <stddef.h>
typedef ptrdiff_t isize;
#define offset_of(Type, element) ((isize)&(((Type *)0)->element))
#define JOIN2_IND(a, b) a##b
#define JOIN2(a, b) JOIN2_IND(a, b)
#define JOIN3(a, b, c) JOIN2(JOIN2(a, b), c)
#define MEMBER_TYPE(Type) JOIN2(MetaMember_, Type)
#define STRUCT_MEMBERS(Type) JOIN2(struct_members_of, Type)
#define ENUM_MEMBERS(Type) JOIN2(enum_members_of, Type)
#define member_memory(memory, member) (void *)((char *)(memory) + (member).offset)
#define tag_enum(Parent, Type) JOIN3(Parent, Enum_, Type)
#define tag_type(Parent, Type) JOIN3(Parent, Tag, Type)
typedef struct EnumMember EnumMember;
struct EnumMember {
char const *string;
long long value;
};
typedef enum MemberType MemberType;
enum MemberType {
MEMBER_TYPE(float),
MEMBER_TYPE(TestType),
MEMBER_TYPE(unsigned),
MEMBER_TYPE(char),
MEMBER_TYPE(EntityTagEnum),
MEMBER_TYPE(__Count)
};
static EnumMember ENUM_MEMBERS(MemberType)[] = {
{"float", MEMBER_TYPE(float)},
{"TestType", MEMBER_TYPE(TestType)},
{"unsigned", MEMBER_TYPE(unsigned)},
{"char", MEMBER_TYPE(char)},
{"EntityTagEnum", MEMBER_TYPE(EntityTagEnum)},
};
typedef struct StructMember StructMember;
struct StructMember {
MemberType type;
char *name;
int pointer_count;
int is_array; // boolean
isize array_count;
isize size, offset;
};
#endif
#include <stdio.h>
enum TestType {
Test_0,
Test_1,
Test_3 = 3,
Test_4,
Test_5,
};
struct Test {
float x;
float y;
TestType type;
};
static int func_count = 0;
struct Entity {
unsigned id;
float x, y;
char *name;
tagged_union {
Tree { int leaf_count; };
Frog { int ribbits; };
Gun { int bullets; };
};
};
void func(void) {
printf("func() %d\n", func_count++);
}
int main() {
int a = 0;
defer func();
if (true) {
printf("here\n");
if (func_count == 0)
return 1;
}
return 0;
}
#line 1 "W:/AugmentedC/data/test.augc"
#ifndef TEST_AUGC_GUARD
#define TEST_AUGC_GUARD
// TODO(bill): This still doesn't have #line macros everywhere to make the original `test.aug` easier to debug
#ifndef AUGMENTED_C_GUARD
#include "augmented.c"
#endif
#include <stdio.h>
typedef enum TestType TestType;
#line 3
enum TestType {
Test_0,
Test_1,
Test_3 = 3,
Test_4,
Test_5,
};
#line 10
static EnumMember ENUM_MEMBERS(TestType)[] = {
{"Test_0", Test_0},
{"Test_1", Test_1},
{"Test_3", Test_3},
{"Test_4", Test_4},
{"Test_5", Test_5},
};
typedef struct Test Test;
#line 11
struct Test {
float x;
float y;
TestType type;
};
static StructMember STRUCT_MEMBERS(Test)[] = {
{MEMBER_TYPE(float), "x", 0, 0, 0, (isize)sizeof(((Test *)0)->x), offset_of(Test, x)},
{MEMBER_TYPE(float), "y", 0, 0, 0, (isize)sizeof(((Test *)0)->y), offset_of(Test, y)},
{MEMBER_TYPE(TestType), "type", 0, 0, 0, (isize)sizeof(((Test *)0)->type), offset_of(Test, type)},
};
#line 15
static int func_count = 0;
typedef struct Entity Entity;
typedef enum EntityTagEnum EntityTagEnum;
enum EntityTagEnum {
tag_enum(Entity, Tree),
tag_enum(Entity, Frog),
tag_enum(Entity, Gun),
};
typedef struct tag_type(Entity, Tree) tag_type(Entity, Tree);
struct tag_type(Entity, Tree) {
int leaf_count;
};
typedef struct tag_type(Entity, Frog) tag_type(Entity, Frog);
struct tag_type(Entity, Frog) {
int ribbits;
};
typedef struct tag_type(Entity, Gun) tag_type(Entity, Gun);
struct tag_type(Entity, Gun) {
int bullets;
};
#line 19
struct Entity {
unsigned id;
float x, y;
char *name;
EntityTagEnum tag_type;
union {
tag_type(Entity, Tree) Tree;
tag_type(Entity, Frog) Frog;
tag_type(Entity, Gun) Gun;
};
};
static StructMember STRUCT_MEMBERS(Entity)[] = {
{MEMBER_TYPE(unsigned), "id", 0, 0, 0, (isize)sizeof(((Entity *)0)->id), offset_of(Entity, id)},
{MEMBER_TYPE(float), "x", 0, 0, 0, (isize)sizeof(((Entity *)0)->x), offset_of(Entity, x)},
{MEMBER_TYPE(float), "y", 0, 0, 0, (isize)sizeof(((Entity *)0)->y), offset_of(Entity, y)},
{MEMBER_TYPE(char), "name", 1, 0, 0, (isize)sizeof(((Entity *)0)->name), offset_of(Entity, name)},
{MEMBER_TYPE(EntityTagEnum), "tag_type", 0, 0, 0, (isize)sizeof(((Entity *)0)->tag_type), offset_of(Entity, tag_type)},
};
#line 29
void func(void) {
printf("func() %d\n", func_count++);
}
int main() {
int a = 0;
if (true) {
printf("here\n");
if (func_count == 0)
{{
#line 38
func();} return 1;}
#line 46
}
{{
#line 38
func();} return 0;}
#line 49
}
#endif // TEST_AUGC_GUARD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment