1
0

Reimplement functions

This commit is contained in:
Jeff 2023-10-10 17:12:38 -04:00
parent 8188aa41a5
commit ea633fbc59
7 changed files with 50 additions and 23 deletions

View File

@ -50,7 +50,7 @@ impl AbstractTree for Assignment {
}
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> {
let key = self.identifier.run(source, context)?.to_string();
let key = self.identifier.inner().clone();
let mut value = self.statement.run(source, context)?;
match self.operator {

View File

@ -79,8 +79,8 @@ impl AbstractTree for FunctionCall {
let mut function_context = context.clone();
for (identifier, expression) in id_expr_pairs {
let key = identifier.run(source, context)?.to_string();
let value = expression.run(source, &mut function_context)?;
let key = identifier.inner().clone();
let value = expression.run(source, context)?;
function_context.set_value(key, value)?;
}

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Result, Value, VariableMap};
use crate::{AbstractTree, Error, Result, Value, VariableMap};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Identifier(String);
@ -28,7 +28,11 @@ impl AbstractTree for Identifier {
}
fn run(&self, _source: &str, context: &mut VariableMap) -> Result<Value> {
let value = context.get_value(&self.0)?.unwrap_or_default();
let value = if let Some(value) = context.get_value(&self.0)? {
value
} else {
return Err(Error::VariableIdentifierNotFound(self.inner().clone()));
};
Ok(value)
}

View File

@ -3,7 +3,10 @@ use std::{collections::BTreeMap, ops::Range};
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Identifier, Result, Value, ValueType, VariableMap};
use crate::{
AbstractTree, Error, Expression, Function, Identifier, Item, Result, Value, ValueType,
VariableMap,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct ValueNode {
@ -64,17 +67,36 @@ impl AbstractTree for ValueNode {
Identifier::from_syntax_node(source, child_syntax_node)?.take_inner();
}
if child_syntax_node.kind() == "value" {
let child_value = ValueNode::from_syntax_node(source, child_syntax_node)?;
if child_syntax_node.kind() == "expression" {
let key = current_key.clone();
let expression = Expression::from_syntax_node(source, child_syntax_node)?;
child_nodes.insert(key, child_value);
child_nodes.insert(key, expression);
}
}
ValueType::Map(child_nodes)
}
"function" => ValueType::Function,
"function" => {
let mut identifiers = Vec::new();
let item_node = child.child(child.child_count() - 2).unwrap();
let item = Item::from_syntax_node(source, item_node)?;
for index in 1..child.child_count() - 3 {
let child_node = child.child(index).unwrap();
if child_node.kind() == "identifier" {
let identifier = Identifier::from_syntax_node(source, child_node)?;
identifiers.push(identifier);
}
}
let function = Function::new(identifiers, item);
ValueType::Function(function)
}
_ => {
return Err(Error::UnexpectedSyntaxNode {
expected:
@ -129,7 +151,7 @@ impl AbstractTree for ValueNode {
Value::Map(values)
}
ValueType::Table => todo!(),
ValueType::Function => todo!(),
ValueType::Function(function) => Value::Function(function.clone()),
};
Ok(value)

View File

@ -476,13 +476,13 @@ impl fmt::Display for Error {
),
VariableIdentifierNotFound(identifier) => write!(
f,
"Variable identifier is not bound to anything by context: {:?}.",
"Variable identifier is not bound to anything by context: {}.",
identifier
),
FunctionIdentifierNotFound(identifier) => write!(
f,
"Function identifier is not bound to anything by context: {:?}.",
identifier
"Function identifier is not bound to anything by context: {}.",
identifier.inner()
),
TypeError { expected, actual } => {
write!(

View File

@ -5,7 +5,7 @@ use std::{
use serde::{Deserialize, Serialize};
use crate::{value_node::ValueNode, Expression, Value};
use crate::{value_node::ValueNode, Expression, Function, Value};
/// The type of a `Value`.
#[derive(Clone, Serialize, Deserialize, PartialOrd, Ord)]
@ -17,9 +17,9 @@ pub enum ValueType {
Boolean,
ListExact(Vec<Expression>),
Empty,
Map(BTreeMap<String, ValueNode>),
Map(BTreeMap<String, Expression>),
Table,
Function,
Function(Function),
}
impl Eq for ValueType {}
@ -37,7 +37,7 @@ impl PartialEq for ValueType {
(ValueType::Empty, ValueType::Empty) => true,
(ValueType::Map(left), ValueType::Map(right)) => left == right,
(ValueType::Table, ValueType::Table) => true,
(ValueType::Function, ValueType::Function) => true,
(ValueType::Function(left), ValueType::Function(right)) => left == right,
_ => false,
}
}
@ -66,7 +66,7 @@ impl Display for ValueType {
ValueType::Empty => write!(f, "empty"),
ValueType::Map(_map) => write!(f, "map"),
ValueType::Table => write!(f, "table"),
ValueType::Function => write!(f, "function"),
ValueType::Function(function) => write!(f, "{function}"),
}
}
}
@ -99,14 +99,15 @@ impl From<&Value> for ValueType {
for (key, value) in map.inner() {
let value_type = ValueType::from(value);
let value_node = ValueNode::new(value_type, 0, 0);
let expression = Expression::Value(value_node);
value_nodes.insert(key.to_string(), value_node);
value_nodes.insert(key.to_string(), expression);
}
ValueType::Map(value_nodes)
}
Value::Table { .. } => ValueType::Table,
Value::Function(_) => ValueType::Function,
Value::Table(_) => ValueType::Table,
Value::Function(function) => ValueType::Function(function.clone()),
}
}
}

@ -1 +1 @@
Subproject commit 532c751c36a1f8c33197b018dd6e947b73abbdc4
Subproject commit a207087ccafb71764b007db88580b35d5e37dae3