git.delta.rocks / jrsonnet / refs/commits / 0b0d703c6d05

difftreelog

refactor do not desugar mod/slice

Yaroslav Bolyukin2021-07-04parent: #53ec857.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
after · crates/jrsonnet-evaluator/src/builtin/mod.rs
1use crate::{2	equals,3	error::{Error::*, Result},4	operator::evaluate_mod_op,5	parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,6	FuncVal, IndexableVal, LazyVal, Val,7};8use format::{format_arr, format_obj};9use jrsonnet_gc::Gc;10use jrsonnet_interner::IStr;11use jrsonnet_parser::{ArgsDesc, ExprLocation};12use jrsonnet_types::ty;13use std::{collections::HashMap, path::PathBuf, rc::Rc};1415pub mod stdlib;16pub use stdlib::*;1718use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};1920pub mod format;21pub mod manifest;22pub mod sort;2324pub fn std_format(str: IStr, vals: Val) -> Result<Val> {25	push(26		Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),27		|| format!("std.format of {}", str),28		|| {29			Ok(match vals {30				Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),31				Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),32				o => Val::Str(format_arr(&str, &[o])?.into()),33			})34		},35	)36}3738pub fn std_slice(39	indexable: IndexableVal,40	index: Option<usize>,41	end: Option<usize>,42	step: Option<usize>,43) -> Result<Val> {44	let index = index.unwrap_or(0);45	let end = end.unwrap_or_else(|| match &indexable {46		IndexableVal::Str(_) => usize::MAX,47		IndexableVal::Arr(v) => v.len(),48	});49	let step = step.unwrap_or(1);50	match &indexable {51		IndexableVal::Str(s) => Ok(Val::Str(52			(s.chars()53				.skip(index)54				.take(end - index)55				.step_by(step)56				.collect::<String>())57			.into(),58		)),59		IndexableVal::Arr(arr) => Ok(Val::Arr(60			(arr.iter()61				.skip(index)62				.take(end - index)63				.step_by(step)64				.collect::<Result<Vec<Val>>>()?)65			.into(),66		)),67	}68}6970type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;7172type BuiltinsType = HashMap<Box<str>, Builtin>;7374thread_local! {75	static BUILTINS: BuiltinsType = {76		[77			("length".into(), builtin_length as Builtin),78			("type".into(), builtin_type),79			("makeArray".into(), builtin_make_array),80			("codepoint".into(), builtin_codepoint),81			("objectFieldsEx".into(), builtin_object_fields_ex),82			("objectHasEx".into(), builtin_object_has_ex),83			("slice".into(), builtin_slice),84			("primitiveEquals".into(), builtin_primitive_equals),85			("equals".into(), builtin_equals),86			("modulo".into(), builtin_modulo),87			("mod".into(), builtin_mod),88			("floor".into(), builtin_floor),89			("log".into(), builtin_log),90			("pow".into(), builtin_pow),91			("extVar".into(), builtin_ext_var),92			("native".into(), builtin_native),93			("filter".into(), builtin_filter),94			("map".into(), builtin_map),95			("foldl".into(), builtin_foldl),96			("foldr".into(), builtin_foldr),97			("sortImpl".into(), builtin_sort_impl),98			("format".into(), builtin_format),99			("range".into(), builtin_range),100			("char".into(), builtin_char),101			("encodeUTF8".into(), builtin_encode_utf8),102			("md5".into(), builtin_md5),103			("base64".into(), builtin_base64),104			("trace".into(), builtin_trace),105			("join".into(), builtin_join),106			("escapeStringJson".into(), builtin_escape_string_json),107			("manifestJsonEx".into(), builtin_manifest_json_ex),108			("reverse".into(), builtin_reverse),109			("id".into(), builtin_id),110			("strReplace".into(), builtin_str_replace),111			("parseJson".into(), builtin_parse_json),112		].iter().cloned().collect()113	};114}115116fn builtin_length(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {117	parse_args!(context, "length", args, 1, [118		0, x: ty!((string | object | array));119	], {120		Ok(match x {121			Val::Str(n) => Val::Num(n.chars().count() as f64),122			Val::Arr(a) => Val::Num(a.len() as f64),123			Val::Obj(o) => Val::Num(124				o.fields_visibility()125					.into_iter()126					.filter(|(_k, v)| *v)127					.count() as f64,128			),129			_ => unreachable!(),130		})131	})132}133134fn builtin_type(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {135	parse_args!(context, "type", args, 1, [136		0, x: ty!(any);137	], {138		Ok(Val::Str(x.value_type().name().into()))139	})140}141142fn builtin_make_array(143	context: Context,144	_loc: Option<&ExprLocation>,145	args: &ArgsDesc,146) -> Result<Val> {147	parse_args!(context, "makeArray", args, 2, [148		0, sz: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;149		1, func: ty!(function) => Val::Func;150	], {151		let mut out = Vec::with_capacity(sz as usize);152		for i in 0..sz as usize {153			out.push(LazyVal::new_resolved(func.evaluate_values(154				context.clone(),155				&[Val::Num(i as f64)]156			)?))157		}158		Ok(Val::Arr(out.into()))159	})160}161162fn builtin_codepoint(163	context: Context,164	_loc: Option<&ExprLocation>,165	args: &ArgsDesc,166) -> Result<Val> {167	parse_args!(context, "codepoint", args, 1, [168		0, str: ty!(char) => Val::Str;169	], {170		Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))171	})172}173174fn builtin_object_fields_ex(175	context: Context,176	_loc: Option<&ExprLocation>,177	args: &ArgsDesc,178) -> Result<Val> {179	parse_args!(context, "objectFieldsEx", args, 2, [180		0, obj: ty!(object) => Val::Obj;181		1, inc_hidden: ty!(boolean) => Val::Bool;182	], {183		let out = obj.fields_ex(inc_hidden);184		Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))185	})186}187188fn builtin_object_has_ex(189	context: Context,190	_loc: Option<&ExprLocation>,191	args: &ArgsDesc,192) -> Result<Val> {193	parse_args!(context, "objectHasEx", args, 3, [194		0, obj: ty!(object) => Val::Obj;195		1, f: ty!(string) => Val::Str;196		2, inc_hidden: ty!(boolean) => Val::Bool;197	], {198		Ok(Val::Bool(obj.has_field_ex(f, inc_hidden)))199	})200}201202fn builtin_parse_json(203	context: Context,204	_loc: Option<&ExprLocation>,205	args: &ArgsDesc,206) -> Result<Val> {207	parse_args!(context, "parseJson", args, 1, [208		0, s: ty!(string) => Val::Str;209	], {210		let state = EvaluationState::default();211		let path = PathBuf::from("std.parseJson").into();212		state.evaluate_snippet_raw(path ,s)213	})214}215216// faster217fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {218	parse_args!(context, "slice", args, 4, [219		0, indexable: ty!((string | array));220		1, index: ty!((number | null));221		2, end: ty!((number | null));222		3, step: ty!((number | null));223	], {224		std_slice(225			indexable.to_indexable()?,226			index.try_cast_nullable_num("index")?.map(|v| v as usize),227			end.try_cast_nullable_num("end")?.map(|v| v as usize),228			step.try_cast_nullable_num("step")?.map(|v| v as usize),229		)230	})231}232233// faster234fn builtin_primitive_equals(235	context: Context,236	_loc: Option<&ExprLocation>,237	args: &ArgsDesc,238) -> Result<Val> {239	parse_args!(context, "primitiveEquals", args, 2, [240		0, a: ty!(any);241		1, b: ty!(any);242	], {243		Ok(Val::Bool(primitive_equals(&a, &b)?))244	})245}246247// faster248fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {249	parse_args!(context, "equals", args, 2, [250		0, a: ty!(any);251		1, b: ty!(any);252	], {253		Ok(Val::Bool(equals(&a, &b)?))254	})255}256257fn builtin_modulo(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {258	parse_args!(context, "modulo", args, 2, [259		0, a: ty!(number) => Val::Num;260		1, b: ty!(number) => Val::Num;261	], {262		Ok(Val::Num(a % b))263	})264}265266fn builtin_mod(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {267	parse_args!(context, "mod", args, 2, [268		0, a: ty!((number | string));269		1, b: ty!(any);270	], {271		evaluate_mod_op(&a, &b)272	})273}274275fn builtin_floor(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {276	parse_args!(context, "floor", args, 1, [277		0, x: ty!(number) => Val::Num;278	], {279		Ok(Val::Num(x.floor()))280	})281}282283fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {284	parse_args!(context, "log", args, 1, [285		0, n: ty!(number) => Val::Num;286	], {287		Ok(Val::Num(n.ln()))288	})289}290291fn builtin_pow(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {292	parse_args!(context, "pow", args, 2, [293		0, x: ty!(number) => Val::Num;294		1, n: ty!(number) => Val::Num;295	], {296		Ok(Val::Num(x.powf(n)))297	})298}299300fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {301	parse_args!(context, "extVar", args, 1, [302		0, x: ty!(string) => Val::Str;303	], {304		Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)305	})306}307308fn builtin_native(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {309	parse_args!(context, "native", args, 1, [310		0, x: ty!(string) => Val::Str;311	], {312		Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)313	})314}315316fn builtin_filter(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {317	parse_args!(context, "filter", args, 2, [318		0, func: ty!(function) => Val::Func;319		1, arr: ty!(array) => Val::Arr;320	], {321		Ok(Val::Arr(arr.filter(|val| func322			.evaluate_values(context.clone(), &[val.clone()])?323			.try_cast_bool("filter predicate"))?))324	})325}326327fn builtin_map(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {328	parse_args!(context, "map", args, 2, [329		0, func: ty!(function) => Val::Func;330		1, arr: ty!(array) => Val::Arr;331	], {332		Ok(Val::Arr(arr.map(|val| func333			.evaluate_values(context.clone(), &[val]))?))334	})335}336337fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {338	parse_args!(context, "foldl", args, 3, [339		0, func: ty!(function) => Val::Func;340		1, arr: ty!(array) => Val::Arr;341		2, init: ty!(any);342	], {343		let mut acc = init;344		for i in arr.iter() {345			acc = func.evaluate_values(context.clone(), &[acc, i?])?;346		}347		Ok(acc)348	})349}350351fn builtin_foldr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {352	parse_args!(context, "foldr", args, 3, [353		0, func: ty!(function) => Val::Func;354		1, arr: ty!(array) => Val::Arr;355		2, init: ty!(any);356	], {357		let mut acc = init;358		for i in arr.iter().rev() {359			acc = func.evaluate_values(context.clone(), &[acc, i?])?;360		}361		Ok(acc)362	})363}364365#[allow(non_snake_case)]366fn builtin_sort_impl(367	context: Context,368	_loc: Option<&ExprLocation>,369	args: &ArgsDesc,370) -> Result<Val> {371	parse_args!(context, "sort", args, 2, [372		0, arr: ty!(array) => Val::Arr;373		1, keyF: ty!(function) => Val::Func;374	], {375		if arr.len() <= 1 {376			return Ok(Val::Arr(arr))377		}378		Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))379	})380}381382// faster383fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {384	parse_args!(context, "format", args, 2, [385		0, str: ty!(string) => Val::Str;386		1, vals: ty!(any)387	], {388		std_format(str, vals)389	})390}391392fn builtin_range(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {393	parse_args!(context, "range", args, 2, [394		0, from: ty!(number) => Val::Num;395		1, to: ty!(number) => Val::Num;396	], {397		if to < from {398			return Ok(Val::Arr(ArrValue::new_eager()))399		}400		let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));401		for i in from as usize..=to as usize {402			out.push(Val::Num(i as f64));403		}404		Ok(Val::Arr(out.into()))405	})406}407408fn builtin_char(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {409	parse_args!(context, "char", args, 1, [410		0, n: ty!(number) => Val::Num;411	], {412		let mut out = String::new();413		out.push(std::char::from_u32(n as u32).ok_or_else(||414			InvalidUnicodeCodepointGot(n as u32)415		)?);416		Ok(Val::Str(out.into()))417	})418}419420fn builtin_encode_utf8(421	context: Context,422	_loc: Option<&ExprLocation>,423	args: &ArgsDesc,424) -> Result<Val> {425	parse_args!(context, "encodeUTF8", args, 1, [426		0, str: ty!(string) => Val::Str;427	], {428		Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))429	})430}431432fn builtin_md5(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {433	parse_args!(context, "md5", args, 1, [434		0, str: ty!(string) => Val::Str;435	], {436		Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))437	})438}439440fn builtin_trace(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {441	parse_args!(context, "trace", args, 2, [442		0, str: ty!(string) => Val::Str;443		1, rest: ty!(any);444	], {445		eprint!("TRACE:");446		if let Some(loc) = loc {447			with_state(|s|{448				let locs = s.map_source_locations(&loc.0, &[loc.1]);449				eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);450			});451		}452		eprintln!(" {}", str);453		Ok(rest)454	})455}456457fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {458	parse_args!(context, "base64", args, 1, [459		0, input: ty!((string | (Array<number>)));460	], {461		Ok(Val::Str(match input {462			Val::Str(s) => {463				base64::encode(s.bytes().collect::<Vec<_>>()).into()464			},465			Val::Arr(a) => {466				base64::encode(a.iter().map(|v| {467					Ok(v?.unwrap_num()? as u8)468				}).collect::<Result<Vec<_>>>()?).into()469			},470			_ => unreachable!()471		}))472	})473}474475fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {476	parse_args!(context, "join", args, 2, [477		0, sep: ty!((string | array));478		1, arr: ty!(array) => Val::Arr;479	], {480		Ok(match sep {481			Val::Arr(joiner_items) => {482				let mut out = Vec::new();483484				let mut first = true;485				for item in arr.iter() {486					let item = item?.clone();487					if let Val::Arr(items) = item {488						if !first {489							out.reserve(joiner_items.len());490							// TODO: extend491							for item in joiner_items.iter() {492								out.push(item?);493							}494						}495						first = false;496						out.reserve(items.len());497						// TODO: extend498						for item in items.iter() {499							out.push(item?);500						}501					} else {502						throw!(RuntimeError("in std.join all items should be arrays".into()));503					}504				}505506				Val::Arr(out.into())507			},508			Val::Str(sep) => {509				let mut out = String::new();510511				let mut first = true;512				for item in arr.iter() {513					let item = item?.clone();514					if let Val::Str(item) = item {515						if !first {516							out += &sep;517						}518						first = false;519						out += &item;520					} else {521						throw!(RuntimeError("in std.join all items should be strings".into()));522					}523				}524525				Val::Str(out.into())526			},527			_ => unreachable!()528		})529	})530}531532// faster533fn builtin_escape_string_json(534	context: Context,535	_loc: Option<&ExprLocation>,536	args: &ArgsDesc,537) -> Result<Val> {538	parse_args!(context, "escapeStringJson", args, 1, [539		0, str_: ty!(string) => Val::Str;540	], {541		Ok(Val::Str(escape_string_json(&str_).into()))542	})543}544545// faster546fn builtin_manifest_json_ex(547	context: Context,548	_loc: Option<&ExprLocation>,549	args: &ArgsDesc,550) -> Result<Val> {551	parse_args!(context, "manifestJsonEx", args, 2, [552		0, value: ty!(any);553		1, indent: ty!(string) => Val::Str;554	], {555		Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {556			padding: &indent,557			mtype: ManifestType::Std,558		})?.into()))559	})560}561562// faster563fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {564	parse_args!(context, "reverse", args, 1, [565		0, value: ty!(array) => Val::Arr;566	], {567		Ok(Val::Arr(value.reversed()))568	})569}570571fn builtin_id(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {572	parse_args!(context, "id", args, 1, [573		0, v: ty!(any);574	], {575		Ok(v)576	})577}578579// faster580fn builtin_str_replace(581	context: Context,582	_loc: Option<&ExprLocation>,583	args: &ArgsDesc,584) -> Result<Val> {585	parse_args!(context, "strReplace", args, 3, [586		0, str: ty!(string) => Val::Str;587		1, from: ty!(string) => Val::Str;588		2, to: ty!(string) => Val::Str;589	], {590		let mut out = String::new();591		let mut last_idx = 0;592		while let Some(idx) = (&str[last_idx..]).find(&from as &str) {593			out.push_str(&str[last_idx..last_idx+idx]);594			out.push_str(&to);595			last_idx += idx + from.len();596		}597		if last_idx == 0 {598			return Ok(Val::Str(str))599		}600		out.push_str(&str[last_idx..]);601		Ok(Val::Str(out.into()))602	})603}604605pub fn call_builtin(606	context: Context,607	loc: Option<&ExprLocation>,608	name: &str,609	args: &ArgsDesc,610) -> Result<Val> {611	BUILTINS612		.with(|builtins| builtins.get(name).copied())613		.ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)614}
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -73,6 +73,8 @@
 	ValueIndexMustBeTypeGot(ValType, ValType, ValType),
 	#[error("cant index into {0}")]
 	CantIndexInto(ValType),
