Skip to content

Commit

Permalink
Implement opSkip.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Mar 24, 2024
1 parent 18353f8 commit d1ac3b5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions parser/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func Execute(program []Instruction, reader io.ByteReader, writer *bufio.Writer)
} else if err != nil {
break
}
case opSkip:
for data[dataPtr] != 0 {
dataPtr = (operand + dataPtr) & dataMask
}
case opJmpZ:
if data[dataPtr] == 0 {
pc = operand
Expand Down
5 changes: 3 additions & 2 deletions parser/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestExecuteSmall(t *testing.T) {

func TestTokeniseExecuteSmall(t *testing.T) {
program, _ := Tokenise(bufio.NewReader(strings.NewReader(
`+> >+> >+> >+> >
`>>>>>>>>>>
<+< <+< <+< <+ [>>]
>++++++++
[
<
Expand All @@ -40,7 +41,7 @@ func TestTokeniseExecuteSmall(t *testing.T) {
outputBuf := bufio.NewWriter(os.Stdout)
inputBuf := bufio.NewReader(strings.NewReader("no input."))
data := Execute(program, inputBuf, outputBuf)[:10]
want := []int{170, 0, 0, 0, 0, 0, 0, 0, 0, 0}
want := []int{0, 0, 0, 170, 0, 0, 0, 0, 0, 0}

if !reflect.DeepEqual(data, want) {
t.Errorf("got %v want %v", data, want)
Expand Down
2 changes: 2 additions & 0 deletions parser/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
opJmpZ
opJmpNz
opMove
opSkip
)

// String representation of Instruction
Expand All @@ -37,6 +38,7 @@ func (inst Instruction) String() string {
"jmpz",
"jmpnz",
"mov",
"skip",
}
return fmt.Sprintf("%s:%v", opName[inst.operator], inst.operand)
}
Expand Down
1 change: 1 addition & 0 deletions parser/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestSameOp(t *testing.T) {
opJmpZ,
opJmpNz,
opMove,
opSkip,
}

for row, rval := range opsList {
Expand Down
5 changes: 5 additions & 0 deletions parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) {
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opSetVal, 0})
} else if pc-jmpPc == 2 && program[pc-1].operator == opAddDp {
offset := program[pc-1].operand
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opSkip, offset})
} else if pc-jmpPc == 5 &&
program[pc-4].Complement(program[pc-2]) &&
program[pc-3].Complement(program[pc-1]) {
Expand Down
8 changes: 8 additions & 0 deletions parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func TestTokenise(t *testing.T) {
Instruction{opAddVal, -4},
},
},
{
"op_skip",
"[>>>>>]",
[]Instruction{
Instruction{opNoop, 0},
Instruction{opSkip, 5},
},
},
{
"op_move",
"[->>+<<][<<<+>>>-]",
Expand Down

0 comments on commit d1ac3b5

Please sign in to comment.