git.delta.rocks / jrsonnet / refs/commits / 4aecc221ffa5

difftreelog

feat(evaluator) ArrComp support

Лач2020-06-03parent: #56fe309.patch.diff
in: master

7 files changed

modifiedcrates/jsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/ctx.rs
+++ b/crates/jsonnet-evaluator/src/ctx.rs
@@ -1,4 +1,7 @@
-use crate::{future_wrapper, rc_fn_helper, LazyBinding, LazyVal, ObjValue};
+use crate::{
+	future_wrapper, lazy_binding, lazy_val, rc_fn_helper, LazyBinding, LazyVal, ObjValue, Val,
+};
+use closure::closure;
 use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
 
 rc_fn_helper!(
@@ -62,6 +65,17 @@
 		ctx.unwrap()
 	}
 
+	pub fn with_var(&self, name: String, value: Val) -> Context {
+		let mut new_bindings: HashMap<_, LazyBinding> = HashMap::new();
+		new_bindings.insert(
+			name,
+			lazy_binding!(
+				closure!(clone value, |_t, _s|lazy_val!(closure!(clone value, ||value.clone())))
+			),
+		);
+		self.extend(new_bindings, None, None, None)
+	}
+
 	pub fn extend(
 		&self,
 		new_bindings: HashMap<String, LazyBinding>,
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -5,8 +5,9 @@
 };
 use closure::closure;
 use jsonnet_parser::{
-	ArgsDesc, BinaryOpType, BindSpec, Expr, FieldMember, LiteralType, LocExpr, Member, ObjBody,
-	ParamsDesc, UnaryOpType, Visibility,
+	el, Arg, ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember,
+	ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,
+	Visibility,
 };
 use std::{
 	collections::{BTreeMap, HashMap},
@@ -94,29 +95,64 @@
 	}
 }
 
