From 039d27a837683aa0500d3d5379039664cbafa10c Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Thu, 15 May 2025 20:11:10 +0000 Subject: [PATCH 1/4] support blc2 --- src/target.c | 4 +++ src/targets/blc.c | 77 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/target.c b/src/target.c index dbb4c63..11886d7 100644 --- a/src/target.c +++ b/src/target.c @@ -10,12 +10,16 @@ extern struct target_spec target_unblc; extern struct target_spec target_unbblc; extern struct target_spec target_blc; extern struct target_spec target_bblc; +extern struct target_spec target_blc2; +extern struct target_spec target_bblc2; static struct target_spec *targets[] = { &target_unblc, &target_unbblc, &target_blc, &target_bblc, + &target_blc2, + &target_bblc2, }; void exec_target(char *name, struct bloc_parsed *bloc, FILE *file) diff --git a/src/targets/blc.c b/src/targets/blc.c index c4c51d4..614f3fc 100644 --- a/src/targets/blc.c +++ b/src/targets/blc.c @@ -19,6 +19,7 @@ struct context { enum { WRITE_BITS, WRITE_ASCII } type; + _Bool blc2; FILE *file; char *byte; int *bit; @@ -52,6 +53,24 @@ static void write_context(struct context *context, const char *bits) } } +static void write_blc2_context(struct context *context, int size){ + if(size == 0){ + write_context(context, "10"); + return; + } + int len = 8 * sizeof(size) - __builtin_clz(size); + write_context(context, "1"); + write_blc2_context(context, len); + for(int i = 0; i < len - 2; i++){ + int j = size & (1 << i); + if(j){ + write_context(context, "1"); + }else{ + write_context(context, "0"); + } + } +} + static void write_blc_substituted(struct term *term, int depth, struct context *context) { @@ -66,9 +85,13 @@ static void write_blc_substituted(struct term *term, int depth, write_blc_substituted(term->u.app.rhs, depth, context); break; case VAR: - for (int i = 0; i <= term->u.var.index; i++) - write_context(context, "1"); - write_context(context, "0"); + if(!context->blc2){ + for (int i = 0; i <= term->u.var.index; i++) + write_context(context, "1"); + write_context(context, "0"); + }else{ + write_blc2_context(context, term->u.var.index); + }; break; case REF: if (term->u.ref.index + 1 >= context->bloc->length) @@ -80,9 +103,13 @@ static void write_blc_substituted(struct term *term, int depth, context->position) - 1; assert(index >= 0); - for (int i = 0; i <= index; i++) - write_context(context, "1"); - write_context(context, "0"); + if(!context->blc2){ + for (int i = 0; i <= index; i++) + write_context(context, "1"); + write_context(context, "0"); + }else{ + write_blc2_context(context, index); + } } else { write_blc_substituted( context->bloc->entries[term->u.ref.index], @@ -240,6 +267,7 @@ static void write_blc_ascii(struct bloc_parsed *bloc, FILE *file) struct context context = { .type = WRITE_ASCII, .file = file, + .blc2 =0, }; write_blc(bloc, &context); } @@ -251,6 +279,33 @@ static void write_blc_bits(struct bloc_parsed *bloc, FILE *file) struct context context = { .type = WRITE_BITS, .file = file, + .blc2 = 0, + .byte = &byte, + .bit = &bit, + }; + write_blc(bloc, &context); + if (bit) + fwrite(&byte, 1, 1, file); +} + +static void write_blc2_ascii(struct bloc_parsed *bloc, FILE *file) +{ + struct context context = { + .type = WRITE_ASCII, + .file = file, + .blc2 = 1, + }; + write_blc(bloc, &context); +} + +static void write_blc2_bits(struct bloc_parsed *bloc, FILE *file) +{ + char byte = 0; + int bit = 0; + struct context context = { + .type = WRITE_BITS, + .file = file, + .blc2 = 1, .byte = &byte, .bit = &bit, }; @@ -268,3 +323,13 @@ struct target_spec target_bblc = { .name = "bblc", .exec = write_blc_bits, }; + +struct target_spec target_blc2 = { + .name = "blc2", + .exec = write_blc2_ascii, +}; + +struct target_spec target_bblc2 = { + .name = "bblc2", + .exec = write_blc2_bits, +}; From f77e9880eea7f9b59c0689cf2ba7b8c8a37158d0 Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Thu, 15 May 2025 20:29:12 +0000 Subject: [PATCH 2/4] fix off by one --- src/targets/blc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/targets/blc.c b/src/targets/blc.c index 614f3fc..b80c05d 100644 --- a/src/targets/blc.c +++ b/src/targets/blc.c @@ -60,7 +60,7 @@ static void write_blc2_context(struct context *context, int size){ } int len = 8 * sizeof(size) - __builtin_clz(size); write_context(context, "1"); - write_blc2_context(context, len); + write_blc2_context(context, len - 1); for(int i = 0; i < len - 2; i++){ int j = size & (1 << i); if(j){ From 2d1dac4989327e9fa4e7dfe2806c88e0cbe8dd7c Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Sat, 17 May 2025 20:32:05 +0000 Subject: [PATCH 3/4] add generation of `blc2` files --- test/run | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/run b/test/run index 736521a..2e3d56a 100755 --- a/test/run +++ b/test/run @@ -11,6 +11,8 @@ for file in *.blc; do bloc --from-blc -i "$file" -o ../build/"$file".bloc ../build/blocade -i ../build/"$file".bloc -t blc -o ../build/"$file".bloc.blc ../build/blocade -i ../build/"$file".bloc -t bblc -o ../build/"$file".bloc.bblc + ../build/blocade -i ../build/"$file".bloc -t blc2 -o ../build/"$file".bloc.blc2 + ../build/blocade -i ../build/"$file".bloc -t bblc2 -o ../build/"$file".bloc.bblc2 bruijn -E "$file" &>../build/"$file".out bruijn -E ../build/"$file".bloc.blc &>../build/"$file".bloc.blc.out @@ -30,6 +32,8 @@ for file in *.blc.io; do bloc --from-blc -i "$file" -o ../build/"$file".bloc ../build/blocade -i ../build/"$file".bloc -t blc -o ../build/"$file".bloc.blc ../build/blocade -i ../build/"$file".bloc -t bblc -o ../build/"$file".bloc.bblc + ../build/blocade -i ../build/"$file".bloc -t blc2 -o ../build/"$file".bloc.blc2 + ../build/blocade -i ../build/"$file".bloc -t bblc2 -o ../build/"$file".bloc.bblc2 cat "$file".in | bruijn -E "$file" &>../build/"$file".out cat "$file".in | bruijn -E ../build/"$file".bloc.blc &>../build/"$file".bloc.blc.out From c8e1a66a18dda7cf9bcb39bbaa3f4a13697e1f62 Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Sat, 17 May 2025 20:33:25 +0000 Subject: [PATCH 4/4] clean blc2 --- test/run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/run b/test/run index 2e3d56a..8feffa1 100755 --- a/test/run +++ b/test/run @@ -5,7 +5,7 @@ FAIL="\033[0;31m[FAIL]\033[0m " SUCC="\033[0;32m[ OK ]\033[0m " -rm -f ../build/*.out ../build/*.blc ../build/*.bloc +rm -f ../build/*.out ../build/*.blc ../build/*.bloc ../build/*.blc2 for file in *.blc; do bloc --from-blc -i "$file" -o ../build/"$file".bloc