Skip to main content

ruby_ir/
lib.rs

1//! Ruby Intermediate Representation (IR)
2//!
3//! This crate provides a structured intermediate representation for Ruby code,
4//! designed to facilitate optimization passes and code generation.
5
6#![warn(missing_docs)]
7
8use ruby_types::{RubyError, RubyResult, RubyValue};
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12/// Type alias for Ruby result
13pub type Result<T> = RubyResult<T>;
14
15/// IR node ID type
16pub type NodeId = u32;
17
18/// IR basic block ID type
19pub type BlockId = u32;
20
21/// IR function ID type
22pub type FunctionId = u32;
23
24/// IR class ID type
25pub type ClassId = u32;
26
27/// IR module ID type
28pub type ModuleId = u32;
29
30/// Expression types in the IR
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub enum Expression {
33    /// Literal value
34    Literal(RubyValue),
35    /// Variable reference
36    Variable(String),
37    /// Global variable reference
38    GlobalVariable(String),
39    /// Instance variable reference
40    InstanceVariable(String),
41    /// Class variable reference
42    ClassVariable(String),
43    /// Method call
44    MethodCall {
45        /// Method receiver
46        receiver: Box<Expression>,
47        /// Method name
48        method: String,
49        /// Method arguments
50        arguments: Vec<Expression>,
51    },
52    /// Binary operation
53    BinaryOp {
54        /// Left operand
55        left: Box<Expression>,
56        /// Binary operator
57        op: BinaryOperator,
58        /// Right operand
59        right: Box<Expression>,
60    },
61    /// Unary operation
62    UnaryOp {
63        /// Unary operator
64        op: UnaryOperator,
65        /// Operand
66        operand: Box<Expression>,
67    },
68    /// Array literal
69    ArrayLiteral(Vec<Expression>),
70    /// Hash literal
71    HashLiteral(HashMap<String, Expression>),
72    /// Block creation
73    Block {
74        /// Block parameters
75        parameters: Vec<String>,
76        /// Block body statements
77        body: Vec<Statement>,
78    },
79    /// Self reference
80    SelfRef,
81    /// Super call
82    SuperCall {
83        /// Super call arguments
84        arguments: Vec<Expression>,
85    },
86}
87
88/// Binary operators
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub enum BinaryOperator {
91    /// Addition
92    Add,
93    /// Subtraction
94    Sub,
95    /// Multiplication
96    Mul,
97    /// Division
98    Div,
99    /// Modulo
100    Mod,
101    /// Exponentiation
102    Exp,
103    /// Equality
104    Eq,
105    /// Inequality
106    Neq,
107    /// Less than
108    Lt,
109    /// Less than or equal
110    Lte,
111    /// Greater than
112    Gt,
113    /// Greater than or equal
114    Gte,
115    /// Logical AND
116    And,
117    /// Logical OR
118    Or,
119    /// Assignment
120    Assign,
121}
122
123/// Unary operators
124#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
125pub enum UnaryOperator {
126    /// Logical NOT
127    Not,
128    /// Negation
129    Neg,
130    /// Plus
131    Plus,
132    /// Bitwise NOT
133    BitNot,
134}
135
136/// Statement types in the IR
137#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
138pub enum Statement {
139    /// Expression statement
140    Expression(Expression),
141    /// Variable assignment
142    Assignment {
143        /// Variable name
144        name: String,
145        /// Assignment value
146        value: Expression,
147    },
148    /// Global variable assignment
149    GlobalAssignment {
150        /// Global variable name
151        name: String,
152        /// Assignment value
153        value: Expression,
154    },
155    /// Instance variable assignment
156    InstanceAssignment {
157        /// Instance variable name
158        name: String,
159        /// Assignment value
160        value: Expression,
161    },
162    /// Class variable assignment
163    ClassAssignment {
164        /// Class variable name
165        name: String,
166        /// Assignment value
167        value: Expression,
168    },
169    /// If statement
170    If {
171        /// Condition expression
172        condition: Expression,
173        /// Then branch statements
174        then_branch: Vec<Statement>,
175        /// Else branch statements
176        else_branch: Vec<Statement>,
177    },
178    /// While loop
179    While {
180        /// Loop condition
181        condition: Expression,
182        /// Loop body statements
183        body: Vec<Statement>,
184    },
185    /// Until loop
186    Until {
187        /// Loop condition
188        condition: Expression,
189        /// Loop body statements
190        body: Vec<Statement>,
191    },
192    /// Case statement
193    Case {
194        /// Value to match
195        value: Expression,
196        /// When clauses (condition, body)
197        when_clauses: Vec<(Expression, Vec<Statement>)>,
198        /// Else clause statements
199        else_clause: Vec<Statement>,
200    },
201    /// For loop
202    For {
203        /// Loop variable
204        variable: String,
205        /// Iterator expression
206        iterator: Expression,
207        /// Loop body statements
208        body: Vec<Statement>,
209    },
210    /// Break statement
211    Break,
212    /// Next statement
213    Next,
214    /// Redo statement
215    Redo,
216    /// Return statement
217    Return(Option<Expression>),
218    /// Method definition
219    MethodDefinition {
220        /// Method name
221        name: String,
222        /// Method parameters
223        parameters: Vec<String>,
224        /// Method body statements
225        body: Vec<Statement>,
226    },
227    /// Class definition
228    ClassDefinition {
229        /// Class name
230        name: String,
231        /// Superclass name
232        superclass: Option<String>,
233        /// Class body statements
234        body: Vec<Statement>,
235    },
236    /// Module definition
237    ModuleDefinition {
238        /// Module name
239        name: String,
240        /// Module body statements
241        body: Vec<Statement>,
242    },
243    /// Require statement
244    Require(Expression),
245    /// Load statement
246    Load(Expression),
247}
248
249/// Basic block in the IR
250#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
251pub struct BasicBlock {
252    /// Block ID
253    pub id: BlockId,
254    /// Statements in the block
255    pub statements: Vec<Statement>,
256    /// Successor blocks
257    pub successors: Vec<BlockId>,
258}
259
260/// Function in the IR
261#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
262pub struct Function {
263    /// Function ID
264    pub id: FunctionId,
265    /// Function name
266    pub name: String,
267    /// Parameters
268    pub parameters: Vec<String>,
269    /// Basic blocks
270    pub blocks: HashMap<BlockId, BasicBlock>,
271    /// Entry block ID
272    pub entry_block: BlockId,
273}
274
275/// Class in the IR
276#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
277pub struct Class {
278    /// Class ID
279    pub id: ClassId,
280    /// Class name
281    pub name: String,
282    /// Superclass name
283    pub superclass: Option<String>,
284    /// Methods
285    pub methods: HashMap<String, FunctionId>,
286}
287
288/// Module in the IR
289#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
290pub struct Module {
291    /// Module ID
292    pub id: ModuleId,
293    /// Module name
294    pub name: String,
295    /// Methods
296    pub methods: HashMap<String, FunctionId>,
297}
298
299/// Program in the IR
300#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
301pub struct Program {
302    /// Functions
303    pub functions: HashMap<FunctionId, Function>,
304    /// Classes
305    pub classes: HashMap<ClassId, Class>,
306    /// Modules
307    pub modules: HashMap<ModuleId, Module>,
308    /// Global statements
309    pub global_statements: Vec<Statement>,
310}
311
312impl Program {
313    /// Create a new empty program
314    pub fn new() -> Self {
315        Self { functions: HashMap::new(), classes: HashMap::new(), modules: HashMap::new(), global_statements: Vec::new() }
316    }
317
318    /// Add a function to the program
319    pub fn add_function(&mut self, function: Function) {
320        self.functions.insert(function.id, function);
321    }
322
323    /// Add a class to the program
324    pub fn add_class(&mut self, class: Class) {
325        self.classes.insert(class.id, class);
326    }
327
328    /// Add a module to the program
329    pub fn add_module(&mut self, module: Module) {
330        self.modules.insert(module.id, module);
331    }
332
333    /// Add a global statement
334    pub fn add_global_statement(&mut self, statement: Statement) {
335        self.global_statements.push(statement);
336    }
337}
338
339/// Serialize the program to JSON
340pub fn serialize_program(program: &Program) -> Result<String> {
341    serde_json::to_string(program).map_err(|e| RubyError::RuntimeError(format!("Serialization error: {}", e)))
342}
343
344/// Deserialize the program from JSON
345pub fn deserialize_program(json: &str) -> Result<Program> {
346    serde_json::from_str(json).map_err(|e| RubyError::RuntimeError(format!("Deserialization error: {}", e)))
347}
348
349/// Traversal and manipulation utilities
350pub mod traversal;
351
352/// Optimization utilities
353pub mod optimization;