2023-10-06 17:32:58 +00:00
|
|
|
//! The top level of Dust's API with functions to interpret Dust code.
|
|
|
|
//!
|
|
|
|
//! You can use this library externally by calling either of the "eval"
|
|
|
|
//! functions or by constructing your own Evaluator.
|
|
|
|
use std::fmt::{self, Debug, Formatter};
|
|
|
|
|
|
|
|
use tree_sitter::{Parser, Tree as TSTree};
|
|
|
|
|
|
|
|
use crate::{abstract_tree::item::Item, language, AbstractTree, Result, Value, VariableMap};
|
|
|
|
|
|
|
|
/// Evaluate the given source code.
|
|
|
|
///
|
|
|
|
/// Returns a vector of results from evaluating the source code. Each comment
|
|
|
|
/// and statemtent will have its own result.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use dust::*;
|
|
|
|
/// assert_eq!(evaluate("1 + 2 + 3"), vec![Ok(Value::Integer(6))]);
|
|
|
|
/// ```
|
2023-10-09 19:54:47 +00:00
|
|
|
pub fn evaluate(source: &str) -> Result<Value> {
|
2023-10-06 17:32:58 +00:00
|
|
|
let mut context = VariableMap::new();
|
|
|
|
|
|
|
|
evaluate_with_context(source, &mut context)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate the given source code with the given context.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use dust::*;
|
|
|
|
/// let mut context = VariableMap::new();
|
|
|
|
///
|
|
|
|
/// context.set_value("one".into(), 1.into());
|
|
|
|
/// context.set_value("two".into(), 2.into());
|
|
|
|
/// context.set_value("three".into(), 3.into());
|
|
|
|
///
|
|
|
|
/// let dust_code = "four = 4 one + two + three + four";
|
|
|
|
///
|
|
|
|
/// assert_eq!(
|
|
|
|
/// evaluate_with_context(dust_code, &mut context),
|
|
|
|
/// vec![Ok(Value::Empty), Ok(Value::Integer(10))]
|
|
|
|
/// );
|
|
|
|
/// ```
|
2023-10-09 19:54:47 +00:00
|
|
|
pub fn evaluate_with_context(source: &str, context: &mut VariableMap) -> Result<Value> {
|
2023-10-06 17:32:58 +00:00
|
|
|
let mut parser = Parser::new();
|
|
|
|
parser.set_language(language()).unwrap();
|
|
|
|
|
|
|
|
Evaluator::new(parser, context, source).run()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A collection of statements and comments interpreted from a syntax tree.
|
|
|
|
///
|
|
|
|
/// The Evaluator turns a tree sitter concrete syntax tree into a vector of
|
|
|
|
/// abstract trees called [Item][]s that can be run to execute the source code.
|
|
|
|
pub struct Evaluator<'context, 'code> {
|
|
|
|
_parser: Parser,
|
|
|
|
context: &'context mut VariableMap,
|
|
|
|
source: &'code str,
|
|
|
|
syntax_tree: TSTree,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Evaluator<'_, '_> {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "Evaluator context: {}", self.context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'context, 'code> Evaluator<'context, 'code> {
|
|
|
|
fn new(mut parser: Parser, context: &'context mut VariableMap, source: &'code str) -> Self {
|
|
|
|
let syntax_tree = parser.parse(source, None).unwrap();
|
|
|
|
|
|
|
|
Evaluator {
|
|
|
|
_parser: parser,
|
|
|
|
context,
|
|
|
|
source,
|
|
|
|
syntax_tree,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
fn run(self) -> Result<Value> {
|
2023-10-06 17:32:58 +00:00
|
|
|
let mut cursor = self.syntax_tree.walk();
|
|
|
|
let root_node = cursor.node();
|
2023-10-09 19:54:47 +00:00
|
|
|
let mut prev_result = Ok(Value::Empty);
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-10-09 21:01:30 +00:00
|
|
|
println!("{}", root_node.to_sexp());
|
|
|
|
|
2023-10-06 17:32:58 +00:00
|
|
|
for item_node in root_node.children(&mut cursor) {
|
2023-10-10 17:29:11 +00:00
|
|
|
let item = Item::from_syntax_node(self.source, item_node)?;
|
|
|
|
prev_result = item.run(self.source, self.context);
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
prev_result
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-10-10 17:29:11 +00:00
|
|
|
use crate::Table;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_empty() {
|
2023-10-09 19:54:47 +00:00
|
|
|
assert_eq!(evaluate("x = 9"), Ok(Value::Empty));
|
|
|
|
assert_eq!(evaluate("x = 1 + 1"), Ok(Value::Empty));
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_integer() {
|
2023-10-09 19:54:47 +00:00
|
|
|
assert_eq!(evaluate("1"), Ok(Value::Integer(1)));
|
|
|
|
assert_eq!(evaluate("123"), Ok(Value::Integer(123)));
|
|
|
|
assert_eq!(evaluate("-666"), Ok(Value::Integer(-666)));
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_float() {
|
2023-10-09 19:54:47 +00:00
|
|
|
assert_eq!(evaluate("0.1"), Ok(Value::Float(0.1)));
|
|
|
|
assert_eq!(evaluate("12.3"), Ok(Value::Float(12.3)));
|
|
|
|
assert_eq!(evaluate("-6.66"), Ok(Value::Float(-6.66)));
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_string() {
|
2023-10-09 19:54:47 +00:00
|
|
|
assert_eq!(evaluate("\"one\""), Ok(Value::String("one".to_string())));
|
|
|
|
assert_eq!(evaluate("'one'"), Ok(Value::String("one".to_string())));
|
|
|
|
assert_eq!(evaluate("`one`"), Ok(Value::String("one".to_string())));
|
|
|
|
assert_eq!(evaluate("`'one'`"), Ok(Value::String("'one'".to_string())));
|
|
|
|
assert_eq!(evaluate("'`one`'"), Ok(Value::String("`one`".to_string())));
|
2023-10-06 17:32:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
evaluate("\"'one'\""),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::String("'one'".to_string()))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_list() {
|
|
|
|
assert_eq!(
|
|
|
|
evaluate("[1, 2, 'foobar']"),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::List(vec![
|
2023-10-06 17:32:58 +00:00
|
|
|
Value::Integer(1),
|
|
|
|
Value::Integer(2),
|
|
|
|
Value::String("foobar".to_string()),
|
2023-10-09 19:54:47 +00:00
|
|
|
]))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_map() {
|
|
|
|
let mut map = VariableMap::new();
|
|
|
|
|
|
|
|
map.set_value("x".to_string(), Value::Integer(1)).unwrap();
|
|
|
|
map.set_value("foo".to_string(), Value::String("bar".to_string()))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
assert_eq!(evaluate("{ x = 1 foo = 'bar' }"), Ok(Value::Map(map)));
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_table() {
|
|
|
|
let mut table = Table::new(vec!["messages".to_string(), "numbers".to_string()]);
|
|
|
|
|
|
|
|
table
|
|
|
|
.insert(vec![Value::String("hiya".to_string()), Value::Integer(42)])
|
|
|
|
.unwrap();
|
|
|
|
table
|
|
|
|
.insert(vec![Value::String("foo".to_string()), Value::Integer(57)])
|
|
|
|
.unwrap();
|
|
|
|
table
|
|
|
|
.insert(vec![Value::String("bar".to_string()), Value::Float(99.99)])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
evaluate(
|
|
|
|
"
|
2023-10-18 23:26:49 +00:00
|
|
|
table <messages, numbers> [
|
2023-10-06 17:32:58 +00:00
|
|
|
['hiya', 42]
|
|
|
|
['foo', 57]
|
|
|
|
['bar', 99.99]
|
2023-10-18 23:26:49 +00:00
|
|
|
]
|
2023-10-06 17:32:58 +00:00
|
|
|
"
|
|
|
|
),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::Table(table))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-10-09 21:01:30 +00:00
|
|
|
fn evaluate_if() {
|
2023-10-06 17:32:58 +00:00
|
|
|
assert_eq!(
|
2023-10-09 21:01:30 +00:00
|
|
|
evaluate("if true { 'true' }"),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::String("true".to_string()))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-10-09 21:01:30 +00:00
|
|
|
fn evaluate_if_else() {
|
|
|
|
assert_eq!(evaluate("if false { 1 } else { 2 }"), Ok(Value::Integer(2)));
|
2023-10-06 17:32:58 +00:00
|
|
|
assert_eq!(
|
2023-10-09 21:01:30 +00:00
|
|
|
evaluate("if true { 1.0 } else { 42.0 }"),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::Float(1.0))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_if_else_else_if_else() {
|
|
|
|
assert_eq!(
|
|
|
|
evaluate(
|
|
|
|
"
|
2023-10-09 21:01:30 +00:00
|
|
|
if false {
|
|
|
|
'no'
|
|
|
|
} else if 1 + 1 == 3 {
|
|
|
|
'nope'
|
|
|
|
} else {
|
2023-10-06 17:32:58 +00:00
|
|
|
'ok'
|
2023-10-09 21:01:30 +00:00
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
"
|
|
|
|
),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::String("ok".to_string()))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_if_else_else_if_else_if_else_if_else() {
|
|
|
|
assert_eq!(
|
|
|
|
evaluate(
|
|
|
|
"
|
2023-10-09 21:01:30 +00:00
|
|
|
if false {
|
|
|
|
'no'
|
|
|
|
} else if 1 + 1 == 1 {
|
|
|
|
'nope'
|
|
|
|
} else if 9 / 2 == 4 {
|
|
|
|
'nope'
|
|
|
|
} else if 'foo' == 'bar' {
|
|
|
|
'nope'
|
|
|
|
} else {
|
|
|
|
'ok'
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
"
|
|
|
|
),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::String("ok".to_string()))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_function_call() {
|
|
|
|
let mut context = VariableMap::new();
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-10-06 21:11:50 +00:00
|
|
|
evaluate_with_context(
|
|
|
|
"
|
|
|
|
foobar = function <message> { message }
|
|
|
|
(foobar 'Hiya')
|
|
|
|
",
|
|
|
|
&mut context
|
|
|
|
),
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(Value::String("Hiya".to_string()))
|
2023-10-06 17:32:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_tool_call() {
|
2023-10-09 19:54:47 +00:00
|
|
|
assert_eq!(evaluate("(output 'Hiya')"), Ok(Value::Empty));
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|