+	#[error("{0} is not indexable")]
+	ValueIsNotIndexable(ValType),
 
 	#[error("super can't be used standalone")]
 	StandaloneSuper,
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,4 +1,5 @@
 use crate::{
+	builtin::std_slice,
 	error::Error::*,
 	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
 	push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
@@ -679,6 +680,28 @@
 				}
 			}
 		}
+		Slice(value, desc) => {
+			let indexable = evaluate(context.clone(), value)?;
+
+			fn parse_num(
+				context: &Context,
+				expr: Option<&LocExpr>,
+				desc: &'static str,
+			) -> Result<Option<usize>> {
+				Ok(match expr {
+					Some(s) => evaluate(context.clone(), &s)?
+						.try_cast_nullable_num(desc)?
+						.map(|v| v as usize),
+					None => None,
+				})
+			}
+
+			let start = parse_num(&context, desc.start.as_ref(), "start")?;
+			let end = parse_num(&context, desc.end.as_ref(), "end")?;
+			let step = parse_num(&context, desc.step.as_ref(), "step")?;
+
+			std_slice(indexable.to_indexable()?, start, end, step)?
+		}
 		Import(path) => {
 			let tmp = loc
 				.clone()
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -1,3 +1,4 @@
+use crate::builtin::std_format;
 use crate::{equals, evaluate, Context, Val};
 use crate::{error::Error::*, throw, Result};
 use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
@@ -41,6 +42,19 @@
 	})
 }
 
+pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {
+	use Val::*;
+	match (a, b) {
+		(Num(a), Num(b)) => Ok(Num(a % b)),
+		(Str(str), vals) => std_format(str.clone(), vals.clone()),
+		(a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(
+			BinaryOpType::Mod,
+			a.value_type(),
+			b.value_type()
+		)),
+	}
+}
+
 pub fn evaluate_binary_op_special(
 	context: Context,
 	a: &LocExpr,
@@ -60,13 +74,14 @@
 	use BinaryOpType::*;
 	use Val::*;
 	Ok(match (a, op, b) {
-		(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
-
 		(a, Add, b) => evaluate_add_op(a, b)?,
 
 		(a, Eq, b) => Bool(equals(a, b)?),
 		(a, Neq, b) => Bool(!equals(a, b)?),
 
+		(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
+		(a, Mod, b) => evaluate_mod_op(a, b)?,
+
 		(Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),
 
 		// Bool X Bool
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -345,6 +345,11 @@
 	}
 }
 
+pub enum IndexableVal {
+	Str(IStr),
+	Arr(ArrValue),
+}
+
 #[derive(Debug, Clone, Trace)]
 #[trivially_drop]
 pub enum Val {
@@ -402,6 +407,17 @@
 		self.assert_type(context, ValType::Num)?;
 		self.unwrap_num()
 	}
+	pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {
+		Ok(match self {
+			Val::Null => None,
+			Val::Num(num) => Some(num),
+			_ => throw!(TypeMismatch(
+				context,
+				vec![ValType::Null, ValType::Num],
+				self.value_type()
+			)),
+		})
+	}
 	pub const fn value_type(&self) -> ValType {
 		match self {
 			Self::Str(..) => ValType::Str,
@@ -580,6 +596,13 @@
 			.try_cast_str("to json")
 		})
 	}
+	pub fn to_indexable(self) -> Result<IndexableVal> {
+		Ok(match self {
+			Val::Str(s) => IndexableVal::Str(s),
+			Val::Arr(arr) => IndexableVal::Arr(arr),
+			_ => throw!(ValueIsNotIndexable(self.value_type())),
+		})
+	}
 }
 
 const fn is_function_like(val: &Val) -> bool {
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -274,6 +274,8 @@
 	False,
 }
 
+#[cfg_attr(feature = "serialize", derive(Serialize))]
+#[cfg_attr(feature = "deserialize", derive(Deserialize))]
 #[derive(Debug, PartialEq, Trace)]
 #[trivially_drop]
 pub struct SliceDesc {
@@ -349,6 +351,7 @@
 		cond_then: LocExpr,
 		cond_else: Option<LocExpr>,
 	},
+	Slice(LocExpr, SliceDesc),
 }
 
 /// file, begin offset, end offset
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -14,6 +14,17 @@
 	pub file_name: Rc<Path>,
 }
 
