2021-09-14 17:56:04 +02:00
|
|
|
|
package field
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 32-bit Befunge-98
|
|
|
|
|
// =================
|
|
|
|
|
// |-2,147,483,648
|
|
|
|
|
// |
|
|
|
|
|
// | x
|
|
|
|
|
// -----+-----
|
|
|
|
|
// -2,147,483,648 | 2,147,483,647
|
|
|
|
|
// |
|
|
|
|
|
// y|2,147,483,647
|
|
|
|
|
|
|
|
|
|
type Field struct {
|
2021-09-17 00:44:35 +02:00
|
|
|
|
x int
|
|
|
|
|
y int
|
|
|
|
|
lx int
|
|
|
|
|
ly int
|
|
|
|
|
lines []Line
|
2021-09-14 17:56:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Line struct {
|
2021-09-17 00:44:35 +02:00
|
|
|
|
x int
|
|
|
|
|
l int
|
|
|
|
|
columns []int
|
2021-09-14 17:56:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 01:22:18 +02:00
|
|
|
|
func Load(fd io.Reader) (*Field, error) {
|
2021-09-14 17:56:04 +02:00
|
|
|
|
f := new(Field)
|
|
|
|
|
l := new(Line)
|
|
|
|
|
trailingSpaces := 0
|
2021-10-07 18:17:03 +02:00
|
|
|
|
lastReadIsCR := false
|
2021-09-14 17:56:04 +02:00
|
|
|
|
for {
|
|
|
|
|
data := make([]byte, 4096)
|
|
|
|
|
if n, errRead := fd.Read(data); errRead != nil {
|
|
|
|
|
if errRead == io.EOF {
|
2021-10-02 00:59:38 +02:00
|
|
|
|
if f.ly == 0 {
|
|
|
|
|
if l.l == 0 {
|
|
|
|
|
return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
|
|
|
|
|
}
|
|
|
|
|
f.x = l.x
|
2021-09-14 17:56:04 +02:00
|
|
|
|
}
|
2021-09-17 00:44:35 +02:00
|
|
|
|
if l.l > 0 {
|
|
|
|
|
f.ly++
|
2021-10-02 00:59:38 +02:00
|
|
|
|
if f.x < l.x {
|
|
|
|
|
f.x = l.x
|
|
|
|
|
}
|
|
|
|
|
if f.lx < l.l+l.x-f.x {
|
|
|
|
|
f.lx = l.l + l.x - f.x
|
2021-09-17 00:44:35 +02:00
|
|
|
|
}
|
2021-09-14 17:56:04 +02:00
|
|
|
|
f.lines = append(f.lines, *l)
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
} else {
|
|
|
|
|
return nil, newReadError(errRead)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for i := 0; i < n; i++ {
|
2021-09-24 18:16:30 +02:00
|
|
|
|
if data[i] == '' {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-10-07 18:17:03 +02:00
|
|
|
|
if lastReadIsCR && data[i] == '\n' {
|
|
|
|
|
lastReadIsCR = false
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-09-14 17:56:04 +02:00
|
|
|
|
if data[i] == '\n' || data[i] == '\r' {
|
2021-10-02 00:59:38 +02:00
|
|
|
|
if f.ly == 0 {
|
|
|
|
|
if l.l == 0 {
|
|
|
|
|
return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
|
|
|
|
|
}
|
|
|
|
|
f.x = l.x
|
2021-09-14 17:56:04 +02:00
|
|
|
|
}
|
2021-09-17 00:44:35 +02:00
|
|
|
|
f.ly++
|
2021-10-07 18:17:03 +02:00
|
|
|
|
if l.l > 0 {
|
|
|
|
|
if f.x > l.x {
|
|
|
|
|
f.x = l.x
|
|
|
|
|
}
|
|
|
|
|
if f.lx < l.l+l.x-f.x {
|
|
|
|
|
f.lx = l.l + l.x - f.x
|
|
|
|
|
}
|
2021-09-17 00:44:35 +02:00
|
|
|
|
}
|
2021-09-14 17:56:04 +02:00
|
|
|
|
f.lines = append(f.lines, *l)
|
|
|
|
|
l = new(Line)
|
|
|
|
|
trailingSpaces = 0
|
2021-10-07 18:17:03 +02:00
|
|
|
|
if data[i] == '\r' {
|
|
|
|
|
if i+1 < n && data[i+1] == '\n' {
|
|
|
|
|
i++
|
|
|
|
|
} else {
|
|
|
|
|
lastReadIsCR = true
|
|
|
|
|
}
|
2021-09-14 17:56:04 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-09-17 00:44:35 +02:00
|
|
|
|
if l.l == 0 && data[i] == ' ' {
|
|
|
|
|
l.x++ // trim leading spaces
|
2021-09-14 17:56:04 +02:00
|
|
|
|
} else {
|
|
|
|
|
if data[i] == ' ' {
|
|
|
|
|
trailingSpaces++
|
|
|
|
|
} else {
|
2021-09-17 00:44:35 +02:00
|
|
|
|
for j := 0; j < trailingSpaces; j++ {
|
|
|
|
|
l.columns = append(l.columns, ' ')
|
|
|
|
|
}
|
|
|
|
|
l.l += trailingSpaces + 1
|
2021-09-14 17:56:04 +02:00
|
|
|
|
trailingSpaces = 0
|
2021-09-17 00:44:35 +02:00
|
|
|
|
l.columns = append(l.columns, int(data[i]))
|
2021-09-14 17:56:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|