Reimplement functions
This commit is contained in:
parent
8188aa41a5
commit
ea633fbc59
@ -50,7 +50,7 @@ impl AbstractTree for Assignment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> {
|
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)?;
|
let mut value = self.statement.run(source, context)?;
|
||||||
|
|
||||||
match self.operator {
|
match self.operator {
|
||||||
|
@ -79,8 +79,8 @@ impl AbstractTree for FunctionCall {
|
|||||||
let mut function_context = context.clone();
|
let mut function_context = context.clone();
|
||||||
|
|
||||||
for (identifier, expression) in id_expr_pairs {
|
for (identifier, expression) in id_expr_pairs {
|
||||||
let key = identifier.run(source, context)?.to_string();
|
let key = identifier.inner().clone();
|
||||||
let value = expression.run(source, &mut function_context)?;
|
let value = expression.run(source, context)?;
|
||||||
|
|
||||||
function_context.set_value(key, value)?;
|
function_context.set_value(key, value)?;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
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)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub struct Identifier(String);
|
pub struct Identifier(String);
|
||||||
@ -28,7 +28,11 @@ impl AbstractTree for Identifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, _source: &str, context: &mut VariableMap) -> Result<Value> {
|
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)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,10 @@ use std::{collections::BTreeMap, ops::Range};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
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)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub struct ValueNode {
|
pub struct ValueNode {
|
||||||
@ -64,17 +67,36 @@ impl AbstractTree for ValueNode {
|
|||||||
Identifier::from_syntax_node(source, child_syntax_node)?.take_inner();
|
Identifier::from_syntax_node(source, child_syntax_node)?.take_inner();
|
||||||
}
|
}
|
||||||
|
|
||||||
if child_syntax_node.kind() == "value" {
|
if child_syntax_node.kind() == "expression" {
|
||||||
let child_value = ValueNode::from_syntax_node(source, child_syntax_node)?;
|
|
||||||
let key = current_key.clone();
|
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)
|
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 {
|
return Err(Error::UnexpectedSyntaxNode {
|
||||||
expected:
|
expected:
|
||||||
@ -129,7 +151,7 @@ impl AbstractTree for ValueNode {
|
|||||||
Value::Map(values)
|
Value::Map(values)
|
||||||
}
|
}
|
||||||
ValueType::Table => todo!(),
|
ValueType::Table => todo!(),
|
||||||
ValueType::Function => todo!(),
|
ValueType::Function(function) => Value::Function(function.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
|
@ -476,13 +476,13 @@ impl fmt::Display for Error {
|
|||||||
),
|
),
|
||||||
VariableIdentifierNotFound(identifier) => write!(
|
VariableIdentifierNotFound(identifier) => write!(
|
||||||
f,
|
f,
|
||||||
"Variable identifier is not bound to anything by context: {:?}.",
|
"Variable identifier is not bound to anything by context: {}.",
|
||||||
identifier
|
identifier
|
||||||
),
|
),
|
||||||
FunctionIdentifierNotFound(identifier) => write!(
|
FunctionIdentifierNotFound(identifier) => write!(
|
||||||
f,
|
f,
|
||||||
"Function identifier is not bound to anything by context: {:?}.",
|
"Function identifier is not bound to anything by context: {}.",
|
||||||
identifier
|
identifier.inner()
|
||||||
),
|
),
|
||||||
TypeError { expected, actual } => {
|
TypeError { expected, actual } => {
|
||||||
write!(
|
write!(
|
||||||
|
@ -5,7 +5,7 @@ use std::{
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{value_node::ValueNode, Expression, Value};
|
use crate::{value_node::ValueNode, Expression, Function, Value};
|
||||||
|
|
||||||
/// The type of a `Value`.
|
/// The type of a `Value`.
|
||||||
#[derive(Clone, Serialize, Deserialize, PartialOrd, Ord)]
|
#[derive(Clone, Serialize, Deserialize, PartialOrd, Ord)]
|
||||||
@ -17,9 +17,9 @@ pub enum ValueType {
|
|||||||
Boolean,
|
Boolean,
|
||||||
ListExact(Vec<Expression>),
|
ListExact(Vec<Expression>),
|
||||||
Empty,
|
Empty,
|
||||||
Map(BTreeMap<String, ValueNode>),
|
Map(BTreeMap<String, Expression>),
|
||||||
Table,
|
Table,
|
||||||
Function,
|
Function(Function),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Eq for ValueType {}
|
impl Eq for ValueType {}
|
||||||
@ -37,7 +37,7 @@ impl PartialEq for ValueType {
|
|||||||
(ValueType::Empty, ValueType::Empty) => true,
|
(ValueType::Empty, ValueType::Empty) => true,
|
||||||
(ValueType::Map(left), ValueType::Map(right)) => left == right,
|
(ValueType::Map(left), ValueType::Map(right)) => left == right,
|
||||||
(ValueType::Table, ValueType::Table) => true,
|
(ValueType::Table, ValueType::Table) => true,
|
||||||
(ValueType::Function, ValueType::Function) => true,
|
(ValueType::Function(left), ValueType::Function(right)) => left == right,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ impl Display for ValueType {
|
|||||||
ValueType::Empty => write!(f, "empty"),
|
ValueType::Empty => write!(f, "empty"),
|
||||||
ValueType::Map(_map) => write!(f, "map"),
|
ValueType::Map(_map) => write!(f, "map"),
|
||||||
ValueType::Table => write!(f, "table"),
|
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() {
|
for (key, value) in map.inner() {
|
||||||
let value_type = ValueType::from(value);
|
let value_type = ValueType::from(value);
|
||||||
let value_node = ValueNode::new(value_type, 0, 0);
|
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)
|
ValueType::Map(value_nodes)
|
||||||
}
|
}
|
||||||
Value::Table { .. } => ValueType::Table,
|
Value::Table(_) => ValueType::Table,
|
||||||
Value::Function(_) => ValueType::Function,
|
Value::Function(function) => ValueType::Function(function.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 532c751c36a1f8c33197b018dd6e947b73abbdc4
|
Subproject commit a207087ccafb71764b007db88580b35d5e37dae3
|
Loading…
Reference in New Issue
Block a user