aboutsummaryrefslogtreecommitdiff
path: root/pkg/field/field.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pkg/field/field.go44
1 files changed, 26 insertions, 18 deletions
diff --git a/pkg/field/field.go b/pkg/field/field.go
index fde478a..0495635 100644
--- a/pkg/field/field.go
+++ b/pkg/field/field.go
@@ -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
- lines []Line
+ 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]))
}
}
}