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.
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]),
],
}
}

View File

@ -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<Value> {
todo!()
}
}

View File

@ -8,9 +8,10 @@ pub enum ValueType {
Any,
String,
Float,
Int,
Integer,
Boolean,
List(Vec<ValueType>),
List,
ListOf(Vec<ValueType>),
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::<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 {
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,

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