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 #78 from KrzysztofSzewczyk/long-names
Browse files Browse the repository at this point in the history
Long name aliases, new preprocessor.
  • Loading branch information
kspalaiologos authored May 15, 2020
2 parents 7c1c8e4 + 65d6578 commit 8d17668
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bin
bfdata.c
bflabels.c
constpp.c
*.o
bfpp/bfpp
*.pyc
*.pyc
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Krzysztof Szewczyk, Jul 2019

export CFLAGS=-Ofast -march=native -funroll-loops -fomit-frame-pointer -w $(COVERAGE) $(OPTIONS)
TARGETS=bfasm bfi bfintd bconv bfstrip bfderle bflabels bfdata
TARGETS=bfasm bfi bfintd bconv bfstrip bfderle bflabels constpp bfdata

.PHONY: all clean setup test bfpp

Expand Down Expand Up @@ -50,13 +50,19 @@ test-clean:
rm -f test/*.b

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

bflabels.c: bflabels.lex
lex -o $@ $^

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

constpp.c: constpp.lex
lex -o $@ $^

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

bfdata.c: bfdata.lex
lex -o $@ $^
2 changes: 1 addition & 1 deletion bfmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ fi
# A special case: no file supplied
[ "$file" == "" ] && exit 2

{ ( [ -z "$no_libbfm" ] && echo "" || cat "$HOME/.asmbf/lib/lib-bfm.lua" ) ; cat "$file" | sed -e "1!b" -e '/#/d' ; } | "$HOME/.asmbf/bfpp" | "$HOME/.asmbf/bflabels" | "$HOME/.asmbf/bfdata" | sed '/^#/ d' | "$HOME/.asmbf/bfasm" | ( [ -z "$no_strip" ] && cat || "$HOME/.asmbf/bfstrip" ) > "${output}"
{ ( [ -z "$no_libbfm" ] && echo "" || cat "$HOME/.asmbf/lib/lib-bfm.lua" ) ; cat "$file" | sed -e "1!b" -e '/#/d' ; } | "$HOME/.asmbf/bfpp" | "$HOME/.asmbf/bflabels" | "$HOME/.asmbf/bfdata" | sed '/^#/ d' | "$HOME/.asmbf/constpp" | "$HOME/.asmbf/bfasm" | ( [ -z "$no_strip" ] && cat || "$HOME/.asmbf/bfstrip" ) > "${output}"
71 changes: 71 additions & 0 deletions constpp.lex
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

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

struct def_t {
char * find, * replace;
struct def_t * next;
};

struct def_t * ctx, * ctx_ptr;

struct def_t * new_def(void) {
struct def_t * ret = malloc(sizeof(struct def_t));
assert(ret);
return ret;
}

void pop_def(char * text) {
struct def_t * p = ctx;
while(p->next && p->find && p->replace) {
if(!strcmp(text, p->find)) {
printf("%s", p->replace);
return;
}

p = p->next;
}
printf("%s", text);
}

void push_def(char * text) {
/* Skip the whitespace. */
while(isspace((unsigned char)*text))
text++;

/* Skip the questionmark */
text++;

char * find = text, * replace = strchr(text, '=') + 1;
unsigned find_len = replace - find - 1, replace_len = strlen(replace);

ctx_ptr->find = malloc(find_len);
memcpy(ctx_ptr->find, find, find_len);

ctx_ptr->replace = malloc(replace_len);
memcpy(ctx_ptr->replace, replace, replace_len);

ctx_ptr->next = new_def();
ctx_ptr->next->find = ctx_ptr->next->replace = NULL;
ctx_ptr = ctx_ptr->next;
}

int yywrap(void) { return 1; }

int main(void) {
ctx = ctx_ptr = new_def();

yylex();
}

%}

%%
^[ \t]*\?([A-Za-z_][A-Za-z0-9_]*)\=([A-Za-z_][A-Za-z0-9_]*) { push_def(yytext); }
(([A-Za-z_][A-Za-z0-9_]*)|"[^"\n]*([A-Za-z_][A-Za-z0-9_]*)) { pop_def(yytext); }
. { putchar(yytext[0]); }
%%
29 changes: 29 additions & 0 deletions lib-bfm.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@

; Don't ask questions, this is beyond explaining
?band=x00
?bor=x01
?bxor=x02
?bneg=x03

; Some common defines
?push=psh
?xchg=swp

; Conditional instructions
?cadd=cad
?csub=csu
?cmul=cmu
?cdiv=cdi
?cmod=cmd
?casl=csl
?casr=csr
?cpow=cpw
?cpush=cps
?cpsh=cps
?cpop=cpo
?cxchg=csw
?cswp=csw
?csrv=crv
?cmov=cmo
?crcl=crc
?csto=cst

$(

function include(path)
Expand Down
6 changes: 6 additions & 0 deletions test/xchg.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

mov r1, .0
mov r2, .1
xchg r1, r2
out r1
out r2
1 change: 1 addition & 0 deletions test/xchg.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions test/xchg.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10

0 comments on commit 8d17668

Please sign in to comment.