Interpreter - Define functions from name/params/body

* Used to be defined from statement, but this is cleaner and easier
This commit is contained in:
Emmanuel BENOîT 2023-01-02 20:07:15 +01:00
parent c23ce47420
commit 7b20ec2f3a

View file

@ -1,3 +1,5 @@
use std::{cell::RefCell, rc::Rc};
use itertools::izip;
use crate::{
@ -17,17 +19,18 @@ pub(crate) struct Function {
body: Vec<ast::StmtNode>,
}
impl From<&ast::StmtNode> for Function {
fn from(node: &ast::StmtNode) -> Self {
if let ast::StmtNode::FunDecl { name, params, body } = node {
Self {
name: name.clone(),
params: params.clone(),
body: body.clone(),
}
} else {
panic!("initializing Function from non-function statement");
}
impl Function {
pub(crate) fn new(
name: &Token,
params: &Vec<Token>,
body: &Vec<ast::StmtNode>,
) -> Rc<RefCell<Self>> {
let fun = Self {
name: name.clone(),
params: params.clone(),
body: body.clone(),
};
Rc::new(RefCell::new(fun))
}
}