5 releases
Uses new Rust 2024
| 0.0.4 | Mar 30, 2026 |
|---|---|
| 0.0.3 | Mar 28, 2026 |
| 0.0.2 | Mar 27, 2026 |
| 0.0.1 | Mar 25, 2026 |
| 0.0.0 | Mar 24, 2026 |
#621 in Programming languages
Used in 3 crates
165KB
3.5K
SLoC
๐ typescript-ir
Rusty TypeScript Intermediate Representation ๐ฆโก๏ธ
๐ Introduction
typescript-ir is the intermediate representation layer for the Rusty TypeScript compiler. It transforms AST into optimized IR and provides various optimization and analysis capabilities, serving as the bridge between parsing and code generation.
โจ Core Features
| Feature | Description | Status |
|---|---|---|
| ๐ AST โ IR | Transform Oak AST to internal IR | โ Ready |
| โก Constant Folding | Compile-time constant expression evaluation | โ Ready |
| ๐๏ธ Dead Code Elimination | Remove unreachable code | โ Ready |
| ๐ Control Flow Graph | CFG construction and analysis | โ Ready |
| ๐ฏ Type Information | Type-aware IR representation | โ Ready |
| ๐ Visitor Pattern | Flexible IR traversal | โ Ready |
๐ Quick Start
Add Dependency
[dependencies]
typescript-ir = { path = "../compilers/typescript-ir" }
Basic Usage
use typescript_ir::{Program, Expression, Statement, BinaryOp};
use typescript_types::TsValue;
fn main() {
// ๐ Create IR program
let program = Program {
statements: vec![
Statement::VariableDeclaration {
name: "x".to_string(),
ty: None,
initializer: Some(Box::new(Expression::Binary {
left: Box::new(Expression::Literal(TsValue::Number(10.0))),
op: BinaryOp::Add,
right: Box::new(Expression::Literal(TsValue::Number(20.0))),
})),
}
],
};
println!("๐ฏ IR program created successfully: {:?}", program);
}
AST to IR Conversion
use typescript_ir::ast_to_ir;
use oak_typescript::ast::TypeScriptRoot;
fn compile(ast: &TypeScriptRoot) -> Program {
// ๐ Convert Oak AST to IR
let ir = ast_to_ir::program(ast);
println!("โ
AST to IR conversion completed");
ir
}
Expression Optimization
use typescript_ir::{TypedExpression, Expression, TypeAnnotation};
fn optimize_expr(expr: Expression) -> TypedExpression {
// ๐ฏ Create typed expression
let typed = TypedExpression::new(expr, TypeAnnotation::Number);
// โก Perform optimization
let optimized = typed.optimize();
println!("๐ Optimization completed, is constant: {}", optimized.is_constant);
optimized
}
โก Optimization Features
Constant Folding
use typescript_ir::{TypedExpression, Expression, TypeAnnotation, BinaryOp};
use typescript_types::TsValue;
// Input: 10 + 20 * 2
let expr = Expression::Binary {
left: Box::new(Expression::Literal(TsValue::Number(10.0))),
op: BinaryOp::Add,
right: Box::new(Expression::Binary {
left: Box::new(Expression::Literal(TsValue::Number(20.0))),
op: BinaryOp::Mul,
right: Box::new(Expression::Literal(TsValue::Number(2.0))),
}),
};
let typed = TypedExpression::new(expr, TypeAnnotation::Number);
let optimized = typed.optimize();
// Output: 50 (constant value)
assert!(optimized.is_constant);
assert_eq!(optimized.constant_value, Some(TsValue::Number(50.0)));
Control Flow Graph
use typescript_ir::ControlFlowGraph;
// Create CFG
let mut cfg = ControlFlowGraph::new();
// Add nodes
let node_a = cfg.add_node();
let node_b = cfg.add_node();
let node_c = cfg.add_node();
// Add edges
cfg.add_edge(node_a, node_b);
cfg.add_edge(node_b, node_c);
println!("๐ CFG node count: {}", cfg.nodes.len());
๐ Visitor Pattern
use typescript_ir::{Visitor, Expression, Statement, Program};
/// Custom visitor
struct MyVisitor {
result: Vec<String>,
}
impl Visitor<()> for MyVisitor {
fn visit_expression(&mut self, expr: &Expression) {
match expr {
Expression::Identifier(name) => {
self.result.push(format!("Found identifier: {}", name));
}
_ => {}
}
}
fn visit_statement(&mut self, stmt: &Statement) {
// Handle statements...
}
fn visit_program(&mut self, program: &Program) {
for stmt in &program.statements {
self.visit_statement(stmt);
}
}
// ... other methods
}
๐งช Testing
# Run all tests
cargo test --package typescript-ir
# Run optimization tests
cargo test --package typescript-ir -- optimization
# View output
cargo test --package typescript-ir -- --nocapture
๐ Dependencies
- typescript-types - Type definitions
- oak-typescript - AST definitions (re-export)
- serde - Serialization support
๐ค Contributing
We welcome issues and PRs! Please ensure:
- โ
Code passes
cargo clippychecks - โ
Code is formatted with
cargo fmt - โ
All tests pass with
cargo test - โ New features include corresponding tests
๐ License
MIT License - see LICENSE
๐ฆ Intermediate representation, the bridge between compilation ๐
Dependencies
~0.3โ1MB
~20K SLoC