Skip to content

Commit

Permalink
Factor out print_ret().
Browse files Browse the repository at this point in the history
  • Loading branch information
katef committed Aug 7, 2024
1 parent ba70069 commit 218e90c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
26 changes: 18 additions & 8 deletions src/libfsm/print/go.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ print_fetch(FILE *f, const struct fsm_options *opt)
}
}

static void
print_ret(FILE *f, const unsigned *ids, size_t count)
{
size_t i;

fprintf(f, "[]uint{");
for (i = 0; i < count; i++) {
fprintf(f, "%u", ids[i]);
if (i + 1 < count) {
fprintf(f, ", ");
}
}
fprintf(f, "}");
}

/* TODO: eventually to be non-static */
static int
fsm_print_gofrag(FILE *f,
Expand Down Expand Up @@ -372,14 +387,9 @@ fsm_print_go(FILE *f,

if (opt->ambig == AMBIG_MULTIPLE) {
for (size_t i = 0; i < retlist->count; i++) {
fprintf(f, "var ret%zu []uint = []uint{", i);
for (size_t j = 0; j < retlist->a[i].count; j++) {
fprintf(f, "%u", retlist->a[i].ids[j]);
if (j + 1 < retlist->a[i].count) {
fprintf(f, ", ");
}
}
fprintf(f, "}\n");
fprintf(f, "var ret%zu []uint = ", i);
print_ret(f, retlist->a[i].ids, retlist->a[i].count);
fprintf(f, "\n");
}
fprintf(f, "\n");
}
Expand Down
26 changes: 18 additions & 8 deletions src/libfsm/print/rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ print_fetch(FILE *f)
fprintf(f, "bytes.next()");
}

static void
print_ret(FILE *f, const unsigned *ids, size_t count)
{
size_t i;

fprintf(f, "[");
for (i = 0; i < count; i++) {
fprintf(f, "%u", ids[i]);
if (i + 1 < count) {
fprintf(f, ", ");
}
}
fprintf(f, "];");
}

/* TODO: eventually to be non-static */
static int
fsm_print_rustfrag(FILE *f,
Expand All @@ -234,14 +249,9 @@ fsm_print_rustfrag(FILE *f,

if (opt->ambig == AMBIG_MULTIPLE) {
for (size_t i = 0; i < retlist->count; i++) {
fprintf(f, " static RET%zu: [u32; %zu] = [", i, retlist->a[i].count);
for (size_t j = 0; j < retlist->a[i].count; j++) {
fprintf(f, "%u", retlist->a[i].ids[j]);
if (j + 1 < retlist->a[i].count) {
fprintf(f, ", ");
}
}
fprintf(f, "];\n");
fprintf(f, " static RET%zu: [u32; %zu] = ", i, retlist->a[i].count);
print_ret(f, retlist->a[i].ids, retlist->a[i].count);
fprintf(f, "\n");
}
fprintf(f, "\n");
}
Expand Down
26 changes: 18 additions & 8 deletions src/libfsm/print/vmops.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ print_fetch(FILE *f, const struct fsm_options *opt, const char *prefix)
return 0;
}

static void
print_ret(FILE *f, const unsigned *ids, size_t count)
{
size_t i;

fprintf(f, "{ (const unsigned []) { ");
for (i = 0; i < count; i++) {
fprintf(f, "%u", ids[i]);
if (i + 1 < count) {
fprintf(f, ", ");
}
}
fprintf(f, " }, %zu }", count);
}

int
fsm_print_vmops_c(FILE *f,
const struct fsm_options *opt,
Expand Down Expand Up @@ -235,14 +250,9 @@ fsm_print_vmops_c(FILE *f,
if (opt->ambig != AMBIG_NONE) {
fprintf(f, "struct %sret %sRet[] = {\n", prefix, prefix);
for (size_t i = 0; i < retlist->count; i++) {
fprintf(f, "\t{ (const unsigned []) { ");
for (size_t j = 0; j < retlist->a[i].count; j++) {
fprintf(f, "%u", retlist->a[i].ids[j]);
if (j + 1 < retlist->a[i].count) {
fprintf(f, ", ");
}
}
fprintf(f, " }, %zu },\n", retlist->a[i].count);
fprintf(f, "\t");
print_ret(f, retlist->a[i].ids, retlist->a[i].count);
fprintf(f, ",\n");
}
fprintf(f, "};\n");
fprintf(f, "const size_t %sRet_count = sizeof %sRet / sizeof *%sRet;\n", prefix, prefix, prefix);
Expand Down

0 comments on commit 218e90c

Please sign in to comment.