Implement function calls
This commit is contained in:
parent
e82dd6736e
commit
fd9a4c04cb
@ -55,8 +55,8 @@ impl AbstractTree for FunctionCall {
|
||||
|
||||
let mut results = Vec::with_capacity(self.expressions.len());
|
||||
|
||||
for statement in definition.statements() {
|
||||
let result = statement.run(context)?;
|
||||
for item in definition.items() {
|
||||
let result = item.run(context)?;
|
||||
|
||||
results.push(result);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
//! Top-level unit of Dust code.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tree_sitter::Node;
|
||||
|
||||
use crate::{AbstractTree, Error, Result, Statement, Value, VariableMap};
|
||||
@ -9,7 +10,7 @@ use crate::{AbstractTree, Error, Result, Statement, Value, VariableMap};
|
||||
/// Items are either comments, which do nothing, or statements, which can be run
|
||||
/// to produce a single value or interact with a context by creating or
|
||||
/// referencing variables.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub enum Item {
|
||||
Comment(String),
|
||||
Statement(Statement),
|
||||
@ -17,6 +18,8 @@ pub enum Item {
|
||||
|
||||
impl AbstractTree for Item {
|
||||
fn from_syntax_node(node: Node, source: &str) -> Result<Self> {
|
||||
debug_assert_eq!("item", node.kind());
|
||||
|
||||
let child = node.child(0).unwrap();
|
||||
|
||||
if child.kind() == "comment" {
|
||||
|
@ -1,11 +1,11 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{AbstractTree, Expression, Statement};
|
||||
use crate::{AbstractTree, Expression, Item};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct While {
|
||||
expression: Expression,
|
||||
statement: Statement,
|
||||
item: Item,
|
||||
}
|
||||
|
||||
impl AbstractTree for While {
|
||||
@ -15,18 +15,15 @@ impl AbstractTree for While {
|
||||
let expression_node = node.child(1).unwrap();
|
||||
let expression = Expression::from_syntax_node(expression_node, source)?;
|
||||
|
||||
let statement_node = node.child(3).unwrap();
|
||||
let statement = Statement::from_syntax_node(statement_node, source)?;
|
||||
let item_node = node.child(3).unwrap();
|
||||
let item = Item::from_syntax_node(item_node, source)?;
|
||||
|
||||
Ok(While {
|
||||
expression,
|
||||
statement,
|
||||
})
|
||||
Ok(While { expression, item })
|
||||
}
|
||||
|
||||
fn run(&self, context: &mut crate::VariableMap) -> crate::Result<crate::Value> {
|
||||
while self.expression.run(context)?.as_boolean()? {
|
||||
self.statement.run(context)?;
|
||||
self.item.run(context)?;
|
||||
}
|
||||
|
||||
Ok(crate::Value::Empty)
|
||||
|
@ -272,8 +272,8 @@ mod tests {
|
||||
fn evaluate_function() {
|
||||
let function = Function::new(
|
||||
vec![Identifier::new("message".to_string())],
|
||||
vec![Statement::Expression(Expression::Identifier(
|
||||
Identifier::new("message".to_string()),
|
||||
vec![Item::Statement(Statement::Expression(
|
||||
Expression::Identifier(Identifier::new("message".to_string())),
|
||||
))],
|
||||
);
|
||||
|
||||
|
@ -2,28 +2,28 @@ use std::fmt::{self, Display, Formatter};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{Identifier, Statement};
|
||||
use crate::{Identifier, Item};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Function {
|
||||
identifiers: Vec<Identifier>,
|
||||
statements: Vec<Statement>,
|
||||
parameters: Vec<Identifier>,
|
||||
body: Vec<Item>,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub fn new(identifiers: Vec<Identifier>, statements: Vec<Statement>) -> Self {
|
||||
pub fn new(identifiers: Vec<Identifier>, items: Vec<Item>) -> Self {
|
||||
Function {
|
||||
identifiers,
|
||||
statements,
|
||||
parameters: identifiers,
|
||||
body: items,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn identifiers(&self) -> &Vec<Identifier> {
|
||||
&self.identifiers
|
||||
&self.parameters
|
||||
}
|
||||
|
||||
pub fn statements(&self) -> &Vec<Statement> {
|
||||
&self.statements
|
||||
pub fn items(&self) -> &Vec<Item> {
|
||||
&self.body
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ impl Display for Function {
|
||||
write!(
|
||||
f,
|
||||
"function < {:?} > {{ {:?} }}", // TODO: Correct this output
|
||||
self.identifiers, self.statements
|
||||
self.parameters, self.body
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Types that represent runtime values.
|
||||
use crate::{
|
||||
error::{Error, Result},
|
||||
AbstractTree, Function, Identifier, Statement, Table, ValueType, VariableMap,
|
||||
AbstractTree, Function, Identifier, Item, Table, ValueType, VariableMap,
|
||||
};
|
||||
|
||||
use json::JsonValue;
|
||||
@ -157,25 +157,27 @@ impl Value {
|
||||
"function" => {
|
||||
let child_count = child.child_count();
|
||||
let mut identifiers = Vec::new();
|
||||
let mut statements = Vec::new();
|
||||
let mut items = Vec::new();
|
||||
|
||||
for index in 0..child_count {
|
||||
let child = child.child(index).unwrap();
|
||||
|
||||
println!("{child:?}");
|
||||
|
||||
if child.kind() == "identifier" {
|
||||
let identifier = Identifier::from_syntax_node(child, source)?;
|
||||
|
||||
identifiers.push(identifier)
|
||||
}
|
||||
|
||||
if child.kind() == "statement" {
|
||||
let statement = Statement::from_syntax_node(child, source)?;
|
||||
if child.kind() == "item" {
|
||||
let item = Item::from_syntax_node(child, source)?;
|
||||
|
||||
statements.push(statement)
|
||||
items.push(item)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::Function(Function::new(identifiers, statements)))
|
||||
Ok(Value::Function(Function::new(identifiers, items)))
|
||||
}
|
||||
_ => Err(Error::UnexpectedSyntax {
|
||||
expected: "string, integer, float, boolean, list, table, map, function or empty",
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit b55420d51b8431ab9d60f46bf5be753bcf55d953
|
||||
Subproject commit d7ff4e57c58d9ff0708dc2902262384b99c4bc2c
|
Loading…
Reference in New Issue
Block a user