From e97daafdcacd6476634680a975b11a59650fd3ef Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 22 Aug 2023 14:26:49 -0400 Subject: [PATCH] Implement get --- src/macros/collections.rs | 12 +++++++--- src/macros/logic.rs | 22 ++++------------- src/value/value_type.rs | 50 +++++++++++++++++++++++---------------- tests/collections.ds | 7 ++++++ tests/dust_tests.rs | 10 ++++++++ 5 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 tests/collections.ds create mode 100644 tests/dust_tests.rs diff --git a/src/macros/collections.rs b/src/macros/collections.rs index 9c36888..f68d85c 100644 --- a/src/macros/collections.rs +++ b/src/macros/collections.rs @@ -1,6 +1,6 @@ //! Macros for collection values: strings, lists, maps and tables. -use crate::{Error, Macro, MacroInfo, Result, Table, Value, VariableMap}; +use crate::{Error, Macro, MacroInfo, Result, Table, Value, ValueType, VariableMap}; pub struct Transform; @@ -10,7 +10,10 @@ impl Macro for Transform { identifier: "transform", description: "Change each value with a function.", group: "collections", - inputs: vec![], + inputs: vec![ValueType::ListOf(vec![ + ValueType::List, + ValueType::Function, + ])], } } @@ -177,7 +180,10 @@ impl Macro for Get { identifier: "get", description: "Retrieve a value from a collection.", group: "collections", - inputs: vec![], + inputs: vec![ + ValueType::ListOf(vec![ValueType::List, ValueType::Integer]), + ValueType::ListOf(vec![ValueType::Map, ValueType::String]), + ], } } diff --git a/src/macros/logic.rs b/src/macros/logic.rs index 2c1e7a7..4db05cd 100644 --- a/src/macros/logic.rs +++ b/src/macros/logic.rs @@ -36,7 +36,7 @@ impl Macro for AssertEqual { identifier: "assert_equal", description: "Panic if two values do not match.", group: "test", - inputs: vec![ValueType::List(vec![ValueType::Any, ValueType::Any])], + inputs: vec![ValueType::ListOf(vec![ValueType::Any, ValueType::Any])], } } @@ -61,11 +61,11 @@ impl Macro for If { description: "Evaluates the first argument. If true, it does the second argument.", group: "logic", inputs: vec![ - ValueType::List(vec![ + ValueType::ListOf(vec![ ValueType::Boolean, ValueType::Any, ]), - ValueType::List(vec![ + ValueType::ListOf(vec![ ValueType::Function, ValueType::Any, ])], @@ -99,12 +99,12 @@ impl Macro for IfElse { description: "Evaluates the first argument. If true, it does the second argument. If false, it does the third argument", group: "logic", inputs: vec![ - ValueType::List(vec![ + ValueType::ListOf(vec![ ValueType::Boolean, ValueType::Any, ValueType::Any, ]), - ValueType::List(vec![ + ValueType::ListOf(vec![ ValueType::Function, ValueType::Any, ValueType::Any, @@ -157,15 +157,3 @@ impl Macro for Loop { Loop.run(argument) } } - -pub struct While; - -impl Macro for While { - fn info(&self) -> MacroInfo<'static> { - todo!() - } - - fn run(&self, _argument: &Value) -> Result { - todo!() - } -} diff --git a/src/value/value_type.rs b/src/value/value_type.rs index 8223b84..ea2f4d4 100644 --- a/src/value/value_type.rs +++ b/src/value/value_type.rs @@ -8,9 +8,10 @@ pub enum ValueType { Any, String, Float, - Int, + Integer, Boolean, - List(Vec), + List, + ListOf(Vec), Empty, Map, Table, @@ -27,9 +28,12 @@ impl PartialEq for ValueType { (_, ValueType::Any) => true, (ValueType::String, ValueType::String) => true, (ValueType::Float, ValueType::Float) => true, - (ValueType::Int, ValueType::Int) => true, + (ValueType::Integer, ValueType::Integer) => true, (ValueType::Boolean, ValueType::Boolean) => true, - (ValueType::List(left), ValueType::List(right)) => left == right, + (ValueType::ListOf(left), ValueType::ListOf(right)) => left == right, + (ValueType::ListOf(_), ValueType::List) => true, + (ValueType::List, ValueType::ListOf(_)) => true, + (ValueType::List, ValueType::List) => true, (ValueType::Empty, ValueType::Empty) => true, (ValueType::Map, ValueType::Map) => true, (ValueType::Table, ValueType::Table) => true, @@ -42,21 +46,27 @@ impl PartialEq for ValueType { impl Display for ValueType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let text = match &self { - ValueType::Any => "any", - ValueType::String => "string", - ValueType::Float => "float", - ValueType::Int => "integer", - ValueType::Boolean => "boolean", - ValueType::List(_) => "list", - ValueType::Empty => "empty", - ValueType::Map => "map", - ValueType::Table => "table", - ValueType::Function => "function", - ValueType::Time => "time", - }; + match &self { + ValueType::Any => write!(f, "any"), + ValueType::String => write!(f, "string"), + ValueType::Float => write!(f, "float"), + ValueType::Integer => write!(f, "integer"), + ValueType::Boolean => write!(f, "boolean"), + ValueType::List => write!(f, "list"), + ValueType::ListOf(items) => { + let items = items + .iter() + .map(|value_type| value_type.to_string() + " ") + .collect::(); - write!(f, "{text}") + write!(f, "list of {items}") + } + ValueType::Empty => write!(f, "empty"), + ValueType::Map => write!(f, "map"), + ValueType::Table => write!(f, "table"), + ValueType::Function => write!(f, "function"), + ValueType::Time => write!(f, "time"), + } } } @@ -65,10 +75,10 @@ impl From<&Value> for ValueType { match value { Value::String(_) => ValueType::String, Value::Float(_) => ValueType::Float, - Value::Integer(_) => ValueType::Int, + Value::Integer(_) => ValueType::Integer, Value::Boolean(_) => ValueType::Boolean, Value::List(list) => { - ValueType::List(list.iter().map(|value| value.value_type()).collect()) + ValueType::ListOf(list.iter().map(|value| value.value_type()).collect()) } Value::Empty => ValueType::Empty, Value::Map(_) => ValueType::Map, diff --git a/tests/collections.ds b/tests/collections.ds new file mode 100644 index 0000000..8c53ca6 --- /dev/null +++ b/tests/collections.ds @@ -0,0 +1,7 @@ +list = (1, 2, 3); + +test = transform(list, 'input + 1'); +assert_equal((2, 3, 4), test); + +test = get(list, 0); +assert_equal(1, test); diff --git a/tests/dust_tests.rs b/tests/dust_tests.rs new file mode 100644 index 0000000..664dc83 --- /dev/null +++ b/tests/dust_tests.rs @@ -0,0 +1,10 @@ +use std::fs::read_to_string; + +use whale_lib::*; + +#[test] +fn collections() { + let file_contents = read_to_string("tests/collections.ds").unwrap(); + + eval(&file_contents).unwrap(); +}