diff --git a/parser/execute.go b/parser/execute.go index f95b93a..d916b9e 100644 --- a/parser/execute.go +++ b/parser/execute.go @@ -19,9 +19,9 @@ type Number interface { // Execute a compiled program func Execute[T Number](data []T, program []Instruction, reader io.ByteReader, writer *bufio.Writer) []T { - var dataPtr, operand, writeCount int = 0, 0, 0 + var dataPtr, writeCount int = 0, 0 for pc := 0; pc < len(program); pc++ { - operand = program[pc].operand + operand := program[pc].operand switch program[pc].operator { case opAddDp: dataPtr = (operand + dataPtr) & DataMask diff --git a/parser/execute_test.go b/parser/execute_test.go index e90607a..5ea158f 100644 --- a/parser/execute_test.go +++ b/parser/execute_test.go @@ -10,12 +10,12 @@ import ( func TestExecuteSmall(t *testing.T) { program := []Instruction{ - Instruction{opNoop, 0}, - Instruction{opAddDp, 5}, - Instruction{opSetVal, 0}, - Instruction{opAddVal, 5}, - Instruction{opMove, 2}, - Instruction{opAddDp, 2}, + {opNoop, 0}, + {opAddDp, 5}, + {opSetVal, 0}, + {opAddVal, 5}, + {opMove, 2}, + {opAddDp, 2}, } startdata := make([]int, 65536) outputBuf := bufio.NewWriter(os.Stdout) diff --git a/parser/instruction_test.go b/parser/instruction_test.go index 99e85e7..ab18035 100644 --- a/parser/instruction_test.go +++ b/parser/instruction_test.go @@ -9,15 +9,15 @@ import ( func TestNewInstruction(t *testing.T) { sourceCode := "g><+-.,[]" program := []Instruction{ - Instruction{opNoop, 0}, - Instruction{opAddDp, 1}, - Instruction{opAddDp, -1}, - Instruction{opAddVal, 1}, - Instruction{opAddVal, -1}, - Instruction{opOut, 1}, - Instruction{opIn, 1}, - Instruction{opJmpZ, 0}, - Instruction{opJmpNz, 0}, + {opNoop, 0}, + {opAddDp, 1}, + {opAddDp, -1}, + {opAddVal, 1}, + {opAddVal, -1}, + {opOut, 1}, + {opIn, 1}, + {opJmpZ, 0}, + {opJmpNz, 0}, } for idx, val := range []byte(sourceCode) { diff --git a/parser/tokenise.go b/parser/tokenise.go index 8467eb1..448bcdf 100644 --- a/parser/tokenise.go +++ b/parser/tokenise.go @@ -7,7 +7,7 @@ import ( // Tokenise sourcecode into an array of operators func Tokenise(input io.ByteReader) (program []Instruction, err error) { - var pc, jmpPc int = 0, 0 + var pc int = 0 jmpStack := make([]int, 0) program = append(program, Instruction{opNoop, 0}) pc++ @@ -47,7 +47,7 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) { if len(jmpStack) == 0 { return nil, errors.New("tokenisation error: unbalanced braces") } - jmpPc = jmpStack[len(jmpStack)-1] + jmpPc := jmpStack[len(jmpStack)-1] jmpStack = jmpStack[:len(jmpStack)-1] program[pc].operand = jmpPc program[jmpPc].operand = pc diff --git a/parser/tokenise_test.go b/parser/tokenise_test.go index 5010dd3..eb62a2e 100644 --- a/parser/tokenise_test.go +++ b/parser/tokenise_test.go @@ -18,86 +18,86 @@ func TestTokenise(t *testing.T) { "small_prog", ">>>>>[-]zero+++++>+++++[->>+<<]move>>", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opAddDp, 5}, - Instruction{opSetVal, 5}, - Instruction{opAddDp, 1}, - Instruction{opAddVal, 5}, - Instruction{opMove, 2}, - Instruction{opAddDp, 2}, + {opNoop, 0}, + {opAddDp, 5}, + {opSetVal, 5}, + {opAddDp, 1}, + {opAddVal, 5}, + {opMove, 2}, + {opAddDp, 2}, }, }, { "op_dp", ">>>>>>><<<<<<>", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opAddDp, 2}, + {opNoop, 0}, + {opAddDp, 2}, }, }, { "op_val", "----++----++", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opAddVal, -4}, + {opNoop, 0}, + {opAddVal, -4}, }, }, { "op_skip", "[>>>>>]", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opSkip, 5}, + {opNoop, 0}, + {opSkip, 5}, }, }, { "op_move", "[->>+<<][<<<+>>>-]", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opMove, 2}, - Instruction{opMove, -3}, + {opNoop, 0}, + {opMove, 2}, + {opMove, -3}, }, }, { "op_jmp_z_nz", "[->>+>+<<<]", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opJmpZ, 8}, - Instruction{opAddVal, -1}, - Instruction{opAddDp, 2}, - Instruction{opAddVal, 1}, - Instruction{opAddDp, 1}, - Instruction{opAddVal, 1}, - Instruction{opAddDp, -3}, - Instruction{opJmpNz, 1}, + {opNoop, 0}, + {opJmpZ, 8}, + {opAddVal, -1}, + {opAddDp, 2}, + {opAddVal, 1}, + {opAddDp, 1}, + {opAddVal, 1}, + {opAddDp, -3}, + {opJmpNz, 1}, }, }, { "op_nested", "[[[[[[[,]]]]]]][comment.]", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opJmpZ, 15}, - Instruction{opJmpZ, 14}, - Instruction{opJmpZ, 13}, - Instruction{opJmpZ, 12}, - Instruction{opJmpZ, 11}, - Instruction{opJmpZ, 10}, - Instruction{opJmpZ, 9}, - Instruction{opIn, 1}, - Instruction{opJmpNz, 7}, - Instruction{opJmpNz, 6}, - Instruction{opJmpNz, 5}, - Instruction{opJmpNz, 4}, - Instruction{opJmpNz, 3}, - Instruction{opJmpNz, 2}, - Instruction{opJmpNz, 1}, - Instruction{opJmpZ, 18}, - Instruction{opOut, 1}, - Instruction{opJmpNz, 16}, + {opNoop, 0}, + {opJmpZ, 15}, + {opJmpZ, 14}, + {opJmpZ, 13}, + {opJmpZ, 12}, + {opJmpZ, 11}, + {opJmpZ, 10}, + {opJmpZ, 9}, + {opIn, 1}, + {opJmpNz, 7}, + {opJmpNz, 6}, + {opJmpNz, 5}, + {opJmpNz, 4}, + {opJmpNz, 3}, + {opJmpNz, 2}, + {opJmpNz, 1}, + {opJmpZ, 18}, + {opOut, 1}, + {opJmpNz, 16}, }, }, }