Reuh / candran

a Lua dialect and simple preprocessor
MIT License
28 stars 2 forks source link

Compiler options #5

Closed splitice closed 3 years ago

splitice commented 3 years ago

Would be useful to have available on the command line:

Reuh commented 3 years ago

1) canc import=module would work as if we used #import("module")?

2) Currently, the variables accessible to the preprocessor are the same as the compiler options, and there's no check for unknown options. So if you call canc -DEBUG N=2 you will have the variables DEBUG=true and N=2 available in the preprocessor.

There's not a lot of compiler options right now so there's not much risk of conflict, but it's indeed not a great solution. I can think of two easy ways to distinguish between the two:

splitice commented 3 years ago
  1. I'd suggest something more explicit so as not to be confused with filenames
  2. more c like i.e -DDEBUG would be preferred in my eyes. Although this does highlight an issue with the single quote style of argument. It becomes unclear. Perhaps given the single quote style -define DEBUG=1
Reuh commented 3 years ago

The argument parsing library I use right now has the advantage of working very nicely with Lua values, but doesn't really work in more complex cases like this. -define DEBUG=1 would be parsed as {define=true, DEBUG=1}...

I don't think anyone except you and me is currently using Candran right now, so we should be able to do a few breaking changes. I feel like it may be worth it to switch to a proper POSIX argument parsing library like alt-getop or argparse, as it will be both more familiar and flexible.

So instead of canc out=a.lua file.can it'd be canc --output a.lua file.can or canc -o a.lua file.can (both short and long versions of argument would be defined).

So for preprocessor constants: -D DEBUG=1 or --define DEBUG=1.

For static imports: -I module or --import module.

Reuh commented 3 years ago

Worked on this on the argparse branch. argparse is great, it's way more pleasant to use that the previous library and way more capable :)

Everything that was possible before should already be possible, and I added a way to define preprocessor constants: canc -D VAR value, or canc --define VAR value (value can be omitted and defaults to true).

Regarding static requires, I'm not sure what use case you have exactly in mind (as opposed to just using #import in the file).

It might be useful for making a packaged "release" version of a Lua library, with every file bundled in one (like Candran does itself, but with #import directly), but still having separate files for developement. In which case, I think it would make sense if it included the module in the compiled file, but didn't automatically assign it to a local variable (like if you called #import("module", {loadLoacal=false})), as you would already have a local module = require("module") in your file. Or were you thinking of something else?

splitice commented 3 years ago

The main use case for static requires is to enable the building of an entire project with a set of preprocessor macros without needing to add an import to every file in a project (which can easily be missed)

Reuh commented 3 years ago

Ok, I see. Indeed not what I was thinking of, makes more sense :)

I have added this to the argparse branch: canc -I module or canc --import module

It works by adding a #import("module",{loadLocal=false}) at the beginning of each compiled file, which will correctly import preprocessor macros defined in the included file.

Reuh commented 3 years ago

Merged and released with Candran 1.0.0 (bumped a major version since it's breaking).