Skip to content

Commit

Permalink
use more Rc s
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jan 14, 2024
1 parent b34806f commit 1980e5e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ pub fn parse_block(ptr_base: usize, xs: &[Cirru], looped: bool, collector: &mut
looped,
from: ptr_base + 1,
to: p,
params_types,
ret_types,
params_types: Rc::new(params_types),
ret_types: Rc::new(ret_types),
},
);
Ok(chunk)
Expand Down
8 changes: 4 additions & 4 deletions src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ pub enum CalxInstr {
Jmp(usize), // internal
JmpIf(usize), // internal
Block {
params_types: Vec<CalxType>,
ret_types: Vec<CalxType>,
params_types: Rc<Vec<CalxType>>,
ret_types: Rc<Vec<CalxType>>,
/// bool to indicate loop
looped: bool,
from: usize,
Expand Down Expand Up @@ -214,8 +214,8 @@ impl CalxError {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
pub struct BlockData {
pub looped: bool,
pub params_types: Vec<CalxType>,
pub ret_types: Vec<CalxType>,
pub params_types: Rc<Vec<CalxType>>,
pub ret_types: Rc<Vec<CalxType>>,
pub from: usize,
pub to: usize,
pub initial_stack_size: usize,
Expand Down
19 changes: 9 additions & 10 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ impl std::fmt::Debug for CalxVM {

impl CalxVM {
pub fn new(fns: Vec<CalxFunc>, globals: Vec<Calx>, imports: CalxImportsDict) -> Self {
let mut fns = fns.to_owned();
let main_func = find_func(&mut fns, "main").expect("main function is required");
let main_func = find_func(&fns, "main").expect("main function is required");
let main_frame = CalxFrame {
initial_stack_size: 0,
blocks_track: vec![],
instrs: main_func.instrs.to_owned(),
instrs: main_func.instrs.clone(),
pointer: 0,
locals: vec![],
ret_types: main_func.ret_types.to_owned(),
ret_types: main_func.ret_types.clone(),
};
CalxVM {
stack: vec![],
Expand Down Expand Up @@ -380,7 +379,7 @@ impl CalxVM {
}
CalxInstr::Call(f_name) => {
// println!("frame size: {}", self.frames.len());
match find_func(&mut self.funcs, &f_name) {
match find_func(&self.funcs, &f_name) {
Some(f) => {
let instrs = f.instrs.to_owned();
let ret_types = f.ret_types.to_owned();
Expand All @@ -407,7 +406,7 @@ impl CalxVM {
}
CalxInstr::ReturnCall(f_name) => {
// println!("frame size: {}", self.frames.len());
match find_func(&mut self.funcs, &f_name) {
match find_func(&self.funcs, &f_name) {
Some(f) => {
// println!("examine stack: {:?}", self.stack);
let instrs = f.instrs.to_owned();
Expand Down Expand Up @@ -564,7 +563,7 @@ impl CalxVM {

ops.push(CalxInstr::Nop)
}
CalxInstr::Call(f_name) => match find_func(&mut self.funcs, &f_name) {
CalxInstr::Call(f_name) => match find_func(&self.funcs, &f_name) {
Some(f) => {
if stack_size < f.params_types.len() {
return Err(format!("insufficient size to call: {} {:?}", stack_size, f.params_types));
Expand All @@ -574,7 +573,7 @@ impl CalxVM {
}
None => return Err(format!("cannot find function named: {}", f_name)),
},
CalxInstr::ReturnCall(f_name) => match find_func(&mut self.funcs, &f_name) {
CalxInstr::ReturnCall(f_name) => match find_func(&self.funcs, &f_name) {
Some(f) => {
if stack_size < f.params_types.len() {
return Err(format!("insufficient size to call: {} {:?}", stack_size, f.params_types));
Expand Down Expand Up @@ -706,8 +705,8 @@ impl CalxVM {
}
}

pub fn find_func<'a>(funcs: &'a mut [CalxFunc], name: &str) -> Option<&'a mut CalxFunc> {
funcs.iter_mut().find(|x| &*x.name == name)
pub fn find_func<'a>(funcs: &'a [CalxFunc], name: &str) -> Option<&'a CalxFunc> {
funcs.iter().find(|x| &*x.name == name)
}

/// notice that some of the instrs are special and need to handle manually
Expand Down

0 comments on commit 1980e5e

Please sign in to comment.