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
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,13 +1,14 @@
 use crate::{
 	equals,
 	error::{Error::*, Result},
+	operator::evaluate_mod_op,
 	parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
-	FuncVal, LazyVal, Val,
+	FuncVal, IndexableVal, LazyVal, Val,
 };
 use format::{format_arr, format_obj};
 use jrsonnet_gc::Gc;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};
+use jrsonnet_parser::{ArgsDesc, ExprLocation};
 use jrsonnet_types::ty;
 use std::{collections::HashMap, path::PathBuf, rc::Rc};
 
@@ -20,7 +21,7 @@
 pub mod manifest;
 pub mod sort;
 
-fn std_format(str: IStr, vals: Val) -> Result<Val> {
+pub fn std_format(str: IStr, vals: Val) -> Result<Val> {
 	push(
 		Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),
 		|| format!("std.format of {}", str),
@@ -34,6 +35,38 @@
 	)
 }
 
+pub fn std_slice(
+	indexable: IndexableVal,
+	index: Option<usize>,
+	end: Option<usize>,
+	step: Option<usize>,
+) -> Result<Val> {
+	let index = index.unwrap_or(0);
+	let end = end.unwrap_or_else(|| match &indexable {
+		IndexableVal::Str(_) => usize::MAX,
+		IndexableVal::Arr(v) => v.len(),
+	});
+	let step = step.unwrap_or(1);
+	match &indexable {
+		IndexableVal::Str(s) => Ok(Val::Str(
+			(s.chars()
+				.skip(index)
+				.take(end - index)
+				.step_by(step)
+				.collect::<String>())
+			.into(),
+		)),
+		IndexableVal::Arr(arr) => Ok(Val::Arr(
+			(arr.iter()
+				.skip(index)
+				.take(end - index)
+				.step_by(step)
+				.collect::<Result<Vec<Val>>>()?)
+			.into(),
+		)),
+	}
+}
+
 type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;
 
 type BuiltinsType = HashMap<Box<str>, Builtin>;
@@ -188,34 +221,12 @@
 		2, end: ty!((number | null));
 		3, step: ty!((number | null));
 	], {
-		let index = match index {
-			Val::Num(v) => v as usize,
-			Val::Null => 0,
-			_ => unreachable!(),
-		};
-		let end = match end {
-			Val::Num(v) => v as usize,
-			Val::Null => match &indexable {
-				Val::Str(s) => s.chars().count(),
-				Val::Arr(v) => v.len(),
-				_ => unreachable!()
-			},
-			_ => unreachable!()
-		};
-		let step = match step {
-			Val::Num(v) => v as usize,
-			Val::Null => 1,
-			_ => unreachable!()
-		};
-		match &indexable {
-			Val::Str(s) => {
-				Ok(Val::Str((s.chars().skip(index).take(end-index).step_by(step).collect::<String>()).into()))
-			}
-			Val::Arr(arr) => {
-				Ok(Val::Arr((arr.iter().skip(index).take(end-index).step_by(step).collect::<Result<Vec<Val>>>()?).into()))
-			}
-			_ => unreachable!()
-		}
+		std_slice(
+			indexable.to_indexable()?,
+			index.try_cast_nullable_num("index")?.map(|v| v as usize),
+			end.try_cast_nullable_num("end")?.map(|v| v as usize),
+			step.try_cast_nullable_num("step")?.map(|v| v as usize),
+		)
 	})
 }
 
