Lines
98.2 %
Functions
83.33 %
Branches
100 %
//! Primitive emit helpers — nil, bool, number, string, symbol — plus
//! the `push_ratio` numeric kernel and the `compile_text` machinery
//! `compile_string` and `compile_symbol` share.
use scripting_format::ValueType;
use uuid::Uuid;
use crate::ast::{Fraction, Number, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::layout::GcLocals;
use crate::error::{Error, Result};
use super::{LOCAL_GC_ARR, LOCAL_IDX};
pub(in crate::compiler) fn compile_nil(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
) -> Result<()> {
ctx.serializer().write_debug_nil(emit)
}
/// Emits a typed nil stack value for `ty` — the canonical nil for a
/// binding/accumulator/arm-reconciliation result declared with that wasm
/// type. Routes through [`nil_repr`] (ADR-0033): a `Never` type (an Index,
/// Scalar, Money or Closure) has no valid nil and errors rather than
/// fabricate a made-up zero — the nil-punning the Common Lisp boolean model
/// retires. The message names the fix regardless of which `Never` type
/// triggered it, since the remedy is the same for all of them.
pub(in crate::compiler) fn emit_nil_default(
ctx: &CompileContext,
ty: WasmType,
match ty {
// `nil_repr` classifies these `Never`: no fabricated zero, ever.
// Pinned against this match by
// `nil_repr_covers_every_wasm_type_and_matches_emit_nil_default`.
WasmType::I32 | WasmType::Ratio | WasmType::Commodity | WasmType::Closure(_) => Err(
Error::Compile("nil is not a scalar; initialise with 0".to_string()),
),
WasmType::Bool => {
emit.i32_const(0);
Ok(())
WasmType::StringRef => {
emit.ref_null(ctx.ids.ty_i8_array);
WasmType::SymbolRef => {
emit.ref_null(ctx.ids.ty_symbol);
WasmType::PairRef(_) => {
emit.ref_null(ctx.ids.ty_pair);
WasmType::EntityRef(kind) => {
emit.ref_null(ctx.ids.entity_type(kind));
WasmType::StructRef(_) => {
emit.ref_null(ctx.ids.ty_struct);
WasmType::AnyRef => {
emit.ref_null_any();
pub(in crate::compiler) fn compile_t(
ctx.serializer().write_debug_t(emit)
pub(in crate::compiler) fn compile_number(
number: Number,
ctx.serializer().write_debug_number(emit, number)
fn compile_text(
value_type: ValueType,
s: &str,
let data_idx = ctx.add_data(s.as_bytes())?;
let gc = GcLocals {
type_idx: ctx.ids.ty_i8_array,
arr: LOCAL_GC_ARR,
idx: LOCAL_IDX,
};
let len = u32::try_from(s.len())
.map_err(|_| Error::Compile(format!("string literal of {} bytes exceeds u32", s.len())))?;
ctx.serializer()
.write_debug_string_gc(emit, value_type, data_idx, len, &gc)
pub(in crate::compiler) fn compile_string(
compile_text(ctx, emit, ValueType::String, s)
pub(in crate::compiler) fn compile_symbol(
compile_text(ctx, emit, ValueType::Symbol, s)
/// Pushes a `$symbol` whose name is `name`, the printed name.
pub(in crate::compiler) fn push_symbol(
name: &str,
) -> Result<WasmType> {
let data_idx = ctx.add_data(name.as_bytes())?;
let len = i32::try_from(name.len())
.map_err(|_| Error::Compile(format!("symbol of {} bytes exceeds i32", name.len())))?;
emit.i32_const(len);
emit.array_new_data(ctx.ids.ty_i8_array, data_idx);
emit.struct_new(ctx.ids.ty_symbol);
Ok(WasmType::SymbolRef)
pub(in crate::compiler) fn push_ratio(
numer: i64,
denom: i64,
) {
emit.i64_const(numer);
emit.i64_const(denom);
emit.call(ctx.ids.ratio_new);
/// Pushes an atomic money value: the rational and the commodity's UUID halves,
/// built by the module's `commodity_new` (null unit term).
pub(in crate::compiler) fn push_money(
amount: Fraction,
commodity_id: Uuid,
let (hi, lo) = commodity_id.as_u64_pair();
emit.i64_const(*amount.numer());
emit.i64_const(*amount.denom());
emit.i64_const(hi as i64);
emit.i64_const(lo as i64);
emit.call(ctx.ids.commodity_new);
#[cfg(test)]
#[path = "atoms_tests.rs"]
mod tests;