git.delta.rocks / jrsonnet / refs/commits / bc97bc99f5fd

difftreelog

fix(evaluator) missing intrinsics for slice, mod

Yaroslav Bolyukin2021-01-06parent: #7c1d01a.patch.diff
in: master
Fixes #30

3 files changed

modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -39,7 +39,8 @@
 								if **name == *"join" || **name == *"manifestJsonEx" ||
 								**name == *"escapeStringJson" || **name == *"equals" ||
 								**name == *"base64" || **name == *"foldl" || **name == *"foldr" ||
-								**name == *"sortImpl" || **name == *"format" || **name == *"range" || **name == *"reverse"
+								**name == *"sortImpl" || **name == *"format" || **name == *"range" ||
+								**name == *"reverse" || **name == *"slice" || **name == *"mod"
 							)
 						})
 						.collect(),
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -16,6 +16,20 @@
 pub mod manifest;
 pub mod sort;
 
+fn std_format(str: Rc<str>, vals: Val) -> Result<Val> {
+	push(
+		&Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),
+		|| format!("std.format of {}", str),
+		|| {
+			Ok(match vals {
+				Val::Arr(vals) => Val::Str(format_arr(&str, &vals)?.into()),
+				Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),
+				o => Val::Str(format_arr(&str, &[o])?.into()),
+			})
+		},
+	)
+}
+
 #[allow(clippy::cognitive_complexity)]
 pub fn call_builtin(
 	context: Context,
@@ -99,6 +113,43 @@
 					.any(|(k, _v)| *k == *f),
 			))
 		})?,
+
+		// faster
+		"slice" => parse_args!(context, "slice", args, 4, [
+			0, indexable: [Val::Str | Val::Arr], vec![ValType::Str, ValType::Arr];
+			1, index, vec![ValType::Num, ValType::Null];
+			2, end, vec![ValType::Num, ValType::Null];
+			3, step, vec![ValType::Num, ValType::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).cloned().collect::<Vec<Val>>()).into()))
+				}
+				_ => unreachable!()
+			}
+		})?,
 		"primitiveEquals" => parse_args!(context, "std.primitiveEquals", args, 2, [
 			0, a, vec![];
 			1, b, vec![];
@@ -112,6 +163,16 @@
 		], {
 			Ok(Val::Bool(equals(&a, &b)?))
 		})?,
