From bc1bc91a33bcef567dbd7f846dbfac9d19a0994e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 16 May 2022 15:55:50 +0200 Subject: [PATCH] Allow adding `none` and anything --- src/eval/ops.rs | 6 +++--- tests/typ/code/dict.typ | 2 +- tests/typ/code/ops.typ | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/eval/ops.rs b/src/eval/ops.rs index 57390a65b..b3f2f3b47 100644 --- a/src/eval/ops.rs +++ b/src/eval/ops.rs @@ -61,6 +61,9 @@ pub fn neg(value: Value) -> StrResult { /// Compute the sum of two values. pub fn add(lhs: Value, rhs: Value) -> StrResult { Ok(match (lhs, rhs) { + (a, None) => a, + (None, b) => b, + (Int(a), Int(b)) => Int(a + b), (Int(a), Float(b)) => Float(a as f64 + b), (Float(a), Int(b)) => Float(a + b as f64), @@ -83,9 +86,6 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult { (Fraction(a), Fraction(b)) => Fraction(a + b), (Str(a), Str(b)) => Str(a + b), - - (Content(a), None) => Content(a), - (None, Content(b)) => Content(b), (Content(a), Content(b)) => Content(a + b), (Content(a), Str(b)) => Content(a + model::Content::Text(b)), (Str(a), Content(b)) => Content(model::Content::Text(a) + b), diff --git a/tests/typ/code/dict.typ b/tests/typ/code/dict.typ index 182f53d95..344d8efcf 100644 --- a/tests/typ/code/dict.typ +++ b/tests/typ/code/dict.typ @@ -39,8 +39,8 @@ // Missing lvalue is automatically none-initialized. { let dict = (:) - // Error: 3-17 cannot add none and integer dict("b") += 1 + test(dict, (b: 1)) } --- diff --git a/tests/typ/code/ops.typ b/tests/typ/code/ops.typ index 33a1a4a4f..012b8c870 100644 --- a/tests/typ/code/ops.typ +++ b/tests/typ/code/ops.typ @@ -27,6 +27,8 @@ // Addition. #test(2 + 4, 6) #test("a" + "b", "ab") +#test("a" + if false { "b" }, "a") +#test("a" + if true { "b" }, "ab") #test(13 * "a" + "bbbbbb", "aaaaaaaaaaaaabbbbbb") #test((1, 2) + (3, 4), (1, 2, 3, 4)) #test((a: 1) + (b: 2, c: 3), (a: 1, b: 2, c: 3))