Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 71 additions & 6 deletions src/targets/blc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

struct context {
enum { WRITE_BITS, WRITE_ASCII } type;
_Bool blc2;
FILE *file;
char *byte;
int *bit;
Expand Down Expand Up @@ -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 - 1);
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)
{
Expand All @@ -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)
Expand All @@ -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],
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
};
Expand All @@ -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,
};
6 changes: 5 additions & 1 deletion test/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
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
../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
Expand All @@ -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
Expand Down