+macro_rules! expr_bin {
+	($a:ident $op:ident $b:ident) => {
+		loc_expr_todo!(Expr::BinaryOp($a, $op, $b))
+	};
+}
+macro_rules! expr_un {
+	($op:ident $a:ident) => {
+		loc_expr_todo!(Expr::UnaryOp($op, $a))
+	};
+}
+
 parser! {
 	grammar jsonnet_parser() for str {
 		use peg::ParseLiteral;
@@ -219,54 +230,43 @@
 
 
 		use BinaryOpType::*;
+		use UnaryOpType::*;
 		rule expr(s: &ParserSettings) -> LocExpr
 			= start:position!() a:precedence! {
-				a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Or, b))}
+				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
 				--
-				a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, And, b))}
+				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}
 				--
-				a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BitOr, b))}
+				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}
 				--
-				a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BitXor, b))}
+				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}
 				--
-				a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BitAnd, b))}
+				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}
 				--
-				a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Eq, b))}
-				a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Neq, b))}
+				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}
+				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}
 				--
-				a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lt, b))}
-				a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Gt, b))}
-				a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lte, b))}
-				a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Gte, b))}
-				a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, In, b))}
+				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}
+				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}
+				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}
+				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}
+				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}
 				--
-				a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lhs, b))}
-				a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Rhs, b))}
+				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}
+				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}
 				--
-				a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Add, b))}
-				a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Sub, b))}
+				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}
+				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}
 				--
-				a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Mul, b))}
-				a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Div, b))}
-				a:(@) _ binop(<"%">) _ b:@ {loc_expr_todo!(Expr::Apply(
-					el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
-					false
-				))}
+				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}
+				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}
+				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}
 				--
-						unaryop(<"-">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}
-						unaryop(<"!">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}
-						unaryop(<"~">) _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }
+						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}
+						unaryop(<"!">) _ b:@ {expr_un!(Not b)}
+						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}
 				--
-				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(
-					el!(Expr::Intrinsic("slice".into())),
-					ArgsDesc(vec![
-						Arg(None, a),
-						Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),
-						Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),
-						Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),
-					]),
-					true,
-				))}
+				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}
 				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}
 				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}
 				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}