summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/eval/ops.rs8
-rw-r--r--tests/typ/compiler/ops.typ5
2 files changed, 9 insertions, 4 deletions
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index 43ee6ceb..48e74c8f 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -53,7 +53,7 @@ pub fn pos(value: Value) -> StrResult<Value> {
/// Compute the negation of a value.
pub fn neg(value: Value) -> StrResult<Value> {
Ok(match value {
- Int(v) => Int(-v),
+ Int(v) => Int(v.checked_neg().ok_or("value is too large")?),
Float(v) => Float(-v),
Length(v) => Length(-v),
Angle(v) => Angle(-v),
@@ -70,7 +70,7 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
(a, None) => a,
(None, b) => b,
- (Int(a), Int(b)) => Int(a + b),
+ (Int(a), Int(b)) => Int(a.checked_add(b).ok_or("value is too large")?),
(Int(a), Float(b)) => Float(a as f64 + b),
(Float(a), Int(b)) => Float(a + b as f64),
(Float(a), Float(b)) => Float(a + b),
@@ -137,7 +137,7 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
/// Compute the difference of two values.
pub fn sub(lhs: Value, rhs: Value) -> StrResult<Value> {
Ok(match (lhs, rhs) {
- (Int(a), Int(b)) => Int(a - b),
+ (Int(a), Int(b)) => Int(a.checked_sub(b).ok_or("value is too large")?),
(Int(a), Float(b)) => Float(a as f64 - b),
(Float(a), Int(b)) => Float(a - b as f64),
(Float(a), Float(b)) => Float(a - b),
@@ -165,7 +165,7 @@ pub fn sub(lhs: Value, rhs: Value) -> StrResult<Value> {
/// Compute the product of two values.
pub fn mul(lhs: Value, rhs: Value) -> StrResult<Value> {
Ok(match (lhs, rhs) {
- (Int(a), Int(b)) => Int(a * b),
+ (Int(a), Int(b)) => Int(a.checked_mul(b).ok_or("value is too large")?),
(Int(a), Float(b)) => Float(a as f64 * b),
(Float(a), Int(b)) => Float(a * b as f64),
(Float(a), Float(b)) => Float(a * b),
diff --git a/tests/typ/compiler/ops.typ b/tests/typ/compiler/ops.typ
index 2bb06e4d..937e9024 100644
--- a/tests/typ/compiler/ops.typ
+++ b/tests/typ/compiler/ops.typ
@@ -33,6 +33,11 @@
#test((1, 2) + (3, 4), (1, 2, 3, 4))
#test((a: 1) + (b: 2, c: 3), (a: 1, b: 2, c: 3))
+---
+// Error: 3-26 value is too large
+#(9223372036854775807 + 1)
+
+---
// Subtraction.
#test(1-4, 3*-1)
#test(4cm - 2cm, 2cm)