-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support more expressive indexing
Interpreter has been updated to support nested expression evaluation (e.g. `var1[var2]`). BREAKING CHANGES: - `Argument` -> `Expression` - `Expression`, `Variable`, and `Path` APIs have changed. - More remifications of this spreading out.
- Loading branch information
Showing
20 changed files
with
205 additions
and
269 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
This file was deleted.
Oops, something went wrong.
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,45 @@ | ||
use std::fmt; | ||
|
||
use error::Result; | ||
use value::Scalar; | ||
use value::Value; | ||
|
||
use super::Context; | ||
use variable::Variable; | ||
|
||
/// An un-evaluated `Value`. | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum Expression { | ||
/// Un-evaluated. | ||
Variable(Variable), | ||
/// Evaluated. | ||
Literal(Value), | ||
} | ||
|
||
impl Expression { | ||
/// Create an expression from a scalar literal. | ||
pub fn with_literal<S: Into<Scalar>>(literal: S) -> Self { | ||
Expression::Literal(Value::scalar(literal)) | ||
} | ||
|
||
/// Convert to a `Value`. | ||
pub fn evaluate(&self, context: &Context) -> Result<Value> { | ||
let val = match *self { | ||
Expression::Literal(ref x) => x.clone(), | ||
Expression::Variable(ref x) => { | ||
let path = x.evaluate(context)?; | ||
context.stack().get(&path)?.clone() | ||
} | ||
}; | ||
Ok(val) | ||
} | ||
} | ||
|
||
impl fmt::Display for Expression { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
Expression::Literal(ref x) => write!(f, "{}", x), | ||
Expression::Variable(ref x) => write!(f, "{}", x), | ||
} | ||
} | ||
} |
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
Oops, something went wrong.