deimos-lang/dvm-lib/src/vm/function.rs

57 lines
1.1 KiB
Rust

use crate::instruction::Instruction;
use std::fmt::Display;
use std::rc::Rc;
pub struct Function {
name: Rc<str>,
parameter_count: usize,
stack_size: isize,
instructions: Vec<Instruction>,
}
impl Function {
pub fn new(
name: Rc<str>,
parameter_count: usize,
stack_size: isize,
instructions: Vec<Instruction>,
) -> Self {
Self {
name,
parameter_count,
stack_size,
instructions,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn name_owned(&self) -> Rc<str> {
self.name.clone()
}
pub fn parameter_count(&self) -> usize {
self.parameter_count
}
pub fn stack_size(&self) -> isize {
self.stack_size
}
pub fn instructions(&self) -> &Vec<Instruction> {
&self.instructions
}
}
impl Display for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "fn {}", self.name)?;
for instruction in &self.instructions {
writeln!(f, " {}", instruction)?;
}
Ok(())
}
}