@@ -257,11 +268,7 @@
 		0, a: ty!((number | string));
 		1, b: ty!(any);
 	], {
-		match (a, b) {
-			(Val::Num(a), Val::Num(b)) => Ok(Val::Num(a % b)),
-			(Val::Str(str), vals) => std_format(str, vals),
-			(a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(BinaryOpType::Mod, a.value_type(), b.value_type()))
-		}
+		evaluate_mod_op(&a, &b)
 	})
 }
 
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/error.rs
1use crate::{2	builtin::{format::FormatError, sort::SortError},3	typed::TypeLocError,4};5use jrsonnet_gc::Trace;6use jrsonnet_interner::IStr;7use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};8use jrsonnet_types::ValType;9use std::{10	path::{Path, PathBuf},11	rc::Rc,12};13use thiserror::Error;1415#[derive(Error, Debug, Clone, Trace)]16#[trivially_drop]17pub enum Error {18	#[error("intrinsic not found: {0}")]19	IntrinsicNotFound(IStr),20	#[error("argument reordering in intrisics not supported yet")]21	IntrinsicArgumentReorderingIsNotSupportedYet,2223	#[error("operator {0} does not operate on type {1}")]24	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),25	#[error("binary operation {1} {0} {2} is not implemented")]26	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2728	#[error("no top level object in this context")]29	NoTopLevelObjectFound,30	#[error("self is only usable inside objects")]31	CantUseSelfOutsideOfObject,32	#[error("no super found")]33	NoSuperFound,3435	#[error("for loop can only iterate over arrays")]36	InComprehensionCanOnlyIterateOverArray,3738	#[error("array out of bounds: {0} is not within [0,{1})")]39	ArrayBoundsError(usize, usize),4041	#[error("assert failed: {0}")]42	AssertionFailed(IStr),4344	#[error("variable is not defined: {0}")]45	VariableIsNotDefined(IStr),46	#[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]47	TypeMismatch(&'static str, Vec<ValType>, ValType),48	#[error("no such field: {0}")]49	NoSuchField(IStr),5051	#[error("only functions can be called, got {0}")]52	OnlyFunctionsCanBeCalledGot(ValType),53	#[error("parameter {0} is not defined")]54	UnknownFunctionParameter(String),55	#[error("argument {0} is already bound")]56	BindingParameterASecondTime(IStr),57	#[error("too many args, function has {0}")]58	TooManyArgsFunctionHas(usize),59	#[error("founction argument is not passed: {0}")]60	FunctionParameterNotBoundInCall(IStr),6162	#[error("external variable is not defined: {0}")]63	UndefinedExternalVariable(IStr),64	#[error("native is not defined: {0}")]65	UndefinedExternalFunction(IStr),6667	#[error("field name should be string, got {0}")]68	FieldMustBeStringGot(ValType),6970	#[error("attempted to index array with string {0}")]71	AttemptedIndexAnArrayWithString(IStr),72	#[error("{0} index type should be {1}, got {2}")]73	ValueIndexMustBeTypeGot(ValType, ValType, ValType),74	#[error("cant index into {0}")]75	CantIndexInto(ValType),7677	#[error("super can't be used standalone")]78	StandaloneSuper,7980	#[error("can't resolve {1} from {0}")]81	ImportFileNotFound(PathBuf, PathBuf),82	#[error("resolved file not found: {0}")]83	ResolvedFileNotFound(PathBuf),84	#[error("imported file is not valid utf-8: {0:?}")]85	ImportBadFileUtf8(PathBuf),86	#[error("tried to import {1} from {0}, but imports is not supported")]87	ImportNotSupported(PathBuf, PathBuf),88	#[error(89		"syntax error, expected one of {}, got {:?}",90		.error.expected,91		.source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())92	)]93	ImportSyntaxError {94		path: Rc<Path>,95		source_code: IStr,96		#[unsafe_ignore_trace]97		error: Box<jrsonnet_parser::ParseError>,98	},99100	#[error("runtime error: {0}")]101	RuntimeError(IStr),102	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]103	StackOverflow,104	#[error("infinite recursion detected")]105	RecursiveLazyValueEvaluation,106	#[error("tried to index by fractional value")]107	FractionalIndex,108	#[error("attempted to divide by zero")]109	DivisionByZero,110111	#[error("string manifest output is not an string")]112	StringManifestOutputIsNotAString,113	#[error("stream manifest output is not an array")]114	StreamManifestOutputIsNotAArray,115	#[error("multi manifest output is not an object")]116	MultiManifestOutputIsNotAObject,117118	#[error("cant recurse stream manifest")]119	StreamManifestOutputCannotBeRecursed,120	#[error("stream manifest output cannot consist of raw strings")]121	StreamManifestCannotNestString,122123	#[error("{0}")]124	ImportCallbackError(String),125	#[error("invalid unicode codepoint: {0}")]126	InvalidUnicodeCodepointGot(u32),127128	#[error("format error: {0}")]129	Format(#[from] FormatError),130	#[error("type error: {0}")]131	TypeError(TypeLocError),132	#[error("sort error: {0}")]133	Sort(#[from] SortError),134135	#[cfg(feature = "anyhow-error")]136	#[error(transparent)]137	Other(Rc<anyhow::Error>),138}139140#[cfg(feature = "anyhow-error")]141impl From<anyhow::Error> for LocError {142	fn from(e: anyhow::Error) -> Self {143		Self::new(Error::Other(Rc::new(e)))144	}145}146147impl From<Error> for LocError {148	fn from(e: Error) -> Self {149		Self::new(e)150	}151}152153#[derive(Clone, Debug, Trace)]154#[trivially_drop]155pub struct StackTraceElement {156	pub location: Option<ExprLocation>,157	pub desc: String,158}159#[derive(Debug, Clone, Trace)]160#[trivially_drop]161pub struct StackTrace(pub Vec<StackTraceElement>);162163#[derive(Debug, Clone, Trace)]164#[trivially_drop]165pub struct LocError(Box<(Error, StackTrace)>);166impl LocError {167	pub fn new(e: Error) -> Self {168		Self(Box::new((e, StackTrace(vec![]))))169	}170171	pub const fn error(&self) -> &Error {172		&(self.0).0173	}174	pub fn error_mut(&mut self) -> &mut Error {175		&mut (self.0).0176	}177	pub const fn trace(&self) -> &StackTrace {178		&(self.0).1179	}180	pub fn trace_mut(&mut self) -> &mut StackTrace {181		&mut (self.0).1182	}183}184185pub type Result<V> = std::result::Result<V, LocError>;186187#[macro_export]188macro_rules! throw {189	($e: expr) => {190		return Err($e.into());191	};192}
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()))}