Continued implementing the Instruction Pointer
This commit is contained in:
parent
46170dd5b7
commit
309dcb5a02
4 changed files with 54 additions and 3 deletions
|
@ -6,13 +6,16 @@ type Pointer struct {
|
||||||
x int
|
x int
|
||||||
y int
|
y int
|
||||||
delta *Delta
|
delta *Delta
|
||||||
|
// The Storage offset
|
||||||
|
sox int
|
||||||
|
soy int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPointer() *Pointer {
|
func NewPointer() *Pointer {
|
||||||
return &Pointer{delta: NewDelta(1, 0)}
|
return &Pointer{delta: NewDelta(1, 0)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Pointer) ForkPointer() *Pointer {
|
func (p Pointer) Split() *Pointer {
|
||||||
return &p // p is already a copy
|
return &p // p is already a copy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,14 +12,14 @@ func TestNewPointer(t *testing.T) {
|
||||||
require.Equal(t, NewPointer(), &Pointer{delta: &Delta{1, 0}})
|
require.Equal(t, NewPointer(), &Pointer{delta: &Delta{1, 0}})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestForkPointer(t *testing.T) {
|
func TestSplit(t *testing.T) {
|
||||||
file, err := os.Open("../field/test_data/hello.b98")
|
file, err := os.Open("../field/test_data/hello.b98")
|
||||||
require.NoError(t, err, "Failed to open file")
|
require.NoError(t, err, "Failed to open file")
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
f, err := field.Load(file)
|
f, err := field.Load(file)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
p2 := p.ForkPointer()
|
p2 := p.Split()
|
||||||
// We check that p2 is a real copy
|
// We check that p2 is a real copy
|
||||||
p.Step(*f)
|
p.Step(*f)
|
||||||
p2.Step(*f)
|
p2.Step(*f)
|
||||||
|
|
13
pkg/pointer/storage-offset.go
Normal file
13
pkg/pointer/storage-offset.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package pointer
|
||||||
|
|
||||||
|
func (p Pointer) GetStorageOffset() (x, y int) {
|
||||||
|
return p.sox, p.soy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pointer) CalculateNewStorageOffset() {
|
||||||
|
p.sox, p.soy = p.x+p.delta.x, p.y+p.delta.y
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pointer) SetStorageOffset(x, y int) {
|
||||||
|
p.sox, p.soy = x, y
|
||||||
|
}
|
35
pkg/pointer/storage-offset_test.go
Normal file
35
pkg/pointer/storage-offset_test.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package pointer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetStorageOffset(t *testing.T) {
|
||||||
|
p := NewPointer()
|
||||||
|
x, y := p.GetStorageOffset()
|
||||||
|
require.Equal(t, x, 0)
|
||||||
|
require.Equal(t, y, 0)
|
||||||
|
p.SetStorageOffset(3, 8)
|
||||||
|
x, y = p.GetStorageOffset()
|
||||||
|
require.Equal(t, x, 3)
|
||||||
|
require.Equal(t, y, 8)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalculateNewStorageOffset(t *testing.T) {
|
||||||
|
p := NewPointer()
|
||||||
|
p.CalculateNewStorageOffset()
|
||||||
|
x, y := p.GetStorageOffset()
|
||||||
|
require.Equal(t, x, 1)
|
||||||
|
require.Equal(t, y, 0)
|
||||||
|
p.sox, p.soy = 3, 2
|
||||||
|
x, y = p.GetStorageOffset()
|
||||||
|
require.Equal(t, x, 3)
|
||||||
|
require.Equal(t, y, 2)
|
||||||
|
p.x, p.y = 8, 12
|
||||||
|
p.CalculateNewStorageOffset()
|
||||||
|
x, y = p.GetStorageOffset()
|
||||||
|
require.Equal(t, x, 9)
|
||||||
|
require.Equal(t, y, 12)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue