ruby 0.0.2

Pure Rust implementation of Ruby runtime environment
Documentation

Rusty Ruby Compiler

A modern, high-performance Ruby compiler and runtime written in Rust, featuring advanced compilation techniques and memory management.

🎯 Project Overview

Rusty Ruby is a complete Ruby implementation that combines the elegance of Ruby with the performance and safety of Rust. It features a multi-tier compilation architecture, advanced garbage collection, and a rich ecosystem of tools and libraries.

🌟 Key Features

  • Multi-tier Compilation: Interpreter, baseline compiler, and optimizing compiler for balanced performance
  • Advanced Garbage Collection: Generational, incremental, and concurrent GC strategies
  • JIT Compilation: Just-In-Time compilation for hot code paths
  • FFI Interface: Seamless integration with C libraries
  • Performance Analysis: Built-in profiler and debugging tools
  • Register-based VM: Efficient virtual machine implementation

πŸš€ Quick Start

use ruby::Ruby;

fn main() -> ruby::Result<()> {
    let mut ruby = Ruby::new()?;
    
    // Execute Ruby script
    ruby.execute_script("$result = 1 + 2 * 3")?;
    
    // Get result
    let result = ruby.get_global("$result")?;
    println!("Result: {:?}", result);
    
    Ok(())
}

πŸ—οΈ Architecture

Compiler Pipeline

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Source Codeβ”‚ -> β”‚  Lexical    β”‚ -> β”‚  Syntax     β”‚ -> β”‚    AST      β”‚
β”‚             β”‚    β”‚  Analysis   β”‚    β”‚  Analysis   β”‚    β”‚             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚
                                              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Execution  β”‚ <- β”‚ Execution   β”‚ <- β”‚  VM         β”‚ <- β”‚    IR       β”‚
β”‚  Results    β”‚    β”‚ Engine      β”‚    β”‚  Instructionsβ”‚    β”‚             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚         Multi-tier Compilation  β”‚
              β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
              β”‚             β”‚             β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
       β”‚  Interpreterβ”‚ β”‚ Baseline  β”‚ β”‚ Optimizing β”‚
       β”‚             β”‚ β”‚ Compiler  β”‚ β”‚ Compiler  β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚         Garbage Collection      β”‚
              β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
              β”‚             β”‚             β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
       β”‚ Generationalβ”‚ β”‚ Incrementalβ”‚ β”‚ Concurrentβ”‚
       β”‚ GC          β”‚ β”‚ GC         β”‚ β”‚ GC        β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Directory Structure

compilers/ruby/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ lib.rs          # Main entry point, Ruby runtime environment
β”‚   β”œβ”€β”€ vm.rs           # Register-based virtual machine implementation
β”‚   β”œβ”€β”€ jit.rs          # JIT compiler implementation
β”‚   β”œβ”€β”€ codegen.rs      # Code generation (AST to IR to VM instructions)
β”‚   β”œβ”€β”€ architecture/   # Multi-tier compilation architecture
β”‚   β”‚   β”œβ”€β”€ mod.rs      # Architecture module definition
β”‚   β”‚   β”œβ”€β”€ interpreter.rs        # Interpreter implementation
β”‚   β”‚   β”œβ”€β”€ baseline_compiler.rs  # Baseline compiler implementation
β”‚   β”‚   β”œβ”€β”€ optimizing_compiler.rs # Optimizing compiler implementation
β”‚   β”‚   β”œβ”€β”€ execution_engine.rs   # Execution engine implementation
β”‚   β”‚   └── hotness_detector.rs   # Hotness detector implementation
β”‚   β”œβ”€β”€ gc/             # Garbage collection system
β”‚   β”‚   β”œβ”€β”€ mod.rs      # GC module definition
β”‚   β”‚   β”œβ”€β”€ generational.rs  # Generational garbage collector
β”‚   β”‚   β”œβ”€β”€ incremental.rs   # Incremental garbage collector
β”‚   β”‚   └── concurrent.rs    # Concurrent garbage collector
β”‚   └── profiler/       # Performance analysis and debugging tools
β”‚       β”œβ”€β”€ mod.rs      # Tools module definition
β”‚       β”œβ”€β”€ profiler.rs      # Profiler implementation
β”‚       β”œβ”€β”€ debugger.rs       # Debugger implementation
β”‚       └── visualizer.rs     # Visualization tool implementation
β”œβ”€β”€ tests/              # Test suite
β”‚   β”œβ”€β”€ ffi_test.rs     # FFI tests
β”‚   β”œβ”€β”€ gc_test.rs      # Garbage collector tests
β”‚   β”œβ”€β”€ jit_test.rs     # JIT compiler tests
β”‚   β”œβ”€β”€ vm_test.rs      # Virtual machine tests
β”‚   β”œβ”€β”€ runtime_test.rs # Runtime tests
β”‚   β”œβ”€β”€ performance_test.rs # Performance tests
β”‚   └── optimization_test.rs # Optimization tests
└── Cargo.toml          # Dependency configuration

πŸ› οΈ Development

# Build the project
cargo build

# Run tests
cargo test

# Run specific test
cargo test gc_test

# Build in release mode
cargo build --release

πŸ“š Core Modules

Frontend

  • Lexical Analysis: Converts source code to token stream
  • Syntax Analysis: Builds abstract syntax tree (AST)
  • Semantic Analysis: Checks types, scopes, etc.

Virtual Machine

  • Register-based: Uses 16 general-purpose registers
  • Instruction Set: Load/store, arithmetic, comparison, logic, control flow, method calls
  • Execution Environment: Manages global, local, instance, and class variables

Garbage Collection

  • Mark-Sweep Algorithm: Marks root objects, sweeps unmarked objects
  • Memory Management: Tracks memory usage, automatically reclaims unused objects
  • Root Objects: Global variables, local variables, instance variables, class variables

JIT Compiler

  • Just-In-Time Compilation: Compiles hot code paths to machine code
  • Optimization: Type inference, inlining, loop optimization, etc.
  • Performance: Significantly faster execution for hot code

FFI Interface

  • Dynamic Library Loading: Loads external C libraries
  • Function Calling: Calls C functions and handles return values
  • Type Conversion: Converts between Ruby values and C types

🀝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to help improve Rusty Ruby.

🌟 Future Development

  • Complete Ruby Language Support: Implement all Ruby syntax features
  • Advanced Optimization: More complex JIT optimizations, inline caching, escape analysis
  • Multi-platform Support: Support for more operating systems and hardware architectures
  • Debugging Tools: Enhanced debugger, performance analysis tools
  • Garbage Collection Optimization: More efficient GC algorithms
  • Concurrency Support: Better concurrent processing capabilities
  • Ecosystem: Complete Ruby ecosystem, including standard library and gems