Skip to content

Commit

Permalink
Add Set val instruction.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Feb 29, 2024
1 parent dcab6b6 commit ee1a795
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Uses signed 16bit ints for data, memory wraps around at 65535, EOF returns -1.

repeat ++++ or --- are replaced with a single addition/subtraction

addition/subtraction after zero does a blind set.

repeat >>> or <<< are replaced with a single pointer jump

[>>+<<-] and [->>+<<] merged into a move instruction.
Expand Down
2 changes: 2 additions & 0 deletions parser/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func Execute(program []Instruction, reader io.ByteReader, writer *bufio.Writer)
dataPtr = dataPtr & 0xFFFF
case opAddVal:
data[dataPtr] += int16(program[pc].operand)
case opSetVal:
data[dataPtr] = int16(program[pc].operand)
case opOut:
writer.WriteByte(byte(data[dataPtr] & 0xff))
writeCount++
Expand Down
2 changes: 2 additions & 0 deletions parser/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
opNoop Opcode = iota
opAddDp
opAddVal
opSetVal
opOut
opIn
opJmpZ
Expand All @@ -31,6 +32,7 @@ func (inst Instruction) String() string {
"nop",
"ptr",
"add",
"set",
"out",
"in",
"jmpz",
Expand Down
9 changes: 9 additions & 0 deletions parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) {
program[pc-1].operand += program[pc].operand
program = program[:pc]
pc--
} else if program[pc-1].operator == opSetVal {
program[pc-1].operand += program[pc].operand
program = program[:pc]
pc--
} else if program[pc-1].operator == opJmpNz ||
program[pc-1].operator == opZero {
operand := program[pc].operand
program = program[:pc]
program = append(program, Instruction{opSetVal, operand})
}
case opJmpZ:
jmpStack = append(jmpStack, pc)
Expand Down
4 changes: 3 additions & 1 deletion parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ func TestTokenise(t *testing.T) {
}{
{
"small_prog",
">>>>>[-]zero+++++[->>+<<]move>>",
">>>>>[-]zero+++++>+++++[->>+<<]move>>",
[]Instruction{
Instruction{opNoop, 0},
Instruction{opAddDp, 5},
Instruction{opZero, 0},
Instruction{opSetVal, 5},
Instruction{opAddDp, 1},
Instruction{opAddVal, 5},
Instruction{opMove, 2},
Instruction{opAddDp, 2},
Expand Down
59 changes: 59 additions & 0 deletions sample/minify.bf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bf
[SPDX-FileCopyrightText: Brainfuck Enterprise Solutions
SPDX-License-Identifier: WTFPL]
[format.bf---Brainfuck code minifier.
Removes all the non-command character from the input. The input
should be valid Brainfuck code with balanced square brackets,
followed by the exclamation sign. The output is minified code as one
line without non-command characters
Memory layout is:
[case flag][char][char copy]
Code starts here:]
read char until EOF (-1)
,+
[ restore the char
-
[>+>+<<-] duplicate the char
+> set case flag
[ plus (43)
----- ----- ----- -----
----- ----- ----- ----- ---
[ comma (44)
-
[ minus (45)
-
[ period (46)
-
[ less than (60)
----- ----- ----
[ more than (62)
--
[ opening bracket (91)
----- ----- ----- ----- ----- ----
[ closing bracket (93)
--
default: kill the char and case flag and copy
[<->[-]>[-]<]
command case: kill flag
<[-]>]
command case: kill flag
<[-]>]
command case: kill flag
<[-]>]
command case: kill flag
<[-]>]
command case: kill flag
<[-]>]
command case: kill flag
<[-]>]
command case: kill flag
<[-]>]
command case: kill flag
<[-]>]
>[.[-]] print and kill the copy
read a new char and subtract exclamation mark
<<,+]
++++++++++.

0 comments on commit ee1a795

Please sign in to comment.