+		"mod" => parse_args!(context, "std.mod", args, 2, [
+			0, a: [Val::Num | Val::Str], vec![ValType::Num, ValType::Str];
+			1, b, vec![];
+		], {
+			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(jrsonnet_parser::BinaryOpType::Mod, a.value_type()?, b.value_type()?))
+			}
+		})?,
 		"modulo" => parse_args!(context, "std.modulo", args, 2, [
 			0, a: [Val::Num]!!Val::Num, vec![ValType::Num];
 			1, b: [Val::Num]!!Val::Num, vec![ValType::Num];
@@ -219,13 +280,7 @@
 			0, str: [Val::Str]!!Val::Str, vec![ValType::Str];
 			1, vals, vec![]
 		], {
-			push(&Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)), ||format!("std.format of {}", str), ||{
-				Ok(match vals {
-					Val::Arr(vals) => Val::Str(format_arr(&str, &vals)?.into()),
-					Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),
-					o => Val::Str(format_arr(&str, &[o])?.into()),
-				})
-			})
+			std_format(str, vals)
 		})?,
 		// faster
 		"range" => parse_args!(context, "std.range", args, 2, [
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/expr.rs
1#[cfg(feature = "deserialize")]2use serde::Deserialize;3#[cfg(feature = "serialize")]4use serde::Serialize;5use std::{6	fmt::{Debug, Display},7	ops::Deref,8	path::PathBuf,9	rc::Rc,10};11#[cfg(feature = "dump")]12use structdump_derive::Codegen;1314#[cfg_attr(feature = "dump", derive(Codegen))]15#[cfg_attr(feature = "serialize", derive(Serialize))]16#[cfg_attr(feature = "deserialize", derive(Deserialize))]17#[derive(Debug, PartialEq)]18pub enum FieldName {19	/// {fixed: 2}20	Fixed(Rc<str>),21	/// {["dyn"+"amic"]: 3}22	Dyn(LocExpr),23}2425#[cfg_attr(feature = "dump", derive(Codegen))]26#[cfg_attr(feature = "serialize", derive(Serialize))]27#[cfg_attr(feature = "deserialize", derive(Deserialize))]28#[derive(Debug, Clone, Copy, PartialEq)]29pub enum Visibility {30	/// :31	Normal,32	/// ::33	Hidden,34	/// :::35	Unhide,36}3738#[cfg_attr(feature = "dump", derive(Codegen))]39#[cfg_attr(feature = "serialize", derive(Serialize))]40#[cfg_attr(feature = "deserialize", derive(Deserialize))]41#[derive(Debug, PartialEq)]42pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4344#[cfg_attr(feature = "dump", derive(Codegen))]45#[cfg_attr(feature = "serialize", derive(Serialize))]46#[cfg_attr(feature = "deserialize", derive(Deserialize))]47#[derive(Debug, PartialEq)]48pub struct FieldMember {49	pub name: FieldName,50	pub plus: bool,51	pub params: Option<ParamsDesc>,52	pub visibility: Visibility,53	pub value: LocExpr,54}5556#[cfg_attr(feature = "dump", derive(Codegen))]57#[cfg_attr(feature = "serialize", derive(Serialize))]58#[cfg_attr(feature = "deserialize", derive(Deserialize))]59#[derive(Debug, PartialEq)]60pub enum Member {61	Field(FieldMember),62	BindStmt(BindSpec),63	AssertStmt(AssertStmt),64}6566#[cfg_attr(feature = "dump", derive(Codegen))]67#[cfg_attr(feature = "serialize", derive(Serialize))]68#[cfg_attr(feature = "deserialize", derive(Deserialize))]69#[derive(Debug, Clone, Copy, PartialEq)]70pub enum UnaryOpType {71	Plus,72	Minus,73	BitNot,74	Not,75}76impl Display for UnaryOpType {77	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {78		use UnaryOpType::*;79		write!(80			f,81			"{}",82			match self {83				Plus => "+",84				Minus => "-",85				BitNot => "~",86				Not => "!",87			}88		)89	}90}9192#[cfg_attr(feature = "dump", derive(Codegen))]93#[cfg_attr(feature = "serialize", derive(Serialize))]94#[cfg_attr(feature = "deserialize", derive(Deserialize))]95#[derive(Debug, Clone, Copy, PartialEq)]96pub enum BinaryOpType {97	Mul,98	Div,99100	Add,101	Sub,102103	Lhs,104	Rhs,105106	Lt,107	Gt,108	Lte,109	Gte,110111	BitAnd,112	BitOr,113	BitXor,114115	And,116	Or,117}118impl Display for BinaryOpType {119	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {120		use BinaryOpType::*;121		write!(122			f,123			"{}",124			match self {125				Mul => "*",126				Div => "/",127				Add => "+",128				Sub => "-",129				Lhs => "<<",130				Rhs => ">>",131				Lt => "<",132				Gt => ">",133				Lte => "<=",134				Gte => ">=",135				BitAnd => "&",136				BitOr => "|",137				BitXor => "^",138				And => "&&",139				Or => "||",140			}141		)142	}143}144145/// name, default value146#[cfg_attr(feature = "dump", derive(Codegen))]147#[cfg_attr(feature = "serialize", derive(Serialize))]148#[cfg_attr(feature = "deserialize", derive(Deserialize))]149#[derive(Debug, PartialEq)]150pub struct Param(pub Rc<str>, pub Option<LocExpr>);151152/// Defined function parameters153#[cfg_attr(feature = "dump", derive(Codegen))]154#[cfg_attr(feature = "serialize", derive(Serialize))]155#[cfg_attr(feature = "deserialize", derive(Deserialize))]156#[derive(Debug, Clone, PartialEq)]157pub struct ParamsDesc(pub Rc<Vec<Param>>);158impl Deref for ParamsDesc {159	type Target = Vec<Param>;160	fn deref(&self) -> &Self::Target {161		&self.0162	}163}164165#[cfg_attr(feature = "dump", derive(Codegen))]166#[cfg_attr(feature = "serialize", derive(Serialize))]167#[cfg_attr(feature = "deserialize", derive(Deserialize))]168#[derive(Debug, PartialEq)]169pub struct Arg(pub Option<String>, pub LocExpr);170171#[cfg_attr(feature = "dump", derive(Codegen))]172#[cfg_attr(feature = "serialize", derive(Serialize))]173#[cfg_attr(feature = "deserialize", derive(Deserialize))]174#[derive(Debug, PartialEq)]175pub struct ArgsDesc(pub Vec<Arg>);176impl Deref for ArgsDesc {177	type Target = Vec<Arg>;178	fn deref(&self) -> &Self::Target {179		&self.0180	}181}182183#[cfg_attr(feature = "dump", derive(Codegen))]184#[cfg_attr(feature = "serialize", derive(Serialize))]185#[cfg_attr(feature = "deserialize", derive(Deserialize))]186#[derive(Debug, Clone, PartialEq)]187pub struct BindSpec {188	pub name: Rc<str>,189	pub params: Option<ParamsDesc>,190	pub value: LocExpr,191}192193#[cfg_attr(feature = "dump", derive(Codegen))]194#[cfg_attr(feature = "serialize", derive(Serialize))]195#[cfg_attr(feature = "deserialize", derive(Deserialize))]196#[derive(Debug, PartialEq)]197pub struct IfSpecData(pub LocExpr);198199#[cfg_attr(feature = "dump", derive(Codegen))]200#[cfg_attr(feature = "serialize", derive(Serialize))]201#[cfg_attr(feature = "deserialize", derive(Deserialize))]202#[derive(Debug, PartialEq)]203pub struct ForSpecData(pub Rc<str>, pub LocExpr);204205#[cfg_attr(feature = "dump", derive(Codegen))]206#[cfg_attr(feature = "serialize", derive(Serialize))]207#[cfg_attr(feature = "deserialize", derive(Deserialize))]208#[derive(Debug, PartialEq)]209pub enum CompSpec {210	IfSpec(IfSpecData),211	ForSpec(ForSpecData),212}213214#[cfg_attr(feature = "dump", derive(Codegen))]215#[cfg_attr(feature = "serialize", derive(Serialize))]216#[cfg_attr(feature = "deserialize", derive(Deserialize))]217#[derive(Debug, PartialEq)]218pub struct ObjComp {219	pub pre_locals: Vec<BindSpec>,220	pub key: LocExpr,221	pub value: LocExpr,222	pub post_locals: Vec<BindSpec>,223	pub compspecs: Vec<CompSpec>,224}225226#[cfg_attr(feature = "dump", derive(Codegen))]227#[cfg_attr(feature = "serialize", derive(Serialize))]228#[cfg_attr(feature = "deserialize", derive(Deserialize))]229#[derive(Debug, PartialEq)]230pub enum ObjBody {231	MemberList(Vec<Member>),232	ObjComp(ObjComp),233}234235#[cfg_attr(feature = "dump", derive(Codegen))]236#[cfg_attr(feature = "serialize", derive(Serialize))]237#[cfg_attr(feature = "deserialize", derive(Deserialize))]238#[derive(Debug, PartialEq, Clone, Copy)]239pub enum LiteralType {240	This,241	Super,242	Dollar,243	Null,244	True,245	False,246}247248#[derive(Debug, PartialEq)]249pub struct SliceDesc {250	pub start: Option<LocExpr>,251	pub end: Option<LocExpr>,252	pub step: Option<LocExpr>,253}254255/// Syntax base256#[cfg_attr(feature = "dump", derive(Codegen))]257#[cfg_attr(feature = "serialize", derive(Serialize))]258#[cfg_attr(feature = "deserialize", derive(Deserialize))]259#[derive(Debug, PartialEq)]260pub enum Expr {261	Literal(LiteralType),262263	/// String value: "hello"264	Str(Rc<str>),265	/// Number: 1, 2.0, 2e+20266	Num(f64),267	/// Variable name: test268	Var(Rc<str>),269270	/// Array of expressions: [1, 2, "Hello"]271	Arr(Vec<LocExpr>),272	/// Array comprehension:273	/// ```jsonnet274	///  ingredients: [275	///    { kind: kind, qty: 4 / 3 }276	///    for kind in [277	///      'Honey Syrup',278	///      'Lemon Juice',279	///      'Farmers Gin',280	///    ]281	///  ],282	/// ```283	ArrComp(LocExpr, Vec<CompSpec>),284285	/// Object: {a: 2}286	Obj(ObjBody),287	/// Object extension: var1 {b: 2}288	ObjExtend(LocExpr, ObjBody),289290	/// (obj)291	Parened(LocExpr),292293	/// -2294	UnaryOp(UnaryOpType, LocExpr),295	/// 2 - 2296	BinaryOp(LocExpr, BinaryOpType, LocExpr),297	/// assert 2 == 2 : "Math is broken"298	AssertExpr(AssertStmt, LocExpr),299	/// local a = 2; { b: a }300	LocalExpr(Vec<BindSpec>, LocExpr),301302	/// import "hello"303	Import(PathBuf),304	/// importStr "file.txt"305	ImportStr(PathBuf),306	/// error "I'm broken"307	ErrorStmt(LocExpr),308	/// a(b, c)309	Apply(LocExpr, ArgsDesc, bool),310	/// a[b]311	Index(LocExpr, LocExpr),312	/// function(x) x313	Function(ParamsDesc, LocExpr),314	/// std.primitiveEquals315	Intrinsic(Rc<str>),316	/// if true == false then 1 else 2317	IfElse {318		cond: IfSpecData,319		cond_then: LocExpr,320		cond_else: Option<LocExpr>,321	},322}323324/// file, begin offset, end offset325#[cfg_attr(feature = "dump", derive(Codegen))]326#[cfg_attr(feature = "serialize", derive(Serialize))]327#[cfg_attr(feature = "deserialize", derive(Deserialize))]328#[derive(Clone, PartialEq)]329pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);330impl Debug for ExprLocation {331	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {332		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)333	}334}335336/// Holds AST expression and its location in source file337#[cfg_attr(feature = "dump", derive(Codegen))]338#[cfg_attr(feature = "serialize", derive(Serialize))]339#[cfg_attr(feature = "deserialize", derive(Deserialize))]340#[derive(Clone, PartialEq)]341pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);342impl Debug for LocExpr {343	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {344		write!(f, "{:?} from {:?}", self.0, self.1)345	}346}347348/// Creates LocExpr from Expr and ExprLocation components349#[macro_export]350macro_rules! loc_expr {351	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {352		LocExpr(353			std::rc::Rc::new($expr),354			if $need_loc {355				Some(ExprLocation($name, $start, $end))356			} else {357				None358				},359			)360	};361}362363/// Creates LocExpr without location info364#[macro_export]365macro_rules! loc_expr_todo {366	($expr:expr) => {367		LocExpr(Rc::new($expr), None)368	};369}