From a952eaabb7f33072d2b4e474903156266d8a3a29 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 5 Oct 2021 22:39:45 +0200 Subject: Refactoring --- nimfunge98.nimble | 2 +- src/field.nim | 12 +++++++----- tests/field.nim | 35 ++++++++++------------------------- 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..', ':', '#', ',', '_', '@'].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('@')) -- cgit v1.2.3