1#![warn(missing_docs)]
7
8use ruby_types::{RubyError, RubyResult, RubyValue};
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12pub type Result<T> = RubyResult<T>;
14
15pub type NodeId = u32;
17
18pub type BlockId = u32;
20
21pub type FunctionId = u32;
23
24pub type ClassId = u32;
26
27pub type ModuleId = u32;
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub enum Expression {
33 Literal(RubyValue),
35 Variable(String),
37 GlobalVariable(String),
39 InstanceVariable(String),
41 ClassVariable(String),
43 MethodCall {
45 receiver: Box<Expression>,
47 method: String,
49 arguments: Vec<Expression>,
51 },
52 BinaryOp {
54 left: Box<Expression>,
56 op: BinaryOperator,
58 right: Box<Expression>,
60 },
61 UnaryOp {
63 op: UnaryOperator,
65 operand: Box<Expression>,
67 },
68 ArrayLiteral(Vec<Expression>),
70 HashLiteral(HashMap<String, Expression>),
72 Block {
74 parameters: Vec<String>,
76 body: Vec<Statement>,
78 },
79 SelfRef,
81 SuperCall {
83 arguments: Vec<Expression>,
85 },
86}
87
88#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub enum BinaryOperator {
91 Add,
93 Sub,
95 Mul,
97 Div,
99 Mod,
101 Exp,
103 Eq,
105 Neq,
107 Lt,
109 Lte,
111 Gt,
113 Gte,
115 And,
117 Or,
119 Assign,
121}
122
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
125pub enum UnaryOperator {
126 Not,
128 Neg,
130 Plus,
132 BitNot,
134}
135
136#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
138pub enum Statement {
139 Expression(Expression),
141 Assignment {
143 name: String,
145 value: Expression,
147 },
148 GlobalAssignment {
150 name: String,
152 value: Expression,
154 },
155 InstanceAssignment {
157 name: String,
159 value: Expression,
161 },
162 ClassAssignment {
164 name: String,
166 value: Expression,
168 },
169 If {
171 condition: Expression,
173 then_branch: Vec<Statement>,
175 else_branch: Vec<Statement>,
177 },
178 While {
180 condition: Expression,
182 body: Vec<Statement>,
184 },
185 Until {
187 condition: Expression,
189 body: Vec<Statement>,
191 },
192 Case {
194 value: Expression,
196 when_clauses: Vec<(Expression, Vec<Statement>)>,
198 else_clause: Vec<Statement>,
200 },
201 For {
203 variable: String,
205 iterator: Expression,
207 body: Vec<Statement>,
209 },
210 Break,
212 Next,
214 Redo,
216 Return(Option<Expression>),
218 MethodDefinition {
220 name: String,
222 parameters: Vec<String>,
224 body: Vec<Statement>,
226 },
227 ClassDefinition {
229 name: String,
231 superclass: Option<String>,
233 body: Vec<Statement>,
235 },
236 ModuleDefinition {
238 name: String,
240 body: Vec<Statement>,
242 },
243 Require(Expression),
245 Load(Expression),
247}
248
249#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
251pub struct BasicBlock {
252 pub id: BlockId,
254 pub statements: Vec<Statement>,
256 pub successors: Vec<BlockId>,
258}
259
260#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
262pub struct Function {
263 pub id: FunctionId,
265 pub name: String,
267 pub parameters: Vec<String>,
269 pub blocks: HashMap<BlockId, BasicBlock>,
271 pub entry_block: BlockId,
273}
274
275#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
277pub struct Class {
278 pub id: ClassId,
280 pub name: String,
282 pub superclass: Option<String>,
284 pub methods: HashMap<String, FunctionId>,
286}
287
288#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
290pub struct Module {
291 pub id: ModuleId,
293 pub name: String,
295 pub methods: HashMap<String, FunctionId>,
297}
298
299#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
301pub struct Program {
302 pub functions: HashMap<FunctionId, Function>,
304 pub classes: HashMap<ClassId, Class>,
306 pub modules: HashMap<ModuleId, Module>,
308 pub global_statements: Vec<Statement>,
310}
311
312impl Program {
313 pub fn new() -> Self {
315 Self { functions: HashMap::new(), classes: HashMap::new(), modules: HashMap::new(), global_statements: Vec::new() }
316 }
317
318 pub fn add_function(&mut self, function: Function) {
320 self.functions.insert(function.id, function);
321 }
322
323 pub fn add_class(&mut self, class: Class) {
325 self.classes.insert(class.id, class);
326 }
327
328 pub fn add_module(&mut self, module: Module) {
330 self.modules.insert(module.id, module);
331 }
332
333 pub fn add_global_statement(&mut self, statement: Statement) {
335 self.global_statements.push(statement);
336 }
337}
338
339pub fn serialize_program(program: &Program) -> Result<String> {
341 serde_json::to_string(program).map_err(|e| RubyError::RuntimeError(format!("Serialization error: {}", e)))
342}
343
344pub fn deserialize_program(json: &str) -> Result<Program> {
346 serde_json::from_str(json).map_err(|e| RubyError::RuntimeError(format!("Deserialization error: {}", e)))
347}
348
349pub mod traversal;
351
352pub mod optimization;