add noexec operator to pass named functions to functions
This commit is contained in:
@@ -353,6 +353,24 @@ where
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,7 @@ pub(crate) enum ParseTree {
|
||||
FunctionCall(String, Vec<ParseTree>),
|
||||
Variable(String),
|
||||
Constant(Value),
|
||||
NonCall(String),
|
||||
|
||||
// Type Casts
|
||||
IntCast(Box<ParseTree>),
|
||||
@@ -257,7 +258,10 @@ impl ParseTree {
|
||||
|
||||
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))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,7 +328,6 @@ impl<R: BufRead> Tokenizer<R> {
|
||||
return;
|
||||
}
|
||||
};
|
||||
println!("{n} + {count}");
|
||||
|
||||
Op::FunctionDefine(n + count)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user