use std::collections::HashMap; use crate::module::CompiledModule; use crate::opcode::{self, OpCode}; use crate::symbol_table::{Binding, SymbolTable}; pub use crate::hash::structural_hash; pub use crate::module::Constant; use crate::symbol_table::ScopeKind; pub use oxide_parser::VariableDeclarationKind; pub use oxide_parser::{AssignmentOperator, BinaryOperator, Expression, Statement, UnaryOperator}; pub struct Compiler; pub(crate) fn is_int_literal(value: f64) -> bool { value.fract() == 0.0 && value >= i32::MIN as f64 && value <= i32::MAX as f64 } pub(crate) fn is_side_effect_free(expr: &Expression) -> bool { matches!( expr, Expression::NumericLiteral(_) | Expression::StringLiteral(_) | Expression::BooleanLiteral(_) | Expression::NullLiteral(_) | Expression::Identifier(_) | Expression::RegExpLiteral(_) ) } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[allow(dead_code)] pub(crate) enum Label { IfElse(u32), IfEnd(u32), WhileStart(u32), WhileEnd(u32), ForStart(u32), ForUpdate(u32), ForEnd(u32), TernaryEnd(u32), TernaryElse(u32), DoWhileStart(u32), DoWhileEnd(u32), ForInStart(u32), ForInEnd(u32), SwitchEnd(u32), SwitchCase(u32, u32), CatchBody(u32), FinallyBody(u32), TryEnd(u32), } pub(crate) struct CompileCtx { pub(crate) bytecode: Vec, pub(crate) constants: Vec, constant_map: HashMap, next_reg: u8, pub(crate) max_regs: u8, reserved_reg_start: u8, symbols: SymbolTable, pub(crate) label_map: HashMap, pub(crate) loop_stack: Vec<(Label, Label)>, #[allow(dead_code)] pub(crate) switch_stack: Vec