nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.63k stars 1.47k forks source link

`repr` doesn't work with hex literals (and other `repr` bugs now fixed) #17292

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

bugs

Example 1

when true:
  macro deb(a) =
    echo a.repr

  deb:
    let f = () => (discard)
    f()
    let x = 0xffffffffffffffff
    template fn(body: untyped): untyped = true
    doAssert(fn do: nonexistant)
    template foo(x, y) =
      x
      y
    foo:
      discard
    do:
      discard

Current Output

let f = () =>
  discard
f()
let x = 0xFFFFFFFF
template fn(body: untyped): untyped =
  true

doAssert(fn:
  nonexistant)
template foo(x, y) =
  x
  y

foo(
  discard ):
  discard

Expected Output

let f = () =>
  (discard)
f()
let x = 0xffffffffffffffff
template fn(body: untyped): untyped =
  true

doAssert(fn do: nonexistant)
template foo(x, y) =
  x
  y

foo:
  discard
do:
  discard

Example 2

repr doesn't work with nnkAccQuoted:

import macros
macro deb(a) =
  echo a.repr
deb:
  proc `=destroy`(a: Foo) = discard
  proc `'foo`(a: string): int = discard

prints:

proc `= destroy`(a: Foo) =
  discard

proc `' foo`(a: string): int =
  discard

the culprit is the spaces inserted here in renderer.nim:

  of nkAccQuoted:
    put(g, tkAccent, "`")
    if n.len > 0: gsub(g, n[0])
    for i in 1..<n.len:
      put(g, tkSpaces, Space)
      gsub(g, n[i])
    put(g, tkAccent, "`")

Example 3

Additional Information

ghost commented 3 years ago

From https://github.com/pietroppeter/nimib/issues/20 - might this issue be related to this bug?

import std/macros

macro testOut(x: untyped) = 
  echo treeRepr x
  echo repr x

testOut:
  *header: {headers}

Currently the renderer's output is *header which is obviously wrong

timotheecour commented 3 years ago

I think it's a separate issue, unrelated to repr (repr shows badly but that seems like a symptom)

@Araq isn't that a parser bug?

when true:
  import std/macros

  macro testOut(x: untyped) = 
    echo treeRepr x
    echo lispRepr x
    echo repr x

  testOut:
    *a: b
StmtList
  Prefix
    Ident "*"
    Ident "a"
    StmtList
      Ident "b"
(StmtList (Prefix (Ident "*") (Ident "a") (StmtList (Ident "b"))))

*a

how come Prefix has 3 children instead of 2?

Araq commented 3 years ago

how come Prefix has 3 children instead of 2?

I think the colon is parsed much like a do: in this case so it adds a third child. Definitely something we need to investigate. :-)