From 1a82b85817646aded501051f4e9d651f7c0d4970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 30 Jan 2021 18:42:11 +0100 Subject: tuple values --- src/lib.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index bdd4ae4..248a589 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -314,6 +314,7 @@ a() <=> 4 pub enum Value { Blob(usize), BlobInstance(usize, Rc>>), + Tuple(Rc>), Float(f64), Int(i64), Bool(bool), @@ -378,6 +379,7 @@ impl Debug for Value { Value::ExternFunction(slot) => write!(fmt, "(extern fn {})", slot), Value::Unkown => write!(fmt, "(unkown)"), Value::Nil => write!(fmt, "(nil)"), + Value::Tuple(v) => write!(fmt, "({:?})", v), } } } @@ -393,18 +395,7 @@ impl Value { } fn as_type(&self) -> Type { - match self { - Value::BlobInstance(i, _) => Type::BlobInstance(*i), - Value::Blob(i) => Type::Blob(*i), - Value::Float(_) => Type::Float, - Value::Int(_) => Type::Int, - Value::Bool(_) => Type::Bool, - Value::String(_) => Type::String, - Value::Function(_, block) => block.borrow().ty.clone(), - Value::ExternFunction(_) => Type::Void, //TODO - Value::Unkown => Type::UnknownType, - Value::Nil => Type::Void, - } + Type::from(self) } } @@ -415,6 +406,7 @@ pub enum Op { Pop, PopUpvalue, Constant(Value), + Tuple(usize), Get(String), Set(String), @@ -582,6 +574,7 @@ pub enum Type { Float, Bool, String, + Tuple(Vec), Function(Vec, Box), Blob(usize), BlobInstance(usize), @@ -609,6 +602,9 @@ impl From<&Value> for Type { match value { Value::BlobInstance(i, _) => Type::BlobInstance(*i), Value::Blob(i) => Type::Blob(*i), + Value::Tuple(v) => { + Type::Tuple(v.iter().map(|x| Type::from(x)).collect()) + } Value::Int(_) => Type::Int, Value::Float(_) => Type::Float, Value::Bool(_) => Type::Bool, @@ -632,6 +628,9 @@ impl Type { Type::Void => Value::Nil, Type::Blob(i) => Value::Blob(*i), Type::BlobInstance(i) => Value::BlobInstance(*i, Rc::new(RefCell::new(Vec::new()))), + Type::Tuple(fields) => { + Value::Tuple(Rc::new(fields.iter().map(|x| x.as_value()).collect())) + } Type::UnknownType => Value::Unkown, Type::Int => Value::Int(1), Type::Float => Value::Float(1.0), -- cgit v1.2.1 From 4253c1c20013ab16564aa3ec34585dd1a358d182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 30 Jan 2021 22:34:19 +0100 Subject: add in tuples --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 248a589..9a9aaa7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -408,6 +408,7 @@ pub enum Op { Constant(Value), Tuple(usize), + Index, Get(String), Set(String), @@ -590,6 +591,9 @@ impl PartialEq for Type { (Type::Float, Type::Float) => true, (Type::Bool, Type::Bool) => true, (Type::String, Type::String) => true, + (Type::Tuple(a), Type::Tuple(b)) => { + a.iter().zip(b.iter()).all(|(a, b)| a == b) + } (Type::Function(a_args, a_ret), Type::Function(b_args, b_ret)) => a_args == b_args && a_ret == b_ret, _ => false, -- cgit v1.2.1 From bdcdbf7d65b7a05216c33f802f1f1646e1f68718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 20:25:06 +0100 Subject: basic arithmatic for tuples --- src/lib.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 9a9aaa7..c7841f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -384,6 +384,109 @@ impl Debug for Value { } } +mod op { + use super::Value; + use std::rc::Rc; + + fn tuple_op(a: &Rc>, b: &Rc>, f: fn (&Value, &Value) -> Value) -> Value { + Value::Tuple(Rc::new(a.iter().zip(b.iter()).map(|(a, b)| f(a, b)).collect())) + } + + pub fn neg(value: &Value) -> Value { + match value { + Value::Float(a) => Value::Float(-a), + Value::Int(a) => Value::Int(-a), + Value::Tuple(a) => Value::Tuple(Rc::new(a.iter().map(neg).collect())), + _ => Value::Nil, + } + } + + pub fn not(value: &Value) -> Value { + match value { + Value::Bool(a) => Value::Bool(!a), + Value::Tuple(a) => Value::Tuple(Rc::new(a.iter().map(not).collect())), + _ => Value::Nil, + } + } + + + pub fn add(a: &Value, b: &Value) -> Value { + match (a, b) { + (Value::Float(a), Value::Float(b)) => Value::Float(a + b), + (Value::Int(a), Value::Int(b)) => Value::Int(a + b), + (Value::String(a), Value::String(b)) => Value::String(Rc::from(format!("{}{}", a, b))), + (Value::Tuple(a), Value::Tuple(b)) if a.len() == b.len() => tuple_op(a, b, add), + _ => Value::Nil, + } + } + + pub fn sub(a: &Value, b: &Value) -> Value { + add(a, &neg(b)) + } + + pub fn mul(a: &Value, b: &Value) -> Value { + match (a, b) { + (Value::Float(a), Value::Float(b)) => Value::Float(a * b), + (Value::Int(a), Value::Int(b)) => Value::Int(a * b), + (Value::Tuple(a), Value::Tuple(b)) if a.len() == b.len() => tuple_op(a, b, mul), + _ => Value::Nil, + } + } + + pub fn div(a: &Value, b: &Value) -> Value { + match (a, b) { + (Value::Float(a), Value::Float(b)) => Value::Float(a / b), + (Value::Int(a), Value::Int(b)) => Value::Int(a / b), + (Value::Tuple(a), Value::Tuple(b)) if a.len() == b.len() => tuple_op(a, b, div), + _ => Value::Nil, + } + } + + pub fn eq(a: &Value, b: &Value) -> Value { + match (a, b) { + (Value::Float(a), Value::Float(b)) => Value::Bool(a == b), + (Value::Int(a), Value::Int(b)) => Value::Bool(a == b), + (Value::String(a), Value::String(b)) => Value::Bool(a == b), + (Value::Bool(a), Value::Bool(b)) => Value::Bool(a == b), + (Value::Tuple(a), Value::Tuple(b)) if a.len() == b.len() => tuple_op(a, b, eq), + _ => Value::Nil, + } + } + + pub fn less(a: &Value, b: &Value) -> Value { + match (a, b) { + (Value::Float(a), Value::Float(b)) => Value::Bool(a < b), + (Value::Int(a), Value::Int(b)) => Value::Bool(a < b), + (Value::String(a), Value::String(b)) => Value::Bool(a < b), + (Value::Bool(a), Value::Bool(b)) => Value::Bool(a < b), + (Value::Tuple(a), Value::Tuple(b)) if a.len() == b.len() => tuple_op(a, b, less), + _ => Value::Nil, + } + } + + pub fn greater(a: &Value, b: &Value) -> Value { + less(b, a) + } + + pub fn and(a: &Value, b: &Value) -> Value { + match (a, b) { + (Value::Bool(a), Value::Bool(b)) => Value::Bool(*a && *b), + (Value::Tuple(a), Value::Tuple(b)) if a.len() == b.len() => tuple_op(a, b, and), + _ => Value::Nil, + } + } + + pub fn or(a: &Value, b: &Value) -> Value { + match (a, b) { + (Value::Bool(a), Value::Bool(b)) => Value::Bool(*a || *b), + (Value::Tuple(a), Value::Tuple(b)) if a.len() == b.len() => tuple_op(a, b, or), + _ => Value::Nil, + } + } + +} + + impl Value { fn identity(self) -> Self { match self { @@ -394,6 +497,13 @@ impl Value { } } + fn is_nil(&self) -> bool { + match self { + Value::Nil => true, + _ => false, + } + } + fn as_type(&self) -> Type { Type::from(self) } -- cgit v1.2.1 From 0499cbf559fee79ec895dbbe30b9fed5962426ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 20:53:18 +0100 Subject: tuple tests --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index c7841f7..d07f2a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -306,6 +306,14 @@ a() <=> 4 5 <=> a.a + a.b" ); + test_multiple!(tuples, + add: "(1, 2, 3, 4) + (4, 3, 2, 1) <=> (5, 5, 5, 5)", + sub: "(1, -2, 3, -4) - (4, 3, -2, -1) <=> (-3, 1, 1, -5)", + mul: "(0, 1, 2) * (2, 3, 4) <=> (0, 3, 8)", + types: "a: (int, float, int) = (1, 1., 1)", + more_types: "a: (str, bool, int) = (\"abc\", true, 1)", + ); + test_file!(scoping, "tests/scoping.tdy"); test_file!(for_, "tests/for.tdy"); } -- cgit v1.2.1