-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(experimental): Add scan pass and
into_expression
for comptime…
… 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.
- Loading branch information
Showing
22 changed files
with
667 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::{node_interner::DefinitionId, Type}; | ||
use acvm::FieldElement; | ||
use noirc_errors::Location; | ||
|
||
use super::value::Value; | ||
|
||
/// The possible errors that can halt the interpreter. | ||
#[derive(Debug)] | ||
pub enum InterpreterError { | ||
ArgumentCountMismatch { expected: usize, actual: usize, call_location: Location }, | ||
TypeMismatch { expected: Type, value: Value, location: Location }, | ||
NoValueForId { id: DefinitionId, location: Location }, | ||
IntegerOutOfRangeForType { value: FieldElement, typ: Type, location: Location }, | ||
ErrorNodeEncountered { location: Location }, | ||
NonFunctionCalled { value: Value, location: Location }, | ||
NonBoolUsedInIf { value: Value, location: Location }, | ||
NonBoolUsedInConstrain { value: Value, location: Location }, | ||
FailingConstraint { message: Option<Value>, location: Location }, | ||
NoMethodFound { object: Value, typ: Type, location: Location }, | ||
NonIntegerUsedInLoop { value: Value, location: Location }, | ||
NonPointerDereferenced { value: Value, location: Location }, | ||
NonTupleOrStructInMemberAccess { value: Value, location: Location }, | ||
NonArrayIndexed { value: Value, location: Location }, | ||
NonIntegerUsedAsIndex { value: Value, location: Location }, | ||
NonIntegerIntegerLiteral { typ: Type, location: Location }, | ||
NonIntegerArrayLength { typ: Type, location: Location }, | ||
NonNumericCasted { value: Value, location: Location }, | ||
IndexOutOfBounds { index: usize, length: usize, location: Location }, | ||
ExpectedStructToHaveField { value: Value, field_name: String, location: Location }, | ||
TypeUnsupported { typ: Type, location: Location }, | ||
InvalidValueForUnary { value: Value, operator: &'static str, location: Location }, | ||
InvalidValuesForBinary { lhs: Value, rhs: Value, operator: &'static str, location: Location }, | ||
CastToNonNumericType { typ: Type, location: Location }, | ||
QuoteInRuntimeCode { location: Location }, | ||
NonStructInConstructor { typ: Type, location: Location }, | ||
CannotInlineMacro { value: Value, location: Location }, | ||
UnquoteFoundDuringEvaluation { location: Location }, | ||
|
||
Unimplemented { item: &'static str, location: Location }, | ||
|
||
// Perhaps this should be unreachable! due to type checking also preventing this error? | ||
// Currently it and the Continue variant are the only interpreter errors without a Location field | ||
BreakNotInLoop { location: Location }, | ||
ContinueNotInLoop { location: Location }, | ||
|
||
// These cases are not errors, they are just used to prevent us from running more code | ||
// until the loop can be resumed properly. These cases will never be displayed to users. | ||
Break, | ||
Continue, | ||
} | ||
|
||
#[allow(unused)] | ||
pub(super) type IResult<T> = std::result::Result<T, InterpreterError>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.