cordillera 0.1.2

IR generation library for Sierra
Documentation
//! # Context Module
//! - Made to hold the Context that Builder uses

use super::entities::{Symbol};

pub struct Context<'a> {
    pub context: Vec<Symbol<'a>>,
    pub func_dent: Vec<String>
}

#[allow(dead_code)]
impl<'a> Context<'a> {
    /// Constructs a new context
    /// 
    /// # Examples
    /// let context = Context::new();
    pub fn create() -> Self {
        Context {
            context: vec![],
            func_dent: Vec::new()
        }
    }
    /// Inserts a new symbol
    /// 
    /// # Arguments
    /// * `symbol` - The symbol to insert
    /// 
    /// # Examples
    /// context.insert_symbol(
    ///     Symbol {
    ///         symbol_type: SymbolType::Function,
    ///         value: Value::Void
    ///     }
    /// )
    pub fn insert_symbol(&mut self, symbol: Symbol<'a>) {
        self.context.push(symbol)
    }
    /// Get a symbol from the context
    /// 
    /// # Arguments:
    /// * `name` - The name of the symbol
    /// 
    /// # Examples:
    /// ```rust
    /// let my_symbol_type = context.get("my_symbol");
    /// ```
    pub fn get(&self, name: &str) -> Option<&Symbol> {
        let mut found: Option<&Symbol> = None;
        self.context
        .iter()
        .for_each(|symbol| if symbol.symbol_name == name {
            found = Some(&symbol)
        });
        found
    }
}