1use std::cell::RefCell;
24use std::rc::Rc;
25
26use crate::ast::stmt::BinaryOpKind;
27use crate::interpreter::RuntimeValue;
28use crate::semantics::{arith, collections, compare};
29
30pub struct RuntimeRef<'a>(RuntimeRefInner<'a>);
42
43enum RuntimeRefInner<'a> {
44 Borrowed(&'a RuntimeValue),
45 #[cfg(feature = "narrow-value")]
46 Owned(RuntimeValue),
47}
48
49impl std::ops::Deref for RuntimeRef<'_> {
50 type Target = RuntimeValue;
51 #[inline]
52 fn deref(&self) -> &RuntimeValue {
53 match &self.0 {
54 RuntimeRefInner::Borrowed(r) => r,
55 #[cfg(feature = "narrow-value")]
56 RuntimeRefInner::Owned(v) => v,
57 }
58 }
59}
60
61impl AsRef<RuntimeValue> for RuntimeRef<'_> {
62 #[inline]
63 fn as_ref(&self) -> &RuntimeValue {
64 self
65 }
66}
67
68impl std::fmt::Debug for RuntimeRef<'_> {
69 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70 (**self).fmt(f)
71 }
72}
73
74#[cfg(not(feature = "narrow-value"))]
79mod repr {
80 use super::*;
81
82 #[derive(Clone, Debug)]
84 pub struct Value(pub(super) RuntimeValue);
85
86 impl Value {
87 #[inline]
88 pub fn int(n: i64) -> Self {
89 Value(RuntimeValue::Int(n))
90 }
91 #[inline]
92 pub fn float(f: f64) -> Self {
93 Value(RuntimeValue::Float(f))
94 }
95 #[inline]
96 pub fn bool(b: bool) -> Self {
97 Value(RuntimeValue::Bool(b))
98 }
99 #[inline]
100 pub fn nothing() -> Self {
101 Value(RuntimeValue::Nothing)
102 }
103 #[inline]
107 pub fn is_nothing(&self) -> bool {
108 matches!(&*self.as_runtime(), RuntimeValue::Nothing)
109 }
110
111 #[inline]
112 pub fn from_runtime(rv: RuntimeValue) -> Self {
113 Value(rv)
114 }
115 #[inline]
116 pub fn into_runtime(self) -> RuntimeValue {
117 self.0
118 }
119
120 #[inline]
123 pub fn as_runtime(&self) -> RuntimeRef<'_> {
124 RuntimeRef(RuntimeRefInner::Borrowed(&self.0))
125 }
126
127 #[inline]
133 pub fn as_runtime_ref(&self) -> Option<&RuntimeValue> {
134 Some(&self.0)
135 }
136
137 #[inline]
139 pub fn as_runtime_mut(&mut self) -> &mut RuntimeValue {
140 &mut self.0
141 }
142
143 #[inline]
144 pub fn as_int(&self) -> Option<i64> {
145 match &self.0 {
146 RuntimeValue::Int(n) => Some(*n),
147 _ => None,
148 }
149 }
150 #[inline]
151 pub fn as_bool(&self) -> Option<bool> {
152 match &self.0 {
153 RuntimeValue::Bool(b) => Some(*b),
154 _ => None,
155 }
156 }
157 #[inline]
158 pub fn as_float(&self) -> Option<f64> {
159 match &self.0 {
160 RuntimeValue::Float(f) => Some(*f),
161 _ => None,
162 }
163 }
164 #[inline]
165 pub fn is_int(&self) -> bool {
166 matches!(self.0, RuntimeValue::Int(_))
167 }
168
169 #[inline]
177 pub fn add(&self, rhs: &Value) -> Result<Value, String> {
178 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
179 if let Some(s) = a.checked_add(*b) {
180 return Ok(Value::int(s));
181 }
182 }
183 arith::add(self.0.clone(), rhs.0.clone()).map(Value)
184 }
185 #[inline]
186 pub fn sub(&self, rhs: &Value) -> Result<Value, String> {
187 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
188 if let Some(s) = a.checked_sub(*b) {
189 return Ok(Value::int(s));
190 }
191 }
192 arith::subtract(self.0.clone(), rhs.0.clone()).map(Value)
193 }
194 #[inline]
195 pub fn mul(&self, rhs: &Value) -> Result<Value, String> {
196 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
197 if let Some(p) = a.checked_mul(*b) {
198 return Ok(Value::int(p));
199 }
200 }
201 arith::multiply(self.0.clone(), rhs.0.clone()).map(Value)
202 }
203
204 #[inline]
207 pub fn lt(&self, rhs: &Value) -> Result<Value, String> {
208 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
209 return Ok(Value::bool(a < b));
210 }
211 compare::compare(BinaryOpKind::Lt, &self.0, &rhs.0).map(Value)
212 }
213 #[inline]
214 pub fn gt(&self, rhs: &Value) -> Result<Value, String> {
215 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
216 return Ok(Value::bool(a > b));
217 }
218 compare::compare(BinaryOpKind::Gt, &self.0, &rhs.0).map(Value)
219 }
220 #[inline]
221 pub fn lte(&self, rhs: &Value) -> Result<Value, String> {
222 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
223 return Ok(Value::bool(a <= b));
224 }
225 compare::compare(BinaryOpKind::LtEq, &self.0, &rhs.0).map(Value)
226 }
227 #[inline]
228 pub fn gte(&self, rhs: &Value) -> Result<Value, String> {
229 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
230 return Ok(Value::bool(a >= b));
231 }
232 compare::compare(BinaryOpKind::GtEq, &self.0, &rhs.0).map(Value)
233 }
234 #[inline]
235 pub fn eq_op(&self, rhs: &Value) -> Result<Value, String> {
236 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
237 return Ok(Value::bool(a == b));
238 }
239 compare::equality(&self.0, &rhs.0, false).map(Value)
240 }
241 #[inline]
242 pub fn neq_op(&self, rhs: &Value) -> Result<Value, String> {
243 if let (RuntimeValue::Int(a), RuntimeValue::Int(b)) = (&self.0, &rhs.0) {
244 return Ok(Value::bool(a != b));
245 }
246 compare::equality(&self.0, &rhs.0, true).map(Value)
247 }
248 }
249}
250
251#[cfg(feature = "narrow-value")]
256mod repr {
257 use super::*;
258 use crate::vm::nanbox::Narrow;
259
260 #[derive(Clone, Debug)]
262 pub struct Value(pub(super) Narrow);
263
264 impl Value {
265 #[inline]
266 pub fn int(n: i64) -> Self {
267 Value(Narrow::int(n))
268 }
269 #[inline]
270 pub fn float(f: f64) -> Self {
271 Value(Narrow::float(f))
272 }
273 #[inline]
274 pub fn bool(b: bool) -> Self {
275 Value(Narrow::bool(b))
276 }
277 #[inline]
278 pub fn nothing() -> Self {
279 Value(Narrow::nothing())
280 }
281 #[inline]
285 pub fn is_nothing(&self) -> bool {
286 matches!(&*self.as_runtime(), RuntimeValue::Nothing)
287 }
288
289 #[inline]
290 pub fn from_runtime(rv: RuntimeValue) -> Self {
291 Value(Narrow::from_runtime(rv))
292 }
293 #[inline]
294 pub fn into_runtime(self) -> RuntimeValue {
295 self.0.into_runtime()
296 }
297
298 #[inline]
302 pub fn as_runtime(&self) -> RuntimeRef<'_> {
303 match self.0.as_heap() {
304 Some(rv) => RuntimeRef(RuntimeRefInner::Borrowed(rv)),
305 None => RuntimeRef(RuntimeRefInner::Owned(self.0.to_runtime())),
306 }
307 }
308
309 #[inline]
317 pub fn as_runtime_ref(&self) -> Option<&RuntimeValue> {
318 self.0.as_heap()
319 }
320
321 #[inline]
326 pub fn as_runtime_mut(&mut self) -> &mut RuntimeValue {
327 self.0.make_heap_mut()
328 }
329
330 #[inline]
331 pub fn as_int(&self) -> Option<i64> {
332 self.0.as_int()
333 }
334 #[inline]
335 pub fn as_bool(&self) -> Option<bool> {
336 self.0.as_bool()
337 }
338 #[inline]
339 pub fn as_float(&self) -> Option<f64> {
340 self.0.as_float()
341 }
342 #[inline]
343 pub fn is_int(&self) -> bool {
344 self.0.as_int().is_some()
345 }
346
347 #[inline]
354 pub fn add(&self, rhs: &Value) -> Result<Value, String> {
355 if let Some(n) = self.0.int_add(&rhs.0) {
356 return Ok(Value(n));
357 }
358 arith::add(self.0.to_runtime(), rhs.0.to_runtime()).map(Value::from_runtime)
359 }
360 #[inline]
361 pub fn sub(&self, rhs: &Value) -> Result<Value, String> {
362 if let Some(n) = self.0.int_sub(&rhs.0) {
363 return Ok(Value(n));
364 }
365 arith::subtract(self.0.to_runtime(), rhs.0.to_runtime()).map(Value::from_runtime)
366 }
367 #[inline]
368 pub fn mul(&self, rhs: &Value) -> Result<Value, String> {
369 if let Some(n) = self.0.int_mul(&rhs.0) {
370 return Ok(Value(n));
371 }
372 arith::multiply(self.0.to_runtime(), rhs.0.to_runtime()).map(Value::from_runtime)
373 }
374
375 #[inline]
378 pub fn lt(&self, rhs: &Value) -> Result<Value, String> {
379 if let Some(b) = self.0.int_lt(&rhs.0) {
380 return Ok(Value::bool(b));
381 }
382 compare::compare(BinaryOpKind::Lt, &self.0.to_runtime(), &rhs.0.to_runtime())
383 .map(Value::from_runtime)
384 }
385 #[inline]
386 pub fn gt(&self, rhs: &Value) -> Result<Value, String> {
387 if let (Some(a), Some(b)) = (self.0.as_inline_int(), rhs.0.as_inline_int()) {
388 return Ok(Value::bool(a > b));
389 }
390 compare::compare(BinaryOpKind::Gt, &self.0.to_runtime(), &rhs.0.to_runtime())
391 .map(Value::from_runtime)
392 }
393 #[inline]
394 pub fn lte(&self, rhs: &Value) -> Result<Value, String> {
395 if let (Some(a), Some(b)) = (self.0.as_inline_int(), rhs.0.as_inline_int()) {
396 return Ok(Value::bool(a <= b));
397 }
398 compare::compare(
399 BinaryOpKind::LtEq,
400 &self.0.to_runtime(),
401 &rhs.0.to_runtime(),
402 )
403 .map(Value::from_runtime)
404 }
405 #[inline]
406 pub fn gte(&self, rhs: &Value) -> Result<Value, String> {
407 if let (Some(a), Some(b)) = (self.0.as_inline_int(), rhs.0.as_inline_int()) {
408 return Ok(Value::bool(a >= b));
409 }
410 compare::compare(
411 BinaryOpKind::GtEq,
412 &self.0.to_runtime(),
413 &rhs.0.to_runtime(),
414 )
415 .map(Value::from_runtime)
416 }
417 #[inline]
418 pub fn eq_op(&self, rhs: &Value) -> Result<Value, String> {
419 if let Some(b) = self.0.int_eq(&rhs.0) {
420 return Ok(Value::bool(b));
421 }
422 compare::equality(&self.0.to_runtime(), &rhs.0.to_runtime(), false)
423 .map(Value::from_runtime)
424 }
425 #[inline]
426 pub fn neq_op(&self, rhs: &Value) -> Result<Value, String> {
427 if let Some(b) = self.0.int_eq(&rhs.0) {
428 return Ok(Value::bool(!b));
429 }
430 compare::equality(&self.0.to_runtime(), &rhs.0.to_runtime(), true)
431 .map(Value::from_runtime)
432 }
433 }
434}
435
436pub use repr::Value;
437
438impl Value {
443 #[inline]
444 pub fn text(s: String) -> Self {
445 Value::from_runtime(RuntimeValue::Text(Rc::new(s)))
446 }
447
448 #[inline]
450 pub fn decided_truth(&self) -> Result<bool, String> {
451 if let Some(b) = self.as_bool() {
452 return Ok(b);
453 }
454 if let Some(n) = self.as_int() {
455 return Ok(n != 0);
456 }
457 self.as_runtime().decided_truth()
458 }
459
460 #[inline]
461 pub fn is_truthy(&self) -> bool {
462 if let Some(b) = self.as_bool() {
465 return b;
466 }
467 if let Some(n) = self.as_int() {
468 return n != 0;
469 }
470 self.as_runtime().is_truthy()
471 }
472 #[inline]
473 pub fn to_display_string(&self) -> String {
474 self.as_runtime().to_display_string()
475 }
476 #[inline]
481 pub fn type_name(&self) -> &str {
482 if self.as_bool().is_some() {
485 return "Bool";
486 }
487 if self.as_int().is_some() {
488 return "Int";
489 }
490 if self.as_float().is_some() {
491 return "Float";
492 }
493 match self.as_runtime_ref() {
494 Some(rv) => rv.type_name(),
495 None => "Nothing",
496 }
497 }
498
499 #[inline]
502 pub fn values_equal(&self, other: &Value) -> bool {
503 compare::values_equal(&self.as_runtime(), &other.as_runtime())
504 }
505
506 pub fn list(items: Vec<Value>) -> Self {
509 let values: Vec<RuntimeValue> = items.into_iter().map(Value::into_runtime).collect();
510 Value::from_runtime(RuntimeValue::List(Rc::new(RefCell::new(
511 crate::interpreter::ListRepr::from_values(values),
512 ))))
513 }
514 pub fn int_list(items: Vec<i64>) -> Self {
517 Value::from_runtime(RuntimeValue::List(Rc::new(RefCell::new(
518 crate::interpreter::ListRepr::Ints(items),
519 ))))
520 }
521
522 pub fn word32_list(items: Vec<i64>) -> Self {
525 if items.is_empty() {
526 return Value::empty_list();
527 }
528 Value::from_runtime(RuntimeValue::List(Rc::new(RefCell::new(
529 crate::interpreter::ListRepr::Words32(items),
530 ))))
531 }
532
533 pub fn empty_list() -> Self {
534 Value::from_runtime(RuntimeValue::List(Rc::new(RefCell::new(
535 crate::interpreter::ListRepr::Ints(Vec::new()),
536 ))))
537 }
538 pub fn empty_list_with_capacity(capacity: usize) -> Self {
541 Value::from_runtime(RuntimeValue::List(Rc::new(RefCell::new(
542 crate::interpreter::ListRepr::Ints(Vec::with_capacity(capacity)),
543 ))))
544 }
545 pub fn empty_list_i32() -> Self {
548 Value::from_runtime(RuntimeValue::List(Rc::new(RefCell::new(
549 crate::interpreter::ListRepr::IntsI32(Vec::new()),
550 ))))
551 }
552 pub fn empty_set() -> Self {
553 Value::from_runtime(RuntimeValue::Set(Rc::new(RefCell::new(Vec::new()))))
554 }
555 pub fn empty_map() -> Self {
556 Value::from_runtime(RuntimeValue::Map(Rc::new(RefCell::new(
557 crate::interpreter::MapStorage::default(),
558 ))))
559 }
560 pub fn empty_map_with_capacity(capacity: usize) -> Self {
564 let mut map = crate::interpreter::MapStorage::default();
565 let _ = map.try_reserve(capacity);
566 Value::from_runtime(RuntimeValue::Map(Rc::new(RefCell::new(map))))
567 }
568 pub fn int_range(lo: i64, hi: i64) -> Self {
569 Value::from_runtime(RuntimeValue::List(Rc::new(RefCell::new(
570 crate::interpreter::ListRepr::Ints((lo..=hi).collect()),
571 ))))
572 }
573
574 pub fn list_push(&self, value: Value) -> Result<(), String> {
575 collections::list_push(&self.as_runtime(), value.into_runtime())
576 }
577
578 pub fn len(&self) -> Result<i64, String> {
579 match collections::length_of(&self.as_runtime())? {
580 RuntimeValue::Int(n) => Ok(n),
581 _ => unreachable!("length_of always returns Int"),
582 }
583 }
584
585 pub fn index_get(&self, idx: &Value) -> Result<Value, String> {
586 collections::index_get(&self.as_runtime(), &idx.as_runtime()).map(Value::from_runtime)
587 }
588
589 pub fn set_add(&self, value: Value) -> Result<(), String> {
590 collections::set_add(&self.as_runtime(), value.into_runtime())
591 }
592
593 pub fn remove_from(&self, value: &Value) -> Result<(), String> {
594 collections::remove_from(&self.as_runtime(), &value.as_runtime())
595 }
596
597 pub fn index_set(&self, idx: &Value, value: Value) -> Result<(), String> {
598 collections::index_set(&self.as_runtime(), &idx.as_runtime(), value.into_runtime())
599 }
600
601 pub fn contains(&self, value: &Value) -> Result<bool, String> {
602 match collections::contains(&self.as_runtime(), &value.as_runtime())? {
603 RuntimeValue::Bool(b) => Ok(b),
604 _ => unreachable!("contains always returns Bool"),
605 }
606 }
607
608 pub fn div(&self, rhs: &Value) -> Result<Value, String> {
611 arith::divide(self.runtime_owned(), rhs.runtime_owned()).map(Value::from_runtime)
612 }
613
614 pub fn exact_div(&self, rhs: &Value) -> Result<Value, String> {
618 arith::exact_divide(self.runtime_owned(), rhs.runtime_owned()).map(Value::from_runtime)
619 }
620
621 pub fn floor_div(&self, rhs: &Value) -> Result<Value, String> {
625 arith::floor_divide(self.runtime_owned(), rhs.runtime_owned()).map(Value::from_runtime)
626 }
627
628 pub fn modulo(&self, rhs: &Value) -> Result<Value, String> {
629 arith::modulo(self.runtime_owned(), rhs.runtime_owned()).map(Value::from_runtime)
630 }
631
632 pub fn concat(&self, rhs: &Value) -> Result<Value, String> {
633 arith::concat(self.runtime_owned(), rhs.runtime_owned()).map(Value::from_runtime)
634 }
635
636 pub fn seq_concat(&self, rhs: &Value) -> Result<Value, String> {
638 arith::seq_concat(self.runtime_owned(), rhs.runtime_owned()).map(Value::from_runtime)
639 }
640
641 pub fn pow(&self, rhs: &Value) -> Result<Value, String> {
642 arith::binary_op(BinaryOpKind::Pow, self.runtime_owned(), rhs.runtime_owned())
643 .map(Value::from_runtime)
644 }
645
646 pub fn bitxor(&self, rhs: &Value) -> Result<Value, String> {
647 arith::binary_op(
648 BinaryOpKind::BitXor,
649 self.runtime_owned(),
650 rhs.runtime_owned(),
651 )
652 .map(Value::from_runtime)
653 }
654
655 pub fn bitand(&self, rhs: &Value) -> Result<Value, String> {
656 arith::binary_op(
657 BinaryOpKind::BitAnd,
658 self.runtime_owned(),
659 rhs.runtime_owned(),
660 )
661 .map(Value::from_runtime)
662 }
663
664 pub fn bitor(&self, rhs: &Value) -> Result<Value, String> {
665 arith::binary_op(
666 BinaryOpKind::BitOr,
667 self.runtime_owned(),
668 rhs.runtime_owned(),
669 )
670 .map(Value::from_runtime)
671 }
672
673 pub fn shl(&self, rhs: &Value) -> Result<Value, String> {
674 arith::binary_op(BinaryOpKind::Shl, self.runtime_owned(), rhs.runtime_owned())
675 .map(Value::from_runtime)
676 }
677
678 pub fn shr(&self, rhs: &Value) -> Result<Value, String> {
679 arith::binary_op(BinaryOpKind::Shr, self.runtime_owned(), rhs.runtime_owned())
680 .map(Value::from_runtime)
681 }
682
683 pub fn not_op(&self) -> Result<Value, String> {
685 arith::not_value(self.runtime_owned()).map(Value::from_runtime)
686 }
687
688 #[inline]
692 fn runtime_owned(&self) -> RuntimeValue {
693 self.as_runtime().clone()
694 }
695}
696
697#[cfg(test)]
698#[path = "../../../logicaffeine_tests/unit/logicaffeine_compile/vm/value/fast_path_kernel_differential.rs"]
699mod fast_path_kernel_differential;
700
701#[cfg(test)]
702#[path = "../../../logicaffeine_tests/unit/logicaffeine_compile/vm/value/value_comparison_tests.rs"]
703mod value_comparison_tests;