difftreelog
feat(evaluator) use LocExpr
in: master
2 files changed
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -5,8 +5,8 @@
};
use closure::closure;
use jsonnet_parser::{
- ArgsDesc, BinaryOpType, BindSpec, Expr, FieldMember, LiteralType, Member, ObjBody, ParamsDesc,
- UnaryOpType, Visibility,
+ ArgsDesc, BinaryOpType, BindSpec, Expr, FieldMember, LiteralType, LocExpr, Member, ObjBody,
+ ParamsDesc, UnaryOpType, Visibility,
};
use std::{
collections::{BTreeMap, HashMap},
@@ -40,7 +40,7 @@
}
}
-pub fn evaluate_method(ctx: Context, expr: &Expr, arg_spec: ParamsDesc) -> Val {
+pub fn evaluate_method(ctx: Context, expr: &LocExpr, arg_spec: ParamsDesc) -> Val {
Val::Func(FuncDesc {
ctx,
params: arg_spec,
@@ -236,9 +236,10 @@
}
}
-pub fn evaluate(context: Context, expr: &Expr) -> Val {
+pub fn evaluate(context: Context, expr: &LocExpr) -> Val {
use Expr::*;
- match &*expr {
+ let LocExpr(expr, _location) = expr;
+ match &**expr {
Literal(LiteralType::This) => Val::Obj(
context
.this()
@@ -260,7 +261,7 @@
}
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)),
Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),
- Index(box value, box index) => {
+ Index(value, index) => {
match (
evaluate(context.clone(), value).unwrap_if_lazy(),
evaluate(context.clone(), index),
@@ -284,7 +285,7 @@
.unwrap_or_else(|| panic!("out of bounds"))
.clone(),
(Val::Str(s), Val::Num(n)) => {
- Val::Str(s.chars().skip(n as usize - 1).take(1).collect())
+ Val::Str(s.chars().skip(n as usize).take(1).collect())
}
(v, i) => todo!("not implemented: {:?}[{:?}]", v, i.unwrap_if_lazy()),
}
@@ -307,10 +308,10 @@
let context = context
.extend(new_bindings, None, None, None)
.into_future(future_context);
- evaluate(context, &*returned.clone())
+ evaluate(context, &returned.clone())
}
Obj(body) => Val::Obj(evaluate_object(context, body.clone())),
- Apply(box value, ArgsDesc(args)) => {
+ Apply(value, ArgsDesc(args)) => {
let value = evaluate(context.clone(), value).unwrap_if_lazy();
match value {
// TODO: Capture context of application
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth29rc_fn_helper!(29rc_fn_helper!(30 FunctionDefault,30 FunctionDefault,31 function_default,31 function_default,32 dyn Fn(Context, Expr) -> Val32 dyn Fn(Context, LocExpr) -> Val33);33);343435#[cfg(test)]35#[cfg(test)]393940 macro_rules! eval {40 macro_rules! eval {41 ($str: expr) => {41 ($str: expr) => {42 evaluate(Context::new(), &parse($str).unwrap())42 evaluate(Context::new(), &parse($str, &ParserSettings {43 loc_data: false,44 file_name: "test.jsonnet".to_owned(),45 }).unwrap())43 };46 };44 }47 }454846 macro_rules! eval_stdlib {49 macro_rules! eval_stdlib {47 ($str: expr) => {{50 ($str: expr) => {{48 let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";51 let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";49 evaluate(Context::new(), &parse(&(std + $str)).unwrap())52 evaluate(Context::new(), &parse(&(std + $str), &ParserSettings {53 loc_data: false,54 file_name: "test.jsonnet".to_owned(),55 }).unwrap())50 }};56 }};51 }57 }525853 macro_rules! assert_eval {59 macro_rules! assert_eval {54 ($str: expr) => {60 ($str: expr) => {55 assert_eq!(61 assert_eq!(56 evaluate(Context::new(), &parse($str).unwrap()),62 evaluate(Context::new(), &parse($str, &ParserSettings {63 loc_data: false,64 file_name: "test.jsonnet".to_owned(),65 }).unwrap()),57 Val::Literal(LiteralType::True)66 Val::Literal(LiteralType::True)58 )67 )59 };68 };60 }69 }61 macro_rules! assert_json {70 macro_rules! assert_json {62 ($str: expr, $out: expr) => {71 ($str: expr, $out: expr) => {63 assert_eq!(72 assert_eq!(64 format!("{}", evaluate(Context::new(), &parse($str).unwrap())),73 format!("{}", evaluate(Context::new(), &parse($str, &ParserSettings {74 loc_data: false,75 file_name: "test.jsonnet".to_owned(),76 }).unwrap())),65 $out77 $out66 )78 )67 };79 };74 macro_rules! assert_eval_neg {86 macro_rules! assert_eval_neg {75 ($str: expr) => {87 ($str: expr) => {76 assert_eq!(88 assert_eq!(77 evaluate(Context::new(), &parse($str).unwrap()),89 evaluate(Context::new(), &parse($str, &ParserSettings {90 loc_data: false,91 file_name: "test.jsonnet".to_owned(),92 }).unwrap()),78 Val::Literal(LiteralType::False)93 Val::Literal(LiteralType::False)79 )94 )80 };95 };