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

Added codeRepr and dumpCode to the macros module. #5778

Merged
merged 9 commits into from
Jul 25, 2017
Merged

Conversation

PMunch
Copy link
Contributor

@PMunch PMunch commented May 4, 2017

This allows those writing macros to write examples, get the code to generate the AST for that example, and then modify that code to be dynamic with the macro function.

PMunch added 2 commits May 4, 2017 15:55
This allows those writing macros to write examples, get the code to generate the AST for that example, and then modify that code to be dynamic with the macro function.
for i in 0..level-1: res.add " "
res.add(")")

if n.kind in {nnkEmpty, nnkNilLit, nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit, nnkIdent, nnkSym, nnkNone}:
Copy link
Member

Choose a reason for hiding this comment

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

DRY.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dry?

## See also `repr`, `treeRepr`, and `lispRepr`.
proc traverse(res: var string, level: int, n: NimNode) {.benign.} =
for i in 0..level-1: res.add " "
if n.kind in {nnkEmpty, nnkNilLit, nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit, nnkIdent, nnkSym, nnkNone}:
Copy link
Member

Choose a reason for hiding this comment

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

Line too long.

PMunch added 3 commits May 4, 2017 19:12
Escaped characters for proper string handling

Changed from new<>Node to newLit for applicable types
@Araq
Copy link
Member

Araq commented May 13, 2017

Add a test case please, but the code bloat bothers me.

@dom96
Copy link
Contributor

dom96 commented May 16, 2017

In what way is this different to echo toStrLit(node)?

@PMunch
Copy link
Contributor Author

PMunch commented May 16, 2017

@dom96 echo toStrLit(node) outputs the code represented by that node. codeRepr creates the code to generate the AST. An example:

toStrLitMacro:
  echo "Hello world"

outputs:

echo "Hello world"
dumpCode:
  echo "Hello world"

outputs:

nnkStmtList.newTree(
  nnkCommand.newTree(
    newIdentNode(!"echo"),
    newLit("Hello world")
  )
)

As you can see dumpCode outputs code you can put into your macro to generate the code you give it. This is helpful as you can write up an example output, then run dumpCode on it and copy the contents into your macro while editing in the macro logic.

@dom96
Copy link
Contributor

dom96 commented May 16, 2017

I see. It should get a different name then (dumpCodeGen?), and the docs need to have this explanation.

@PMunch
Copy link
Contributor Author

PMunch commented May 16, 2017

Hmm, a different name sure is possible. But not sure what you mean by the docs, I've tried to explain what it does in the doc-strings added to the file. If you have another wording I'd be happy to change it!

@dom96
Copy link
Contributor

dom96 commented May 16, 2017

I mean: show what this produces, just like you've shown above.

@PMunch
Copy link
Contributor Author

PMunch commented May 17, 2017

Something like that?

@Araq
Copy link
Member

Araq commented Jun 13, 2017

Add a test case please.

@Araq Araq merged commit e00953c into nim-lang:devel Jul 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants