Compiler

Struct Compiler 

Source
struct Compiler<'a> {
    code: Vec<Op>,
    consts: Vec<f64>,
    ctx: &'a mut MilkContext,
}

Fields§

§code: Vec<Op>§consts: Vec<f64>§ctx: &'a mut MilkContext

Borrowed context — used to intern cold-variable names into the slab so each LoadCold/StoreCold opcode carries a slab index rather than a name.

Implementations§

Source§

impl<'a> Compiler<'a>

Source

fn new(ctx: &'a mut MilkContext) -> Self

Source

fn emit(&mut self, op: Op)

Source

fn add_const(&mut self, f: f64) -> u32

Source

fn add_cold(&mut self, name: &str) -> u32

Intern a cold-variable name into MilkContext::cold and return its slab index. Subsequent references to the same name reuse the existing slot; auto-seeded MD2 defaults (bass, zoom, …) hit pre-existing slots without growing the slab.

Source

fn finish(self) -> CompiledBytecode

Source

fn compile_node(&mut self, node: &Node) -> Result<(), CompileError>

Compile a node so that it leaves exactly one f64 on the stack.

Source

fn emit_read(&mut self, name: &str)

Emit either LoadHot(i), LoadQ(i), or LoadCold(name_idx) depending on which class the identifier falls into.

Source

fn emit_write(&mut self, name: &str)

Source

fn compile_unary( &mut self, children: &[Node], op: Op, ) -> Result<(), CompileError>

Source

fn compile_binary( &mut self, children: &[Node], op: Op, ) -> Result<(), CompileError>

Source

fn compile_assign( &mut self, children: &[Node], compound: Option<Op>, ) -> Result<(), CompileError>

Compile x = expr (when compound is None) or x op= expr (when compound is Some(arith_op)). In both cases the statement leaves a placeholder 0.0 on the stack so the outer statement-discard Pop stays balanced.

Source

fn compile_call( &mut self, name: &str, children: &[Node], ) -> Result<(), CompileError>

Lower a function call. evalexpr wraps every parenthesised subexpression in a RootNode, so a call like pow(2, 5) lands as FunctionIdentifier → RootNode → Tuple → [RootNode→2, RootNode→5]. We unwrap the outer RootNode, then either split a Tuple into args or treat the whole thing as a single arg.

Auto Trait Implementations§

§

impl<'a> Freeze for Compiler<'a>

§

impl<'a> !RefUnwindSafe for Compiler<'a>

§

impl<'a> Send for Compiler<'a>

§

impl<'a> Sync for Compiler<'a>

§

impl<'a> Unpin for Compiler<'a>

§

impl<'a> !UnwindSafe for Compiler<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.