difftreelog
feat(evaluator) use LocExpr
in: master
2 files changed
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth5};5};6use closure::closure;6use closure::closure;7use jsonnet_parser::{7use jsonnet_parser::{8 ArgsDesc, BinaryOpType, BindSpec, Expr, FieldMember, LiteralType, Member, ObjBody, ParamsDesc,8 ArgsDesc, BinaryOpType, BindSpec, Expr, FieldMember, LiteralType, LocExpr, Member, ObjBody,9 UnaryOpType, Visibility,9 ParamsDesc, UnaryOpType, Visibility,10};10};11use std::{11use std::{40 }40 }41}41}424243pub fn evaluate_method(ctx: Context, expr: &Expr, arg_spec: ParamsDesc) -> Val {43pub fn evaluate_method(ctx: Context, expr: &LocExpr, arg_spec: ParamsDesc) -> Val {44 Val::Func(FuncDesc {44 Val::Func(FuncDesc {45 ctx,45 ctx,46 params: arg_spec,46 params: arg_spec,236 }236 }237}237}238238239pub fn evaluate(context: Context, expr: &Expr) -> Val {239pub fn evaluate(context: Context, expr: &LocExpr) -> Val {240 use Expr::*;240 use Expr::*;241 let LocExpr(expr, _location) = expr;241 match &*expr {242 match &**expr {242 Literal(LiteralType::This) => Val::Obj(243 Literal(LiteralType::This) => Val::Obj(243 context244 context244 .this()245 .this()260 }261 }261 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)),262 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)),262 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),263 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),263 Index(box value, box index) => {264 Index(value, index) => {264 match (265 match (265 evaluate(context.clone(), value).unwrap_if_lazy(),266 evaluate(context.clone(), value).unwrap_if_lazy(),266 evaluate(context.clone(), index),267 evaluate(context.clone(), index),284 .unwrap_or_else(|| panic!("out of bounds"))285 .unwrap_or_else(|| panic!("out of bounds"))285 .clone(),286 .clone(),286 (Val::Str(s), Val::Num(n)) => {287 (Val::Str(s), Val::Num(n)) => {287 Val::Str(s.chars().skip(n as usize - 1).take(1).collect())288 Val::Str(s.chars().skip(n as usize).take(1).collect())288 }289 }289 (v, i) => todo!("not implemented: {:?}[{:?}]", v, i.unwrap_if_lazy()),290 (v, i) => todo!("not implemented: {:?}[{:?}]", v, i.unwrap_if_lazy()),290 }291 }307 let context = context308 let context = context308 .extend(new_bindings, None, None, None)309 .extend(new_bindings, None, None, None)309 .into_future(future_context);310 .into_future(future_context);310 evaluate(context, &*returned.clone())311 evaluate(context, &returned.clone())311 }312 }312 Obj(body) => Val::Obj(evaluate_object(context, body.clone())),313 Obj(body) => Val::Obj(evaluate_object(context, body.clone())),313 Apply(box value, ArgsDesc(args)) => {314 Apply(value, ArgsDesc(args)) => {314 let value = evaluate(context.clone(), value).unwrap_if_lazy();315 let value = evaluate(context.clone(), value).unwrap_if_lazy();315 match value {316 match value {316 // TODO: Capture context of application317 // TODO: Capture context of applicationcrates/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 };