1use std::fmt;
6
7use num_bigint::{BigInt, BigUint};
8use num_rational::BigRational;
9use num_traits::Zero;
10use ordered_float::OrderedFloat;
11
12use crate::lit_model::{LitModel, LitOpDesc, LitSortDesc};
13use crate::literal::LitVal;
14
15macro_rules! define_litval {
20 ($name:ident, [ $( $Var:ident($Ty:ty) => $sort:literal ),* $(,)? ]) => {
21 #[derive(Clone, PartialEq, Eq, Hash)]
22 pub enum $name { Bool(bool), $( $Var($Ty), )* }
23 impl LitVal for $name {}
24 impl fmt::Debug for $name {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self { Self::Bool(v) => write!(f, "{v}"), $( Self::$Var(v) => write!(f, "{v:?}"), )* }
27 }
28 }
29 impl fmt::Display for $name {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self { Self::Bool(v) => write!(f, "{v}"), $( Self::$Var(v) => write!(f, "{v}"), )* }
32 }
33 }
34 impl $name {
35 pub fn sort_of(&self) -> &'static str {
36 match self { Self::Bool(_) => "bool", $( Self::$Var(_) => $sort, )* }
37 }
38 pub fn is_truthy(&self) -> bool { matches!(self, Self::Bool(true)) }
39 }
40 };
41}
42
43define_litval!(MachineLit, [
44 I64(i64) => "i64", U64(u64) => "u64", F64(OrderedFloat<f64>) => "f64",
45 Usize(usize) => "usize", Str(String) => "String",
46]);
47define_litval!(BignumLit, [
48 IBig(BigInt) => "IBig", UBig(BigUint) => "UBig", RBig(BigRational) => "RBig",
49]);
50define_litval!(AllLit, [
51 I64(i64) => "i64", U64(u64) => "u64", F64(OrderedFloat<f64>) => "f64",
52 Usize(usize) => "usize", Str(String) => "String",
53 IBig(BigInt) => "IBig", UBig(BigUint) => "UBig", RBig(BigRational) => "RBig",
54]);
55
56#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61pub enum TypeGroup {
62 Machine,
63 Bignum,
64}
65
66impl TypeGroup {
67 pub fn parse(s: &str) -> Option<Self> {
68 match s {
69 "machine" => Some(Self::Machine),
70 "bignum" => Some(Self::Bignum),
71 _ => None,
72 }
73 }
74}
75
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
77pub enum LitValChoice {
78 Machine,
79 Bignum,
80 All,
81}
82
83pub fn choose_litval(groups: &[TypeGroup]) -> LitValChoice {
84 let m = groups.contains(&TypeGroup::Machine);
85 let b = groups.contains(&TypeGroup::Bignum);
86 match (m, b) {
87 (true, false) => LitValChoice::Machine,
88 (false, true) | (false, false) => LitValChoice::Bignum,
89 (true, true) => LitValChoice::All,
90 }
91}
92
93macro_rules! bool_ops {
98 ($E:ident) => {
99 &[
100 LitOpDesc {
101 name: "and",
102 arg_sorts: &["bool", "bool"],
103 ret_sort: "bool",
104 eval: |a| match (a[0], a[1]) {
105 ($E::Bool(x), $E::Bool(y)) => $E::Bool(*x && *y),
106 _ => panic!(),
107 },
108 },
109 LitOpDesc {
110 name: "or",
111 arg_sorts: &["bool", "bool"],
112 ret_sort: "bool",
113 eval: |a| match (a[0], a[1]) {
114 ($E::Bool(x), $E::Bool(y)) => $E::Bool(*x || *y),
115 _ => panic!(),
116 },
117 },
118 LitOpDesc {
119 name: "not",
120 arg_sorts: &["bool"],
121 ret_sort: "bool",
122 eval: |a| match a[0] {
123 $E::Bool(x) => $E::Bool(!x),
124 _ => panic!(),
125 },
126 },
127 LitOpDesc {
128 name: "bool::if",
129 arg_sorts: &["bool", "bool", "bool"],
130 ret_sort: "bool",
131 eval: |a| match a[0] {
132 $E::Bool(c) => {
133 if *c {
134 a[1].clone()
135 } else {
136 a[2].clone()
137 }
138 }
139 _ => panic!(),
140 },
141 },
142 ]
143 };
144}
145
146macro_rules! checked_binop {
147 ($E:ident,$V:ident,$n:expr,$s:expr,$m:ident) => {
148 LitOpDesc {
149 name: $n,
150 arg_sorts: &[$s, $s],
151 ret_sort: $s,
152 eval: |a| match (a[0], a[1]) {
153 ($E::$V(x), $E::$V(y)) => $E::$V(x.$m(*y).expect($n)),
154 _ => panic!(),
155 },
156 }
157 };
158}
159macro_rules! wrapping_binop {
160 ($E:ident,$V:ident,$n:expr,$s:expr,$m:ident) => {
161 LitOpDesc {
162 name: $n,
163 arg_sorts: &[$s, $s],
164 ret_sort: $s,
165 eval: |a| match (a[0], a[1]) {
166 ($E::$V(x), $E::$V(y)) => $E::$V(x.$m(*y)),
167 _ => panic!(),
168 },
169 }
170 };
171}
172macro_rules! saturating_binop {
173 ($E:ident,$V:ident,$n:expr,$s:expr,$m:ident) => {
174 LitOpDesc {
175 name: $n,
176 arg_sorts: &[$s, $s],
177 ret_sort: $s,
178 eval: |a| match (a[0], a[1]) {
179 ($E::$V(x), $E::$V(y)) => $E::$V(x.$m(*y)),
180 _ => panic!(),
181 },
182 }
183 };
184}
185macro_rules! cmp_op { ($E:ident,$V:ident,$n:expr,$s:expr,$op:tt) => {
186 LitOpDesc { name: $n, arg_sorts: &[$s,$s], ret_sort: "bool",
187 eval: |a| match (a[0],a[1]) { ($E::$V(x),$E::$V(y)) => $E::Bool(x $op y), _=>panic!() } }
188};}
189macro_rules! if_op {
190 ($E:ident,$n:expr,$s:expr) => {
191 LitOpDesc {
192 name: $n,
193 arg_sorts: &["bool", $s, $s],
194 ret_sort: $s,
195 eval: |a| match a[0] {
196 $E::Bool(c) => {
197 if *c {
198 a[1].clone()
199 } else {
200 a[2].clone()
201 }
202 }
203 _ => panic!(),
204 },
205 }
206 };
207}
208macro_rules! min_op {
209 ($E:ident,$V:ident,$n:expr,$s:expr) => {
210 LitOpDesc {
211 name: $n,
212 arg_sorts: &[$s, $s],
213 ret_sort: $s,
214 eval: |a| match (a[0], a[1]) {
215 ($E::$V(x), $E::$V(y)) => $E::$V(*x.min(y)),
216 _ => panic!(),
217 },
218 }
219 };
220}
221macro_rules! max_op {
222 ($E:ident,$V:ident,$n:expr,$s:expr) => {
223 LitOpDesc {
224 name: $n,
225 arg_sorts: &[$s, $s],
226 ret_sort: $s,
227 eval: |a| match (a[0], a[1]) {
228 ($E::$V(x), $E::$V(y)) => $E::$V(*x.max(y)),
229 _ => panic!(),
230 },
231 }
232 };
233}
234
235macro_rules! signed_int_ops { ($E:ident, $V:ident, $s:expr) => { &[
236 checked_binop!($E,$V,concat!($s,"::+"),$s,checked_add),
237 checked_binop!($E,$V,concat!($s,"::-"),$s,checked_sub),
238 checked_binop!($E,$V,concat!($s,"::*"),$s,checked_mul),
239 checked_binop!($E,$V,concat!($s,"::/"),$s,checked_div),
240 checked_binop!($E,$V,concat!($s,"::%"),$s,checked_rem),
241 LitOpDesc { name: concat!($s,"::pow"), arg_sorts: &[$s,$s], ret_sort: $s,
242 eval: |a| match (a[0], a[1]) {
243 ($E::$V(x), $E::$V(e)) => {
244 let e = u32::try_from(*e).expect(concat!($s,"::pow exponent out of range"));
245 $E::$V(x.checked_pow(e).expect(concat!($s,"::pow overflow")))
246 }
247 _ => panic!(),
248 } },
249 LitOpDesc { name: concat!($s,"::neg"), arg_sorts: &[$s], ret_sort: $s,
250 eval: |a| match a[0] { $E::$V(x) => $E::$V(x.checked_neg().expect(concat!($s,"::neg overflow"))), _=>panic!() } },
251 LitOpDesc { name: concat!($s,"::abs"), arg_sorts: &[$s], ret_sort: $s,
252 eval: |a| match a[0] { $E::$V(x) => $E::$V(x.checked_abs().expect(concat!($s,"::abs overflow"))), _=>panic!() } },
253 wrapping_binop!($E,$V,concat!($s,"::wrapping_add"),$s,wrapping_add),
254 wrapping_binop!($E,$V,concat!($s,"::wrapping_sub"),$s,wrapping_sub),
255 wrapping_binop!($E,$V,concat!($s,"::wrapping_mul"),$s,wrapping_mul),
256 LitOpDesc { name: concat!($s,"::wrapping_neg"), arg_sorts: &[$s], ret_sort: $s,
257 eval: |a| match a[0] { $E::$V(x) => $E::$V(x.wrapping_neg()), _=>panic!() } },
258 LitOpDesc { name: concat!($s,"::wrapping_abs"), arg_sorts: &[$s], ret_sort: $s,
259 eval: |a| match a[0] { $E::$V(x) => $E::$V(x.wrapping_abs()), _=>panic!() } },
260 saturating_binop!($E,$V,concat!($s,"::saturating_add"),$s,saturating_add),
261 saturating_binop!($E,$V,concat!($s,"::saturating_sub"),$s,saturating_sub),
262 saturating_binop!($E,$V,concat!($s,"::saturating_mul"),$s,saturating_mul),
263 LitOpDesc { name: concat!($s,"::saturating_neg"), arg_sorts: &[$s], ret_sort: $s,
264 eval: |a| match a[0] { $E::$V(x) => $E::$V(x.saturating_neg()), _=>panic!() } },
265 LitOpDesc { name: concat!($s,"::saturating_abs"), arg_sorts: &[$s], ret_sort: $s,
266 eval: |a| match a[0] { $E::$V(x) => $E::$V(x.saturating_abs()), _=>panic!() } },
267 min_op!($E,$V,concat!($s,"::min"),$s), max_op!($E,$V,concat!($s,"::max"),$s),
268 cmp_op!($E,$V,concat!($s,"::<"),$s,<), cmp_op!($E,$V,concat!($s,"::<="),$s,<=),
269 cmp_op!($E,$V,concat!($s,"::>"),$s,>), cmp_op!($E,$V,concat!($s,"::>="),$s,>=),
270 cmp_op!($E,$V,concat!($s,"::=="),$s,==), cmp_op!($E,$V,concat!($s,"::!="),$s,!=),
271 if_op!($E,concat!($s,"::if"),$s),
272]};}
273
274macro_rules! unsigned_int_ops { ($E:ident, $V:ident, $s:expr) => { &[
275 checked_binop!($E,$V,concat!($s,"::+"),$s,checked_add),
276 checked_binop!($E,$V,concat!($s,"::-"),$s,checked_sub),
277 checked_binop!($E,$V,concat!($s,"::*"),$s,checked_mul),
278 checked_binop!($E,$V,concat!($s,"::/"),$s,checked_div),
279 checked_binop!($E,$V,concat!($s,"::%"),$s,checked_rem),
280 wrapping_binop!($E,$V,concat!($s,"::wrapping_add"),$s,wrapping_add),
281 wrapping_binop!($E,$V,concat!($s,"::wrapping_sub"),$s,wrapping_sub),
282 wrapping_binop!($E,$V,concat!($s,"::wrapping_mul"),$s,wrapping_mul),
283 saturating_binop!($E,$V,concat!($s,"::saturating_add"),$s,saturating_add),
284 saturating_binop!($E,$V,concat!($s,"::saturating_sub"),$s,saturating_sub),
285 saturating_binop!($E,$V,concat!($s,"::saturating_mul"),$s,saturating_mul),
286 min_op!($E,$V,concat!($s,"::min"),$s), max_op!($E,$V,concat!($s,"::max"),$s),
287 cmp_op!($E,$V,concat!($s,"::<"),$s,<), cmp_op!($E,$V,concat!($s,"::<="),$s,<=),
288 cmp_op!($E,$V,concat!($s,"::>"),$s,>), cmp_op!($E,$V,concat!($s,"::>="),$s,>=),
289 cmp_op!($E,$V,concat!($s,"::=="),$s,==), cmp_op!($E,$V,concat!($s,"::!="),$s,!=),
290 if_op!($E,concat!($s,"::if"),$s),
291]};}
292
293macro_rules! f64_ops { ($E:ident) => { &[
294 LitOpDesc { name: "f64::+", arg_sorts: &["f64","f64"], ret_sort: "f64",
295 eval: |a| match (a[0],a[1]) { ($E::F64(x),$E::F64(y)) => $E::F64(OrderedFloat(*x.as_ref()+*y.as_ref())), _=>panic!() } },
296 LitOpDesc { name: "f64::-", arg_sorts: &["f64","f64"], ret_sort: "f64",
297 eval: |a| match (a[0],a[1]) { ($E::F64(x),$E::F64(y)) => $E::F64(OrderedFloat(*x.as_ref()-*y.as_ref())), _=>panic!() } },
298 LitOpDesc { name: "f64::*", arg_sorts: &["f64","f64"], ret_sort: "f64",
299 eval: |a| match (a[0],a[1]) { ($E::F64(x),$E::F64(y)) => $E::F64(OrderedFloat(*x.as_ref()*(*y.as_ref()))), _=>panic!() } },
300 LitOpDesc { name: "f64::/", arg_sorts: &["f64","f64"], ret_sort: "f64",
301 eval: |a| match (a[0],a[1]) { ($E::F64(x),$E::F64(y)) => $E::F64(OrderedFloat(*x.as_ref()/(*y.as_ref()))), _=>panic!() } },
302 LitOpDesc { name: "f64::neg", arg_sorts: &["f64"], ret_sort: "f64",
303 eval: |a| match a[0] { $E::F64(x) => $E::F64(OrderedFloat(-*x.as_ref())), _=>panic!() } },
304 LitOpDesc { name: "f64::abs", arg_sorts: &["f64"], ret_sort: "f64",
305 eval: |a| match a[0] { $E::F64(x) => $E::F64(OrderedFloat(x.as_ref().abs())), _=>panic!() } },
306 LitOpDesc { name: "f64::min", arg_sorts: &["f64","f64"], ret_sort: "f64",
307 eval: |a| match (a[0],a[1]) { ($E::F64(x),$E::F64(y)) => $E::F64(*x.min(y)), _=>panic!() } },
308 LitOpDesc { name: "f64::max", arg_sorts: &["f64","f64"], ret_sort: "f64",
309 eval: |a| match (a[0],a[1]) { ($E::F64(x),$E::F64(y)) => $E::F64(*x.max(y)), _=>panic!() } },
310 cmp_op!($E,F64,"f64::<","f64",<), cmp_op!($E,F64,"f64::<=","f64",<=),
311 cmp_op!($E,F64,"f64::>","f64",>), cmp_op!($E,F64,"f64::>=","f64",>=),
312 cmp_op!($E,F64,"f64::==","f64",==), cmp_op!($E,F64,"f64::!=","f64",!=),
313 if_op!($E,"f64::if","f64"),
314]};}
315
316macro_rules! string_ops { ($E:ident) => { &[
317 LitOpDesc { name: "String::concat", arg_sorts: &["String","String"], ret_sort: "String",
318 eval: |a| match (a[0],a[1]) { ($E::Str(x),$E::Str(y)) => { let mut r=x.clone(); r.push_str(y); $E::Str(r) } _=>panic!() } },
319 LitOpDesc { name: "String::contains", arg_sorts: &["String","String"], ret_sort: "bool",
320 eval: |a| match (a[0],a[1]) { ($E::Str(x),$E::Str(y)) => $E::Bool(x.contains(y.as_str())), _=>panic!() } },
321 LitOpDesc { name: "String::replace", arg_sorts: &["String","String","String"], ret_sort: "String",
322 eval: |a| match (a[0],a[1],a[2]) { ($E::Str(s),$E::Str(f),$E::Str(t)) => $E::Str(s.replacen(f.as_str(),t.as_str(),1)), _=>panic!() } },
323 cmp_op!($E,Str,"String::==","String",==), cmp_op!($E,Str,"String::!=","String",!=),
324 if_op!($E,"String::if","String"),
325 LitOpDesc { name: "String::len", arg_sorts: &["String"], ret_sort: "usize",
326 eval: |a| match a[0] { $E::Str(s) => $E::Usize(s.len()), _=>panic!() } },
327 LitOpDesc { name: "String::substr", arg_sorts: &["String","usize","usize"], ret_sort: "String",
328 eval: |a| match (a[0],a[1],a[2]) {
329 ($E::Str(s),$E::Usize(st),$E::Usize(ln)) => $E::Str(s.get(*st..(*st+*ln).min(s.len())).unwrap_or("").to_owned()),
330 _=>panic!(),
331 } },
332 LitOpDesc { name: "String::at", arg_sorts: &["String","usize"], ret_sort: "String",
333 eval: |a| match (a[0],a[1]) { ($E::Str(s),$E::Usize(i)) => $E::Str(s.get(*i..*i+1).unwrap_or("").to_owned()), _=>panic!() } },
334]};}
335
336fn parse_str(s: &str) -> Option<String> {
341 Some(s.strip_prefix('"')?.strip_suffix('"')?.to_owned())
342}
343fn parse_ibig(s: &str) -> Option<BigInt> {
344 s.parse().ok()
345}
346fn parse_ubig(s: &str) -> Option<BigUint> {
347 s.parse().ok()
348}
349fn parse_rbig(s: &str) -> Option<BigRational> {
350 let (n, d) = s.split_once('/')?;
351 let num: BigInt = n.parse().ok()?;
352 let den: BigInt = d.parse().ok()?;
353 if den.is_zero() {
354 return None;
355 }
356 Some(BigRational::new(num, den))
357}
358
359macro_rules! bignum_binop { ($E:ident,$V:ident,$n:expr,$s:expr,$op:tt) => {
364 LitOpDesc { name: $n, arg_sorts: &[$s,$s], ret_sort: $s,
365 eval: |a| match (a[0],a[1]) { ($E::$V(x),$E::$V(y)) => $E::$V(x $op y), _=>panic!() } }
366};}
367macro_rules! bignum_min {
368 ($E:ident,$V:ident,$n:expr,$s:expr) => {
369 LitOpDesc {
370 name: $n,
371 arg_sorts: &[$s, $s],
372 ret_sort: $s,
373 eval: |a| match (a[0], a[1]) {
374 ($E::$V(x), $E::$V(y)) => $E::$V(x.min(y).clone()),
375 _ => panic!(),
376 },
377 }
378 };
379}
380macro_rules! bignum_max {
381 ($E:ident,$V:ident,$n:expr,$s:expr) => {
382 LitOpDesc {
383 name: $n,
384 arg_sorts: &[$s, $s],
385 ret_sort: $s,
386 eval: |a| match (a[0], a[1]) {
387 ($E::$V(x), $E::$V(y)) => $E::$V(x.max(y).clone()),
388 _ => panic!(),
389 },
390 }
391 };
392}
393
394macro_rules! ibig_ops { ($E:ident) => { &[
395 bignum_binop!($E,IBig,"IBig::+","IBig",+), bignum_binop!($E,IBig,"IBig::-","IBig",-),
396 bignum_binop!($E,IBig,"IBig::*","IBig",*), bignum_binop!($E,IBig,"IBig::/","IBig",/),
397 bignum_binop!($E,IBig,"IBig::%","IBig",%),
398 LitOpDesc { name: "IBig::neg", arg_sorts: &["IBig"], ret_sort: "IBig",
399 eval: |a| match a[0] { $E::IBig(x) => $E::IBig(-x), _=>panic!() } },
400 LitOpDesc { name: "IBig::abs", arg_sorts: &["IBig"], ret_sort: "IBig",
401 eval: |a| match a[0] { $E::IBig(x) => $E::IBig(if *x<BigInt::zero() {-x} else {x.clone()}), _=>panic!() } },
402 bignum_min!($E,IBig,"IBig::min","IBig"), bignum_max!($E,IBig,"IBig::max","IBig"),
403 cmp_op!($E,IBig,"IBig::<","IBig",<), cmp_op!($E,IBig,"IBig::<=","IBig",<=),
404 cmp_op!($E,IBig,"IBig::>","IBig",>), cmp_op!($E,IBig,"IBig::>=","IBig",>=),
405 cmp_op!($E,IBig,"IBig::==","IBig",==), cmp_op!($E,IBig,"IBig::!=","IBig",!=),
406 if_op!($E,"IBig::if","IBig"),
407]};}
408macro_rules! ubig_ops { ($E:ident) => { &[
409 bignum_binop!($E,UBig,"UBig::+","UBig",+), bignum_binop!($E,UBig,"UBig::-","UBig",-),
410 bignum_binop!($E,UBig,"UBig::*","UBig",*), bignum_binop!($E,UBig,"UBig::/","UBig",/),
411 bignum_binop!($E,UBig,"UBig::%","UBig",%),
412 bignum_min!($E,UBig,"UBig::min","UBig"), bignum_max!($E,UBig,"UBig::max","UBig"),
413 cmp_op!($E,UBig,"UBig::<","UBig",<), cmp_op!($E,UBig,"UBig::<=","UBig",<=),
414 cmp_op!($E,UBig,"UBig::>","UBig",>), cmp_op!($E,UBig,"UBig::>=","UBig",>=),
415 cmp_op!($E,UBig,"UBig::==","UBig",==), cmp_op!($E,UBig,"UBig::!=","UBig",!=),
416 if_op!($E,"UBig::if","UBig"),
417]};}
418macro_rules! rbig_ops { ($E:ident) => { &[
419 bignum_binop!($E,RBig,"RBig::+","RBig",+), bignum_binop!($E,RBig,"RBig::-","RBig",-),
420 bignum_binop!($E,RBig,"RBig::*","RBig",*), bignum_binop!($E,RBig,"RBig::/","RBig",/),
421 LitOpDesc { name: "RBig::neg", arg_sorts: &["RBig"], ret_sort: "RBig",
422 eval: |a| match a[0] { $E::RBig(x) => $E::RBig(-x), _=>panic!() } },
423 LitOpDesc { name: "RBig::abs", arg_sorts: &["RBig"], ret_sort: "RBig",
424 eval: |a| match a[0] { $E::RBig(x) => { let v=x.clone(); $E::RBig(if v<BigRational::zero() {-v} else {v}) } _=>panic!() } },
425 bignum_min!($E,RBig,"RBig::min","RBig"), bignum_max!($E,RBig,"RBig::max","RBig"),
426 cmp_op!($E,RBig,"RBig::<","RBig",<), cmp_op!($E,RBig,"RBig::<=","RBig",<=),
427 cmp_op!($E,RBig,"RBig::>","RBig",>), cmp_op!($E,RBig,"RBig::>=","RBig",>=),
428 cmp_op!($E,RBig,"RBig::==","RBig",==), cmp_op!($E,RBig,"RBig::!=","RBig",!=),
429 if_op!($E,"RBig::if","RBig"),
430]};}
431
432pub struct MachineModel;
437const MACHINE_SORTS: &[LitSortDesc<MachineLit>] = &[
438 LitSortDesc {
439 name: "bool",
440 parse: |s| match s {
441 "true" => Some(MachineLit::Bool(true)),
442 "false" => Some(MachineLit::Bool(false)),
443 _ => None,
444 },
445 },
446 LitSortDesc {
447 name: "i64",
448 parse: |s| s.parse().ok().map(MachineLit::I64),
449 },
450 LitSortDesc {
451 name: "u64",
452 parse: |s| s.parse().ok().map(MachineLit::U64),
453 },
454 LitSortDesc {
455 name: "f64",
456 parse: |s| {
457 s.parse::<f64>()
458 .ok()
459 .map(|v| MachineLit::F64(OrderedFloat(v)))
460 },
461 },
462 LitSortDesc {
463 name: "usize",
464 parse: |s| s.parse().ok().map(MachineLit::Usize),
465 },
466 LitSortDesc {
467 name: "String",
468 parse: |s| parse_str(s).map(MachineLit::Str),
469 },
470];
471impl LitModel for MachineModel {
472 type Value = MachineLit;
473 fn sorts(&self) -> &[LitSortDesc<MachineLit>] {
474 MACHINE_SORTS
475 }
476 fn ops(&self) -> &[LitOpDesc<MachineLit>] {
477 use std::sync::LazyLock;
478 static OPS: LazyLock<Vec<LitOpDesc<MachineLit>>> = LazyLock::new(|| {
479 let mut v = Vec::new();
480 v.extend_from_slice(bool_ops!(MachineLit));
481 v.extend_from_slice(signed_int_ops!(MachineLit, I64, "i64"));
482 v.extend_from_slice(unsigned_int_ops!(MachineLit, U64, "u64"));
483 v.extend_from_slice(unsigned_int_ops!(MachineLit, Usize, "usize"));
484 v.extend_from_slice(f64_ops!(MachineLit));
485 v.extend_from_slice(string_ops!(MachineLit));
486 v
487 });
488 &OPS
489 }
490 fn sort_of(val: &MachineLit) -> &'static str {
491 val.sort_of()
492 }
493 fn is_truthy(val: &MachineLit) -> bool {
494 val.is_truthy()
495 }
496}
497
498pub struct BignumModel;
503const BIGNUM_SORTS: &[LitSortDesc<BignumLit>] = &[
504 LitSortDesc {
505 name: "bool",
506 parse: |s| match s {
507 "true" => Some(BignumLit::Bool(true)),
508 "false" => Some(BignumLit::Bool(false)),
509 _ => None,
510 },
511 },
512 LitSortDesc {
513 name: "IBig",
514 parse: |s| parse_ibig(s).map(BignumLit::IBig),
515 },
516 LitSortDesc {
517 name: "UBig",
518 parse: |s| parse_ubig(s).map(BignumLit::UBig),
519 },
520 LitSortDesc {
521 name: "RBig",
522 parse: |s| parse_rbig(s).map(BignumLit::RBig),
523 },
524];
525impl LitModel for BignumModel {
526 type Value = BignumLit;
527 fn sorts(&self) -> &[LitSortDesc<BignumLit>] {
528 BIGNUM_SORTS
529 }
530 fn ops(&self) -> &[LitOpDesc<BignumLit>] {
531 use std::sync::LazyLock;
532 static OPS: LazyLock<Vec<LitOpDesc<BignumLit>>> = LazyLock::new(|| {
533 let mut v = Vec::new();
534 v.extend_from_slice(bool_ops!(BignumLit));
535 v.extend_from_slice(ibig_ops!(BignumLit));
536 v.extend_from_slice(ubig_ops!(BignumLit));
537 v.extend_from_slice(rbig_ops!(BignumLit));
538 v
539 });
540 &OPS
541 }
542 fn sort_of(val: &BignumLit) -> &'static str {
543 val.sort_of()
544 }
545 fn is_truthy(val: &BignumLit) -> bool {
546 val.is_truthy()
547 }
548}
549
550pub struct AllModel;
555const ALL_SORTS: &[LitSortDesc<AllLit>] = &[
556 LitSortDesc {
557 name: "bool",
558 parse: |s| match s {
559 "true" => Some(AllLit::Bool(true)),
560 "false" => Some(AllLit::Bool(false)),
561 _ => None,
562 },
563 },
564 LitSortDesc {
565 name: "i64",
566 parse: |s| s.parse().ok().map(AllLit::I64),
567 },
568 LitSortDesc {
569 name: "u64",
570 parse: |s| s.parse().ok().map(AllLit::U64),
571 },
572 LitSortDesc {
573 name: "f64",
574 parse: |s| s.parse::<f64>().ok().map(|v| AllLit::F64(OrderedFloat(v))),
575 },
576 LitSortDesc {
577 name: "usize",
578 parse: |s| s.parse().ok().map(AllLit::Usize),
579 },
580 LitSortDesc {
581 name: "String",
582 parse: |s| parse_str(s).map(AllLit::Str),
583 },
584 LitSortDesc {
585 name: "IBig",
586 parse: |s| parse_ibig(s).map(AllLit::IBig),
587 },
588 LitSortDesc {
589 name: "UBig",
590 parse: |s| parse_ubig(s).map(AllLit::UBig),
591 },
592 LitSortDesc {
593 name: "RBig",
594 parse: |s| parse_rbig(s).map(AllLit::RBig),
595 },
596];
597impl LitModel for AllModel {
598 type Value = AllLit;
599 fn sorts(&self) -> &[LitSortDesc<AllLit>] {
600 ALL_SORTS
601 }
602 fn ops(&self) -> &[LitOpDesc<AllLit>] {
603 use std::sync::LazyLock;
604 static OPS: LazyLock<Vec<LitOpDesc<AllLit>>> = LazyLock::new(|| {
605 let mut v = Vec::new();
606 v.extend_from_slice(bool_ops!(AllLit));
607 v.extend_from_slice(signed_int_ops!(AllLit, I64, "i64"));
608 v.extend_from_slice(unsigned_int_ops!(AllLit, U64, "u64"));
609 v.extend_from_slice(unsigned_int_ops!(AllLit, Usize, "usize"));
610 v.extend_from_slice(f64_ops!(AllLit));
611 v.extend_from_slice(string_ops!(AllLit));
612 v.extend_from_slice(ibig_ops!(AllLit));
613 v.extend_from_slice(ubig_ops!(AllLit));
614 v.extend_from_slice(rbig_ops!(AllLit));
615 v.push(LitOpDesc {
621 name: "RBig::from_int",
622 arg_sorts: &["i64"],
623 ret_sort: "RBig",
624 eval: |a| match a[0] {
625 AllLit::I64(x) => AllLit::RBig(BigRational::from_integer((*x).into())),
626 _ => panic!(),
627 },
628 });
629 v.push(LitOpDesc {
630 name: "RBig::scale",
631 arg_sorts: &["RBig", "i64"],
632 ret_sort: "RBig",
633 eval: |a| match (a[0], a[1]) {
634 (AllLit::RBig(x), AllLit::I64(k)) => {
635 AllLit::RBig(x * BigRational::from_integer((*k).into()))
636 }
637 _ => panic!(),
638 },
639 });
640 v.push(LitOpDesc {
641 name: "RBig::pow",
642 arg_sorts: &["RBig", "i64"],
643 ret_sort: "RBig",
644 eval: |a| match (a[0], a[1]) {
645 (AllLit::RBig(x), AllLit::I64(e)) => {
646 let e = i32::try_from(*e).expect("RBig::pow exponent out of range");
647 assert!(e >= 0, "RBig::pow negative exponent");
648 AllLit::RBig(x.pow(e))
649 }
650 _ => panic!(),
651 },
652 });
653 v
654 });
655 &OPS
656 }
657 fn sort_of(val: &AllLit) -> &'static str {
658 val.sort_of()
659 }
660 fn is_truthy(val: &AllLit) -> bool {
661 val.is_truthy()
662 }
663}