cordillera 0.1.4

IR generation library for Sierra
Documentation
//! # Entities Module
//! - Made to store common structures used by Builder

use std::fmt;
use super::ir::{IRNode, serialize};

/// Represents a symbol in the compiler context (functions, variables, etc.)
/// 
/// Symbols track metadata about named entities during IR construction.
#[derive(Debug)]
#[allow(dead_code)]
pub struct Symbol {
    /// The name of the symbol (function name, variable name, etc.)
    pub symbol_name: String,
    /// The type of symbol (function, variable, etc.)
    pub symbol_type: SymbolType,
    /// The value associated with this symbol (labels for functions, etc.)
    pub symbol_value: Value,
}

/// Types of symbols that can be stored in the context
#[derive(Debug)]
#[allow(dead_code)]
pub enum SymbolType {
    /// Represents a function symbol
    Function,
    /// Represents a variable symbol  
    Variable,
}

/// Values that can be associated with symbols
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum Value {
    /// No value (void)
    Void,
    /// Functions
    Function {
        return_type: Type,
        labels: Vec<String>,
    },
    Int(i64),
    Float(f64)
}

#[allow(dead_code)]
impl Value {
    /// Converts the Value into an IRNode equivalent
    /// 
    /// # Arguments
    /// * `expect` - The message to panic if the Value can't be converted to an IRNode
    /// 
    /// # Panics
    /// - When the Value can't be converted to an IRNode
    /// 
    /// # Examples
    /// ```rust
    /// let ir_node = Value::Int(5).to_ir();
    /// ```
    pub fn to_ir(&self, expect: &str) -> IRNode {
        match self {
            Self::Int(int) => IRNode::ConstInt(*int),
            Self::Float(float) => IRNode::ConstFloat(*float),
            _ => panic!("{}", expect)
        }
    }
}

/// IR Code Types
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum Type {
    Void,
    Int,
    Float
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Void => write!(f, "void"),
            Self::Int => write!(f, "int"),
            Self::Float => write!(f, "float")
        }
    }
}

/// Program wrapper that can be serialized to IR text format
/// 
/// This struct holds the complete IR program and can be displayed
/// or serialized for further processing.
#[derive(Debug, Clone)]
#[must_use = "Program should be used (serialized or processed)"]
pub struct Program {
    pub program: Vec<IRNode>
}

impl fmt::Display for Program {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", serialize(&self.program))
    }
}