From 094462cbdda15f19d2dc071b18201f656b8ddcd4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 12 Feb 2021 23:11:47 +0100 Subject: =?UTF-8?q?Make=20templates=20and=20strings=20summable=20?= =?UTF-8?q?=F0=9F=A5=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eval/ops.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/eval/ops.rs') diff --git a/src/eval/ops.rs b/src/eval/ops.rs index c52a62ca..c4adf587 100644 --- a/src/eval/ops.rs +++ b/src/eval/ops.rs @@ -30,6 +30,7 @@ pub fn neg(value: Value) -> Value { /// Compute the sum of two values. pub fn add(lhs: Value, rhs: Value) -> Value { match (lhs, rhs) { + // Math. (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), @@ -45,14 +46,23 @@ pub fn add(lhs: Value, rhs: Value) -> Value { (Linear(a), Relative(b)) => Linear(a + b), (Linear(a), Linear(b)) => Linear(a + b), + // Collections. (Str(a), Str(b)) => Str(a + &b), (Array(a), Array(b)) => Array(concat(a, b)), (Dict(a), Dict(b)) => Dict(concat(a, b)), - // TODO: Add string and template. + // Templates. (Template(a), Template(b)) => Template(concat(a, b)), (Template(a), None) => Template(a), (None, Template(b)) => Template(b), + (Template(mut a), Str(b)) => Template({ + a.push(TemplateNode::Str(b)); + a + }), + (Str(a), Template(mut b)) => Template({ + b.insert(0, TemplateNode::Str(a)); + b + }), _ => Error, } -- cgit v1.2.3