Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the balanced line #20

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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