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