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

Compile time variables and parameters #455

Open
Moosems opened this issue Dec 15, 2023 · 7 comments
Open

Compile time variables and parameters #455

Moosems opened this issue Dec 15, 2023 · 7 comments

Comments

@Moosems
Copy link

Moosems commented Dec 15, 2023

Similar to how Mojo does it, what do you think of compile time variables and separating parameters (compile time) and arguments (runtime) (see here)?

@Akuli
Copy link
Owner

Akuli commented Dec 15, 2023

Duplicate of #369

@Akuli Akuli marked this as a duplicate of #369 Dec 15, 2023
@Akuli Akuli closed this as completed Dec 15, 2023
@Moosems
Copy link
Author

Moosems commented Dec 16, 2023

I wouldn't group this with constants...

@Akuli
Copy link
Owner

Akuli commented Dec 16, 2023

I see. This is basically what we discussed in #257.

@Akuli Akuli reopened this Dec 16, 2023
@Akuli
Copy link
Owner

Akuli commented Mar 24, 2024

IMO the right solution is to allow accessing environment variables during compile time. Environment variables would be used to decide and configure what gets compiled ("parameters"), and command-line arguments would be passed when running the program ("arguments").

For example, let's say you want to make a hello world program where the greeting (e.g. Hello %s or Hi there, %s) can be configured at compile time, but the target of the greeting (%s) is given at runtime. You would do this:

import "stdlib/io.jou"

def main(argc: int, argv: byte**) -> int:
    format = compile_time_getenv("FORMAT")  # env var (compile-time parameter)
    printf(format, argv[1])  # runtime argument
    return 0

Then you would pass environment variables when compiling the program:

FORMAT="Hello %s" jou -o program program.jou

And run it like this:

./program world

This would print Hello world.

And once #257 is implemented, you could compile and run at once:

FORMAT="Hello %s" jou program.jou world

Here's one situation where accessing env vars during compile time is extremely useful. Let's say you have a project that for whatever reason invokes the Jou compiler through a script. The script is messy and you really don't want to edit it for whatever reason (e.g. it could be auto-generated by a stupid tool such as cmake). With env vars, you could still pass parameters to the Jou compiler through environment variables, because each child process inherits the environment of its parent.

@Moosems
Copy link
Author

Moosems commented Mar 26, 2024

At compile time if I have a hard-coded integer, is that baked into the executable?

@Akuli
Copy link
Owner

Akuli commented Mar 26, 2024

Yes, and whatever compile_time_getenv() returns would also be baked into the executable.

@Moosems Moosems closed this as completed Mar 26, 2024
@Akuli
Copy link
Owner

Akuli commented Mar 26, 2024

No, compile_time_getenv() doesn't exist yet.

@Akuli Akuli reopened this Mar 26, 2024
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