Skip to content

Commit

Permalink
Mirrored multiply.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Apr 30, 2024
1 parent bb07891 commit c1d1e34
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
4 changes: 4 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Security Policy

If you believe you have found a security issue in BFG, please responsibly disclose by contacting me at
[[email protected]](mailto:[email protected]).
45 changes: 26 additions & 19 deletions parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,34 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) {
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opSkip, offset})
} else if pc-jmpPc == 5 && // [<<<+>>>-] or [-<<<+>>>]
program[pc-4].Complement(program[pc-2]) &&
program[pc-3].Complement(program[pc-1]) {
offset := program[pc-4].operand
if program[pc-3].SameOp(NewInstruction('>')) {
} else if pc-jmpPc == 5 { // looking for opMulVal and opMove
var factor, offset = 0, 0
if program[pc-4].Complement(NewInstruction('+')) &&
program[pc-3].Complement(program[pc-1]) &&
program[pc-2].SameOp(NewInstruction('+')) { // open with minus

factor = program[pc-2].operand
offset = program[pc-3].operand

} else if program[pc-1].Complement(NewInstruction('+')) &&
program[pc-4].Complement(program[pc-2]) &&
program[pc-3].SameOp(NewInstruction('+')) { // close with minus

factor = program[pc-3].operand
offset = program[pc-4].operand

}
if factor == 1 {
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opMove, offset})
} else if factor != 0 {
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opMulVal, offset})
pc++
program = append(program, Instruction{opNoop, factor})
}
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opMove, offset})
} else if pc-jmpPc == 5 && // [<<++++>>-]
program[pc-4].Complement(program[pc-2]) &&
program[pc-3].SameOp(NewInstruction('+')) &&
program[pc-1].Complement(NewInstruction('+')) {
offset := program[pc-4].operand
factor := program[pc-3].operand
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opMulVal, offset})
pc++
program = append(program, Instruction{opNoop, factor})
}
}
pc++
Expand Down
14 changes: 9 additions & 5 deletions parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestTokenise(t *testing.T) {
}{
{
"small_prog",
">>>>>[-]zero+++++>+++++[->>+<<]move>>",
">>>>>[-]zero+++++>+++++[->>+<<]move>>[>>+<<-]move",
[]Instruction{
{opNoop, 0},
{opAddDp, 5},
Expand All @@ -25,15 +25,19 @@ func TestTokenise(t *testing.T) {
{opAddVal, 5},
{opMove, 2},
{opAddDp, 2},
{opMove, 2},
},
},
{
"op_mul",
" [<++++++>-]",
" [<++++++>-]>[->>---<<]",
[]Instruction{
Instruction{opNoop, 0},
Instruction{opMulVal, -1}, // dest value pointer
Instruction{opNoop, 6}, // multiplication factor
{opNoop, 0},
{opMulVal, -1}, // dest value pointer
{opNoop, 6}, // multiplication factor
{opAddDp, 1},
{opMulVal, 2}, // dest value pointer
{opNoop, -3}, // multiplication factor
},
},
{
Expand Down

0 comments on commit c1d1e34

Please sign in to comment.