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

Implement unquoting of code values #4590

Closed
Tracked by #4594
jfecher opened this issue Mar 19, 2024 · 0 comments · Fixed by #4884
Closed
Tracked by #4594

Implement unquoting of code values #4590

jfecher opened this issue Mar 19, 2024 · 0 comments · Fixed by #4884
Labels
enhancement New feature or request

Comments

@jfecher
Copy link
Contributor

jfecher commented Mar 19, 2024

Problem

After code is quoted, it needs a way to be unquoted to be inserted back into the program as runtime code.

Happy Case

Implement unquoting as described in https://hackmd.io/Tkzo_ryvTsWMPESmWHPiZw?view

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@jfecher jfecher added the enhancement New feature or request label Mar 19, 2024
@jfecher jfecher changed the title Implement unquoting of code values in comptime functions Implement unquoting of code values Mar 19, 2024
github-merge-queue bot pushed a commit that referenced this issue Apr 23, 2024
… interpreter (#4884)

# Description

## Problem\*

Resolves #4590

## Summary\*

This PR links up the comptime interpreter with the rest of the codebase.
It does so by adding a scanning step where the Hir is scanned for
`comptime` expressions to execute. When one of these is found, the
interpreter switches to evaluation mode and evaluates the expression.
Afterward, the result of the expression is inlined into the Hir via
`Value::into_expression`. For `Code` values, this means the entire code
block is spliced in (a macro expansion).

You can now run simple programs at compile-time now as long as they
don't have expansion of `quote`d values (macros) since those would
require the full loop back to name resolution again. Anyway, here's an
example that works now:

```rs
fn main() {
    let x = comptime { 2 * 4 };
    println(x);
}
```
By monomorphization the compiler sees
```rs
fn main() {
    let x = 8;
    println(x);
}
```
More complex expressions within the `comptime` block should also work.
Just note that this scanning + evaluation is currently only within
functions. So comptime globals won't be evaluated.

## Additional Context

I may try splitting out this PR into several smaller ones but need to
look into where to make the split more. Leaving this up for now in case
people find it useful or it's not as big as I expected.

Future changes:

Architecture-wise we're only missing the ability to make the full loop
and go back to name resolution after a macro expansion now. Since we
already have the Hir -> Ast pass (although it can be considerably
improved since it is rather broken still), we only need to call it after
the comptime interpreter and run name resolution again.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant