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

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

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

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