1
//! Quasiquote data that lowers to its printed form.
2

            
3
use crate::ast::{Expr, WasmType};
4
use crate::compiler::context::CompileContext;
5
use crate::compiler::emit::FunctionEmitter;
6
use crate::error::Result;
7

            
8
use super::format::format_expr;
9
use super::quoted_datum;
10

            
11
/// A quoted (un)quasiquoted form: the data that lowers to its printed form,
12
/// having no `$pair` representation.
13
#[must_use]
14
8210009
pub(super) fn is_printed_datum(expr: &Expr) -> bool {
15
8210009
    matches!(
16
8210009
        quoted_datum(expr),
17
        Some(Expr::Quasiquote(_) | Expr::Unquote(_) | Expr::UnquoteSplicing(_))
18
    )
19
8210009
}
20

            
21
/// Pushes the printed form of a quasiquote datum as a `StringRef` from a data
22
/// segment. A list datum never comes here: it is a
23
/// `$pair` chain.
24
210
pub(super) fn render_datum_string(
25
210
    ctx: &mut CompileContext,
26
210
    emit: &mut FunctionEmitter,
27
210
    datum: &Expr,
28
210
) -> Result<WasmType> {
29
210
    let rendered = format_expr(datum);
30
210
    let data_idx = ctx.add_data(rendered.as_bytes())?;
31
210
    emit.i32_const(0);
32
210
    emit.i32_const(rendered.len() as i32);
33
210
    emit.array_new_data(ctx.ids.ty_i8_array, data_idx);
34
210
    Ok(WasmType::StringRef)
35
210
}