From b3f1792ffa7d25020541cdb48b32551c90277565 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 24 Sep 2021 16:45:18 +0200 Subject: Fixed y command, stack and stack stack manipulation bugs --- pkg/pointer/exec.go | 173 +++++++++++++++++++--------------------- pkg/pointer/stack-stack_test.go | 46 ++++++----- pkg/pointer/stack_test.go | 13 +-- 3 files changed, 117 insertions(+), 115 deletions(-) (limited to 'pkg') diff --git a/pkg/pointer/exec.go b/pkg/pointer/exec.go index 4b96575..0590482 100644 --- a/pkg/pointer/exec.go +++ b/pkg/pointer/exec.go @@ -3,6 +3,7 @@ package pointer import ( "log" "os" + "strings" "time" "unsafe" @@ -189,15 +190,6 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) { p.ss.head.Push(p.CharacterInput()) case 'y': 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() x, y, lx, ly := f.Dump() const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64 @@ -207,95 +199,95 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) { heights[i] = s.height s = s.next } - if n <= 0 { - for _, e := range os.Environ() { - p.ss.head.Push(0) - for i := len(e) - 1; i >= 0; i-- { - p.ss.head.Push(int(e[i])) - } + // 20 + 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 } - } - if n <= 0 { - p.ss.head.Push(0) p.ss.head.Push(0) - for i := len(p.Argv) - 1; i >= 0; i-- { - p.ss.head.Push(0) - for j := len(p.Argv[i]) - 1; j >= 0; j-- { - p.ss.head.Push(int(p.Argv[i][j])) - } + for i := len(e) - 1; i >= 0; i-- { + p.ss.head.Push(int(e[i])) } } - if (n > 22 && n <= 22+p.ss.height) || n <= 0 { - for i := 0; i < len(heights); i++ { - p.ss.head.Push(heights[i]) - } - } - if n == 22 || n <= 0 { - p.ss.head.Push(p.ss.height) - } - if n == 21 || n <= 0 { - p.ss.head.Push((now.Hour() * 256 * 256) + (now.Minute() * 256) + now.Second()) - } - if n == 20 || n <= 0 { - p.ss.head.Push(((now.Year() - 1900) * 256 * 256) + (int(now.Month()) * 256) + now.Day()) - } - if n == 19 || n <= 0 { - p.ss.head.Push(lx + x) - } - if n == 18 || n <= 0 { - p.ss.head.Push(ly + y) - } - if n == 17 || n <= 0 { - p.ss.head.Push(x) - } - if n == 16 || n <= 0 { - p.ss.head.Push(y) - } - if n == 15 || n <= 0 { - p.ss.head.Push(p.sox) - } - if n == 14 || n <= 0 { - p.ss.head.Push(p.soy) - } - if n == 13 || n <= 0 { - p.ss.head.Push(p.dx) - } - if n == 12 || n <= 0 { - p.ss.head.Push(p.dy) - } - if n == 11 || n <= 0 { - p.ss.head.Push(p.x) - } - if n == 10 || n <= 0 { - p.ss.head.Push(p.y) - } - if n == 9 || n <= 0 { + // 19 + p.ss.head.Push(0) + p.ss.head.Push(0) + for i := len(p.Argv) - 1; i >= 0; i-- { p.ss.head.Push(0) + for j := len(p.Argv[i]) - 1; j >= 0; j-- { + p.ss.head.Push(int(p.Argv[i][j])) + } } - if n == 8 || n <= 0 { - p.ss.head.Push(*((*int)(unsafe.Pointer(p)))) - } - if n == 7 || n <= 0 { - p.ss.head.Push(2) - } - if n == 6 || n <= 0 { - p.ss.head.Push('/') - } - if n == 5 || n <= 0 { - p.ss.head.Push(0) // TODO update when implementing = - } - if n == 4 || n <= 0 { - p.ss.head.Push(1) - } - if n == 3 || n <= 0 { - p.ss.head.Push(1048576) + // 18 + for i := 0; i < len(heights); i++ { + p.ss.head.Push(heights[i]) + } + // 17 + p.ss.head.Push(p.ss.height) + // 16 + p.ss.head.Push((now.Hour() * 256 * 256) + (now.Minute() * 256) + now.Second()) + // 15 + p.ss.head.Push(((now.Year() - 1900) * 256 * 256) + (int(now.Month()) * 256) + now.Day()) + // 14 + p.ss.head.Push(lx + x) + p.ss.head.Push(ly + y) + // 13 + p.ss.head.Push(x) + p.ss.head.Push(y) + // 12 + p.ss.head.Push(p.sox) + p.ss.head.Push(p.soy) + // 11 + p.ss.head.Push(p.dx) + p.ss.head.Push(p.dy) + // 10 + p.ss.head.Push(p.x) + p.ss.head.Push(p.y) + // 9 + p.ss.head.Push(0) + // 8 + p.ss.head.Push(*((*int)(unsafe.Pointer(p)))) + // 7 + p.ss.head.Push(2) + // 6 + p.ss.head.Push('/') + // 5 + p.ss.head.Push(0) // TODO update when implementing = + // 4 + p.ss.head.Push(1) + // 3 + p.ss.head.Push(1048576) + // 2 + p.ss.head.Push(uintSize / 8) + // 1 + 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) + } } - if n == 2 || n <= 0 { - p.ss.head.Push(uintSize / 8) + case '(': + n := p.ss.head.Pop() + v := 0 + for i := 0; i < n; i++ { + v = v*256 + p.ss.head.Pop() } - if n == 1 || n <= 0 { - p.ss.head.Push(0b00000) // TODO update when implementing t, i, o and = + // 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': log.Fatalf("Non implemented instruction code %d : %c", c, c) 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) case 't': 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: handled = false } diff --git a/pkg/pointer/stack-stack_test.go b/pkg/pointer/stack-stack_test.go index a5d45c6..dcb1960 100644 --- a/pkg/pointer/stack-stack_test.go +++ b/pkg/pointer/stack-stack_test.go @@ -15,8 +15,8 @@ func TestBegin(t *testing.T) { expected.head.next.Push(0) expected.head.next.Push(0) expected.height++ - ss := NewStackStack() p := NewPointer() + ss := p.ss ss.Begin(p) require.Equal(t, expected, ss) x, y := p.GetStorageOffset() @@ -35,7 +35,12 @@ func TestBegin(t *testing.T) { }) t.Run("negative", func(t *testing.T) { 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.height++ @@ -44,7 +49,7 @@ func TestBegin(t *testing.T) { require.NoError(t, err, "Failed to open file") f, err := field.Load(file) p.Step(*f) - ss := NewStackStack() + ss := p.ss ss.head.Push(-5) ss.Begin(p) require.Equal(t, expected, ss) @@ -56,7 +61,6 @@ func TestBegin(t *testing.T) { expected := NewStackStack() expected.head = &Stack{size: 34, height: 34, data: make([]int, 34), next: expected.head} expected.head.data[33] = 18 - expected.head.next.Push(18) expected.head.next.Push(2) expected.head.next.Push(3) expected.height++ @@ -66,7 +70,7 @@ func TestBegin(t *testing.T) { require.NoError(t, err, "Failed to open file") f, err := field.Load(file) p.Step(*f) - ss := NewStackStack() + ss := p.ss ss.head.Push(18) ss.head.Push(34) ss.Begin(p) @@ -79,16 +83,18 @@ func TestBegin(t *testing.T) { expected := NewStackStack() 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(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(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++ p := NewPointer() p.SetStorageOffset(36, 42) - ss := NewStackStack() + ss := p.ss ss.head.Push(7) ss.head.Push(12) ss.head.Push(14) @@ -104,7 +110,7 @@ func TestEnd(t *testing.T) { t.Run("empty", func(t *testing.T) { expected := NewStackStack() p := NewPointer() - ss := NewStackStack() + ss := p.ss ss.Begin(p) reflect := ss.End(p) require.Equal(t, false, reflect) @@ -121,7 +127,7 @@ func TestEnd(t *testing.T) { expected.head.Pop() expected.head.Pop() p := NewPointer() - ss := NewStackStack() + ss := p.ss ss.head.Push(7) ss.head.Push(12) ss.head.Push(14) @@ -139,7 +145,7 @@ func TestEnd(t *testing.T) { t.Run("drop too much", func(t *testing.T) { expected := NewStackStack() p := NewPointer() - ss := NewStackStack() + ss := p.ss ss.Begin(p) ss.head.Push(-3) reflect := ss.End(p) @@ -149,7 +155,7 @@ func TestEnd(t *testing.T) { t.Run("reflect", func(t *testing.T) { expected := NewStackStack() p := NewPointer() - ss := NewStackStack() + ss := p.ss reflect := ss.End(p) require.Equal(t, true, reflect) require.Equal(t, expected, ss) @@ -164,7 +170,7 @@ func TestEnd(t *testing.T) { expected.head.Push(-2) expected.head.Push(5) p := NewPointer() - ss := NewStackStack() + ss := p.ss ss.head.size = 4 ss.head.data = make([]int, 4) ss.head.Push(7) @@ -188,10 +194,10 @@ func TestEnd(t *testing.T) { func TestUnder(t *testing.T) { t.Run("empty", func(t *testing.T) { expected := NewStackStack() - ss := NewStackStack() - reflect := ss.Under() + p := NewPointer() + reflect := p.ss.Under() require.Equal(t, true, reflect) - require.Equal(t, expected, ss) + require.Equal(t, expected, p.ss) }) t.Run("positive", func(t *testing.T) { pe := NewPointer() @@ -209,7 +215,7 @@ func TestUnder(t *testing.T) { expected.head.next.Pop() expected.head.next.Pop() p := NewPointer() - ss := NewStackStack() + ss := p.ss ss.head.Push(1) ss.head.Push(2) ss.head.Push(3) @@ -237,7 +243,7 @@ func TestUnder(t *testing.T) { expected.head.Pop() expected.head.Pop() p := NewPointer() - ss := NewStackStack() + ss := p.ss ss.Begin(p) ss.head.Push(8) ss.head.Push(5) diff --git a/pkg/pointer/stack_test.go b/pkg/pointer/stack_test.go index 1b16085..35700fd 100644 --- a/pkg/pointer/stack_test.go +++ b/pkg/pointer/stack_test.go @@ -13,16 +13,17 @@ func TestClear(t *testing.T) { } func TestDupicate(t *testing.T) { + expected := NewStack() + expected.height = 2 s := NewStack() - s2 := NewStack() s.Duplicate() - require.Equal(t, s2.height, s.height) + require.Equal(t, expected.height, s.height) s.Push(12) s.Duplicate() - s2.Push(12) - s2.Push(12) - require.Equal(t, s2.height, s.height) - require.Equal(t, s2.data, s.data) + expected.Push(12) + expected.Push(12) + require.Equal(t, expected.height, s.height) + require.Equal(t, expected.data, s.data) } func TestPop(t *testing.T) { -- cgit v1.2.3