Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #41 from KrzysztofSzewczyk/labels-c
Browse files Browse the repository at this point in the history
Label preprocessor in C (yay!)
All tests pass, therefore I'm merging it. In case of any issues with the new label preprocessor, I'll revert the merge.
  • Loading branch information
kspalaiologos authored Mar 1, 2020
2 parents bf31ad3 + af4ac41 commit ddc617d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 12 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

CC=gcc
CFLAGS=-Ofast -march=native -funroll-loops -fomit-frame-pointer $(COVERAGE) $(OPTIONS) -Wall -Wextra
TARGETS=bfasm bfi bfintd bconv bfstrip bfderle
TARGETS=bfasm bfi bfintd bconv bfstrip bfderle bflabels

.PHONY: all clean install uninstall test

Expand All @@ -18,7 +18,7 @@ install:
sudo cp -rf bin/* /bin/

uninstall:
cd /bin && sudo rm -f $(TARGETS) bfpp bfmake data-labels.pl labels.pl bfi-rle && cd -
cd /bin && sudo rm -f $(TARGETS) bfpp bfmake data-labels.pl bfi-rle && cd -

test: test/*.asm
chmod a+x test.pl $^
Expand All @@ -35,9 +35,15 @@ bfasm.b: bfasm bfasm.asm

bin: $(TARGETS)
mkdir -p bin
cp $(TARGETS) bfpp bfmake labels.pl bfi-rle data-labels.pl bin/
cp $(TARGETS) bfpp bfmake bfi-rle data-labels.pl bin/
rm -rf $(TARGETS)

test-clean:
rm -f test/*.aout
rm -f test/*.b

bflabels: bflabels.c
$(CC) $(CFLAGS) $^ -lfl -o $@

bflabels.c: bflabels.lex
lex -o $@ $^
81 changes: 81 additions & 0 deletions bflabels.lex
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct label_t {
struct label_t * next;
char * name;
int id;
};

struct label_t * main_node;

struct label_t * alloc_node(void) {
struct label_t * x = malloc(sizeof(struct label_t));
x->next = x->name = NULL;
x->id = 0;
return x;
}

void getlabel(char * text) {
struct label_t * head;

if(*text == '"') {
printf("%s", text);
return;
}

if(main_node == NULL)
main_node = alloc_node();

text = strchr(text, '%') + 1;
head = main_node;
while(head->next != NULL)
if(head->name && strcmp(text, head->name) == 0) {
printf("%d", head->id);
return;
} else
head = head->next;

addlabel(text);
}

void addlabel(char * text) {
static int curid = 1;
struct label_t * head;
char * cpy, surpress_label = 0;

cpy = strchr(text, '@') + 1;
if(cpy != (void *)1)
text = cpy;
else surpress_label = 1;

if(main_node == NULL)
main_node = alloc_node();

head = main_node;
while(head->next != NULL) {
if(head->name && strcmp(text, head->name) == 0) {
printf("lbl %d\n", head->id);
return;
} else head = head->next;
}

head->next = alloc_node();
head->name = malloc(strlen(text) + 1);
strcpy(head->name, text);
!surpress_label && printf("lbl %d\n", curid);
surpress_label && printf("%d", curid);
head->id = curid++;
}

int yywrap(void) {return 1;}
%}

%%
^[ \t]*\@([A-Za-z_][A-Za-z0-9_]*) { addlabel(yytext); }
(%([A-Za-z_][A-Za-z0-9_]*)|"[^"\n]*%([A-Za-z_][A-Za-z0-9_]*)) { getlabel(yytext); }
. { putchar(yytext[0]); }
%%
4 changes: 2 additions & 2 deletions bfpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -o pipefail

{ cat <<<"#define __NEWLINE__ ÿ"$'\n'"#define __ICOUNTER__ þ"$'\n'"" ; cat "$1" | sed -e "1!b" -e '/#/d' ; } | gcc -E -I. -Ilib - -o "$1.i"
perl -pe "s/\xFF/\x0A/g;" < "$1.i" | perl -pe 's/\xFE/$x++/ge;' | /bin/labels.pl | /bin/data-labels.pl | sed '/^#/ d' > "$1.p"
{ cat <<<"#define __NEWLINE__ "$'\xFF'""$'\n'"#define __ICOUNTER__ "$'\xFE'""$'\n'"" ; cat "$1" | sed -e "1!b" -e '/#/d' ; } | gcc -E -I. -Ilib - -o "$1.i"
perl -pe "s/\xFF/\x0A/g;" < "$1.i" | perl -pe 's/\xFE/$x++/ge;' | /bin/bflabels | /bin/data-labels.pl | sed '/^#/ d' > "$1.p"
rm -f "$1.i"
7 changes: 0 additions & 7 deletions labels.pl

This file was deleted.

0 comments on commit ddc617d

Please sign in to comment.