Skip to content

Commit

Permalink
the balanced line (#20)
Browse files Browse the repository at this point in the history
Balance newlines in various places:

* in sections (var, type etc), use long-form when contents span multiple
lines already
* in if/statement expressions, use compact single-line format when it
fits
* line-break parameters if pragma doesn't fit to make parameters more visible
  • Loading branch information
arnetheduck committed Dec 20, 2023
1 parent 4dc4998 commit 0f26a0a
Show file tree
Hide file tree
Showing 20 changed files with 1,673 additions and 1,339 deletions.
64 changes: 33 additions & 31 deletions src/astcmp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,41 @@ proc equivalent*(a, b: PNode): Outcome =

return Outcome(kind: Different, a: a, b: b)

let eq =
case a.kind
of nkCharLit..nkUInt64Lit:
a.intVal == b.intVal
of nkFloatLit..nkFloat128Lit:
(isNaN(a.floatVal) and isNaN(b.floatVal)) or a.floatVal == b.floatVal
of nkStrLit..nkTripleStrLit:
a.strVal == b.strVal
of nkSym:
raiseAssert "Shouldn't eixst in parser"
of nkIdent:
a.ident.s == b.ident.s
else:
let skipped =
if a.kind == nkStmtListExpr:
# When inserting a `;`, we might get some extra empty statements (?)
{nkEmpty, nkCommentStmt}
else:
{nkCommentStmt}
# TODO don't break comments!
let
af = a.sons.filterIt(it.kind notin skipped)
bf = b.sons.filterIt(it.kind notin skipped)

if af.len() != bf.len():
false
let
eq =
case a.kind
of nkCharLit..nkUInt64Lit:
a.intVal == b.intVal
of nkFloatLit..nkFloat128Lit:
(isNaN(a.floatVal) and isNaN(b.floatVal)) or a.floatVal == b.floatVal
of nkStrLit..nkTripleStrLit:
a.strVal == b.strVal
of nkSym:
raiseAssert "Shouldn't eixst in parser"
of nkIdent:
a.ident.s == b.ident.s
else:
for (aa, bb) in zip(af, bf):
let eq = equivalent(aa, bb)
if eq.kind == Different:
return eq
let
skipped =
if a.kind == nkStmtListExpr:
# When inserting a `;`, we might get some extra empty statements (?)
{nkEmpty, nkCommentStmt}
else:
{nkCommentStmt}
# TODO don't break comments!
let
af = a.sons.filterIt(it.kind notin skipped)
bf = b.sons.filterIt(it.kind notin skipped)

if af.len() != bf.len():
false
else:
for (aa, bb) in zip(af, bf):
let eq = equivalent(aa, bb)
if eq.kind == Different:
return eq

true
true

if not eq:
Outcome(kind: Different, a: a, b: b)
Expand Down
34 changes: 18 additions & 16 deletions src/nph.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ proc writeVersion() =
quit(0)

proc parse(input, filename: string; printTokens: bool; conf: ConfigRef): PNode =
let fn =
if filename == "-":
"stdin"
else:
filename
let
fn =
if filename == "-":
"stdin"
else:
filename

parseString(input, newIdentCache(), conf, fn, printTokens = printTokens)

Expand Down Expand Up @@ -97,17 +98,18 @@ proc prettyPrint(infile, outfile: string; debug, check, printTokens: bool): int
# No formatting difference - don't touch file modificuation date
return QuitSuccess

let eq =
equivalent(
input,
infile,
output,
if infile == "-":
"stdout"
else:
outfile
,
)
let
eq =
equivalent(
input,
infile,
output,
if infile == "-":
"stdout"
else:
outfile
,
)

template writeUnformatted() =
if not debug and (infile != outfile or infile == "-"):
Expand Down
Loading

0 comments on commit 0f26a0a

Please sign in to comment.