Interpreter - Handle function definitions
This commit is contained in:
parent
cf3284cd88
commit
6acf492aa2
1 changed files with 21 additions and 1 deletions
|
@ -7,6 +7,8 @@ use crate::{
|
||||||
tokens::{Token, TokenType},
|
tokens::{Token, TokenType},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::functions::Function;
|
||||||
|
|
||||||
/// Evaluate an interpretable, returning its value.
|
/// Evaluate an interpretable, returning its value.
|
||||||
pub fn evaluate(err_hdl: &mut ErrorHandler, ast: &dyn Interpretable) -> Option<Value> {
|
pub fn evaluate(err_hdl: &mut ErrorHandler, ast: &dyn Interpretable) -> Option<Value> {
|
||||||
let env = Rc::new(RefCell::new(Environment::default()));
|
let env = Rc::new(RefCell::new(Environment::default()));
|
||||||
|
@ -89,9 +91,12 @@ impl Interpretable for ast::ProgramNode {
|
||||||
impl Interpretable for ast::StmtNode {
|
impl Interpretable for ast::StmtNode {
|
||||||
fn interpret(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
fn interpret(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
||||||
match self {
|
match self {
|
||||||
|
ast::StmtNode::VarDecl(name, expr) => self.on_var_decl(environment, name, expr),
|
||||||
|
ast::StmtNode::FunDecl { name, params, body } => {
|
||||||
|
self.on_fun_decl(environment, name, params, body)
|
||||||
|
}
|
||||||
ast::StmtNode::Expression(expr) => expr.interpret(environment),
|
ast::StmtNode::Expression(expr) => expr.interpret(environment),
|
||||||
ast::StmtNode::Print(expr) => self.on_print(environment, expr),
|
ast::StmtNode::Print(expr) => self.on_print(environment, expr),
|
||||||
ast::StmtNode::VarDecl(name, expr) => self.on_var_decl(environment, name, expr),
|
|
||||||
ast::StmtNode::Block(statements) => self.on_block(environment, statements),
|
ast::StmtNode::Block(statements) => self.on_block(environment, statements),
|
||||||
ast::StmtNode::IfStmt {
|
ast::StmtNode::IfStmt {
|
||||||
condition,
|
condition,
|
||||||
|
@ -143,6 +148,21 @@ impl ast::StmtNode {
|
||||||
Ok(InterpreterFlowControl::default())
|
Ok(InterpreterFlowControl::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handle a function declaration.
|
||||||
|
fn on_fun_decl(
|
||||||
|
&self,
|
||||||
|
environment: &EnvironmentRef,
|
||||||
|
name: &Token,
|
||||||
|
params: &Vec<Token>,
|
||||||
|
body: &Vec<ast::StmtNode>,
|
||||||
|
) -> InterpreterResult {
|
||||||
|
let fun = Function::new(name, params, body);
|
||||||
|
environment
|
||||||
|
.borrow_mut()
|
||||||
|
.define(name, Some(Value::Callable(fun)))?;
|
||||||
|
Ok(InterpreterFlowControl::default())
|
||||||
|
}
|
||||||
|
|
||||||
/// Execute the contents of a block.
|
/// Execute the contents of a block.
|
||||||
fn on_block(&self, environment: &EnvironmentRef, stmts: &[ast::StmtNode]) -> InterpreterResult {
|
fn on_block(&self, environment: &EnvironmentRef, stmts: &[ast::StmtNode]) -> InterpreterResult {
|
||||||
let child = Environment::create_child(environment);
|
let child = Environment::create_child(environment);
|
||||||
|
|
Loading…
Reference in a new issue