summaryrefslogtreecommitdiff
path: root/src/eval/ops.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-07-10 20:01:18 +0200
committerLaurenz <laurmaedje@gmail.com>2021-07-10 23:10:17 +0200
commit6a4823461f491aef63451f097ddfe5602e0b2157 (patch)
treead11b0ad169d030942d950573c729d50f7b3291b /src/eval/ops.rs
parent36b3067c19c8743032a44f888ee48702b88d135b (diff)
Reference-count complex values
Rename some nodes types
Diffstat (limited to 'src/eval/ops.rs')
-rw-r--r--src/eval/ops.rs37
1 files changed, 8 insertions, 29 deletions
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index 3b48140c..c1dd726b 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -1,7 +1,6 @@
use std::cmp::Ordering::*;
-use std::rc::Rc;
-use super::{TemplateNode, Value};
+use super::Value;
use Value::*;
/// Apply the plus operator to a value.
@@ -90,11 +89,6 @@ pub fn sub(lhs: Value, rhs: Value) -> Value {
/// Compute the product of two values.
pub fn mul(lhs: Value, rhs: Value) -> Value {
- fn repeat<T: Clone>(vec: Vec<T>, n: usize) -> Vec<T> {
- let len = n * vec.len();
- vec.into_iter().cycle().take(len).collect()
- }
-
match (lhs, rhs) {
(Int(a), Int(b)) => Int(a * b),
(Int(a), Float(b)) => Float(a as f64 * b),
@@ -128,8 +122,8 @@ pub fn mul(lhs: Value, rhs: Value) -> Value {
(Str(a), Int(b)) => Str(a.repeat(b.max(0) as usize)),
(Int(a), Str(b)) => Str(b.repeat(a.max(0) as usize)),
- (Array(a), Int(b)) => Array(repeat(a, b.max(0) as usize)),
- (Int(a), Array(b)) => Array(repeat(b, a.max(0) as usize)),
+ (Array(a), Int(b)) => Array(a.repeat(b.max(0) as usize)),
+ (Int(a), Array(b)) => Array(b.repeat(a.max(0) as usize)),
_ => Error,
}
@@ -240,26 +234,11 @@ pub fn join(lhs: Value, rhs: Value) -> Result<Value, Value> {
fn concat(lhs: Value, rhs: Value) -> Result<Value, Value> {
Ok(match (lhs, rhs) {
(Str(a), Str(b)) => Str(a + &b),
- (Array(mut a), Array(b)) => Array({
- a.extend(b);
- a
- }),
- (Dict(mut a), Dict(b)) => Dict({
- a.extend(b);
- a
- }),
- (Template(mut a), Template(b)) => Template({
- Rc::make_mut(&mut a).extend(b.iter().cloned());
- a
- }),
- (Template(mut a), Str(b)) => Template({
- Rc::make_mut(&mut a).push(TemplateNode::Str(b));
- a
- }),
- (Str(a), Template(mut b)) => Template({
- Rc::make_mut(&mut b).insert(0, TemplateNode::Str(a));
- b
- }),
+ (Array(a), Array(b)) => Array(a + &b),
+ (Dict(a), Dict(b)) => Dict(a + &b),
+ (Template(a), Template(b)) => Template(a + &b),
+ (Template(a), Str(b)) => Template(a + b),
+ (Str(a), Template(b)) => Template(a + b),
(a, _) => return Err(a),
})
}