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 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>,
|
||||||
|
body: &Vec<ast::StmtNode>,
|
||||||
|
) -> Rc<RefCell<Self>> {
|
||||||
|
let fun = Self {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
params: params.clone(),
|
params: params.clone(),
|
||||||
body: body.clone(),
|
body: body.clone(),
|
||||||
}
|
};
|
||||||
} else {
|
Rc::new(RefCell::new(fun))
|
||||||
panic!("initializing Function from non-function statement");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue