rustcc 0.1.1

An little C Complier( now it's just WIP :) )
//! # RustCC Interpreter
//! Evaluates abstract syntax trees generated by the parser.
//! Recursively traverses the AST and computes the result of expressions.

use std::error::Error;
use crate::ast::{ASTNode, NodeType};

/// An interpreter that evaluates abstract syntax trees.
/// The interpreter recursively traverses the AST and computes
/// the value of the expression it represents.
pub struct Interpreter;

impl Interpreter {
    /// Evaluates an AST node and returns its computed value.
    /// 
    /// # Arguments
    /// * `node` - The AST node to evaluate
    /// 
    /// # Returns
    /// * `Ok(i32)` - The computed value of the node
    /// * `Err(Box<dyn Error>)` - If an error occurs during evaluation
    pub fn evaluate(node: &ASTNode) -> Result<i32, Box<dyn Error>> {
        match node.op {
            NodeType::Add => {
                let left_val = Interpreter::evaluate(node.left.as_ref().ok_or("Missing left operand")?)?;
                let right_val = Interpreter::evaluate(node.right.as_ref().ok_or("Missing right operand")?)?;
                Ok(left_val + right_val)
            },
            NodeType::Subtract => {
                let left_val = Interpreter::evaluate(node.left.as_ref().ok_or("Missing left operand")?)?;
                let right_val = Interpreter::evaluate(node.right.as_ref().ok_or("Missing right operand")?)?;
                Ok(left_val - right_val)
            },
            NodeType::Multiply => {
                let left_val = Interpreter::evaluate(node.left.as_ref().ok_or("Missing left operand")?)?;
                let right_val = Interpreter::evaluate(node.right.as_ref().ok_or("Missing right operand")?)?;
                Ok(left_val * right_val)
            },
            NodeType::Divide => {
                let left_val = Interpreter::evaluate(node.left.as_ref().ok_or("Missing left operand")?)?;
                let right_val = Interpreter::evaluate(node.right.as_ref().ok_or("Missing right operand")?)?;
                if right_val == 0 {
                    return Err("Division by zero".into());
                }
                Ok(left_val / right_val)
            },
            NodeType::IntLit => {
                Ok(node.int_value.ok_or("Missing integer value")?)
            },
        }
    }
}