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

Inject code to process #define? #281

Open
geekrelief opened this issue Sep 19, 2021 · 8 comments
Open

Inject code to process #define? #281

geekrelief opened this issue Sep 19, 2021 · 8 comments

Comments

@geekrelief
Copy link

geekrelief commented Sep 19, 2021

I'm trying to wrap The Machinery which has versioned headers and uses a lot of defines for generating code.

An example is this:

#define tm_api_registry_api_version TM_VERSION(0, 3, 0)

Where:

#define TM_VERSION(major, minor, patch) (TM_LITERAL(tm_version_t){ major, minor, patch }

The generated nim file has:
# const 'tm_api_registry_api_version' has unsupported value 'TM_VERSION(0, 3, 0)

Ideally I'd like to pass in my own template TM_VERSION so tm_api_registry_api_version can be declared in my output file correctly like:

const tm_api_registry_api_version = TM_VERSION(0, 3, 0)

I tried adding this to my generator:

cOverride:
  template TM_VERSION (Major, Minor, Patch): untyped =
    tm_version_t(major: Major, minor: Minor, patch: Patch)

But I get an invalid indentation error coming from the nimcache, cPlugin, nimterop nim file where onSymbolOverride is empty.
Is there a way to do this or can I modify nimterop to allow for this?

@geekrelief
Copy link
Author

I forked the repo and added in nnkTemplateDef to findOverrides in cimport.nim and added an extra case where findOverrides is used so nnkTemplateDef maps to nskTemplate. And modified plugin.nim so the nskTemplate exists in cOverrides. But I don't see it in my output file.

@geekrelief
Copy link
Author

I now see I have to modify ast2.nim to handle my use case.

@genotrance
Copy link
Collaborator

You don't need to cOverride - can just define it in your nim file per usual. nimterpo will skip it anyway since it is unsupported and won't conflict with your definition.

@geekrelief
Copy link
Author

I don't follow. If I just define TM_VERSION in my generator it doesn't appear in my output, and tm_api_registory_api_version still isn't defined in the output.

@genotrance
Copy link
Collaborator

cOverride only supports const, type and proc declarations which is why the template info is not propagated. Maybe if you make it a proc, it will work.

@geekrelief
Copy link
Author

Thanks, I switched the cOverride for TM_VERSION to a proc, and I see it in the output now. That gets me a little closer to what I want, but I still want exprparser.nim's processTSNode to handle call_expression so I can get the proper output for #defines. I want to be able to automatically wrap

#define tm_api_registry_api_version TM_VERSION(0, 3, 0)

And I'll probably have to work on a modification to allow templates in cOverride because The Machinery has lots of macros for example:

#define tm_get_api(reg, TYPE) \
    (struct TYPE *)reg->get(#TYPE, TYPE##_version

nimterop ignores these because of the call_expression, and templates would be the only way to support the token pasting operator ## and astToStr for token stringification.

@genotrance
Copy link
Collaborator

Although I'm not maintaining nimterop anymore, I'm totally open to PRs. However, the CI is not longer functional so if you are willing to take this on, hopefully you can help with the CI as well.

@geekrelief
Copy link
Author

geekrelief commented Sep 22, 2021

I'm not sure I can take that on. I'm not familiar with the CI at all. I mean I only started looking at nimble because of nimterop. :) I was trying to upgrade treesitter to v0.20.0, but it wasn't as simple as swapping the branch. Where would I even begin with CI?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants