1
//! `QUOTE` special form. Evaluation returns the argument verbatim;
2
//! compilation routes through `compile_quoted_expr` so list literals
3
//! materialize via the proper cons-cell allocator instead of being
4
//! re-evaluated.
5

            
6
use crate::ast::Expr;
7
use crate::compiler::context::CompileContext;
8
use crate::compiler::emit::FunctionEmitter;
9
use crate::compiler::expr::compile_quoted_expr;
10
use crate::error::{Error, Result};
11
use crate::runtime::SymbolTable;
12

            
13
664
pub(super) fn eval_quote(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
14
664
    quote(args)
15
664
}
16

            
17
166
pub(super) fn spec_compile_quote(
18
166
    ctx: &mut CompileContext,
19
166
    emit: &mut FunctionEmitter,
20
166
    _symbols: &mut SymbolTable,
21
166
    args: &[Expr],
22
166
) -> Result<()> {
23
166
    compile_quote(ctx, emit, args)
24
166
}
25

            
26
166
pub(super) fn compile_quote(
27
166
    ctx: &mut CompileContext,
28
166
    emit: &mut FunctionEmitter,
29
166
    args: &[Expr],
30
166
) -> Result<()> {
31
166
    if args.len() != 1 {
32
        return Err(Error::Arity {
33
            name: "quote".to_string(),
34
            expected: 1,
35
            actual: args.len(),
36
        });
37
166
    }
38
166
    compile_quoted_expr(ctx, emit, &args[0])
39
166
}
40

            
41
664
pub(super) fn quote(args: &[Expr]) -> Result<Expr> {
42
664
    if args.len() != 1 {
43
        return Err(Error::Arity {
44
            name: "quote".to_string(),
45
            expected: 1,
46
            actual: args.len(),
47
        });
48
664
    }
49
664
    Ok(args[0].clone())
50
664
}