Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recursive pretty print #368

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: dsherret/rust-toolchain-file@v1
- uses: dtolnay/rust-toolchain@nightly
- name: compare perf
run: |
git fetch origin main
Expand Down
137 changes: 61 additions & 76 deletions src/hvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2065,84 +2065,69 @@ void pretty_print_numb(Numb word) {
}

void pretty_print_port(Net* net, Book* book, Port port) {
Port stack[256];
stack[0] = port;
u32 len = 1;
u32 num = 0;
while (len > 0) {
Port cur = stack[--len];
switch (get_tag(cur)) {
case CON: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("(");
stack[len++] = new_port(ERA, (u32)(')'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
}
case ERA: {
if (get_val(cur) != 0) {
printf("%c", (char)get_val(cur));
} else {
printf("*");
}
break;
}
case VAR: {
Port got = vars_load(net, get_val(cur));
if (got != NONE) {
stack[len++] = got;
} else {
printf("x%x", get_val(cur));
}
break;
}
case NUM: {
pretty_print_numb(get_val(cur));
break;
}
case DUP: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("{");
stack[len++] = new_port(ERA, (u32)('}'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
}
case OPR: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("$(");
stack[len++] = new_port(ERA, (u32)(')'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
}
case SWI: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("?(");
stack[len++] = new_port(ERA, (u32)(')'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
switch (get_tag(port)) {
case CON: {
Pair node = node_load(net, get_val(port));
printf("(");
pretty_print_port(net, book, get_fst(node));
printf(" ");
pretty_print_port(net, book, get_snd(node));
printf(")");
return;
}
case ERA: {
if (get_val(port) != 0) {
printf("%c", (char) get_val(port));
} else {
printf("*");
}
case REF: {
u32 fid = get_val(cur) & 0xFFFFFFF;
Def* def = &book->defs_buf[fid];
printf("@%s", def->name);
break;
return;
}
case VAR: {
Port val = vars_load(net, get_val(port));
if (val != NONE) {
pretty_print_port(net, book, val);
} else {
printf("x%x", get_val(port));
}
return;
}
case NUM: {
pretty_print_numb(get_val(port));
return;
}
case DUP: {
Pair node = node_load(net, get_val(port));
printf("{");
pretty_print_port(net, book, get_fst(node));
printf(" ");
pretty_print_port(net, book, get_snd(node));
printf("}");
return;
}
case OPR: {
Pair node = node_load(net, get_val(port));
printf("$(");
pretty_print_port(net, book, get_fst(node));
printf(" ");
pretty_print_port(net, book, get_snd(node));
printf(")");
return;
}
case SWI: {
Pair node = node_load(net, get_val(port));
printf("?(");
pretty_print_port(net, book, get_fst(node));
printf(" ");
pretty_print_port(net, book, get_snd(node));
printf(")");
return;
}
case REF: {
u32 fid = get_val(port) & 0xFFFFFFF;
Def* def = &book->defs_buf[fid];
printf("@%s", def->name);
return;
}
}
}
Expand Down
139 changes: 60 additions & 79 deletions src/hvm.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2545,88 +2545,69 @@ __device__ void pretty_print_numb(Numb word) {
}

__device__ void pretty_print_port(Net* net, Port port) {
Port stack[256];
stack[0] = port;
u32 len = 1;
while (len > 0) {
if (len > 256) {
printf("ERROR: result too deep to print. This will be fixed soon(TM)");
--len;
continue;
switch (get_tag(port)) {
case CON: {
Pair node = node_load(net, get_val(port));
printf("(");
pretty_print_port(net, get_fst(node));
printf(" ");
pretty_print_port(net, get_snd(node));
printf(")");
return;
}
Port cur = stack[--len];
switch (get_tag(cur)) {
case CON: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("(");
stack[len++] = new_port(ERA, (u32)(')'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
}
case ERA: {
if (get_val(cur) != 0) {
printf("%c", (char)get_val(cur));
} else {
printf("*");
}
break;
}
case VAR: {
Port got = vars_load(net, get_val(cur));
if (got != NONE) {
stack[len++] = got;
} else {
printf("x%x", get_val(cur));
}
break;
}
case NUM: {
pretty_print_numb(get_val(cur));
break;
}
case DUP: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("{");
stack[len++] = new_port(ERA, (u32)('}'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
}
case OPR: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("$(");
stack[len++] = new_port(ERA, (u32)(')'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
}
case SWI: {
Pair node = node_load(net,get_val(cur));
Port p2 = get_snd(node);
Port p1 = get_fst(node);
printf("?(");
stack[len++] = new_port(ERA, (u32)(')'));
stack[len++] = p2;
stack[len++] = new_port(ERA, (u32)(' '));
stack[len++] = p1;
break;
case ERA: {
if (get_val(port) != 0) {
printf("%c", (char) get_val(port));
} else {
printf("*");
}
case REF: {
u32 fid = get_val(cur) & 0xFFFFFFF;
Def* def = &BOOK.defs_buf[fid];
printf("@%s", def->name);
break;
return;
}
case VAR: {
Port val = vars_load(net, get_val(port));
if (val != NONE) {
pretty_print_port(net, val);
} else {
printf("x%x", get_val(port));
}
return;
}
case NUM: {
pretty_print_numb(get_val(port));
return;
}
case DUP: {
Pair node = node_load(net, get_val(port));
printf("{");
pretty_print_port(net, get_fst(node));
printf(" ");
pretty_print_port(net, get_snd(node));
printf("}");
return;
}
case OPR: {
Pair node = node_load(net, get_val(port));
printf("$(");
pretty_print_port(net, get_fst(node));
printf(" ");
pretty_print_port(net, get_snd(node));
printf(")");
return;
}
case SWI: {
Pair node = node_load(net, get_val(port));
printf("?(");
pretty_print_port(net, get_fst(node));
printf(" ");
pretty_print_port(net, get_snd(node));
printf(")");
return;
}
case REF: {
u32 fid = get_val(port) & 0xFFFFFFF;
Def* def = &BOOK.defs_buf[fid];
printf("@%s", def->name);
return;
}
}
}
Expand Down
Loading