Fixed y command, stack and stack stack manipulation bugs
This commit is contained in:
parent
4cb1cfbef5
commit
b3f1792ffa
3 changed files with 117 additions and 115 deletions
|
@ -3,6 +3,7 @@ package pointer
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
@ -189,15 +190,6 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) {
|
||||||
p.ss.head.Push(p.CharacterInput())
|
p.ss.head.Push(p.CharacterInput())
|
||||||
case 'y':
|
case 'y':
|
||||||
n := p.ss.head.Pop()
|
n := p.ss.head.Pop()
|
||||||
if n > 22+p.ss.height {
|
|
||||||
n = n - 20
|
|
||||||
if p.ss.head.height <= n {
|
|
||||||
p.ss.head.Push(0)
|
|
||||||
} else {
|
|
||||||
p.ss.head.Push(p.ss.head.data[p.ss.head.height-n])
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
x, y, lx, ly := f.Dump()
|
x, y, lx, ly := f.Dump()
|
||||||
const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
|
const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
|
||||||
|
@ -207,15 +199,18 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) {
|
||||||
heights[i] = s.height
|
heights[i] = s.height
|
||||||
s = s.next
|
s = s.next
|
||||||
}
|
}
|
||||||
if n <= 0 {
|
// 20
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() {
|
||||||
|
vars := strings.SplitN(e, "=", 2)
|
||||||
|
if vars[0] != "LC_ALL" && vars[0] != "PWD" && vars[0] != "PATH" && vars[0] != "DISPLAY" && vars[0] != "USER" && vars[0] != "TERM" && vars[0] != "LANG" && vars[0] != "HOME" && vars[0] != "EDITOR" && vars[0] != "SHELL" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
p.ss.head.Push(0)
|
p.ss.head.Push(0)
|
||||||
for i := len(e) - 1; i >= 0; i-- {
|
for i := len(e) - 1; i >= 0; i-- {
|
||||||
p.ss.head.Push(int(e[i]))
|
p.ss.head.Push(int(e[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// 19
|
||||||
if n <= 0 {
|
|
||||||
p.ss.head.Push(0)
|
p.ss.head.Push(0)
|
||||||
p.ss.head.Push(0)
|
p.ss.head.Push(0)
|
||||||
for i := len(p.Argv) - 1; i >= 0; i-- {
|
for i := len(p.Argv) - 1; i >= 0; i-- {
|
||||||
|
@ -224,78 +219,75 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) {
|
||||||
p.ss.head.Push(int(p.Argv[i][j]))
|
p.ss.head.Push(int(p.Argv[i][j]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// 18
|
||||||
if (n > 22 && n <= 22+p.ss.height) || n <= 0 {
|
|
||||||
for i := 0; i < len(heights); i++ {
|
for i := 0; i < len(heights); i++ {
|
||||||
p.ss.head.Push(heights[i])
|
p.ss.head.Push(heights[i])
|
||||||
}
|
}
|
||||||
}
|
// 17
|
||||||
if n == 22 || n <= 0 {
|
|
||||||
p.ss.head.Push(p.ss.height)
|
p.ss.head.Push(p.ss.height)
|
||||||
}
|
// 16
|
||||||
if n == 21 || n <= 0 {
|
|
||||||
p.ss.head.Push((now.Hour() * 256 * 256) + (now.Minute() * 256) + now.Second())
|
p.ss.head.Push((now.Hour() * 256 * 256) + (now.Minute() * 256) + now.Second())
|
||||||
}
|
// 15
|
||||||
if n == 20 || n <= 0 {
|
|
||||||
p.ss.head.Push(((now.Year() - 1900) * 256 * 256) + (int(now.Month()) * 256) + now.Day())
|
p.ss.head.Push(((now.Year() - 1900) * 256 * 256) + (int(now.Month()) * 256) + now.Day())
|
||||||
}
|
// 14
|
||||||
if n == 19 || n <= 0 {
|
|
||||||
p.ss.head.Push(lx + x)
|
p.ss.head.Push(lx + x)
|
||||||
}
|
|
||||||
if n == 18 || n <= 0 {
|
|
||||||
p.ss.head.Push(ly + y)
|
p.ss.head.Push(ly + y)
|
||||||
}
|
// 13
|
||||||
if n == 17 || n <= 0 {
|
|
||||||
p.ss.head.Push(x)
|
p.ss.head.Push(x)
|
||||||
}
|
|
||||||
if n == 16 || n <= 0 {
|
|
||||||
p.ss.head.Push(y)
|
p.ss.head.Push(y)
|
||||||
}
|
// 12
|
||||||
if n == 15 || n <= 0 {
|
|
||||||
p.ss.head.Push(p.sox)
|
p.ss.head.Push(p.sox)
|
||||||
}
|
|
||||||
if n == 14 || n <= 0 {
|
|
||||||
p.ss.head.Push(p.soy)
|
p.ss.head.Push(p.soy)
|
||||||
}
|
// 11
|
||||||
if n == 13 || n <= 0 {
|
|
||||||
p.ss.head.Push(p.dx)
|
p.ss.head.Push(p.dx)
|
||||||
}
|
|
||||||
if n == 12 || n <= 0 {
|
|
||||||
p.ss.head.Push(p.dy)
|
p.ss.head.Push(p.dy)
|
||||||
}
|
// 10
|
||||||
if n == 11 || n <= 0 {
|
|
||||||
p.ss.head.Push(p.x)
|
p.ss.head.Push(p.x)
|
||||||
}
|
|
||||||
if n == 10 || n <= 0 {
|
|
||||||
p.ss.head.Push(p.y)
|
p.ss.head.Push(p.y)
|
||||||
}
|
// 9
|
||||||
if n == 9 || n <= 0 {
|
|
||||||
p.ss.head.Push(0)
|
p.ss.head.Push(0)
|
||||||
}
|
// 8
|
||||||
if n == 8 || n <= 0 {
|
|
||||||
p.ss.head.Push(*((*int)(unsafe.Pointer(p))))
|
p.ss.head.Push(*((*int)(unsafe.Pointer(p))))
|
||||||
}
|
// 7
|
||||||
if n == 7 || n <= 0 {
|
|
||||||
p.ss.head.Push(2)
|
p.ss.head.Push(2)
|
||||||
}
|
// 6
|
||||||
if n == 6 || n <= 0 {
|
|
||||||
p.ss.head.Push('/')
|
p.ss.head.Push('/')
|
||||||
}
|
// 5
|
||||||
if n == 5 || n <= 0 {
|
|
||||||
p.ss.head.Push(0) // TODO update when implementing =
|
p.ss.head.Push(0) // TODO update when implementing =
|
||||||
}
|
// 4
|
||||||
if n == 4 || n <= 0 {
|
|
||||||
p.ss.head.Push(1)
|
p.ss.head.Push(1)
|
||||||
}
|
// 3
|
||||||
if n == 3 || n <= 0 {
|
|
||||||
p.ss.head.Push(1048576)
|
p.ss.head.Push(1048576)
|
||||||
}
|
// 2
|
||||||
if n == 2 || n <= 0 {
|
|
||||||
p.ss.head.Push(uintSize / 8)
|
p.ss.head.Push(uintSize / 8)
|
||||||
}
|
// 1
|
||||||
if n == 1 || n <= 0 {
|
|
||||||
p.ss.head.Push(0b00000) // TODO update when implementing t, i, o and =
|
p.ss.head.Push(0b00000) // TODO update when implementing t, i, o and =
|
||||||
|
if n > 0 {
|
||||||
|
if n > p.ss.head.height {
|
||||||
|
p.ss.head.height = 1
|
||||||
|
p.ss.head.data[0] = 0
|
||||||
|
} else {
|
||||||
|
v := p.ss.head.data[p.ss.head.height-n]
|
||||||
|
p.ss.head.height = heights[0]
|
||||||
|
p.ss.head.Push(v)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
case '(':
|
||||||
|
n := p.ss.head.Pop()
|
||||||
|
v := 0
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
v = v*256 + p.ss.head.Pop()
|
||||||
|
}
|
||||||
|
// No fingerprints supported
|
||||||
|
p.Reverse()
|
||||||
|
case ')':
|
||||||
|
n := p.ss.head.Pop()
|
||||||
|
v := 0
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
v = v*256 + p.ss.head.Pop()
|
||||||
|
}
|
||||||
|
// No fingerprints supported
|
||||||
|
p.Reverse()
|
||||||
case 'i':
|
case 'i':
|
||||||
log.Fatalf("Non implemented instruction code %d : %c", c, c)
|
log.Fatalf("Non implemented instruction code %d : %c", c, c)
|
||||||
case 'o':
|
case 'o':
|
||||||
|
@ -304,6 +296,9 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) {
|
||||||
log.Fatalf("Non implemented instruction code %d : %c", c, c)
|
log.Fatalf("Non implemented instruction code %d : %c", c, c)
|
||||||
case 't':
|
case 't':
|
||||||
log.Fatalf("Non implemented instruction code %d : %c", c, c)
|
log.Fatalf("Non implemented instruction code %d : %c", c, c)
|
||||||
|
case 12:
|
||||||
|
// Trifunge function that resets in befunge
|
||||||
|
p.x, p.y = 0, 0
|
||||||
default:
|
default:
|
||||||
handled = false
|
handled = false
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,8 @@ func TestBegin(t *testing.T) {
|
||||||
expected.head.next.Push(0)
|
expected.head.next.Push(0)
|
||||||
expected.head.next.Push(0)
|
expected.head.next.Push(0)
|
||||||
expected.height++
|
expected.height++
|
||||||
ss := NewStackStack()
|
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
|
ss := p.ss
|
||||||
ss.Begin(p)
|
ss.Begin(p)
|
||||||
require.Equal(t, expected, ss)
|
require.Equal(t, expected, ss)
|
||||||
x, y := p.GetStorageOffset()
|
x, y := p.GetStorageOffset()
|
||||||
|
@ -35,7 +35,12 @@ func TestBegin(t *testing.T) {
|
||||||
})
|
})
|
||||||
t.Run("negative", func(t *testing.T) {
|
t.Run("negative", func(t *testing.T) {
|
||||||
expected := NewStackStack()
|
expected := NewStackStack()
|
||||||
expected.head = &Stack{size: 5, height: 5, data: make([]int, 5), next: expected.head}
|
expected.head = &Stack{size: 5, height: 0, data: make([]int, 5), next: expected.head}
|
||||||
|
expected.head.next.Push(0)
|
||||||
|
expected.head.next.Push(0)
|
||||||
|
expected.head.next.Push(0)
|
||||||
|
expected.head.next.Push(0)
|
||||||
|
expected.head.next.Push(0)
|
||||||
expected.head.next.Push(0)
|
expected.head.next.Push(0)
|
||||||
expected.head.next.Push(0)
|
expected.head.next.Push(0)
|
||||||
expected.height++
|
expected.height++
|
||||||
|
@ -44,7 +49,7 @@ func TestBegin(t *testing.T) {
|
||||||
require.NoError(t, err, "Failed to open file")
|
require.NoError(t, err, "Failed to open file")
|
||||||
f, err := field.Load(file)
|
f, err := field.Load(file)
|
||||||
p.Step(*f)
|
p.Step(*f)
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.head.Push(-5)
|
ss.head.Push(-5)
|
||||||
ss.Begin(p)
|
ss.Begin(p)
|
||||||
require.Equal(t, expected, ss)
|
require.Equal(t, expected, ss)
|
||||||
|
@ -56,7 +61,6 @@ func TestBegin(t *testing.T) {
|
||||||
expected := NewStackStack()
|
expected := NewStackStack()
|
||||||
expected.head = &Stack{size: 34, height: 34, data: make([]int, 34), next: expected.head}
|
expected.head = &Stack{size: 34, height: 34, data: make([]int, 34), next: expected.head}
|
||||||
expected.head.data[33] = 18
|
expected.head.data[33] = 18
|
||||||
expected.head.next.Push(18)
|
|
||||||
expected.head.next.Push(2)
|
expected.head.next.Push(2)
|
||||||
expected.head.next.Push(3)
|
expected.head.next.Push(3)
|
||||||
expected.height++
|
expected.height++
|
||||||
|
@ -66,7 +70,7 @@ func TestBegin(t *testing.T) {
|
||||||
require.NoError(t, err, "Failed to open file")
|
require.NoError(t, err, "Failed to open file")
|
||||||
f, err := field.Load(file)
|
f, err := field.Load(file)
|
||||||
p.Step(*f)
|
p.Step(*f)
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.head.Push(18)
|
ss.head.Push(18)
|
||||||
ss.head.Push(34)
|
ss.head.Push(34)
|
||||||
ss.Begin(p)
|
ss.Begin(p)
|
||||||
|
@ -79,16 +83,18 @@ func TestBegin(t *testing.T) {
|
||||||
expected := NewStackStack()
|
expected := NewStackStack()
|
||||||
expected.head = &Stack{size: 4, height: 4, data: []int{12, 14, -2, 5}, next: expected.head}
|
expected.head = &Stack{size: 4, height: 4, data: []int{12, 14, -2, 5}, next: expected.head}
|
||||||
expected.head.next.Push(7)
|
expected.head.next.Push(7)
|
||||||
expected.head.next.Push(12)
|
|
||||||
expected.head.next.Push(14)
|
|
||||||
expected.head.next.Push(-2)
|
|
||||||
expected.head.next.Push(5)
|
|
||||||
expected.head.next.Push(36)
|
expected.head.next.Push(36)
|
||||||
expected.head.next.Push(42)
|
expected.head.next.Push(42)
|
||||||
|
expected.head.next.Push(-2)
|
||||||
|
expected.head.next.Push(5)
|
||||||
|
expected.head.next.Push(4)
|
||||||
|
expected.head.next.Pop()
|
||||||
|
expected.head.next.Pop()
|
||||||
|
expected.head.next.Pop()
|
||||||
expected.height++
|
expected.height++
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
p.SetStorageOffset(36, 42)
|
p.SetStorageOffset(36, 42)
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.head.Push(7)
|
ss.head.Push(7)
|
||||||
ss.head.Push(12)
|
ss.head.Push(12)
|
||||||
ss.head.Push(14)
|
ss.head.Push(14)
|
||||||
|
@ -104,7 +110,7 @@ func TestEnd(t *testing.T) {
|
||||||
t.Run("empty", func(t *testing.T) {
|
t.Run("empty", func(t *testing.T) {
|
||||||
expected := NewStackStack()
|
expected := NewStackStack()
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.Begin(p)
|
ss.Begin(p)
|
||||||
reflect := ss.End(p)
|
reflect := ss.End(p)
|
||||||
require.Equal(t, false, reflect)
|
require.Equal(t, false, reflect)
|
||||||
|
@ -121,7 +127,7 @@ func TestEnd(t *testing.T) {
|
||||||
expected.head.Pop()
|
expected.head.Pop()
|
||||||
expected.head.Pop()
|
expected.head.Pop()
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.head.Push(7)
|
ss.head.Push(7)
|
||||||
ss.head.Push(12)
|
ss.head.Push(12)
|
||||||
ss.head.Push(14)
|
ss.head.Push(14)
|
||||||
|
@ -139,7 +145,7 @@ func TestEnd(t *testing.T) {
|
||||||
t.Run("drop too much", func(t *testing.T) {
|
t.Run("drop too much", func(t *testing.T) {
|
||||||
expected := NewStackStack()
|
expected := NewStackStack()
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.Begin(p)
|
ss.Begin(p)
|
||||||
ss.head.Push(-3)
|
ss.head.Push(-3)
|
||||||
reflect := ss.End(p)
|
reflect := ss.End(p)
|
||||||
|
@ -149,7 +155,7 @@ func TestEnd(t *testing.T) {
|
||||||
t.Run("reflect", func(t *testing.T) {
|
t.Run("reflect", func(t *testing.T) {
|
||||||
expected := NewStackStack()
|
expected := NewStackStack()
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
reflect := ss.End(p)
|
reflect := ss.End(p)
|
||||||
require.Equal(t, true, reflect)
|
require.Equal(t, true, reflect)
|
||||||
require.Equal(t, expected, ss)
|
require.Equal(t, expected, ss)
|
||||||
|
@ -164,7 +170,7 @@ func TestEnd(t *testing.T) {
|
||||||
expected.head.Push(-2)
|
expected.head.Push(-2)
|
||||||
expected.head.Push(5)
|
expected.head.Push(5)
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.head.size = 4
|
ss.head.size = 4
|
||||||
ss.head.data = make([]int, 4)
|
ss.head.data = make([]int, 4)
|
||||||
ss.head.Push(7)
|
ss.head.Push(7)
|
||||||
|
@ -188,10 +194,10 @@ func TestEnd(t *testing.T) {
|
||||||
func TestUnder(t *testing.T) {
|
func TestUnder(t *testing.T) {
|
||||||
t.Run("empty", func(t *testing.T) {
|
t.Run("empty", func(t *testing.T) {
|
||||||
expected := NewStackStack()
|
expected := NewStackStack()
|
||||||
ss := NewStackStack()
|
p := NewPointer()
|
||||||
reflect := ss.Under()
|
reflect := p.ss.Under()
|
||||||
require.Equal(t, true, reflect)
|
require.Equal(t, true, reflect)
|
||||||
require.Equal(t, expected, ss)
|
require.Equal(t, expected, p.ss)
|
||||||
})
|
})
|
||||||
t.Run("positive", func(t *testing.T) {
|
t.Run("positive", func(t *testing.T) {
|
||||||
pe := NewPointer()
|
pe := NewPointer()
|
||||||
|
@ -209,7 +215,7 @@ func TestUnder(t *testing.T) {
|
||||||
expected.head.next.Pop()
|
expected.head.next.Pop()
|
||||||
expected.head.next.Pop()
|
expected.head.next.Pop()
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.head.Push(1)
|
ss.head.Push(1)
|
||||||
ss.head.Push(2)
|
ss.head.Push(2)
|
||||||
ss.head.Push(3)
|
ss.head.Push(3)
|
||||||
|
@ -237,7 +243,7 @@ func TestUnder(t *testing.T) {
|
||||||
expected.head.Pop()
|
expected.head.Pop()
|
||||||
expected.head.Pop()
|
expected.head.Pop()
|
||||||
p := NewPointer()
|
p := NewPointer()
|
||||||
ss := NewStackStack()
|
ss := p.ss
|
||||||
ss.Begin(p)
|
ss.Begin(p)
|
||||||
ss.head.Push(8)
|
ss.head.Push(8)
|
||||||
ss.head.Push(5)
|
ss.head.Push(5)
|
||||||
|
|
|
@ -13,16 +13,17 @@ func TestClear(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDupicate(t *testing.T) {
|
func TestDupicate(t *testing.T) {
|
||||||
|
expected := NewStack()
|
||||||
|
expected.height = 2
|
||||||
s := NewStack()
|
s := NewStack()
|
||||||
s2 := NewStack()
|
|
||||||
s.Duplicate()
|
s.Duplicate()
|
||||||
require.Equal(t, s2.height, s.height)
|
require.Equal(t, expected.height, s.height)
|
||||||
s.Push(12)
|
s.Push(12)
|
||||||
s.Duplicate()
|
s.Duplicate()
|
||||||
s2.Push(12)
|
expected.Push(12)
|
||||||
s2.Push(12)
|
expected.Push(12)
|
||||||
require.Equal(t, s2.height, s.height)
|
require.Equal(t, expected.height, s.height)
|
||||||
require.Equal(t, s2.data, s.data)
|
require.Equal(t, expected.data, s.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPop(t *testing.T) {
|
func TestPop(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue