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

nifc: fixes top level var decls with non-literals #322

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 20 additions & 5 deletions src/nifc/codegen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,26 @@ proc genVarDecl(c: var GeneratedCode; t: Tree; n: NodePos; vk: VarKind; toExtern
if vis == StaticC:
c.code.insert(Token(StaticKeyword), beforeDecl)
if t[d.value].kind != Empty:
c.add AsgnOpr
if vk != IsLocal: inc c.inSimpleInit
genx c, t, d.value
if vk != IsLocal: dec c.inSimpleInit
c.add Semicolon
if vk == IsGlobal and
t[d.value].kind notin {IntLit, UIntLit, FloatLit,
CharLit, StrLit, FalseC, TrueC, AconstrC,
OconstrC, InfC, NegInfC, NanC, SufC}:
Copy link
Member

@Araq Araq Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need some isLiteral deep check for OconstrC and AconstrC as [1, 2] is very different from [myVar, 3]. Thanks to NIF you can do this iteratively and no recursion is required.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no clues how to deal with Tree iteratively. Do I need to use hasNext to query the end of the tree or something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You use the simple while n.kind != ParRi:... loop but your way is fine too. Also NIFC doesn't yet use the newer APIs so that's probably why you were confused.

c.add Semicolon
moveToInitSection:
c.add name
c.add AsgnOpr
inc c.inSimpleInit
genx c, t, d.value
dec c.inSimpleInit
c.add Semicolon
else:
c.add AsgnOpr
if vk != IsLocal: inc c.inSimpleInit
genx c, t, d.value
if vk != IsLocal: dec c.inSimpleInit
c.add Semicolon
else:
c.add Semicolon
else:
error c.m, "expected SymbolDef but got: ", t, n

Expand Down
17 changes: 17 additions & 0 deletions tests/nimony/sysbasics/ttoplevelvars.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var x = 1
var y = 2
x = y
var z = x

const a = 1
var m = a

let s = x
let n = 3

proc foo =
var m = a
let g = x
const s = a

foo()
Loading