Function calling works kind of
This commit is contained in:
parent
fdbb21990d
commit
b1966d7199
@ -18,6 +18,7 @@ pub enum Stmt {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
|
Unit,
|
||||||
Lit(Lit),
|
Lit(Lit),
|
||||||
Func(Func),
|
Func(Func),
|
||||||
Val(Rc<String>),
|
Val(Rc<String>),
|
||||||
|
@ -404,8 +404,21 @@ impl<'a> State<'a> {
|
|||||||
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Expr> {
|
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Expr> {
|
||||||
match f {
|
match f {
|
||||||
Func::BuiltIn(sigil) => self.apply_builtin(sigil, args),
|
Func::BuiltIn(sigil) => self.apply_builtin(sigil, args),
|
||||||
Func::UserDefined { params, body, .. } => {
|
Func::UserDefined { params, body, name } => {
|
||||||
Err(format!("Function application not done yet"))
|
|
||||||
|
if params.len() != args.len() {
|
||||||
|
return Err(format!("Runtime error: calling a {}-argument function with {} args", params.len(), args.len()))
|
||||||
|
}
|
||||||
|
let mut func_state = State { values: self.values.new_frame(name.map(|n| format!("{}", n))) };
|
||||||
|
for (param, val) in params.into_iter().zip(args.into_iter()) {
|
||||||
|
func_state.values.insert(param, ValueEntry::Binding { constant: true, val });
|
||||||
|
}
|
||||||
|
// TODO figure out function return semantics
|
||||||
|
let mut ret = None;
|
||||||
|
for stmt in body {
|
||||||
|
ret = func_state.statement(stmt)?;
|
||||||
|
}
|
||||||
|
Ok(ret.unwrap_or(Expr::Unit))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user