use crate::chunk::Chunk;
use crate::stmt::Stmt;
use crate::tokens::Token;
#[derive(Copy, Clone)]
pub(crate) enum FunctionType {
Native,
User,
}
#[derive(Clone)]
pub(crate) struct FunctionDefinition {
pub(crate) name: String,
pub(crate) arity: u8,
pub(crate) line: usize,
pub(crate) function_type: FunctionType,
}
impl FunctionDefinition {
pub(crate) fn new(name: String, arity: u8, line: usize, function_type: FunctionType) -> Self {
Self {
name,
arity,
line,
function_type,
}
}
}
#[derive(Clone)]
pub(crate) struct Function {
pub(crate) name: String,
pub(crate) parameters: Vec<Token>,
pub(crate) stmts: Vec<Stmt>,
pub(crate) chunk: Option<Chunk>,
}
impl Function {
pub fn new(name: String, parameters: Vec<Token>, stmts: Vec<Stmt>) -> Self {
Self {
name,
parameters,
stmts,
chunk: None,
}
}
pub fn set_chunk(&mut self, chunk: Chunk) {
self.chunk = Some(chunk);
}
pub fn definition(&self) -> FunctionDefinition {
FunctionDefinition {
name: self.name.clone(),
arity: self.parameters.len() as u8,
line: 0,
function_type: FunctionType::User,
}
}
}