use crate::error::*; use crate::ir::*; use falcon::{il, RC}; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Program { functions: BTreeMap>>, } impl Program { pub fn new() -> Program { Program { functions: BTreeMap::new(), } } pub fn from_il(program: &il::Program) -> Result> { let functions: ::std::result::Result>>, Error> = program.functions_map().into_iter().try_fold( BTreeMap::new(), |mut functions, (index, function)| { functions.insert(index, RC::new(Function::::from_il(function)?)); Ok(functions) }, ); Ok(Program { functions: functions?, }) } pub fn function(&self, index: usize) -> Option<&Function> { self.functions.get(&index).map(|function| function.as_ref()) } pub fn functions(&self) -> Vec<&Function> { self.functions .iter() .map(|(_, function)| function.as_ref()) .collect::>>() } pub fn function_by_name(&self, name: &str) -> Option<&Function> { self.functions .iter() .find(|(_, function)| function.name() == name) .map(|(_, function)| function.as_ref()) } pub fn replace_function(&mut self, index: usize, function: Function) { self.functions.insert(index, RC::new(function)); } } impl Default for Program { fn default() -> Self { Self::new() } }