-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
operator.rs
184 lines (161 loc) · 5.09 KB
/
operator.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::cmp::Ordering;
use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
use crate::{
arr::ArrValue,
bail,
error::ErrorKind::*,
evaluate,
stdlib::std_format,
typed::Typed,
val::{equals, StrValue},
Context, Result, Val,
};
pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
use UnaryOpType::*;
use Val::*;
Ok(match (op, b) {
(Plus, Num(n)) => Val::Num(*n),
(Minus, Num(n)) => Val::try_num(-n.get())?,
(Not, Bool(v)) => Bool(!v),
(BitNot, Num(n)) => Val::try_num(!(n.get() as i64) as f64)?,
(op, o) => bail!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),
})
}
pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {
use Val::*;
Ok(match (a, b) {
(Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),
(Num(a), Str(b)) => Val::string(format!("{a}{b}")),
(Str(a), Num(b)) => Val::string(format!("{a}{b}")),
(Str(a), o) | (o, Str(a)) if a.is_empty() => Val::string(o.clone().to_string()?),
(Str(a), o) => Val::string(format!("{a}{}", o.clone().to_string()?)),
(o, Str(a)) => Val::string(format!("{}{a}", o.clone().to_string()?)),
(Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
(Num(v1), Num(v2)) => Val::try_num(v1.get() + v2.get())?,
#[cfg(feature = "exp-bigint")]
(BigInt(a), BigInt(b)) => BigInt(Box::new(&**a + &**b)),
_ => bail!(BinaryOperatorDoesNotOperateOnValues(
BinaryOpType::Add,
a.value_type(),
b.value_type(),
)),
})
}
pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {
use Val::*;
match (a, b) {
(Num(a), Num(b)) => {
if b.get() == 0.0 {
bail!(DivisionByZero)
}
Ok(Val::try_num(a.get() % b.get())?)
}
(Str(str), vals) => {
String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)
}
(a, b) => bail!(BinaryOperatorDoesNotOperateOnValues(
BinaryOpType::Mod,
a.value_type(),
b.value_type()
)),
}
}
pub fn evaluate_binary_op_special(
ctx: Context,
a: &LocExpr,
op: BinaryOpType,
b: &LocExpr,
) -> Result<Val> {
use BinaryOpType::*;
use Val::*;
Ok(match (evaluate(ctx.clone(), a)?, op, b) {
(Bool(true), Or, _o) => Val::Bool(true),
(Bool(false), And, _o) => Val::Bool(false),
#[cfg(feature = "exp-null-coaelse")]
(Null, NullCoaelse, eb) => evaluate(ctx, eb)?,
#[cfg(feature = "exp-null-coaelse")]
(a, NullCoaelse, _o) => a,
(a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(ctx, eb)?)?,
})
}
pub fn evaluate_compare_op(a: &Val, b: &Val, op: BinaryOpType) -> Result<Ordering> {
use Val::*;
Ok(match (a, b) {
(Str(a), Str(b)) => a.cmp(b),
(Num(a), Num(b)) => a.cmp(b),
#[cfg(feature = "exp-bigint")]
(BigInt(a), BigInt(b)) => a.cmp(b),
(Arr(a), Arr(b)) => {
let ai = a.iter();
let bi = b.iter();
for (a, b) in ai.zip(bi) {
let ord = evaluate_compare_op(&a?, &b?, op)?;
if !ord.is_eq() {
return Ok(ord);
}
}
a.len().cmp(&b.len())
}
(_, _) => bail!(BinaryOperatorDoesNotOperateOnValues(
op,
a.value_type(),
b.value_type()
)),
})
}
pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {
use BinaryOpType::*;
use Val::*;
Ok(match (a, op, b) {
(a, Add, b) => evaluate_add_op(a, b)?,
(a, Eq, b) => Bool(equals(a, b)?),
(a, Neq, b) => Bool(!equals(a, b)?),
(a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),
(a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),
(a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),
(a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),
(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),
(a, Mod, b) => evaluate_mod_op(a, b)?,
(Str(v1), Mul, Num(v2)) => Val::string(v1.to_string().repeat(v2.get() as usize)),
// Bool X Bool
(Bool(a), And, Bool(b)) => Bool(*a && *b),
(Bool(a), Or, Bool(b)) => Bool(*a || *b),
// Num X Num
(Num(v1), Mul, Num(v2)) => Val::try_num(v1.get() * v2.get())?,
(Num(v1), Div, Num(v2)) => {
if v2.get() == 0.0 {
bail!(DivisionByZero)
}
Val::try_num(v1.get() / v2.get())?
}
(Num(v1), Sub, Num(v2)) => Val::try_num(v1.get() - v2.get())?,
(Num(v1), BitAnd, Num(v2)) => Val::try_num((v1.get() as i64 & v2.get() as i64) as f64)?,
(Num(v1), BitOr, Num(v2)) => Val::try_num((v1.get() as i64 | v2.get() as i64) as f64)?,
(Num(v1), BitXor, Num(v2)) => Val::try_num((v1.get() as i64 ^ v2.get() as i64) as f64)?,
(Num(v1), Lhs, Num(v2)) => {
if v2.get() < 0.0 {
bail!("shift by negative exponent")
}
let exp = ((v2.get() as i64) & 63) as u32;
Val::try_num((v1.get() as i64).wrapping_shl(exp) as f64)?
}
(Num(v1), Rhs, Num(v2)) => {
if v2.get() < 0.0 {
bail!("shift by negative exponent")
}
let exp = ((v2.get() as i64) & 63) as u32;
Val::try_num((v1.get() as i64).wrapping_shr(exp) as f64)?
}
// Bigint X Bigint
#[cfg(feature = "exp-bigint")]
(BigInt(a), Mul, BigInt(b)) => BigInt(Box::new(&**a * &**b)),
#[cfg(feature = "exp-bigint")]
(BigInt(a), Sub, BigInt(b)) => BigInt(Box::new(&**a - &**b)),
_ => bail!(BinaryOperatorDoesNotOperateOnValues(
op,
a.value_type(),
b.value_type(),
)),
})
}