1use crate::chunk::Chunk;
2use crate::stmt::Stmt;
3use crate::tokens::Token;
4
5#[derive(Copy, Clone)]
6pub(crate) enum FunctionType {
7 Native,
8 User,
9}
10
11#[derive(Clone)]
12pub(crate) struct FunctionDefinition {
13 pub(crate) name: String,
14 pub(crate) arity: u8,
15 pub(crate) line: usize,
16 pub(crate) function_type: FunctionType,
17}
18
19impl FunctionDefinition {
20 pub(crate) fn new(name: String, arity: u8, line: usize, function_type: FunctionType) -> Self {
21 Self {
22 name,
23 arity,
24 line,
25 function_type,
26 }
27 }
28}
29
30#[derive(Clone)]
31pub(crate) struct Function {
32 pub(crate) name: String,
33 pub(crate) parameters: Vec<Token>,
34 pub(crate) stmts: Vec<Stmt>,
35 pub(crate) chunk: Option<Chunk>,
36}
37
38impl Function {
39 pub fn new(name: String, parameters: Vec<Token>, stmts: Vec<Stmt>) -> Self {
40 Self {
41 name,
42 parameters,
43 stmts,
44 chunk: None,
45 }
46 }
47
48 pub fn set_chunk(&mut self, chunk: Chunk) {
49 self.chunk = Some(chunk);
50 }
51
52 pub fn definition(&self) -> FunctionDefinition {
53 FunctionDefinition {
54 name: self.name.clone(),
55 arity: self.parameters.len() as u8,
56 line: 0,
57 function_type: FunctionType::User,
58 }
59 }
60}