1
0

Implement get

This commit is contained in:
Jeff 2023-08-22 14:26:49 -04:00
parent c698c847c7
commit e97daafdca
5 changed files with 61 additions and 40 deletions

View File

@ -1,6 +1,6 @@
//! Macros for collection values: strings, lists, maps and tables. //! 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; pub struct Transform;
@ -10,7 +10,10 @@ impl Macro for Transform {
identifier: "transform", identifier: "transform",
description: "Change each value with a function.", description: "Change each value with a function.",
group: "collections", group: "collections",
inputs: vec![], inputs: vec![ValueType::ListOf(vec![
ValueType::List,
ValueType::Function,
])],
} }
} }
@ -177,7 +180,10 @@ impl Macro for Get {
identifier: "get", identifier: "get",
description: "Retrieve a value from a collection.", description: "Retrieve a value from a collection.",
group: "collections", group: "collections",
inputs: vec![], inputs: vec![
ValueType::ListOf(vec![ValueType::List, ValueType::Integer]),
ValueType::ListOf(vec![ValueType::Map, ValueType::String]),
],
} }
} }

View File

@ -36,7 +36,7 @@ impl Macro for AssertEqual {
identifier: "assert_equal", identifier: "assert_equal",
description: "Panic if two values do not match.", description: "Panic if two values do not match.",
group: "test", 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.", description: "Evaluates the first argument. If true, it does the second argument.",
group: "logic", group: "logic",
inputs: vec![ inputs: vec![
ValueType::List(vec![ ValueType::ListOf(vec![
ValueType::Boolean, ValueType::Boolean,
ValueType::Any, ValueType::Any,
]), ]),
ValueType::List(vec![ ValueType::ListOf(vec![
ValueType::Function, ValueType::Function,
ValueType::Any, 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", description: "Evaluates the first argument. If true, it does the second argument. If false, it does the third argument",
group: "logic", group: "logic",
inputs: vec![ inputs: vec![
ValueType::List(vec![ ValueType::ListOf(vec![
ValueType::Boolean, ValueType::Boolean,
ValueType::Any, ValueType::Any,
ValueType::Any, ValueType::Any,
]), ]),
ValueType::List(vec![ ValueType::ListOf(vec![
ValueType::Function, ValueType::Function,
ValueType::Any, ValueType::Any,
ValueType::Any, ValueType::Any,
@ -157,15 +157,3 @@ impl Macro for Loop {
Loop.run(argument) Loop.run(argument)
} }
} }
pub struct While;
impl Macro for While {
fn info(&self) -> MacroInfo<'static> {
todo!()
}
fn run(&self, _argument: &Value) -> Result<Value> {
todo!()
}
}

View File

@ -8,9 +8,10 @@ pub enum ValueType {
Any, Any,
String, String,
Float, Float,
Int, Integer,
Boolean, Boolean,
List(Vec<ValueType>), List,
ListOf(Vec<ValueType>),
Empty, Empty,
Map, Map,
Table, Table,
@ -27,9 +28,12 @@ impl PartialEq for ValueType {
(_, ValueType::Any) => true, (_, ValueType::Any) => true,
(ValueType::String, ValueType::String) => true, (ValueType::String, ValueType::String) => true,
(ValueType::Float, ValueType::Float) => true, (ValueType::Float, ValueType::Float) => true,
(ValueType::Int, ValueType::Int) => true, (ValueType::Integer, ValueType::Integer) => true,
(ValueType::Boolean, ValueType::Boolean) => 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::Empty, ValueType::Empty) => true,
(ValueType::Map, ValueType::Map) => true, (ValueType::Map, ValueType::Map) => true,
(ValueType::Table, ValueType::Table) => true, (ValueType::Table, ValueType::Table) => true,
@ -42,21 +46,27 @@ impl PartialEq for ValueType {
impl Display for ValueType { impl Display for ValueType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let text = match &self { match &self {
ValueType::Any => "any", ValueType::Any => write!(f, "any"),
ValueType::String => "string", ValueType::String => write!(f, "string"),
ValueType::Float => "float", ValueType::Float => write!(f, "float"),
ValueType::Int => "integer", ValueType::Integer => write!(f, "integer"),
ValueType::Boolean => "boolean", ValueType::Boolean => write!(f, "boolean"),
ValueType::List(_) => "list", ValueType::List => write!(f, "list"),
ValueType::Empty => "empty", ValueType::ListOf(items) => {
ValueType::Map => "map", let items = items
ValueType::Table => "table", .iter()
ValueType::Function => "function", .map(|value_type| value_type.to_string() + " ")
ValueType::Time => "time", .collect::<String>();
};
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 { match value {
Value::String(_) => ValueType::String, Value::String(_) => ValueType::String,
Value::Float(_) => ValueType::Float, Value::Float(_) => ValueType::Float,
Value::Integer(_) => ValueType::Int, Value::Integer(_) => ValueType::Integer,
Value::Boolean(_) => ValueType::Boolean, Value::Boolean(_) => ValueType::Boolean,
Value::List(list) => { 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::Empty => ValueType::Empty,
Value::Map(_) => ValueType::Map, Value::Map(_) => ValueType::Map,

7
tests/collections.ds Normal file
View File

@ -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);

10
tests/dust_tests.rs Normal file
View File

@ -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();
}