-pub fn evaluate_binary_op(a: &Val, op: BinaryOpType, b: &Val) -> Val {
+pub fn evaluate_add_op(a: &Val, b: &Val) -> Val {
+	match (a, b) {
+		(Val::Str(v1), Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),
+		(Val::Str(v1), Val::Num(v2)) => Val::Str(format!("{}{}", v1, v2)),
+		(Val::Num(v1), Val::Str(v2)) => Val::Str(format!("{}{}", v1, v2)),
+		(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),
+		(Val::Arr(a), Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),
+		(Val::Num(v1), Val::Num(v2)) => Val::Num(v1 + v2),
+		_ => panic!("can't add: {:?} and {:?}", a, b),
+	}
+}
+
+pub fn evaluate_binary_op(
+	context: Context,
+	eval_state: EvaluationState,
+	a: &Val,
+	op: BinaryOpType,
+	b: &Val,
+) -> Val {
 	match (a, op, b) {
-		(Val::Lazy(a), o, b) => evaluate_binary_op(&a.evaluate(), o, b),
-		(a, o, Val::Lazy(b)) => evaluate_binary_op(a, o, &b.evaluate()),
+		(Val::Lazy(a), o, b) => evaluate_binary_op(context, eval_state, &a.evaluate(), o, b),
+		(a, o, Val::Lazy(b)) => evaluate_binary_op(context, eval_state, a, o, &b.evaluate()),
+
+		(a, BinaryOpType::Add, b) => evaluate_add_op(a, b),
 
-		(Val::Str(v1), BinaryOpType::Add, Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),
 		(Val::Str(v1), BinaryOpType::Ne, Val::Str(v2)) => bool_val(v1 != v2),
 
-		(Val::Str(v1), BinaryOpType::Add, Val::Num(v2)) => Val::Str(format!("{}{}", v1, v2)),
 		(Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize)),
+		(Val::Str(format), BinaryOpType::Mod, args) => evaluate(
+			context
+				.with_var("__tmp__format__".to_owned(), Val::Str(format.to_owned()))
+				.with_var(
+					"__tmp__args__".to_owned(),
+					match args {
+						Val::Arr(v) => Val::Arr(v.clone()),
+						v => Val::Arr(vec![v.clone()]),
+					},
+				),
+			eval_state,
+			&el!(Expr::Apply(
+				el!(Expr::Index(
+					el!(Expr::Var("std".to_owned())),
+					el!(Expr::Str("format".to_owned()))
+				)),
+				ArgsDesc(vec![
+					Arg(None, el!(Expr::Var("__tmp__format__".to_owned()))),
+					Arg(None, el!(Expr::Var("__tmp__args__".to_owned())))
+				])
+			)),
+		),
 
 		(Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),
 		(Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),
 
-		(Val::Obj(v1), BinaryOpType::Add, Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),
-
-		(Val::Arr(a), BinaryOpType::Add, Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),
-
 		(Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),
 		(Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => Val::Num(v1 / v2),
 		(Val::Num(v1), BinaryOpType::Mod, Val::Num(v2)) => Val::Num(v1 % v2),
 
-		(Val::Num(v1), BinaryOpType::Add, Val::Num(v2)) => Val::Num(v1 + v2),
 		(Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),
 
 		(Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {
@@ -152,6 +188,42 @@
 future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);
 future_wrapper!(ObjValue, FutureObjValue);
 
+pub fn evaluate_comp(
+	context: Context,
+	eval_state: EvaluationState,
+	value: &LocExpr,
+	specs: &[CompSpec],
+) -> Option<Vec<Val>> {
+	match specs.get(0) {
+		None => Some(vec![evaluate(context, eval_state, &value)]),
+		Some(CompSpec::IfSpec(IfSpecData(cond))) => {
+			match evaluate(context.clone(), eval_state.clone(), &cond).unwrap_if_lazy() {
+				Val::Bool(false) => None,
+				Val::Bool(true) => evaluate_comp(context, eval_state, value, &specs[1..]),
+				_ => panic!("if expression evaluated to non-boolean value"),
+			}
+		}
+		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {
+			match evaluate(context.clone(), eval_state.clone(), &expr).unwrap_if_lazy() {
+				Val::Arr(list) => {
+					let mut out = Vec::new();
+					for item in list {
+						let item = item.clone();
+						out.push(evaluate_comp(
+							context.with_var(var.clone(), item),
+							eval_state.clone(),
+							value,
+							&specs[1..],
+						));
+					}
+					Some(out.iter().flatten().flatten().cloned().collect())
+				}
+				_ => panic!("for expression evaluated to non-iterable value"),
+			}
+		}
+	}
+}
+
 // TODO: Asserts
 pub fn evaluate_object(context: Context, eval_state: EvaluationState, object: ObjBody) -> ObjValue {
 	match object {
@@ -272,11 +344,18 @@
 			Parened(e) => evaluate(context, eval_state.clone(), e),
 			Str(v) => Val::Str(v.clone()),
 			Num(v) => Val::Num(*v),
-			BinaryOp(v1, o, v2) => evaluate_binary_op(
-				&evaluate(context.clone(), eval_state.clone(), v1),
-				*o,
-				&evaluate(context, eval_state.clone(), v2),
-			),
+			BinaryOp(v1, o, v2) => {
+				let a = evaluate(context.clone(), eval_state.clone(), v1).unwrap_if_lazy();
+				let op = *o;
+				let b = evaluate(context.clone(), eval_state.clone(), v2).unwrap_if_lazy();
+				evaluate_binary_op(
+					context,
+					eval_state,
+					&a,
+					op,
+					&b,
+				)
+			},
 			UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, eval_state, v)),
 			Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),
 			Index(value, index) => {
@@ -286,7 +365,7 @@
 				) {
 					(Val::Obj(v), Val::Str(s)) => v
 						.get(&s)
-						.unwrap_or_else(closure!(clone context, || {
+						.unwrap_or_else(closure!(clone context, clone eval_state, || {
 							if let Some(n) = v.get("__intristic_namespace__") {
 								if let Val::Str(n) = n.unwrap_if_lazy() {
 									Val::Intristic(n, s)
@@ -335,12 +414,16 @@
 				}
 				Val::Arr(out)
 			}
+			ArrComp(expr, compspecs) => {
+				Val::Arr(evaluate_comp(context, eval_state, expr, compspecs).unwrap())
+			}
 			Obj(body) => Val::Obj(evaluate_object(context, eval_state, body.clone())),
 			Apply(value, ArgsDesc(args)) => {
 				let value = evaluate(context.clone(), eval_state.clone(), value).unwrap_if_lazy();
 				match value {
 					// TODO: Capture context of application
 					Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {
+						// arr/string/function
 						("std", "length") => {
 							assert_eq!(args.len(), 1);
 							let expr = &args.get(0).unwrap().1;
@@ -350,11 +433,13 @@
 								v => panic!("can't get length of {:?}", v),
 							}
 						}
+						// any
 						("std", "type") => {
 							assert_eq!(args.len(), 1);
 							let expr = &args.get(0).unwrap().1;
 							Val::Str(evaluate(context, eval_state, expr).type_of().to_owned())
 						}
+						// length, idx=>any
 						("std", "makeArray") => {
 							assert_eq!(args.len(), 2);
 							if let (Val::Num(v), Val::Func(d)) = (
@@ -371,6 +456,7 @@
 								panic!("bad makeArray call");
 							}
 						}
+						// string
 						("std", "codepoint") => {
 							assert_eq!(args.len(), 1);
 							if let Val::Str(s) = evaluate(context, eval_state, &args[0].1) {
@@ -383,6 +469,19 @@
 								panic!("bad codepoint call");
 							}
 						}
+						// object, includeHidden
+						("std", "objectFieldsEx") => {
+							assert_eq!(args.len(), 2);
+							if let (Val::Obj(body), Val::Bool(_include_hidden)) = (
+								evaluate(context.clone(), eval_state.clone(), &args[0].1),
+								evaluate(context, eval_state, &args[1].1),
+							) {
+								// TODO: handle visibility (_include_hidden)
+								Val::Arr(body.fields().into_iter().map(Val::Str).collect())
+							} else {
+								panic!("bad objectFieldsEx call");
+							}
+						}
 						(ns, name) => panic!("Intristic not found: {}.{}", ns, name),
 					},
 					Val::Func(f) => f.evaluate(
@@ -402,6 +501,17 @@
 				}
 			}
 			Function(params, body) => evaluate_method(context, eval_state, body, params.clone()),
+			AssertExpr(AssertStmt(value, msg), returned) => {
+				if evaluate(context.clone(), eval_state.clone(), &value).try_cast_bool() {
+					evaluate(context, eval_state, returned)
+				}else {
+					if let Some(msg) = msg {
+						panic!("assertion failed ({:?}): {}", value, evaluate(context, eval_state, msg).try_cast_str());
+					} else {
+						panic!("assertion failed ({:?}): no message", value);
+					}
+				}
+			},
 			Error(e) => panic!("error: {}", evaluate(context, eval_state, e)),
 			IfElse {
 				cond,
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/lib.rs
1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5mod ctx;6mod dynamic;7mod error;8mod evaluate;9mod obj;10mod val;1112pub use ctx::*;13pub use dynamic::*;14pub use error::*;15pub use evaluate::*;16use jsonnet_parser::*;17pub use obj::*;18use std::{cell::RefCell, collections::HashMap, rc::Rc};19pub use val::*;2021rc_fn_helper!(22	Binding,23	binding,24	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Val25);26rc_fn_helper!(27	LazyBinding,28	lazy_binding,29	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> LazyVal30);31rc_fn_helper!(FunctionRhs, function_rhs, dyn Fn(Context) -> Val);32rc_fn_helper!(33	FunctionDefault,34	function_default,35	dyn Fn(Context, LocExpr) -> Val36);3738#[derive(Default, Clone)]39pub struct EvaluationState {40	/// Used for stack-overflows and stacktraces41	pub stack: Rc<RefCell<Vec<(LocExpr, String)>>>,42	/// Contains file source codes and evaluated results for imports and pretty printing stacktraces43	pub files: Rc<RefCell<HashMap<String, (String, Option<Val>)>>>,44}45impl EvaluationState {46	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> T) -> T {47		self.stack.borrow_mut().push((e, comment));48		let result = f();49		self.stack.borrow_mut().pop();50		result51	}52	pub fn print_stack_trace(&self) {53		for e in self54			.stack55			.borrow()56			.iter()57			.rev()58			.map(|(loc, comment)| loc.1.clone().map(|v| (v, comment.clone())))59			.flatten()60		{61			println!("{:?} - {:?}", e.0, e.1)62		}63	}64	pub fn stack_trace(&self) -> Vec<(LocExpr, String)> {65		self.stack66			.borrow()67			.iter()68			.rev()69			.map(|e| e.clone())70			.collect()71	}72}7374#[cfg(test)]75pub mod tests {76	use super::{evaluate, Context, Val};77	use crate::EvaluationState;78	use jsonnet_parser::*;7980	#[test]81	fn eval_state_stacktrace() {82		let state = EvaluationState::default();83		state.push(84			loc_expr!(Expr::Num(0.0), true, ("test1.jsonnet".to_owned(), 10, 20)),85			"outer".to_owned(),86			|| {87				state.push(88					loc_expr!(Expr::Num(0.0), true, ("test2.jsonnet".to_owned(), 30, 40)),89					"inner".to_owned(),90					|| state.print_stack_trace(),91				);92			},93		);94	}9596	macro_rules! eval {97		($str: expr) => {98			evaluate(99				Context::new(),100				EvaluationState::default(),101				&parse(102					$str,103					&ParserSettings {104						loc_data: true,105						file_name: "test.jsonnet".to_owned(),106					},107					)108				.unwrap(),109				)110		};111	}112113	macro_rules! eval_stdlib {114		($str: expr) => {{115			let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";116			evaluate(117				Context::new(),118				EvaluationState::default(),119				&parse(120					&(std + $str),121					&ParserSettings {122						loc_data: true,123						file_name: "test.jsonnet".to_owned(),124					},125					)126				.unwrap(),127				)128			}};129	}130131	macro_rules! assert_eval {132		($str: expr) => {133			assert_eq!(134				evaluate(135					Context::new(),136					EvaluationState::default(),137					&parse(138						$str,139						&ParserSettings {140							loc_data: true,141							file_name: "test.jsonnet".to_owned(),142						}143						)144					.unwrap()145					),146				Val::Bool(true)147				)148		};149	}150	macro_rules! assert_json {151		($str: expr, $out: expr) => {152			assert_eq!(153				format!(154					"{}",155					evaluate(156						Context::new(),157						EvaluationState::default(),158						&parse(159							$str,160							&ParserSettings {161								loc_data: true,162								file_name: "test.jsonnet".to_owned(),163							}164						)165						.unwrap()166						)167					),168				$out169				)170		};171	}172	macro_rules! assert_json_stdlib {173		($str: expr, $out: expr) => {174			assert_eq!(format!("{}", eval_stdlib!($str)), $out)175		};176	}177	macro_rules! assert_eval_neg {178		($str: expr) => {179			assert_eq!(180				evaluate(181					Context::new(),182					EvaluationState::default(),183					&parse(184						$str,185						&ParserSettings {186							loc_data: true,187							file_name: "test.jsonnet".to_owned(),188						}189						)190					.unwrap()191					),192				Val::Bool(false)193				)194		};195	}196197	/// Sanity checking, before trusting to another tests198	#[test]199	fn equality_operator() {200		assert_eval!("2 == 2");201		assert_eval_neg!("2 != 2");202		assert_eval!("2 != 3");203		assert_eval_neg!("2 == 3");204		assert_eval!("'Hello' == 'Hello'");205		assert_eval_neg!("'Hello' != 'Hello'");206		assert_eval!("'Hello' != 'World'");207		assert_eval_neg!("'Hello' == 'World'");208	}209210	#[test]211	fn math_evaluation() {212		assert_eval!("2 + 2 * 2 == 6");213		assert_eval!("3 + (2 + 2 * 2) == 9");214	}215216	#[test]217	fn string_concat() {218		assert_eval!("'Hello' + 'World' == 'HelloWorld'");219		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");220		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");221	}222223	#[test]224	fn local() {225		assert_eval!("local a = 2; local b = 3; a + b == 5");226		assert_eval!("local a = 1, b = a + 1; a + b == 3");227		assert_eval!("local a = 1; local a = 2; a == 2");228	}229230	#[test]231	fn object_lazyness() {232		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);233	}234235	#[test]236	fn object_inheritance() {237		assert_json!("{a: self.b} + {b:3}", r#"{"a":3,"b":3}"#);238	}239240	#[test]241	fn test_object() {242		assert_json!("{a:2}", r#"{"a":2}"#);243		assert_json!("{a:2+2}", r#"{"a":4}"#);244		assert_json!("{a:2}+{b:2}", r#"{"a":2,"b":2}"#);245		assert_json!("{b:3}+{b:2}", r#"{"b":2}"#);246		assert_json!("{b:3}+{b+:2}", r#"{"b":5}"#);247		assert_json!("local test='a'; {[test]:2}", r#"{"a":2}"#);248		assert_json!(249			r#"250				{251					name: "Alice",252					welcome: "Hello " + self.name + "!",253				}254			"#,255			r#"{"name":"Alice","welcome":"Hello Alice!"}"#256		);257		assert_json!(258			r#"259				{260					name: "Alice",261					welcome: "Hello " + self.name + "!",262				} + {263					name: "Bob"264				}265			"#,266			r#"{"name":"Bob","welcome":"Hello Bob!"}"#267		);268	}269270	#[test]271	fn functions() {272		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");273		assert_json!(274			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,275			r#""HelloDearWorld""#276		);277	}278279	#[test]280	fn local_methods() {281		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");282		assert_json!(283			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,284			r#""HelloDearWorld""#285		);286	}287288	#[test]289	fn object_locals() {290		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b":3}"#);291		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b":3}"#);292		assert_json!(293			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,294			r#"{"test":{"test":4}}"#295		);296	}297298	#[test]299	fn direct_self() {300		println!(301			"{:#?}",302			eval!(303				r#"304					{305						local me = self,306						a: 3,307						b(): me.a,308					}309				"#310			)311		);312	}313314	#[test]315	fn indirect_self() {316		// `self` assigned to `me` was lost when being317		// referenced from field318		eval_stdlib!(319			r#"{320				local me = self,321				a: 3,322				b: me.a,323			}.b"#324		);325	}326327	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly328	#[test]329	fn std_assert_ok() {330		eval_stdlib!("std.assertEqual(4.5 << 2, 16)");331	}332333	#[test]334	#[should_panic]335	fn std_assert_failure() {336		eval_stdlib!("std.assertEqual(4.5 << 2, 15)");337	}338339	#[test]340	fn string_is_string() {341		assert_eq!(342			eval_stdlib!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),343			Val::Bool(false)344		);345	}346347	#[test]348	fn base64_works() {349		assert_json_stdlib!(r#"std.base64("test")"#, r#""dGVzdA==""#);350	}351352	#[test]353	fn utf8_chars() {354		assert_json_stdlib!(355			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,356			r#"{"c":128526,"l":1}"#357		)358	}359360	#[test]361	fn json() {362		println!("{:?}", eval_stdlib!(r#"std.manifestJson({a:3, b:4, c:6})"#));363	}364365	#[test]366	fn sjsonnet() {367		eval!(368			r#"369			local x0 = {k: 1};370			local x1 = {k: x0.k + x0.k};371			local x2 = {k: x1.k + x1.k};372			local x3 = {k: x2.k + x2.k};373			local x4 = {k: x3.k + x3.k};374			local x5 = {k: x4.k + x4.k};375			local x6 = {k: x5.k + x5.k};376			local x7 = {k: x6.k + x6.k};377			local x8 = {k: x7.k + x7.k};378			local x9 = {k: x8.k + x8.k};379			local x10 = {k: x9.k + x9.k};380			local x11 = {k: x10.k + x10.k};381			local x12 = {k: x11.k + x11.k};382			local x13 = {k: x12.k + x12.k};383			local x14 = {k: x13.k + x13.k};384			local x15 = {k: x14.k + x14.k};385			local x16 = {k: x15.k + x15.k};386			local x17 = {k: x16.k + x16.k};387			local x18 = {k: x17.k + x17.k};388			local x19 = {k: x18.k + x18.k};389			local x20 = {k: x19.k + x19.k};390			local x21 = {k: x20.k + x20.k};391			x21.k392		"#393		);394	}395}
after · crates/jsonnet-evaluator/src/lib.rs
1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5mod ctx;6mod dynamic;7mod error;8mod evaluate;9mod obj;10mod val;1112pub use ctx::*;13pub use dynamic::*;14pub use error::*;15pub use evaluate::*;16use jsonnet_parser::*;17pub use obj::*;18use std::{cell::RefCell, collections::HashMap, rc::Rc};19pub use val::*;2021rc_fn_helper!(22	Binding,23	binding,24	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Val25);26rc_fn_helper!(27	LazyBinding,28	lazy_binding,29	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> LazyVal30);31rc_fn_helper!(FunctionRhs, function_rhs, dyn Fn(Context) -> Val);32rc_fn_helper!(33	FunctionDefault,34	function_default,35	dyn Fn(Context, LocExpr) -> Val36);3738#[derive(Default, Clone)]39pub struct EvaluationState {40	/// Used for stack-overflows and stacktraces41	pub stack: Rc<RefCell<Vec<(LocExpr, String)>>>,42	/// Contains file source codes and evaluated results for imports and pretty printing stacktraces43	pub files: Rc<RefCell<HashMap<String, (String, Option<Val>)>>>,44}45impl EvaluationState {46	pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> T) -> T {47		self.stack.borrow_mut().push((e, comment));48		let result = f();49		self.stack.borrow_mut().pop();50		result51	}52	pub fn print_stack_trace(&self) {53		for e in self54			.stack55			.borrow()56			.iter()57			.rev()58			.map(|(loc, comment)| loc.1.clone().map(|v| (v, comment.clone())))59			.flatten()60		{61			println!("{:?} - {:?}", e.0, e.1)62		}63	}64	pub fn stack_trace(&self) -> Vec<(LocExpr, String)> {65		self.stack66			.borrow()67			.iter()68			.rev()69			.map(|e| e.clone())70			.collect()71	}72}7374#[cfg(test)]75pub mod tests {76	use super::{evaluate, Context, Val};77	use crate::EvaluationState;78	use jsonnet_parser::*;7980	#[test]81	fn eval_state_stacktrace() {82		let state = EvaluationState::default();83		state.push(84			loc_expr!(Expr::Num(0.0), true, ("test1.jsonnet".to_owned(), 10, 20)),85			"outer".to_owned(),86			|| {87				state.push(88					loc_expr!(Expr::Num(0.0), true, ("test2.jsonnet".to_owned(), 30, 40)),89					"inner".to_owned(),90					|| state.print_stack_trace(),91				);92			},93		);94	}9596	macro_rules! eval {97		($str: expr) => {98			evaluate(99				Context::new(),100				EvaluationState::default(),101				&parse(102					$str,103					&ParserSettings {104						loc_data: true,105						file_name: "test.jsonnet".to_owned(),106					},107					)108				.unwrap(),109				)110		};111	}112113	macro_rules! eval_stdlib {114		($str: expr) => {{115			let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";116			evaluate(117				Context::new(),118				EvaluationState::default(),119				&parse(120					&(std + $str),121					&ParserSettings {122						loc_data: true,123						file_name: "test.jsonnet".to_owned(),124					},125					)126				.unwrap(),127				)128			}};129	}130131	macro_rules! assert_eval {132		($str: expr) => {133			assert_eq!(134				evaluate(135					Context::new(),136					EvaluationState::default(),137					&parse(138						$str,139						&ParserSettings {140							loc_data: true,141							file_name: "test.jsonnet".to_owned(),142						}143						)144					.unwrap()145					),146				Val::Bool(true)147				)148		};149	}150	macro_rules! assert_json {151		($str: expr, $out: expr) => {152			assert_eq!(153				format!(154					"{}",155					evaluate(156						Context::new(),157						EvaluationState::default(),158						&parse(159							$str,160							&ParserSettings {161								loc_data: true,162								file_name: "test.jsonnet".to_owned(),163							}164						)165						.unwrap()166						)167					),168				$out169				)170		};171	}172	macro_rules! assert_json_stdlib {173		($str: expr, $out: expr) => {174			assert_eq!(format!("{}", eval_stdlib!($str)), $out)175		};176	}177	macro_rules! assert_eval_neg {178		($str: expr) => {179			assert_eq!(180				evaluate(181					Context::new(),182					EvaluationState::default(),183					&parse(184						$str,185						&ParserSettings {186							loc_data: true,187							file_name: "test.jsonnet".to_owned(),188						}189						)190					.unwrap()191					),192				Val::Bool(false)193				)194		};195	}196197	/// Sanity checking, before trusting to another tests198	#[test]199	fn equality_operator() {200		assert_eval!("2 == 2");201		assert_eval_neg!("2 != 2");202		assert_eval!("2 != 3");203		assert_eval_neg!("2 == 3");204		assert_eval!("'Hello' == 'Hello'");205		assert_eval_neg!("'Hello' != 'Hello'");206		assert_eval!("'Hello' != 'World'");207		assert_eval_neg!("'Hello' == 'World'");208	}209210	#[test]211	fn math_evaluation() {212		assert_eval!("2 + 2 * 2 == 6");213		assert_eval!("3 + (2 + 2 * 2) == 9");214	}215216	#[test]217	fn string_concat() {218		assert_eval!("'Hello' + 'World' == 'HelloWorld'");219		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");220		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");221	}222223	#[test]224	fn local() {225		assert_eval!("local a = 2; local b = 3; a + b == 5");226		assert_eval!("local a = 1, b = a + 1; a + b == 3");227		assert_eval!("local a = 1; local a = 2; a == 2");228	}229230	#[test]231	fn object_lazyness() {232		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);233	}234235	#[test]236	fn object_inheritance() {237		assert_json!("{a: self.b} + {b:3}", r#"{"a":3,"b":3}"#);238	}239240	#[test]241	fn test_object() {242		assert_json!("{a:2}", r#"{"a":2}"#);243		assert_json!("{a:2+2}", r#"{"a":4}"#);244		assert_json!("{a:2}+{b:2}", r#"{"a":2,"b":2}"#);245		assert_json!("{b:3}+{b:2}", r#"{"b":2}"#);246		assert_json!("{b:3}+{b+:2}", r#"{"b":5}"#);247		assert_json!("local test='a'; {[test]:2}", r#"{"a":2}"#);248		assert_json!(249			r#"250				{251					name: "Alice",252					welcome: "Hello " + self.name + "!",253				}254			"#,255			r#"{"name":"Alice","welcome":"Hello Alice!"}"#256		);257		assert_json!(258			r#"259				{260					name: "Alice",261					welcome: "Hello " + self.name + "!",262				} + {263					name: "Bob"264				}265			"#,266			r#"{"name":"Bob","welcome":"Hello Bob!"}"#267		);268	}269270	#[test]271	fn functions() {272		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");273		assert_json!(274			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,275			r#""HelloDearWorld""#276		);277	}278279	#[test]280	fn local_methods() {281		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");282		assert_json!(283			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,284			r#""HelloDearWorld""#285		);286	}287288	#[test]289	fn object_locals() {290		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b":3}"#);291		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b":3}"#);292		assert_json!(293			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,294			r#"{"test":{"test":4}}"#295		);296	}297298	#[test]299	fn direct_self() {300		println!(301			"{:#?}",302			eval!(303				r#"304					{305						local me = self,306						a: 3,307						b(): me.a,308					}309				"#310			)311		);312	}313314	#[test]315	fn indirect_self() {316		// `self` assigned to `me` was lost when being317		// referenced from field318		eval_stdlib!(319			r#"{320				local me = self,321				a: 3,322				b: me.a,323			}.b"#324		);325	}326327	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly328	#[test]329	fn std_assert_ok() {330		eval_stdlib!("std.assertEqual(4.5 << 2, 16)");331	}332333	#[test]334	#[should_panic]335	fn std_assert_failure() {336		eval_stdlib!("std.assertEqual(4.5 << 2, 15)");337	}338339	#[test]340	fn string_is_string() {341		assert_eq!(342			eval_stdlib!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),343			Val::Bool(false)344		);345	}346347	#[test]348	fn base64_works() {349		assert_json_stdlib!(r#"std.base64("test")"#, r#""dGVzdA==""#);350	}351352	#[test]353	fn utf8_chars() {354		assert_json_stdlib!(355			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,356			r#"{"c":128526,"l":1}"#357		)358	}359360	#[test]361	fn json() {362		println!("{:?}", eval_stdlib!(r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#));363	}364365	#[test]366	fn test() {367		assert_json_stdlib!(r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#, "");368	}369370	#[test]371	fn sjsonnet() {372		eval!(373			r#"374			local x0 = {k: 1};375			local x1 = {k: x0.k + x0.k};376			local x2 = {k: x1.k + x1.k};377			local x3 = {k: x2.k + x2.k};378			local x4 = {k: x3.k + x3.k};379			local x5 = {k: x4.k + x4.k};380			local x6 = {k: x5.k + x5.k};381			local x7 = {k: x6.k + x6.k};382			local x8 = {k: x7.k + x7.k};383			local x9 = {k: x8.k + x8.k};384			local x10 = {k: x9.k + x9.k};385			local x11 = {k: x10.k + x10.k};386			local x12 = {k: x11.k + x11.k};387			local x13 = {k: x12.k + x12.k};388			local x14 = {k: x13.k + x13.k};389			local x15 = {k: x14.k + x14.k};390			local x16 = {k: x15.k + x15.k};391			local x17 = {k: x16.k + x16.k};392			local x18 = {k: x17.k + x17.k};393			local x19 = {k: x18.k + x18.k};394			local x20 = {k: x19.k + x19.k};395			local x21 = {k: x20.k + x20.k};396			x21.k397		"#398		);399	}400}
modifiedcrates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/obj.rs
+++ b/crates/jsonnet-evaluator/src/obj.rs
@@ -1,5 +1,5 @@
-use crate::{evaluate_binary_op, Binding, Val};
-use jsonnet_parser::{BinaryOpType, Visibility};
+use crate::{evaluate_add_op, Binding, Val};
+use jsonnet_parser::Visibility;
 use std::{
 	cell::RefCell,
 	collections::{BTreeMap, BTreeSet, HashMap},
@@ -90,9 +90,8 @@
 			(Some(k), Some(s)) => {
 				let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone());
 				if k.add {
-					s.get_raw(key, real_this).map_or(Some(our.clone()), |v| {
-						Some(evaluate_binary_op(&v, BinaryOpType::Add, &our))
-					})
+					s.get_raw(key, real_this)
+						.map_or(Some(our.clone()), |v| Some(evaluate_add_op(&v, &our)))
 				} else {
 					Some(our)
 				}
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -117,6 +117,18 @@
 	Intristic(String, String),
 }
 impl Val {
+	pub fn try_cast_bool(self) -> bool {
+		match self.unwrap_if_lazy() {
+			Val::Bool(v) => v,
+			v => panic!("expected bool, got {:?}", v),
+		}
+	}
+	pub fn try_cast_str(self) -> String {
+		match self.unwrap_if_lazy() {
+			Val::Str(v) => v,
+			v => panic!("expected bool, got {:?}", v),
+		}
+	}
 	pub fn unwrap_if_lazy(self) -> Self {
 		if let Val::Lazy(v) = self {
 			v.evaluate().unwrap_if_lazy()
modifiedcrates/jsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jsonnet-parser/src/expr.rs
+++ b/crates/jsonnet-parser/src/expr.rs
@@ -165,7 +165,7 @@
 	///    ]
 	///  ],
 	/// ```
-	ArrComp(LocExpr, ForSpecData, Vec<CompSpec>),
+	ArrComp(LocExpr, Vec<CompSpec>),
 
 	/// Object: {a: 2}
 	Obj(ObjBody),
modifiedcrates/jsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-parser/src/lib.rs
+++ b/crates/jsonnet-parser/src/lib.rs
@@ -125,7 +125,7 @@
 		pub rule string_expr(s: &ParserSettings) -> LocExpr = l(s, <s:string() {Expr::Str(s)}>)
 		pub rule obj_expr(s: &ParserSettings) -> LocExpr = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)
 		pub rule array_expr(s: &ParserSettings) -> LocExpr = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)
-		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {Expr::ArrComp(expr, forspec, others.unwrap_or_default())}>)
+		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {Expr::ArrComp(expr, [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat())}>)
 		pub rule number_expr(s: &ParserSettings) -> LocExpr = l(s,<n:number() { expr::Expr::Num(n) }>)
 		pub rule var_expr(s: &ParserSettings) -> LocExpr = l(s,<n:id() { expr::Expr::Var(n) }>)
 		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{
@@ -252,15 +252,18 @@
 	jsonnet_parser::jsonnet(str, settings)
 }
 
+#[macro_export]
+macro_rules! el {
+	($expr:expr) => {
+		LocExpr(std::rc::Rc::new($expr), None)
+	};
+}
+
 #[cfg(test)]
 pub mod tests {
 	use super::{expr::*, parse};
 	use crate::ParserSettings;
-	macro_rules! el {
-		($expr:expr) => {
-			LocExpr(std::rc::Rc::new($expr), None)
-		};
-	}
+
 	macro_rules! parse {
 		($s:expr) => {
 			parse(
@@ -390,8 +393,10 @@
 					)),
 					ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))])
 				)),
-				ForSpecData("x".to_owned(), el!(Var("arr".to_owned()))),
-				vec![]
+				vec![CompSpec::ForSpec(ForSpecData(
+					"x".to_owned(),
+					el!(Var("arr".to_owned()))
+				))]
 			)),
 		)
 	}
@@ -403,24 +408,26 @@
 			parse!("[k for k in std.objectFields(patch) if patch[k] == null]"),
 			el!(ArrComp(
 				el!(Var("k".to_owned())),
-				ForSpecData(
-					"k".to_owned(),
-					el!(Apply(
+				vec![
+					CompSpec::ForSpec(ForSpecData(
+						"k".to_owned(),
+						el!(Apply(
+							el!(Index(
+								el!(Var("std".to_owned())),
+								el!(Str("objectFields".to_owned()))
+							)),
+							ArgsDesc(vec![Arg(None, el!(Var("patch".to_owned())))])
+						))
+					)),
+					CompSpec::IfSpec(IfSpecData(el!(BinaryOp(
 						el!(Index(
-							el!(Var("std".to_owned())),
-							el!(Str("objectFields".to_owned()))
+							el!(Var("patch".to_owned())),
+							el!(Var("k".to_owned()))
 						)),
-						ArgsDesc(vec![Arg(None, el!(Var("patch".to_owned())))])
-					))
-				),
-				vec![CompSpec::IfSpec(IfSpecData(el!(BinaryOp(
-					el!(Index(
-						el!(Var("patch".to_owned())),
-						el!(Var("k".to_owned()))
-					)),
-					BinaryOpType::Eq,
-					el!(Literal(LiteralType::Null))
-				))))]
+						BinaryOpType::Eq,
+						el!(Literal(LiteralType::Null))
+					))))
+				]
 			))
 		);
 	}