Skip to content

Commit

Permalink
Added modulo support and simple game example (odd or even)
Browse files Browse the repository at this point in the history
  • Loading branch information
none-None1 committed Aug 17, 2023
1 parent 1974bb5 commit 947870c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ or
mul(x,<variable>)
```

Modulo:
```
mod(x,<number>)
or
mod(x,<variable>)
```

While loop:
```text
while(<variable or number>)
Expand Down
64 changes: 61 additions & 3 deletions bffuck.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ def program(self, code):
+ ">>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<[-]"
) # https://esolangs.org/wiki/Brainfuck_algorithms#Print_value_of_cell_x_as_number_(8-bit)
self.bf += (
self.movptr(self.playfield-1)
self.movptr(self.playfield - 1)
+ "["
+ self.movptr(self.valdict[vn])
+ "+"
+ self.movptr(self.playfield-1)
+ self.movptr(self.playfield - 1)
+ "-"
+ "]"
)
Expand Down Expand Up @@ -583,6 +583,65 @@ def program(self, code):
self.bf += self.movptr(b) + "[-]"
else:
raise Exception("Variable not found")
elif code.startswith("mod("):
expr = code[4:-1]
w = expr.split(",")
if len(w) != 2:
raise Exception("mod() needs 2 arguments")
else:
a, b = w[0], w[1]
if a in self.valdict:
self.bf += (
self.movptr(self.valdict[a])
+ "["
+ self.movptr(1)
+ "+"
+ self.movptr(self.valdict[a])
+ "-]"
)
if b.isdigit():
self.bf += self.movptr(3) + "+" * int(b)
post = False
else:
if b in self.valdict:
self.bf += (
self.movptr(self.valdict[b])
+ "["
+ self.movptr(3)
+ "+"
+ self.movptr(self.playfield - 1)
+ "+"
+ self.movptr(self.valdict[b])
+ "-]"
)
post = True
else:
raise Exception("Variable not found")
self.bf += (
self.movptr(1)
+ "[>+>->+<[>]>[<+>-]<<[<]>-]"
+ self.movptr(2)
+ "[-]"
+ self.movptr(3)
+ "[-]"
+ self.movptr(4)
+ "["
+ self.movptr(self.valdict[a])
+ "+"
+ self.movptr(4)
+ "-]"
) # https://esolangs.org/wiki/Brainfuck_algorithms#Modulus_algorithm
if post:
self.bf += (
self.movptr(self.playfield - 1)
+ "["
+ self.movptr(self.valdict[b])
+ "+"
+ self.movptr(self.playfield - 1)
+ "-]"
)
else:
raise Exception("Variable not found")
elif code.startswith("print("):
if not (code.endswith(")")):
raise Exception("Unmatched bracket")
Expand Down Expand Up @@ -662,4 +721,3 @@ def compile(self, prog):
clean = self.join_semantically(i.split()).split("#")[0]
self.program(clean)
return self.opt(self.bf)

9 changes: 9 additions & 0 deletions examples/oddoreven.bff
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
a=in
mod(a,2)
if(a)
print(Odd!)
endif
eq(a,0)
if(a)
print(Even!)
endif

0 comments on commit 947870c

Please sign in to comment.