rewrote field data structure for simplicity

This commit is contained in:
Julien Dessaux 2021-09-17 00:44:35 +02:00
parent 4bacbc2375
commit 7458dd8aa8
2 changed files with 86 additions and 69 deletions

View file

@ -1,7 +1,6 @@
package field
import (
"bytes"
"io"
)
@ -16,15 +15,17 @@ import (
// y|2,147,483,647
type Field struct {
firstLineIndex int
length int
x int
y int
lx int
ly int
lines []Line
}
type Line struct {
firstColumnIndex int
length int
columns []byte
x int
l int
columns []int
}
func LoadFile(fd io.Reader) (*Field, error) {
@ -35,11 +36,14 @@ func LoadFile(fd io.Reader) (*Field, error) {
data := make([]byte, 4096)
if n, errRead := fd.Read(data); errRead != nil {
if errRead == io.EOF {
if f.length == 0 && l.length == 0 {
if f.ly == 0 && l.l == 0 {
return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
}
if l.length > 0 {
f.length++
if l.l > 0 {
f.ly++
if f.lx < l.l {
f.lx = l.l
}
f.lines = append(f.lines, *l)
}
break
@ -49,10 +53,13 @@ func LoadFile(fd io.Reader) (*Field, error) {
} else {
for i := 0; i < n; i++ {
if data[i] == '\n' || data[i] == '\r' {
if f.length == 0 && l.length == 0 {
if f.ly == 0 && l.l == 0 {
return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
}
f.length++
f.ly++
if f.lx < l.l {
f.lx = l.l
}
f.lines = append(f.lines, *l)
l = new(Line)
trailingSpaces = 0
@ -60,17 +67,18 @@ func LoadFile(fd io.Reader) (*Field, error) {
i++
}
} else {
if l.length == 0 && data[i] == ' ' {
l.firstColumnIndex++ // trim leading spaces
if l.l == 0 && data[i] == ' ' {
l.x++ // trim leading spaces
} else {
if data[i] == ' ' {
trailingSpaces++
} else {
l.columns = append(l.columns, bytes.Repeat([]byte{' '}, trailingSpaces)...)
l.length += trailingSpaces
for j := 0; j < trailingSpaces; j++ {
l.columns = append(l.columns, ' ')
}
l.l += trailingSpaces + 1
trailingSpaces = 0
l.length++
l.columns = append(l.columns, data[i])
l.columns = append(l.columns, int(data[i]))
}
}
}

View file

@ -6,108 +6,117 @@ import (
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadFile(t *testing.T) {
// minimal b98 file
minimalField := Field{
firstLineIndex: 0,
length: 1,
x: 0,
y: 0,
lx: 1,
ly: 1,
lines: []Line{
Line{
firstColumnIndex: 0,
length: 1,
columns: []byte{'@'},
x: 0,
l: 1,
columns: []int{'@'},
},
},
}
// hello b98 file
helloField := Field{
firstLineIndex: 0,
length: 1,
x: 0,
y: 0,
lx: 24,
ly: 1,
lines: []Line{
Line{
firstColumnIndex: 0,
length: 24,
columns: []byte{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'},
x: 0,
l: 24,
columns: []int{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'},
},
},
}
// factorial b98 file
factorialField := Field{
firstLineIndex: 0,
length: 2,
x: 0,
y: 0,
lx: 15,
ly: 2,
lines: []Line{
Line{
firstColumnIndex: 0,
length: 15,
columns: []byte{'&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'},
x: 0,
l: 15,
columns: []int{'&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'},
},
Line{
firstColumnIndex: 1,
length: 11,
columns: []byte{'^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'},
x: 1,
l: 11,
columns: []int{'^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'},
},
},
}
// dna b98 file
dnaField := Field{
firstLineIndex: 0,
length: 8,
x: 0,
y: 0,
lx: 7,
ly: 8,
lines: []Line{
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'7', '^', 'D', 'N', '>', 'v', 'A'},
x: 0,
l: 7,
columns: []int{'7', '^', 'D', 'N', '>', 'v', 'A'},
},
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'v', '_', '#', 'v', '?', ' ', 'v'},
x: 0,
l: 7,
columns: []int{'v', '_', '#', 'v', '?', ' ', 'v'},
},
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'7', '^', '<', '"', '"', '"', '"'},
x: 0,
l: 7,
columns: []int{'7', '^', '<', '"', '"', '"', '"'},
},
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'3', ' ', ' ', 'A', 'C', 'G', 'T'},
x: 0,
l: 7,
columns: []int{'3', ' ', ' ', 'A', 'C', 'G', 'T'},
},
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'9', '0', '!', '"', '"', '"', '"'},
x: 0,
l: 7,
columns: []int{'9', '0', '!', '"', '"', '"', '"'},
},
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'4', '*', ':', '>', '>', '>', 'v'},
x: 0,
l: 7,
columns: []int{'4', '*', ':', '>', '>', '>', 'v'},
},
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'+', '8', '^', '-', '1', ',', '<'},
x: 0,
l: 7,
columns: []int{'+', '8', '^', '-', '1', ',', '<'},
},
Line{
firstColumnIndex: 0,
length: 7,
columns: []byte{'>', ' ', ',', '+', ',', '@', ')'},
x: 0,
l: 7,
columns: []int{'>', ' ', ',', '+', ',', '@', ')'},
},
},
}
// \r\n file b98 file
rnField := Field{
firstLineIndex: 0,
length: 1,
x: 0,
y: 0,
lx: 24,
ly: 1,
lines: []Line{
Line{
firstColumnIndex: 0,
length: 24,
columns: []byte{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'},
x: 0,
l: 24,
columns: []int{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'},
},
},
}
@ -148,7 +157,7 @@ func TestLoadFile(t *testing.T) {
} else {
require.NoError(t, err)
}
assert.Equal(t, tc.expected, valid, "Invalid value")
require.Equal(t, tc.expected, valid, "Invalid value")
})
}
}