git.delta.rocks / jrsonnet / refs/commits / 5fc1275a239d

difftreelog

source

crates/jrsonnet-evaluator/src/evaluate/operator.rs7.7 KiBsourcehistory
1use std::cmp::Ordering;23use jrsonnet_ir::{BinaryOpType, Expr, UnaryOpType};45use crate::{6	Context, Result, Val,7	arr::ArrValue,8	bail,9	error::ErrorKind::*,10	evaluate,11	stdlib::std_format,12	typed::IntoUntyped as _,13	val::{StrValue, equals},14};1516pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {17	use UnaryOpType::*;18	use Val::*;19	Ok(match (op, b) {20		(Plus, Num(n)) => Val::Num(*n),21		(Minus, Num(n)) => Val::try_num(-n.get())?,22		(Not, Bool(v)) => Bool(!v),23		#[expect(clippy::cast_precision_loss, reason = "as spec")]24		(BitNot, Num(n)) => Val::try_num(!n.truncate_for_bitwise()? as f64)?,25		(op, o) => bail!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),26	})27}2829pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {30	use Val::*;31	Ok(match (a, b) {32		(Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),3334		(Num(a), Str(b)) => Val::string(format!("{a}{b}")),35		(Str(a), Num(b)) => Val::string(format!("{a}{b}")),3637		(Str(a), o) | (o, Str(a)) if a.is_empty() => Val::string(o.clone().to_string()?),38		(Str(a), o) => Val::string(format!("{a}{}", o.clone().to_string()?)),39		(o, Str(a)) => Val::string(format!("{}{a}", o.clone().to_string()?)),4041		(Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),42		(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),4344		(Num(v1), Num(v2)) => Val::try_num(v1.get() + v2.get())?,4546		#[cfg(feature = "exp-bigint")]47		(BigInt(a), BigInt(b)) => BigInt(Box::new(&**a + &**b)),4849		_ => bail!(BinaryOperatorDoesNotOperateOnValues(50			BinaryOpType::Add,51			a.value_type(),52			b.value_type(),53		)),54	})55}5657pub fn evaluate_sub_op(a: &Val, b: &Val) -> Result<Val> {58	use Val::*;59	Ok(match (a, b) {60		(Num(v1), Num(v2)) => Val::try_num(v1.get() - v2.get())?,6162		#[cfg(feature = "exp-bigint")]63		(BigInt(a), BigInt(b)) => BigInt(Box::new(&**a - &**b)),6465		// TODO: Support objects and arrays66		_ => bail!(BinaryOperatorDoesNotOperateOnValues(67			BinaryOpType::Sub,68			a.value_type(),69			b.value_type(),70		)),71	})72}7374pub fn evaluate_mul_op(a: &Val, b: &Val) -> Result<Val> {75	use Val::*;76	Ok(match (a, b) {77		#[expect(78			clippy::cast_possible_truncation,79			clippy::cast_sign_loss,80			reason = "should not be used with values too large, negative == 0"81		)]82		(Str(s), Num(c)) => Val::string(s.to_string().repeat(c.get() as usize)),83		#[expect(84			clippy::cast_possible_truncation,85			clippy::cast_sign_loss,86			reason = "should not be used with values too large"87		)]88		(Num(c), Str(s)) => Val::string(s.to_string().repeat(c.get() as usize)),8990		(Num(v1), Num(v2)) => Val::try_num(v1.get() * v2.get())?,9192		#[cfg(feature = "exp-bigint")]93		(BigInt(a), BigInt(b)) => BigInt(Box::new(&**a * &**b)),9495		_ => bail!(BinaryOperatorDoesNotOperateOnValues(96			BinaryOpType::Mul,97			a.value_type(),98			b.value_type(),99		)),100	})101}102103fn is_attempt_to_divide_by_zero(a: &Val, b: &Val) -> bool {104	use Val::*;105	match (a, b) {106		// string format107		(Str(_), _) => false,108109		(_, Num(b)) => **b == 0.,110		#[cfg(feature = "exp-bigint")]111		(_, BigInt(b)) => **b == num_bigint::BigInt::ZERO,112113		// something else114		_ => false,115	}116}117118pub fn evaluate_div_op(a: &Val, b: &Val) -> Result<Val> {119	use Val::*;120121	if is_attempt_to_divide_by_zero(a, b) {122		bail!(DivisionByZero);123	}124125	Ok(match (a, b) {126		(Num(a), Num(b)) => Val::try_num(a.get() / b.get())?,127		#[cfg(feature = "exp-bigint")]128		(BigInt(a), BigInt(b)) => BigInt(Box::new(&**a / &**b)),129		(a, b) => bail!(BinaryOperatorDoesNotOperateOnValues(130			BinaryOpType::Div,131			a.value_type(),132			b.value_type()133		)),134	})135}136137pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {138	use Val::*;139140	if is_attempt_to_divide_by_zero(a, b) {141		bail!(DivisionByZero);142	}143144	Ok(match (a, b) {145		(Num(a), Num(b)) => Val::try_num(a.get() % b.get())?,146		#[cfg(feature = "exp-bigint")]147		(BigInt(a), BigInt(b)) => BigInt(Box::new(&**a % &**b)),148		(Str(str), vals) => {149			String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)?150		}151		(a, b) => bail!(BinaryOperatorDoesNotOperateOnValues(152			BinaryOpType::Mod,153			a.value_type(),154			b.value_type()155		)),156	})157}158159pub fn evaluate_binary_op_special(160	ctx: Context,161	a: &Expr,162	op: BinaryOpType,163	b: &Expr,164) -> Result<Val> {165	use BinaryOpType::*;166	use Val::*;167	Ok(match (evaluate(ctx.clone(), a)?, op, b) {168		(Bool(true), Or, _o) => Val::Bool(true),169		(Bool(false), And, _o) => Val::Bool(false),170		#[cfg(feature = "exp-null-coaelse")]171		(Null, NullCoaelse, eb) => evaluate(ctx, eb)?,172		#[cfg(feature = "exp-null-coaelse")]173		(a, NullCoaelse, _o) => a,174		(a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(ctx, eb)?)?,175	})176}177178pub fn evaluate_compare_op(a: &Val, b: &Val, op: BinaryOpType) -> Result<Ordering> {179	use Val::*;180	Ok(match (a, b) {181		(Str(a), Str(b)) => a.cmp(b),182183		(Num(a), Num(b)) => a.cmp(b),184185		#[cfg(feature = "exp-bigint")]186		(BigInt(a), BigInt(b)) => a.cmp(b),187188		(Arr(a), Arr(b)) => {189			let ai = a.iter();190			let bi = b.iter();191192			for (a, b) in ai.zip(bi) {193				let ord = evaluate_compare_op(&a?, &b?, op)?;194				if !ord.is_eq() {195					return Ok(ord);196				}197			}198			a.len().cmp(&b.len())199		}200		(_, _) => bail!(BinaryOperatorDoesNotOperateOnValues(201			op,202			a.value_type(),203			b.value_type()204		)),205	})206}207208pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {209	use BinaryOpType::*;210	use Val::*;211	Ok(match (a, op, b) {212		(a, Eq, b) => Bool(equals(a, b)?),213		(a, Neq, b) => Bool(!equals(a, b)?),214215		(a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),216		(a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),217		(a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),218		(a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),219220		(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),221222		// Bool X Bool223		(Bool(a), And, Bool(b)) => Bool(*a && *b),224		(Bool(a), Or, Bool(b)) => Bool(*a || *b),225226		(a, Add, b) => evaluate_add_op(a, b)?,227		(a, Sub, b) => evaluate_sub_op(a, b)?,228		(a, Mul, b) => evaluate_mul_op(a, b)?,229		(a, Div, b) => evaluate_div_op(a, b)?,230		(a, Mod, b) => evaluate_mod_op(a, b)?,231232		(Num(v1), BitAnd, Num(v2)) =>233		{234			#[expect(235				clippy::cast_precision_loss,236				reason = "values are within safe integer ranges"237			)]238			Val::try_num((v1.truncate_for_bitwise()? & v2.truncate_for_bitwise()?) as f64)?239		}240		(Num(v1), BitOr, Num(v2)) =>241		{242			#[expect(243				clippy::cast_precision_loss,244				reason = "values are within safe integer ranges"245			)]246			Val::try_num((v1.truncate_for_bitwise()? | v2.truncate_for_bitwise()?) as f64)?247		}248		(Num(v1), BitXor, Num(v2)) =>249		{250			#[expect(251				clippy::cast_precision_loss,252				reason = "values are within safe integer ranges"253			)]254			Val::try_num((v1.truncate_for_bitwise()? ^ v2.truncate_for_bitwise()?) as f64)?255		}256		(Num(v1), Lhs, Num(v2)) => {257			if v2.get() < 0.0 {258				bail!("shift by negative exponent")259			}260			let base = v1.truncate_for_bitwise()?;261			let exp = v2.truncate_for_bitwise()? % 64;262263			#[expect(clippy::cast_sign_loss, reason = "exp is positive")]264			if exp >= 1 && base >= (1i64 << (63 - exp as u32)) {265				bail!("left shift would overflow")266			}267			#[expect(268				clippy::cast_precision_loss,269				clippy::cast_sign_loss,270				reason = "checked as original impl"271			)]272			Val::try_num(base.wrapping_shl(exp as u32) as f64)?273		}274		(Num(v1), Rhs, Num(v2)) => {275			if v2.get() < 0.0 {276				bail!("shift by negative exponent")277			}278			#[expect(279				clippy::cast_sign_loss,280				clippy::cast_possible_truncation,281				reason = "checked as original impl"282			)]283			let exp = ((v2.get() as i64) & 63) as u32;284			#[expect(clippy::cast_precision_loss, reason = "checked as upstream impl")]285			Val::try_num(v1.truncate_for_bitwise()?.wrapping_shr(exp) as f64)?286		}287288		// Bigint X Bigint289		_ => bail!(BinaryOperatorDoesNotOperateOnValues(290			op,291			a.value_type(),292			b.value_type(),293		)),294	})295}