aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2021-10-05 22:39:45 +0200
committerJulien Dessaux2021-10-05 22:39:45 +0200
commita952eaabb7f33072d2b4e474903156266d8a3a29 (patch)
treede6e2ec1a8d9f8589834c0eb488ee401cb084ef7
parentImplemented Funge-98 IO procedures (diff)
downloadnimfunge98-a952eaabb7f33072d2b4e474903156266d8a3a29.tar.gz
nimfunge98-a952eaabb7f33072d2b4e474903156266d8a3a29.tar.bz2
nimfunge98-a952eaabb7f33072d2b4e474903156266d8a3a29.zip
Refactoring
-rw-r--r--nimfunge98.nimble2
-rw-r--r--src/field.nim12
-rw-r--r--tests/field.nim35
3 files changed, 18 insertions, 31 deletions
diff --git a/nimfunge98.nimble b/nimfunge98.nimble
index f62cd32..392f745 100644
--- a/nimfunge98.nimble
+++ b/nimfunge98.nimble
@@ -32,7 +32,7 @@ task fmt, "Run nimpretty on all git-managed .nim files in the current repo":
# ^^^^^-- That "cd" is required.
if fileIsGitManaged:
let
- cmd = "nimpretty --maxLineLen=180 $1" % [file]
+ cmd = "nimpretty --maxLineLen=220 $1" % [file]
echo "Running $1 .." % [cmd]
exec(cmd)
diff --git a/src/field.nim b/src/field.nim
index 8d609f7..e15d7b4 100644
--- a/src/field.nim
+++ b/src/field.nim
@@ -69,11 +69,13 @@ func Get*(f: Field, x, y: int): int =
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
-proc Load*(f: var Field, filename: string): bool =
+proc Load*(filename: string): ref Field =
var file: File
if not open(file, filename):
- return false
+ return nil
defer: file.close()
+ var f: ref Field
+ new(f)
f.lines.add(Line())
var l = addr f.lines[0]
var trailingSpaces = 0
@@ -83,7 +85,7 @@ proc Load*(f: var Field, filename: string): bool =
if n <= 0:
if f.ly == 0:
if l.l == 0: # we got en empty file!
- return false
+ return nil
f.x = l.x
if l.l > 0:
inc f.ly
@@ -98,7 +100,7 @@ proc Load*(f: var Field, filename: string): bool =
if data[i] == '\n' or data[i] == '\r':
if f.ly == 0:
if l.l == 0:
- return false
+ return nil
f.x = l.x
inc f.ly
if f.x > l.x:
@@ -130,7 +132,7 @@ proc Load*(f: var Field, filename: string): bool =
inc l.l
inc i
f.lines = f.lines[0..<f.ly]
- return true
+ return f
func Set*(f: var Field, x, y, v: int) =
if v == int(' '):
diff --git a/tests/field.nim b/tests/field.nim
index e000016..dcb7530 100644
--- a/tests/field.nim
+++ b/tests/field.nim
@@ -104,36 +104,23 @@ suite "Field":
check minimal.IsIn(0, 0) == true
check minimal.IsIn(1, 0) == false
test "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: @[
+ check Load("nonexistant") == nil
+ check Load("examples/invalid.b98") == nil
+ check Load("examples/empty.b98") == nil
+ check Load("examples/minimal.b98")[] == Field(lx: 1, ly: 1, lines: @[Line(l: 1, columns: @['@'].cols)])
+ let hello = Field(lx: 24, ly: 1, lines: @[Line(l: 24, columns: @['6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'].cols)])
+ check Load("examples/hello.b98")[] == hello
+ check Load("examples/rn.b98")[] == hello
+ check Load("examples/hello2.b98")[] == 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: @[
+ check Load("examples/factorial.b98")[] == 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: @[
+ check Load("examples/dna.b98")[] == 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),
@@ -143,8 +130,6 @@ suite "Field":
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 "Set":
var f = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @['>'].cols)])
f.Set(0, 0, int('@'))