Skip to content

Commit

Permalink
OpVec - vector multiply
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Aug 22, 2024
1 parent 785e6a4 commit 9203a17
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 4 deletions.
10 changes: 10 additions & 0 deletions parser/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func Execute[T Number](data []T, program []Instruction, reader io.ByteReader, wr
data[secondPtr] += data[dataPtr]
data[dataPtr] = 0
pc++
case opVec:
factor := program[pc+2].operand
dataVal := data[dataPtr] * T(factor)
firstPtr := (operand + dataPtr) & DataMask
data[firstPtr] += dataVal
secondPtr := (program[pc+1].operand + dataPtr) & DataMask
data[secondPtr] += dataVal
data[dataPtr] = 0
pc++
pc++
case opNoop:
continue
default:
Expand Down
6 changes: 5 additions & 1 deletion parser/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ func TestExecuteSmall(t *testing.T) {
{opOut, 1},
{opIn, 1},
{opMovN, 2},
{opAddDp, 1},
{opVec, 2},
{opNoop, 3},
{opNoop, 2},
}
startdata := make([]int, 65536)
outputBuf := bufio.NewWriter(&bufferWriter{})
inputBuf := bufio.NewReader(strings.NewReader("no input."))
data := Execute(startdata, program, inputBuf, outputBuf)[:10]
want := []int{0, 0, 0, 0, 10, -100, 0, 0, 0, 0}
want := []int{0, 0, 0, 0, 0, -100, 20, 20, 0, 0}

if !reflect.DeepEqual(data, want) {
t.Errorf("got %v want %v", data, want)
Expand Down
1 change: 1 addition & 0 deletions parser/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
opSkip
opMulVal
opDupVal
opVec
)

var instMap = map[byte]Instruction{
Expand Down
4 changes: 4 additions & 0 deletions parser/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var opName = map[Opcode]string{
opSkip: "skp",
opMulVal: "mul",
opDupVal: "dup",
opVec: "vec",
}

func (inst Instruction) String() string {
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestIsZeroOp(t *testing.T) {
{opMovN, 1},
{opMulVal, 1},
{opDupVal, 1},
{opVec, 1},
}
want := []bool{
true,
Expand All @@ -100,6 +102,7 @@ func TestIsZeroOp(t *testing.T) {
true,
true,
true,
true,
}

for idx, val := range program {
Expand All @@ -123,6 +126,7 @@ func TestSameOp(t *testing.T) {
opSkip,
opMulVal,
opDupVal,
opVec,
}

for row, rval := range opsList {
Expand Down
9 changes: 7 additions & 2 deletions parser/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func repeatDirection(neg, pos string, vect int) string {
return strings.Repeat(neg, abs(vect))
}

func instPrint(inst, lastInst Instruction) string {
func instPrint(inst, lastInst, lastlastInst Instruction) string {
switch inst.operator {
case opAddDp:
return repeatDirection("<", ">", inst.operand)
Expand Down Expand Up @@ -52,6 +52,9 @@ func instPrint(inst, lastInst Instruction) string {
return "[-" + repeatDirection("<", ">", lastInst.operand) + multiplier + repeatDirection(">", "<", lastInst.operand) + "]"
} else if lastInst.operator == opDupVal {
return "[-" + repeatDirection("<", ">", lastInst.operand) + "+" + repeatDirection("<", ">", inst.operand-lastInst.operand) + "+" + repeatDirection(">", "<", inst.operand) + "]"
} else if lastlastInst.operator == opVec {
multiplier := repeatDirection("-", "+", inst.operand)
return "[-" + repeatDirection("<", ">", lastlastInst.operand) + multiplier + repeatDirection("<", ">", lastInst.operand-lastlastInst.operand) + multiplier + repeatDirection(">", "<", lastInst.operand) + "]"
}
return ""
default:
Expand All @@ -65,17 +68,19 @@ func Print(program []Instruction, writer *bufio.Writer) {
startLoop := NewInstruction('[')
endLoop := NewInstruction(']')
lastInst := NewInstruction('!')
lastlastInst := NewInstruction('!')
for _, inst := range program {
if inst.SameOp(endLoop) {
depth--
}
printout := instPrint(inst, lastInst)
printout := instPrint(inst, lastInst, lastlastInst)
if printout != "" {
fmt.Fprintln(writer, strings.Repeat("\t", depth), printout)
}
if inst.SameOp(startLoop) {
depth++
}
lastlastInst = lastInst
lastInst = inst
}
writer.Flush()
Expand Down
5 changes: 4 additions & 1 deletion parser/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func TestPrint(t *testing.T) {
{opNoop, 2},
{opDupVal, 1},
{opNoop, 2},
{opVec, 1},
{opNoop, 2},
{opNoop, 3},
{opJmpNz, 5},
{opAddDp, 2},
{opOut, 1},
Expand All @@ -31,7 +34,7 @@ func TestPrint(t *testing.T) {
outputBuf := bufio.NewWriter(&buf)
Print(program, outputBuf)
got := buf.String()
want := " >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [<<<]\n\t ++\n\t [->>++<<]\n\t [->+>+<<]\n ]\n >>\n .\n [->>-<<]\n"
want := " >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [<<<]\n\t ++\n\t [->>++<<]\n\t [->+>+<<]\n\t [->+++>+++<<]\n ]\n >>\n .\n [->>-<<]\n"

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
Expand Down
8 changes: 8 additions & 0 deletions parser/tokenise.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) {
program = append(program, Instruction{opDupVal, pointers[0]})
pc++
program = append(program, Instruction{opNoop, pointers[1]})
} else if ok && factors[0] == factors[1] {
pc = jmpPc
program = program[:pc]
program = append(program, Instruction{opVec, pointers[0]})
pc++
program = append(program, Instruction{opNoop, pointers[1]})
pc++
program = append(program, Instruction{opNoop, factors[1]})
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions parser/tokenise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ func TestTokenise(t *testing.T) {
{opMovN, 1},
},
},
{
"op_vec",
">[->>+++>+++<<<]",
[]Instruction{
{opNoop, 0},
{opAddDp, 1},
{opVec, 2},
{opNoop, 3},
{opNoop, 3},
},
},
{
"op_dup",
">[->>+>+<<<]>[>>+<<-<<+>>]",
Expand Down

0 comments on commit 9203a17

Please sign in to comment.