Skip to content

Commit

Permalink
feat: Add lengthof generated extern for lookup tables
Browse files Browse the repository at this point in the history
  • Loading branch information
lhearachel committed Jan 15, 2025
1 parent a0b6865 commit ec95481
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 14;
const entry__stdin lookup__stdin[] = {
{ BLASTOISE, "BLASTOISE", },
{ BULBASAUR, "BULBASAUR", },
Expand Down
18 changes: 11 additions & 7 deletions src/generators/generate_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ static const char *lookup_branch_fmt = ""
"\n"
"#ifndef %sLOOKUP_IMPL\n"
"\n"
"extern const long lengthof__%s;\n"
"extern const entry__%s lookup__%s[];\n"
"\n"
"#else\n"
"\n"
"const long lengthof__%s = %d;\n"
"const entry__%s lookup__%s[] = {\n"
"";

Expand Down Expand Up @@ -133,13 +135,15 @@ bool generate_c(enumerator *input, options *opts, FILE *fout)
}

fprintf(fout, lookup_branch_fmt,
guardp.buf,
guardp.buf,
opts->tag.buf,
opts->tag.buf,
guardp.buf,
opts->tag.buf, opts->tag.buf,
opts->tag.buf, opts->tag.buf);
guardp.buf, // "#endif /* %sENUM */\n"
guardp.buf, // "#ifdef %sLOOKUP\n"
opts->tag.buf, // "typedef struct entry__%s {\n"
opts->tag.buf, // "} entry__%s;\n"
guardp.buf, // "#ifndef %sLOOKUP_IMPL\n"
opts->tag.buf, // "extern const long lengthof__%s;\n"
opts->tag.buf, opts->tag.buf, // "extern const entry__%s lookup__%s[];\n"
opts->tag.buf, input->count, // "const long lengthof__%s = %d;\n"
opts->tag.buf, opts->tag.buf); // "const entry__%s lookup__%s[] = {\n"

for (usize i = 0; i < input->count; i++) {
usize padding = input->max_ident_len - genned->table[i].len + 1;
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/append.test
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 17;
const entry__stdin lookup__stdin[] = {
{ APPENDED, "APPENDED", },
{ APPENDED_PLUS_1, "APPENDED_PLUS_1", },
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/base.test
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 14;
const entry__stdin lookup__stdin[] = {
{ BLASTOISE, "BLASTOISE", },
{ BULBASAUR, "BULBASAUR", },
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/direct_assignment.test
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 14;
const entry__stdin lookup__stdin[] = {
{ BLASTOISE, "BLASTOISE", },
{ BULBASAUR, "BULBASAUR", },
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/guard.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ typedef struct entry__stdin {

#ifndef ALTERNATIVE_GUARD_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 14;
const entry__stdin lookup__stdin[] = {
{ BLASTOISE, "BLASTOISE", },
{ BULBASAUR, "BULBASAUR", },
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/leader.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 14;
const entry__stdin lookup__stdin[] = {
{ ALTERNATIVE_LEADER_BLASTOISE, "ALTERNATIVE_LEADER_BLASTOISE", },
{ ALTERNATIVE_LEADER_BULBASAUR, "ALTERNATIVE_LEADER_BULBASAUR", },
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/prepend.test
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 17;
const entry__stdin lookup__stdin[] = {
{ BLASTOISE, "BLASTOISE", },
{ BULBASAUR, "BULBASAUR", },
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/start_from.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 14;
const entry__stdin lookup__stdin[] = {
{ BLASTOISE, "BLASTOISE", },
{ BULBASAUR, "BULBASAUR", },
Expand Down
2 changes: 2 additions & 0 deletions tests/enum/tag_name.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ typedef struct entry__Alternative_TagName {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__Alternative_TagName;
extern const entry__Alternative_TagName lookup__Alternative_TagName[];

#else

const long lengthof__Alternative_TagName = 14;
const entry__Alternative_TagName lookup__Alternative_TagName[] = {
{ BLASTOISE, "BLASTOISE", },
{ BULBASAUR, "BULBASAUR", },
Expand Down
2 changes: 2 additions & 0 deletions tests/mask/base.test
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 16;
const entry__stdin lookup__stdin[] = {
{ ANY, "ANY", },
{ BLASTOISE, "BLASTOISE", },
Expand Down
2 changes: 2 additions & 0 deletions tests/mask/guard.test
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ typedef struct entry__stdin {

#ifndef ALTERNATIVE_GUARD_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 16;
const entry__stdin lookup__stdin[] = {
{ ANY, "ANY", },
{ BLASTOISE, "BLASTOISE", },
Expand Down
2 changes: 2 additions & 0 deletions tests/mask/leader.test
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ typedef struct entry__stdin {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__stdin;
extern const entry__stdin lookup__stdin[];

#else

const long lengthof__stdin = 16;
const entry__stdin lookup__stdin[] = {
{ ALTERNATIVE_LEADER_ANY, "ALTERNATIVE_LEADER_ANY", },
{ ALTERNATIVE_LEADER_BLASTOISE, "ALTERNATIVE_LEADER_BLASTOISE", },
Expand Down
2 changes: 2 additions & 0 deletions tests/mask/tag_name.test
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ typedef struct entry__Alternative_TagName {

#ifndef METANG_LOOKUP_IMPL

extern const long lengthof__Alternative_TagName;
extern const entry__Alternative_TagName lookup__Alternative_TagName[];

#else

const long lengthof__Alternative_TagName = 16;
const entry__Alternative_TagName lookup__Alternative_TagName[] = {
{ ANY, "ANY", },
{ BLASTOISE, "BLASTOISE", },
Expand Down

0 comments on commit ec95481

Please sign in to comment.