add noexec operator to pass named functions to functions

This commit is contained in:
2024-10-16 17:04:25 -04:00
parent d0661512c9
commit 329af77ff4
3 changed files with 23 additions and 2 deletions

View File

@@ -353,6 +353,24 @@ where
} }
} }
ParseTree::LambdaDefinition(func) => Ok(Value::Function(func)), ParseTree::LambdaDefinition(func) => Ok(Value::Function(func)),
ParseTree::NonCall(name) => {
let locals = locals.to_mut();
let func = locals.get(&name).ok_or(RuntimeError::FunctionUndefined(name.clone())).cloned()?;
match func {
Object::Function(func) => Ok(Value::Function(func.clone())),
Object::Variable(var) => match var {
Evaluation::Computed(value) => Ok(value.clone()),
Evaluation::Uncomputed(tree) => {
let v = self.exec(tree, &mut Cow::Borrowed(&locals))?;
locals.insert(name, Object::Variable(Evaluation::Computed(v.clone())));
Ok(v)
}
}
}
}
} }
} }
} }

View File

@@ -79,6 +79,7 @@ pub(crate) enum ParseTree {
FunctionCall(String, Vec<ParseTree>), FunctionCall(String, Vec<ParseTree>),
Variable(String), Variable(String),
Constant(Value), Constant(Value),
NonCall(String),
// Type Casts // Type Casts
IntCast(Box<ParseTree>), IntCast(Box<ParseTree>),
@@ -257,7 +258,10 @@ impl ParseTree {
Ok(ParseTree::LambdaDefinition(f)) Ok(ParseTree::LambdaDefinition(f))
} }
Op::NonCall => todo!(), Op::NonCall => {
let name = Self::get_identifier(tokens.next())?;
Ok(ParseTree::NonCall(name))
},
op => Err(ParseError::UnwantedToken(Token::Operator(op))), op => Err(ParseError::UnwantedToken(Token::Operator(op))),
} }
} }

View File

@@ -328,7 +328,6 @@ impl<R: BufRead> Tokenizer<R> {
return; return;
} }
}; };
println!("{n} + {count}");
Op::FunctionDefine(n + count) Op::FunctionDefine(n + count)
} }