#[derive(Debug)]
pub struct IRModule {
pub functions: Vec<IRFunction>,
pub variables: Vec<IRVariable>, pub imports: Vec<IRImport>, pub classes: Vec<IRClass>, pub metadata: std::collections::HashMap<String, String>, }
#[derive(Debug, Clone)]
pub struct IRFunction {
pub name: String,
pub params: Vec<IRParam>,
pub body: IRBody,
pub return_type: IRType,
pub decorators: Vec<String>, }
#[derive(Debug, Clone)]
pub struct IRParam {
pub name: String,
pub param_type: IRType,
pub default_value: Option<IRExpr>, }
#[derive(Debug, Clone)]
pub struct IRBody {
pub statements: Vec<IRStatement>,
}
#[derive(Debug, Clone)]
pub enum IRStatement {
Return(Option<IRExpr>),
Assign {
target: String,
value: IRExpr,
var_type: Option<IRType>,
},
If {
condition: IRExpr,
then_body: Box<IRBody>,
else_body: Option<Box<IRBody>>,
},
Raise {
exception: Option<IRExpr>,
},
While {
condition: IRExpr,
body: Box<IRBody>,
},
Expression(IRExpr),
TryExcept {
try_body: Box<IRBody>,
except_handlers: Vec<IRExceptHandler>,
finally_body: Option<Box<IRBody>>,
},
For {
target: String,
iterable: IRExpr,
body: Box<IRBody>,
else_body: Option<Box<IRBody>>,
},
With {
context_expr: IRExpr,
optional_vars: Option<String>,
body: Box<IRBody>,
},
AttributeAssign {
object: IRExpr,
attribute: String,
value: IRExpr,
},
AugAssign {
target: String,
value: IRExpr,
op: IROp,
},
AttributeAugAssign {
object: IRExpr,
attribute: String,
value: IRExpr,
op: IROp,
},
DynamicImport {
target: String,
module_name: IRExpr,
},
IndexAssign {
container: IRExpr,
index: IRExpr,
value: IRExpr,
},
TupleUnpack {
targets: Vec<String>,
value: IRExpr,
},
Yield {
value: Option<IRExpr>,
},
ImportModule {
module_name: String,
alias: Option<String>,
},
}
#[derive(Debug, Clone)]
pub struct IRExceptHandler {
pub exception_type: Option<String>,
pub name: Option<String>,
pub body: IRBody,
}
#[derive(Debug)]
pub struct IRVariable {
pub name: String,
pub value: IRExpr,
pub var_type: Option<IRType>,
}
#[derive(Clone, Debug)]
pub struct IRImport {
pub module: String,
pub name: Option<String>,
pub alias: Option<String>,
pub is_from_import: bool,
pub is_star_import: bool, pub is_conditional: bool, pub is_dynamic: bool, pub conditional_fallbacks: Vec<String>, }
#[derive(Debug)]
pub struct IRClass {
pub name: String,
pub bases: Vec<String>,
pub methods: Vec<IRFunction>,
pub class_vars: Vec<IRVariable>,
}
#[derive(Debug, Clone)]
pub enum IRExpr {
Const(IRConstant),
BinaryOp {
left: Box<IRExpr>,
right: Box<IRExpr>,
op: IROp,
},
UnaryOp {
operand: Box<IRExpr>,
op: IRUnaryOp,
},
CompareOp {
left: Box<IRExpr>,
right: Box<IRExpr>,
op: IRCompareOp,
},
Param(String),
Variable(String),
FunctionCall {
function_name: String,
arguments: Vec<IRExpr>,
},
BoolOp {
left: Box<IRExpr>,
right: Box<IRExpr>,
op: IRBoolOp,
},
ListLiteral(Vec<IRExpr>),
DictLiteral(Vec<(IRExpr, IRExpr)>),
SetLiteral(Vec<IRExpr>),
TupleLiteral(Vec<IRExpr>),
Indexing {
container: Box<IRExpr>,
index: Box<IRExpr>,
},
Slicing {
container: Box<IRExpr>,
start: Option<Box<IRExpr>>,
end: Option<Box<IRExpr>>,
step: Option<Box<IRExpr>>,
},
Attribute {
object: Box<IRExpr>,
attribute: String,
},
ListComp {
expr: Box<IRExpr>,
var_name: String,
iterable: Box<IRExpr>,
},
MethodCall {
object: Box<IRExpr>,
method_name: String,
arguments: Vec<IRExpr>,
},
DynamicImportExpr {
module_name: Box<IRExpr>,
},
RangeCall {
start: Option<Box<IRExpr>>,
stop: Box<IRExpr>,
step: Option<Box<IRExpr>>,
},
Lambda {
params: Vec<IRParam>,
body: Box<IRExpr>,
captured_vars: Vec<String>, },
}
#[derive(Debug, Clone)]
pub enum IRConstant {
Int(i32),
Float(f64),
Bool(bool),
String(String),
None,
List(Vec<IRConstant>),
Dict(Vec<(IRConstant, IRConstant)>),
Tuple(Vec<IRConstant>),
Bytes(Vec<u8>),
Set(Vec<IRConstant>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum IRType {
Int,
Float,
Bool,
String,
List(Box<IRType>),
Dict(Box<IRType>, Box<IRType>),
Any,
None,
Unknown,
Tuple(Vec<IRType>),
Optional(Box<IRType>),
Union(Vec<IRType>),
Class(String),
Module(String),
Bytes,
Set(Box<IRType>),
Range,
Callable {
params: Vec<IRType>,
return_type: Box<IRType>,
},
Generator(Box<IRType>), Datetime, Date, Time, Timedelta, }
#[derive(Debug, Clone, PartialEq)]
pub enum IROp {
Add, Sub, Mul, Div, Mod, FloorDiv, Pow, MatMul, LShift, RShift, BitOr, BitXor, BitAnd, }
#[derive(Debug, Clone)]
pub enum IRUnaryOp {
Neg, Not, Invert, UAdd, }
#[derive(Debug, Clone)]
pub enum IRCompareOp {
Eq, NotEq, Lt, LtE, Gt, GtE, In, NotIn, Is, IsNot, }
#[derive(Debug, Clone)]
pub enum IRBoolOp {
And, Or, }
#[derive(Debug, Clone)]
pub struct MemoryLayout {
pub string_offsets: std::collections::HashMap<String, u32>,
pub next_string_offset: u32,
pub bytes_offsets: std::collections::HashMap<Vec<u8>, u32>,
pub next_bytes_offset: u32,
pub set_id_counter: u32,
pub object_heap_offset: u32, pub next_object_id: u32, }
impl Default for MemoryLayout {
fn default() -> Self {
Self::new()
}
}
impl MemoryLayout {
pub fn new() -> Self {
MemoryLayout {
string_offsets: std::collections::HashMap::new(),
next_string_offset: 0,
bytes_offsets: std::collections::HashMap::new(),
next_bytes_offset: 32768,
set_id_counter: 0,
object_heap_offset: 65536,
next_object_id: 0,
}
}
pub fn add_string(&mut self, s: &str) -> u32 {
if let Some(&offset) = self.string_offsets.get(s) {
return offset;
}
let offset = self.next_string_offset;
self.string_offsets.insert(s.to_string(), offset);
self.next_string_offset += (s.len() + 1) as u32;
offset
}
pub fn add_bytes(&mut self, b: &[u8]) -> u32 {
if let Some(&offset) = self.bytes_offsets.get(b) {
return offset;
}
let offset = self.next_bytes_offset;
self.bytes_offsets.insert(b.to_vec(), offset);
self.next_bytes_offset += b.len() as u32;
offset
}
pub fn allocate_object(&mut self, size: u32) -> u32 {
let ptr = self.object_heap_offset;
self.object_heap_offset += size;
self.next_object_id += 1;
ptr
}
pub fn allocate_list(&mut self, element_count: u32) -> u32 {
let size = 4 + (element_count * 4);
let ptr = self.object_heap_offset;
self.object_heap_offset += size;
ptr
}
}
impl Default for IRModule {
fn default() -> Self {
Self::new()
}
}
impl IRModule {
pub fn new() -> Self {
IRModule {
functions: Vec::new(),
variables: Vec::new(),
imports: Vec::new(),
classes: Vec::new(),
metadata: std::collections::HashMap::new(),
}
}
}