1
0

Fix stack overflow; Clean up

This commit is contained in:
Jeff 2024-01-25 09:08:39 -05:00
parent 9a65afa083
commit 54790bc0db
5 changed files with 18 additions and 11 deletions

View File

@ -51,4 +51,3 @@ take_turn(cards 'Conservatory' 'Kitchen')
take_turn(cards 'White' 'Kitchen') take_turn(cards 'White' 'Kitchen')
take_turn(cards 'Green' 'Kitchen') take_turn(cards 'Green' 'Kitchen')
take_turn(cards 'Knife' 'Kitchen') take_turn(cards 'Knife' 'Kitchen')

View File

@ -2,7 +2,7 @@ fib = (i <int>) <int> {
if i <= 1 { if i <= 1 {
1 1
} else { } else {
fib(i - 1) + fib(i - 2) self(i - 1) + self(i - 2)
} }
} }

View File

@ -1,4 +1,7 @@
use std::fmt::{self, Display, Formatter}; use std::{
fmt::{self, Display, Formatter},
sync::Arc,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -65,7 +68,7 @@ impl FunctionNode {
pub fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result<Value> { pub fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result<Value> {
for (key, (value, r#type)) in outer_context.variables()?.iter() { for (key, (value, r#type)) in outer_context.variables()?.iter() {
if let Value::Function(Function::ContextDefined(function_node)) = value { if let Value::Function(Function::ContextDefined(function_node)) = value {
if self == function_node { if self == function_node.as_ref() {
continue; continue;
} }
} }
@ -154,7 +157,9 @@ impl AbstractTree for FunctionNode {
} }
fn run(&self, _source: &str, _context: &Map) -> Result<Value> { fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
Ok(Value::Function(Function::ContextDefined(self.clone()))) Ok(Value::Function(Function::ContextDefined(Arc::new(
self.clone(),
))))
} }
fn expected_type(&self, _context: &Map) -> Result<Type> { fn expected_type(&self, _context: &Map) -> Result<Type> {

View File

@ -1,4 +1,4 @@
use std::collections::BTreeMap; use std::{collections::BTreeMap, sync::Arc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -34,7 +34,7 @@ impl AbstractTree for ValueNode {
"function" => { "function" => {
let function_node = FunctionNode::from_syntax(child, source, context)?; let function_node = FunctionNode::from_syntax(child, source, context)?;
ValueNode::Function(Function::ContextDefined(function_node)) ValueNode::Function(Function::ContextDefined(Arc::new(function_node)))
} }
"integer" => ValueNode::Integer(source[child.byte_range()].to_string()), "integer" => ValueNode::Integer(source[child.byte_range()].to_string()),
"string" => { "string" => {

View File

@ -1,4 +1,7 @@
use std::fmt::{self, Display, Formatter}; use std::{
fmt::{self, Display, Formatter},
sync::Arc,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -7,7 +10,7 @@ use crate::{BuiltInFunction, Format, FunctionNode, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Function { pub enum Function {
BuiltIn(BuiltInFunction), BuiltIn(BuiltInFunction),
ContextDefined(FunctionNode), ContextDefined(Arc<FunctionNode>),
} }
impl Function { impl Function {
@ -19,12 +22,12 @@ impl Function {
Function::ContextDefined(function_node) => { Function::ContextDefined(function_node) => {
function_node.set( function_node.set(
"self".to_string(), "self".to_string(),
Value::Function(Function::ContextDefined(FunctionNode::new( Value::Function(Function::ContextDefined(Arc::new(FunctionNode::new(
function_node.parameters().clone(), function_node.parameters().clone(),
function_node.body().clone(), function_node.body().clone(),
function_node.r#type().clone(), function_node.r#type().clone(),
*function_node.syntax_position(), *function_node.syntax_position(),
))), )))),
)?; )?;
function_node.call(arguments, source, outer_context) function_node.call(arguments, source, outer_context)