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

feature: const evaluation #91

Closed
9 tasks done
udoprog opened this issue Sep 21, 2020 · 1 comment
Closed
9 tasks done

feature: const evaluation #91

udoprog opened this issue Sep 21, 2020 · 1 comment
Labels
compiler Issues related to the compiler. enhancement New feature or request

Comments

@udoprog
Copy link
Collaborator

udoprog commented Sep 21, 2020

Support constant expressions, like these:x

const VALUE = #{hello: "World"};

They can reference other constant expressions, but will fail to compile if there are cycles:

const VALUE = #{hello: OTHER};
const OTHER = "World";

Fail:

const VALUE = #{hello: OTHER};
const OTHER = #{hello: VALUE};

Plumbing is being built in #93. As it stands, constant value resolving is hooked into the query phase through a component called the ConstCompiler. Which will probably be renamed to something involving the world eval, since that is really all it does. Essentially it's an eval function which does AST walking.

When CompileMeta::Const is resolved for a query, it includes the calculated ConstValue. All const resolving is memoized.

Checklist

Current state

The current state of the constant compiler is a very naive ast walker. With the existing design it's not possible to implement break or returns, because each expression is evaluated recursively and that would require returning early everywhere in a manner which would be very cumbersome.

Instead, the constant compiler should be redesigned to execute a stack, where returning simply pops the execution context of each scope until it's reached the thing it should break out of or return from. The root of the execution simply executed the AST that is at the top of the stack until it receives instructions to behave differently.

But the current constant compiler is fully capable of understanding cycles, and can do string templating, like this:

const LIMIT = 10;
const URL = `https://api.example.com/query?limit={LIMIT}`;

fn main() {
    let _ = http::get(URL);
}
@udoprog udoprog added enhancement New feature or request compiler Issues related to the compiler. labels Sep 21, 2020
@udoprog
Copy link
Collaborator Author

udoprog commented Sep 21, 2020

FTR, this is what I'm currently working on.

@udoprog udoprog changed the title Support const expressions Support const evaluation Sep 22, 2020
@udoprog udoprog changed the title Support const evaluation feature: const evaluation Aug 15, 2024
@udoprog udoprog closed this as completed Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Issues related to the compiler. enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant