1
//! `QUOTE` special form. Evaluation returns `Expr::Datum(datum)`, the same
2
//! value the reader's `'x` evaluates to, so both spellings are one value on the eval
3
//! surface (P8's reader half is cut, ISS-040); compilation routes through
4
//! `Expr::quoted` and `compile_expr` so list literals materialize via the proper
5
//! cons-cell allocator instead of being re-evaluated. In effect position a
6
//! quote form is a no-op.
7

            
8
use crate::ast::{Expr, WasmType};
9
use crate::compiler::context::CompileContext;
10
use crate::compiler::emit::FunctionEmitter;
11
use crate::compiler::expr::{compile_expr, compile_for_stack};
12
use crate::error::{Error, Result};
13
use crate::runtime::SymbolTable;
14

            
15
16152
pub(super) fn eval_quote(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
16
16152
    Ok(Expr::quoted(quote(args)?))
17
16152
}
18

            
19
3835
pub(super) fn compile_quote_effect(
20
3835
    _ctx: &mut CompileContext,
21
3835
    _emit: &mut FunctionEmitter,
22
3835
    _symbols: &mut SymbolTable,
23
3835
    args: &[Expr],
24
3835
) -> Result<()> {
25
3835
    quote(args).map(drop)
26
3835
}
27

            
28
422
pub(super) fn spec_compile_quote(
29
422
    ctx: &mut CompileContext,
30
422
    emit: &mut FunctionEmitter,
31
422
    symbols: &mut SymbolTable,
32
422
    args: &[Expr],
33
422
) -> Result<()> {
34
422
    compile_quote(ctx, emit, symbols, args)
35
422
}
36

            
37
422
pub(super) fn compile_quote(
38
422
    ctx: &mut CompileContext,
39
422
    emit: &mut FunctionEmitter,
40
422
    symbols: &mut SymbolTable,
41
422
    args: &[Expr],
42
422
) -> Result<()> {
43
422
    compile_expr(ctx, emit, symbols, &Expr::quoted(quote(args)?))
44
422
}
45

            
46
42
pub(super) fn compile_quote_for_stack(
47
42
    ctx: &mut CompileContext,
48
42
    emit: &mut FunctionEmitter,
49
42
    symbols: &mut SymbolTable,
50
42
    args: &[Expr],
51
42
) -> Result<WasmType> {
52
42
    let datum = quote(args)?;
53
42
    compile_for_stack(ctx, emit, symbols, &Expr::quoted(datum))
54
42
}
55

            
56
20451
pub(super) fn quote(args: &[Expr]) -> Result<Expr> {
57
20451
    if args.len() != 1 {
58
2
        return Err(Error::Arity {
59
2
            name: "quote".to_string(),
60
2
            expected: 1,
61
2
            actual: args.len(),
62
2
        });
63
20449
    }
64
20449
    Ok(args[0].clone())
65
20451
}
66

            
67
#[cfg(test)]
68
#[path = "quote_tests.rs"]
69
mod tests;