aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLizzy Fleckenstein <eliasfleckenstein@web.de>2023-08-20 11:55:09 +0200
committerLizzy Fleckenstein <eliasfleckenstein@web.de>2023-08-20 11:55:18 +0200
commitecd10c643962ac1122fa016e30aee8b7796f8c45 (patch)
tree90bfe7a3df9fd3e0df0d7780a857e2af15a16292
parent2ddf90479e704afa34c54385b5cd9f76ee04374e (diff)
downloadparadox-ecd10c643962ac1122fa016e30aee8b7796f8c45.tar.xz
add brainfuck interpreter
-rw-r--r--.gitignore3
-rw-r--r--README.md2
-rw-r--r--examples/brainfuck.false59
3 files changed, 64 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index fc8aa10..6981bbf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,6 @@
!.gitignore
!run.sh
!LICENSE
+!examples
+examples/*
+!examples/brainfuck.false
diff --git a/README.md b/README.md
index 82002c9..df398e3 100644
--- a/README.md
+++ b/README.md
@@ -119,3 +119,5 @@ It is possible to make memory allocations using strings by compiling your progra
In the program, you can then use `12+;` at the beginning of the file to extract a pointer to your allocation.
Since all operations fetch 64-bits, it is recommended to set the allocation size to 7 bytes higher than desired (if you wish to fetch/write the last few bytes of the allocation individually).
+
+As an example for pointer arithmetic, see `examples/brainfuck.false` which implements brainfuck using paradox pointer arithmetic.
diff --git a/examples/brainfuck.false b/examples/brainfuck.false
new file mode 100644
index 0000000..0ba9adf
--- /dev/null
+++ b/examples/brainfuck.false
@@ -0,0 +1,59 @@
+{
+ to compile:
+
+ (echo "[\"$(head -c30007 /dev/zero)\"]" && cat brainfuck.false) | ../paradox \
+ > brainfuck.asm && nasm -f elf64 brainfuck.asm && ld -o brainfuck brainfuck.o
+
+ REQUIRES PARADOX due to pointer arithmetic.
+
+ stdin is used for both the brainfuck program and its input, separated by a zero byte.
+ to read the program from a file and enter input interactively:
+
+ (cat your_program.b && echo -ne '\0' && cat) | ./brainfuck
+
+ example hello world brainfuck program to try:
+ echo '+[>>>->-[>->----<<<]>>]>.---.>+..+++.>>.<.>>---.<<<.+++.------.<-.>>+.' | ./brainfuck
+}
+12+;$$t:h:l:
+0d: { change this to 0d: to disable debugging, 1_d: to enable debugging }
+0[^$$1_=~\0=~&][\1+]#% $n:
+[
+ r:\r;+
+ 1[$0=~][\
+ $2+ø
+ $'[=[@r;-@@]?
+ $']=[@r;+@@]?
+ %r;+
+ \]#%
+ r;-\
+]b:
+[$0=~][
+ d;[
+ "src: "
+ n;[$0=~][
+ $2ø=$[27,"[31m"]?
+ \$2+ø,1-\
+ [27,"[0m"]?
+ ]#%
+ "mem: "
+ t;h; 1ø1ø>~[\]?% h:
+ l;[$h;>~][
+ $t;=$[27,"[31m"]?
+ \$;255&." "\
+ [27,"[0m"]?
+ 1+
+ ]#%
+ ^%
+ ]?
+
+ $ø
+ $'>=[t;1+$l;30000+=[%l;]?t:]?
+ $'<=[t;$l;=[%l;30000+]?1-t:]?
+ $'+=[t;$;$255~&\1+255&|\:]?
+ $'-=[t;$;$255~&\1-255&|\:]?
+ $'.=[t;;,]?
+ $',=[t;$;255~&^|\:]?
+ $'[=[t;;255&0=[1_b;!]?]?
+ $']=[t;;255&0=~[1b;!]?]?
+ %1-
+]#%