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
568
pub(super) fn eval_quote(_symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
14
568
    quote(args)
15
568
}
16

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

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

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