expr_solver/
metadata.rs

1//! Symbol metadata for bytecode validation and linking.
2
3use crate::symbol::Symbol;
4use serde::{Deserialize, Serialize};
5use std::borrow::Cow;
6
7/// Metadata about a symbol required by compiled bytecode.
8///
9/// This is used to validate and remap symbol indices when linking
10/// bytecode with a symbol table.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct SymbolMetadata {
13    /// The name of the symbol
14    pub name: Cow<'static, str>,
15    /// The kind and requirements of the symbol
16    pub kind: SymbolKind,
17    /// The resolved index in the linked symbol table (None until linked)
18    #[serde(skip)]
19    pub index: Option<usize>,
20}
21
22/// The kind of symbol (constant or function) with its requirements.
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub enum SymbolKind {
25    /// A constant value
26    Const,
27    /// A function with specified arity
28    Func {
29        /// Minimum number of arguments
30        arity: usize,
31        /// Whether the function accepts additional arguments
32        variadic: bool,
33    },
34}
35
36impl From<&Symbol> for SymbolKind {
37    fn from(symbol: &Symbol) -> Self {
38        match symbol {
39            Symbol::Const { .. } => SymbolKind::Const,
40            Symbol::Func { args, variadic, .. } => SymbolKind::Func {
41                arity: *args,
42                variadic: *variadic,
43            },
44        }
45    }
46}
47
48impl From<&Symbol> for SymbolMetadata {
49    fn from(symbol: &Symbol) -> Self {
50        SymbolMetadata {
51            name: symbol.name().to_string().into(),
52            kind: symbol.into(),
53            index: None,
54        }
55    }
56}