Interpreter - Define functions from name/params/body
* Used to be defined from statement, but this is cleaner and easier
This commit is contained in:
parent
c23ce47420
commit
7b20ec2f3a
1 changed files with 14 additions and 11 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue