Finished implementing the funge space field
This commit is contained in:
parent
40257e354d
commit
52be0d0d1e
11 changed files with 131 additions and 12 deletions
8
examples/dna.b98
Normal file
8
examples/dna.b98
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
7^DN>vA
|
||||||
|
v_#v? v
|
||||||
|
7^<""""
|
||||||
|
3 ACGT
|
||||||
|
90!""""
|
||||||
|
4*:>>>v
|
||||||
|
+8^-1,<
|
||||||
|
> ,+,@)
|
0
examples/empty.b98
Normal file
0
examples/empty.b98
Normal file
2
examples/factorial.b98
Normal file
2
examples/factorial.b98
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
&>:1-:v v *_$.@
|
||||||
|
^ _$>\:^
|
1
examples/hello.b98
Normal file
1
examples/hello.b98
Normal file
|
@ -0,0 +1 @@
|
||||||
|
64+"!dlroW ,olleH">:#,_@
|
2
examples/hello2.b98
Normal file
2
examples/hello2.b98
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
v
|
||||||
|
@ > #;>:#,_e-j; "Hello world!"da<
|
2
examples/invalid.b98
Normal file
2
examples/invalid.b98
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
@
|
1
examples/minimal.b98
Normal file
1
examples/minimal.b98
Normal file
|
@ -0,0 +1 @@
|
||||||
|
@
|
1
examples/rn.b98
Normal file
1
examples/rn.b98
Normal file
|
@ -0,0 +1 @@
|
||||||
|
64+"!dlroW ,olleH">:#,_@
|
|
@ -8,7 +8,7 @@ type
|
||||||
lx, ly: int
|
lx, ly: int
|
||||||
lines: seq[Line]
|
lines: seq[Line]
|
||||||
|
|
||||||
proc blank*(f: var Field, x, y: int) =
|
func blank*(f: var Field, x, y: int) =
|
||||||
if y < f.y or y >= f.y+f.ly: # outside the field
|
if y < f.y or y >= f.y+f.ly: # outside the field
|
||||||
return
|
return
|
||||||
var l = addr f.lines[y-f.y]
|
var l = addr f.lines[y-f.y]
|
||||||
|
@ -59,17 +59,80 @@ proc blank*(f: var Field, x, y: int) =
|
||||||
x2 = f.lines[i].x + f.lines[i].l
|
x2 = f.lines[i].x + f.lines[i].l
|
||||||
f.lx = x2-f.x
|
f.lx = x2-f.x
|
||||||
|
|
||||||
proc get*(f: Field, x, y: int): int =
|
func get*(f: Field, x, y: int): int =
|
||||||
if y >= f.y and y < f.y+f.ly:
|
if y >= f.y and y < f.y+f.ly:
|
||||||
let l = f.lines[y-f.y]
|
let l = f.lines[y-f.y]
|
||||||
if x >= l.x and x < l.x+l.l:
|
if x >= l.x and x < l.x+l.l:
|
||||||
return l.columns[x-l.x]
|
return l.columns[x-l.x]
|
||||||
return int(' ')
|
return int(' ')
|
||||||
|
|
||||||
proc isIn*(f: Field, x, y: int): bool =
|
func isIn*(f: Field, x, y: int): bool =
|
||||||
return x >= f.x and y >= f.y and x < f.x+f.lx and y < f.y+f.ly
|
return x >= f.x and y >= f.y and x < f.x+f.lx and y < f.y+f.ly
|
||||||
|
|
||||||
proc set*(f: var Field, x, y, v: int) =
|
proc load*(f: var Field, filename: string): bool =
|
||||||
|
var file: File
|
||||||
|
if not open(file, filename):
|
||||||
|
return false
|
||||||
|
defer: file.close()
|
||||||
|
f.lines.add(Line())
|
||||||
|
var l = addr f.lines[0]
|
||||||
|
var trailingSpaces = 0
|
||||||
|
var data: array[255,char]
|
||||||
|
while true:
|
||||||
|
let n = file.readChars(data, 0, 255)
|
||||||
|
if n <= 0:
|
||||||
|
if f.ly == 0:
|
||||||
|
if l.l == 0: # we got en empty file!
|
||||||
|
return false
|
||||||
|
f.x = l.x
|
||||||
|
if l.l > 0:
|
||||||
|
inc f.ly
|
||||||
|
if f.lx < l.l+l.x-f.x:
|
||||||
|
f.lx = l.l+l.x-f.x
|
||||||
|
break
|
||||||
|
var i = 0
|
||||||
|
while i < n:
|
||||||
|
if data[i] == char(12):
|
||||||
|
inc i
|
||||||
|
continue
|
||||||
|
if data[i] == '\n' or data[i] == '\r':
|
||||||
|
if f.ly == 0:
|
||||||
|
if l.l == 0:
|
||||||
|
return false
|
||||||
|
f.x = l.x
|
||||||
|
inc f.ly
|
||||||
|
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
|
||||||
|
f.lines.add(Line())
|
||||||
|
l = addr f.lines[^1]
|
||||||
|
trailingSpaces = 0
|
||||||
|
if i+1 < n and data[i] == '\r' and data[i+1] == '\n':
|
||||||
|
inc i
|
||||||
|
else:
|
||||||
|
if data[i] == ' ':
|
||||||
|
if l.l == 0: # trim leading spaces
|
||||||
|
inc l.x
|
||||||
|
else:
|
||||||
|
inc trailingSpaces
|
||||||
|
else:
|
||||||
|
if trailingSpaces > 0:
|
||||||
|
let newL = l.l + trailingSpaces + 1
|
||||||
|
l.columns.setlen(newL)
|
||||||
|
for j in l.l..<newL-1:
|
||||||
|
l.columns[j] = int(' ')
|
||||||
|
l.columns[newL-1] = int(data[i])
|
||||||
|
l.l = newL
|
||||||
|
trailingSpaces = 0
|
||||||
|
else:
|
||||||
|
l.columns.add(int(data[i]))
|
||||||
|
inc l.l
|
||||||
|
inc i
|
||||||
|
f.lines = f.lines[0..<f.ly]
|
||||||
|
return true
|
||||||
|
|
||||||
|
func set*(f: var Field, x, y, v: int) =
|
||||||
if v == int(' '):
|
if v == int(' '):
|
||||||
f.blank(x, y)
|
f.blank(x, y)
|
||||||
elif y >= f.y:
|
elif y >= f.y:
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
import nimfunge98/field/field
|
|
||||||
|
|
||||||
let f:Field
|
|
||||||
|
|
||||||
echo f.get(3,2)
|
|
|
@ -2,8 +2,12 @@ import unittest
|
||||||
|
|
||||||
include field
|
include field
|
||||||
|
|
||||||
proc `==`(a, b: Line): bool = a.x == b.x and a.l == b.l and a.columns == b.columns
|
func `==`(a, b: Line): bool = a.x == b.x and a.l == b.l and a.columns == b.columns
|
||||||
proc `==`(a, b: Field): bool = a.x == b.x and a.lx == b.lx and a.y == b.y and a.ly == b.ly and a.lines == b.lines
|
func `==`(a, b: Field): bool = a.x == b.x and a.lx == b.lx and a.y == b.y and a.ly == b.ly and a.lines == b.lines
|
||||||
|
func cols(a: openarray[char]): seq[int] =
|
||||||
|
result.setlen(a.len)
|
||||||
|
for i in 0..<a.len:
|
||||||
|
result[i] = a[i].int()
|
||||||
|
|
||||||
const minimal = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('@')])])
|
const minimal = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('@')])])
|
||||||
|
|
||||||
|
@ -99,8 +103,48 @@ suite "Field":
|
||||||
test "Field.isIn":
|
test "Field.isIn":
|
||||||
check minimal.isIn(0, 0) == true
|
check minimal.isIn(0, 0) == true
|
||||||
check minimal.isIn(1, 0) == false
|
check minimal.isIn(1, 0) == false
|
||||||
|
test "Field.load":
|
||||||
|
var nonexistant: Field
|
||||||
|
check nonexistant.load("nonexistant") == false
|
||||||
|
var invalid: Field
|
||||||
|
check invalid.load("examples/invalid.b98") == false
|
||||||
|
var empty: Field
|
||||||
|
check empty.load("examples/empty.b98") == false
|
||||||
|
var min: Field
|
||||||
|
check min.load("examples/minimal.b98") == true
|
||||||
|
check min == minimal
|
||||||
|
var hello1A: Field; var hello1B = Field(lx: 24, ly: 1, lines: @[Line(l:24, columns: @['6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'].cols)])
|
||||||
|
check hello1A.load("examples/hello.b98") == true
|
||||||
|
check hello1A == hello1B
|
||||||
|
var rn: Field
|
||||||
|
check rn.load("examples/rn.b98") == true
|
||||||
|
check rn == hello1B
|
||||||
|
var hello2A: Field; var hello2B = Field(x: 1, lx: 33, ly: 2,lines: @[
|
||||||
|
Line(x:33, l:1, columns: @['v'].cols),
|
||||||
|
Line(x:1, l:33, columns: @['@', ' ', '>', ' ', '#', ';', '>', ':', '#', ',', '_', 'e', '-', 'j', ';', ' ', '"', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '"', 'd', 'a', '<'].cols)
|
||||||
|
])
|
||||||
|
check hello2A.load("examples/hello2.b98") == true
|
||||||
|
check hello2A == hello2B
|
||||||
|
var factorial2A: Field; var factorial2B = Field(x: 0, lx: 15, ly: 2,lines: @[
|
||||||
|
Line(x:0, l:15, columns: @['&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'].cols),
|
||||||
|
Line(x:1, l:11, columns: @['^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'].cols)
|
||||||
|
])
|
||||||
|
check factorial2A.load("examples/factorial.b98") == true
|
||||||
|
check factorial2A == factorial2B
|
||||||
|
var dna2A: Field; var dna2B = Field(x: 0, lx: 7, ly: 8,lines: @[
|
||||||
|
Line(x:0, l:7, columns: @['7', '^', 'D', 'N', '>', 'v', 'A'].cols),
|
||||||
|
Line(x:0, l:7, columns: @['v', '_', '#', 'v', '?', ' ', 'v'].cols),
|
||||||
|
Line(x:0, l:7, columns: @['7', '^', '<', '"', '"', '"', '"'].cols),
|
||||||
|
Line(x:0, l:7, columns: @['3', ' ', ' ', 'A', 'C', 'G', 'T'].cols),
|
||||||
|
Line(x:0, l:7, columns: @['9', '0', '!', '"', '"', '"', '"'].cols),
|
||||||
|
Line(x:0, l:7, columns: @['4', '*', ':', '>', '>', '>', 'v'].cols),
|
||||||
|
Line(x:0, l:7, columns: @['+', '8', '^', '-', '1', ',', '<'].cols),
|
||||||
|
Line(x:0, l:7, columns: @['>', ' ', ',', '+', ',', '@', ')'].cols),
|
||||||
|
])
|
||||||
|
check dna2A.load("examples/dna.b98") == true
|
||||||
|
check dna2A == dna2B
|
||||||
test "Field.set":
|
test "Field.set":
|
||||||
var f = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('>')])])
|
var f = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @['>'].cols)])
|
||||||
f.set(0,0,int('@'))
|
f.set(0,0,int('@'))
|
||||||
check f == minimal
|
check f == minimal
|
||||||
f.set(1,0,int(' '))
|
f.set(1,0,int(' '))
|
||||||
|
|
Reference in a new issue