expr-solver-lib 1.2.0

Mathematical expression evaluator with bytecode compilation and configurable numeric precision (f64 or 128-bit Decimal)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Type-state program implementation for compile-link-execute workflow.
//!
//! This module provides the [`Program`] type which orchestrates the compilation pipeline:
//!
//! 1. **Parsing** - Source code is parsed into an AST using [`Parser`]
//! 2. **IR Generation** - AST is compiled to bytecode using [`IrBuilder`]
//! 3. **Linking** - Bytecode is linked with a symbol table using [`Linker`]
//! 4. **Execution** - Linked bytecode is executed on the VM
//!
//! The type-state pattern ensures these stages occur in the correct order at compile time.

use super::error::{ParseError, ProgramError};
use super::ir_builder::IrBuilder;
use super::linker::Linker;
use super::metadata::SymbolMetadata;
use super::parser::Parser;
use crate::ir::Instr;
use crate::number::Number;
use crate::span::SpanError;
use crate::symtable::SymTable;
use crate::vm::{Vm, VmError};
use colored::Colorize;
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use unicode_width::UnicodeWidthStr;

/// Current version of the program format
const PROGRAM_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Binary format for serialization
#[cfg(feature = "serialization")]
#[derive(Debug, Clone, Serialize, Deserialize)]
struct BinaryFormat {
    version: String,
    bytecode: Vec<Instr>,
    symbols: Vec<SymbolMetadata>,
}

/// Origin of a compiled program.
#[derive(Debug, Clone)]
pub enum ProgramOrigin {
    /// Loaded from a file (path stored)
    #[cfg(feature = "serialization")]
    File(String),
    /// Compiled from source string
    Source,
    /// Deserialized from bytecode bytes
    #[cfg(feature = "serialization")]
    Bytecode,
}

/// Type-state program using Rust's type system to enforce correct usage.
///
/// # Examples
///
/// ```
/// use expr_solver::{num, Program, SymTable};
///
/// // Compile from source
/// let program = Program::new_from_source("x * 2 + 1").unwrap();
///
/// // Link with symbol table
/// let mut table = SymTable::new();
/// table.add_const("x", num!(5), false).unwrap();
/// let mut linked = program.link(table).unwrap();
///
/// // Execute
/// assert_eq!(linked.execute().unwrap(), num!(11));
/// ```
#[derive(Debug)]
pub struct Program<'src, State> {
    source: Option<&'src str>,
    state: State,
}

/// Compiled state - bytecode ready for linking.
#[derive(Debug)]
pub struct Compiled {
    origin: ProgramOrigin,
    version: String,
    bytecode: Vec<Instr>,
    symbols: Vec<SymbolMetadata>,
}

/// Linked state - ready to execute.
#[derive(Debug)]
pub struct Linked {
    #[allow(dead_code)]
    origin: ProgramOrigin,
    version: String,
    bytecode: Vec<Instr>,
    symtable: SymTable,
}

// ============================================================================
// Program - Public constructors (return Compiled state directly)
// ============================================================================

impl<'src> Program<'src, Compiled> {
    // ========================================================================
    // Public API
    // ========================================================================

    /// Creates a compiled program from source code.
    ///
    /// # Examples
    ///
    /// ```
    /// use expr_solver::Program;
    ///
    /// let program = Program::new_from_source("2 + 3 * 4").unwrap();
    /// ```
    pub fn new_from_source(source: &'src str) -> Result<Self, ProgramError> {
        let trimmed = source.trim();

        // Parse
        let mut parser = Parser::new(trimmed);
        let ast_opt = parser.parse().map_err(|parse_err| {
            // Format error with source highlighting
            let highlighted = Self::highlight_error(trimmed, &parse_err);
            ProgramError::ParseError(format!("{}\n{}", parse_err, highlighted))
        })?;

        // Compile (handle empty input by creating empty bytecode)
        let (bytecode, symbols) = if let Some(ast) = ast_opt {
            IrBuilder::new().build(&ast)?
        } else {
            // Empty input -> empty program (VM will return 0)
            (Vec::new(), Vec::new())
        };

        Ok(Program {
            source: Some(trimmed),
            state: Compiled {
                origin: ProgramOrigin::Source,
                version: PROGRAM_VERSION.to_string(),
                bytecode,
                symbols,
            },
        })
    }

    /// Creates a compiled program from a binary file.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use expr_solver::Program;
    ///
    /// let program = Program::new_from_file("expr.bin").unwrap();
    /// ```
    #[cfg(feature = "serialization")]
    pub fn new_from_file(path: impl Into<String>) -> Result<Self, ProgramError> {
        let path_str = path.into();
        let data = std::fs::read(&path_str)?;
        Self::from_bytecode(&data, ProgramOrigin::File(path_str))
    }

    /// Creates a compiled program from bytecode bytes.
    ///
    /// Deserializes the bytecode and validates the version.
    #[cfg(feature = "serialization")]
    pub fn new_from_bytecode(data: &[u8]) -> Result<Self, ProgramError> {
        Self::from_bytecode(data, ProgramOrigin::Bytecode)
    }

    /// Links the bytecode with a symbol table.
    ///
    /// Validates that all required symbols are present and compatible.
    ///
    /// # Examples
    ///
    /// ```
    /// use expr_solver::{Program, SymTable};
    ///
    /// let program = Program::new_from_source("sin(pi)").unwrap();
    /// let linked = program.link(SymTable::stdlib()).unwrap();
    /// ```
    pub fn link(self, table: SymTable) -> Result<Program<'src, Linked>, ProgramError> {
        let linker = Linker::new(self.state.bytecode, self.state.symbols, table);
        let (bytecode, symtable) = linker.link()?;

        Ok(Program {
            source: self.source,
            state: Linked {
                origin: self.state.origin,
                version: self.state.version,
                bytecode,
                symtable,
            },
        })
    }

    // ========================================================================
    // Private helpers
    // ========================================================================

    /// Internal helper to create program from bytecode with a specific origin.
    #[cfg(feature = "serialization")]
    fn from_bytecode(data: &[u8], origin: ProgramOrigin) -> Result<Self, ProgramError> {
        let config = bincode::config::standard();
        let (binary, _): (BinaryFormat, _) = bincode::serde::decode_from_slice(data, config)?;

        // Validate version
        if binary.version != PROGRAM_VERSION {
            return Err(ProgramError::IncompatibleVersion {
                expected: PROGRAM_VERSION.to_string(),
                found: binary.version,
            });
        }

        Ok(Program {
            source: None, // No source for bytecode
            state: Compiled {
                origin,
                version: binary.version,
                bytecode: binary.bytecode,
                symbols: binary.symbols,
            },
        })
    }

    /// Highlights an error in the source code.
    fn highlight_error(input: &str, error: &ParseError) -> String {
        let span = error.span();
        let pre = Self::escape(&input[..span.start]);
        let tok = Self::escape(&input[span.start..span.end]);
        let post = Self::escape(&input[span.end..]);
        let line = format!("{}{}{}", pre, tok.red().bold(), post);

        let caret = "^".green().bold();
        let squiggly_len = UnicodeWidthStr::width(tok.as_str());
        let caret_offset = UnicodeWidthStr::width(pre.as_str()) + caret.len();

        format!(
            "1 | {0}\n  | {1: >2$}{3}",
            line,
            caret,
            caret_offset,
            "~".repeat(squiggly_len.saturating_sub(1)).green()
        )
    }

    /// Escapes special characters for display.
    fn escape(s: &str) -> String {
        let mut out = String::with_capacity(s.len());
        for c in s.chars() {
            match c {
                '\n' => out.push_str("\\n"),
                '\r' => out.push_str("\\r"),
                other => out.push(other),
            }
        }
        out
    }
}

// ============================================================================
// Program<Linked> - After linking, ready to execute
// ============================================================================

impl<'src> Program<'src, Linked> {
    // ========================================================================
    // Public API
    // ========================================================================

    /// Executes the program and returns the result.
    pub fn execute(&mut self) -> Result<Number, VmError> {
        Vm::run(&self.state.bytecode, &mut self.state.symtable)
    }

    /// Returns a mutable reference to the symbol table.
    pub fn symtable_mut(&mut self) -> &mut SymTable {
        &mut self.state.symtable
    }

    /// Returns a human-readable assembly representation of the program.
    pub fn get_assembly(&self) -> String {
        use std::fmt::Write as _;

        let mut out = String::new();
        out += &format!("; VERSION {}\n", self.state.version)
            .bright_black()
            .to_string();

        for (i, instr) in self.state.bytecode.iter().enumerate() {
            let _ = write!(out, "{} ", format!("{:04X}", i).yellow());
            let line = match instr {
                Instr::Push(v) => format!("{} {}", "PUSH".magenta(), v.to_string().green()),
                Instr::Load(idx) => {
                    let sym_name = self
                        .state
                        .symtable
                        .get_by_index(*idx)
                        .map(|s| s.name())
                        .expect("Symbol not found in assembly");
                    format!("{} {}", "LOAD".magenta(), sym_name.blue())
                }
                Instr::Store(idx) => {
                    let sym_name = self
                        .state
                        .symtable
                        .get_by_index(*idx)
                        .map(|s| s.name())
                        .expect("Symbol not found in assembly");
                    format!("{} {}", "STORE".magenta(), sym_name.blue())
                }
                Instr::Neg => format!("{}", "NEG".magenta()),
                Instr::Add => format!("{}", "ADD".magenta()),
                Instr::Sub => format!("{}", "SUB".magenta()),
                Instr::Mul => format!("{}", "MUL".magenta()),
                Instr::Div => format!("{}", "DIV".magenta()),
                Instr::Pow => format!("{}", "POW".magenta()),
                Instr::Fact => format!("{}", "FACT".magenta()),
                Instr::Call(idx, argc) => {
                    let sym_name = self
                        .state
                        .symtable
                        .get_by_index(*idx)
                        .map(|s| s.name())
                        .expect("Symbol not found in assembly");
                    format!(
                        "{} {} args: {}",
                        "CALL".magenta(),
                        sym_name.cyan(),
                        argc.to_string().bright_blue()
                    )
                }
                Instr::Equal => format!("{}", "EQ".magenta()),
                Instr::NotEqual => format!("{}", "NEQ".magenta()),
                Instr::Less => format!("{}", "LT".magenta()),
                Instr::LessEqual => format!("{}", "LTE".magenta()),
                Instr::Greater => format!("{}", "GT".magenta()),
                Instr::GreaterEqual => format!("{}", "GTE".magenta()),
                Instr::Jmp(target) => {
                    format!("{} {}", "JMP".magenta(), format!("{:04X}", target).yellow())
                }
                Instr::Jz(target) => {
                    format!("{} {}", "JZ".magenta(), format!("{:04X}", target).yellow())
                }
            };
            let _ = writeln!(out, "{}", line);
        }
        out
    }

    /// Converts the program to bytecode bytes.
    ///
    /// This involves reverse-mapping the bytecode indices back to metadata indices.
    #[cfg(feature = "serialization")]
    pub fn to_bytecode(&self) -> Result<Vec<u8>, ProgramError> {
        use std::collections::HashMap;

        let mut reverse_map = HashMap::new();
        let mut symbols = Vec::new();

        // Helper closure to get or create metadata index
        // All indices are valid since we successfully linked
        let mut get_or_create_metadata = |idx: usize| -> usize {
            if let Some(&existing) = reverse_map.get(&idx) {
                existing
            } else {
                let symbol = self
                    .state
                    .symtable
                    .get_by_index(idx)
                    .expect("symbol index must be valid after linking");

                let new_idx = symbols.len();
                symbols.push(symbol.into());
                reverse_map.insert(idx, new_idx);
                new_idx
            }
        };

        // Single pass: build symbol mapping and rewrite bytecode
        let bytecode: Vec<Instr> = self
            .state
            .bytecode
            .iter()
            .map(|instr| match instr {
                Instr::Load(idx) => Instr::Load(get_or_create_metadata(*idx)),
                Instr::Store(idx) => Instr::Store(get_or_create_metadata(*idx)),
                Instr::Call(idx, argc) => Instr::Call(get_or_create_metadata(*idx), *argc),
                other => other.clone(),
            })
            .collect();

        // Serialize
        let binary = BinaryFormat {
            version: self.state.version.clone(),
            bytecode,
            symbols,
        };

        let config = bincode::config::standard();
        Ok(bincode::serde::encode_to_vec(&binary, config)?)
    }

    /// Saves the program bytecode to a file.
    #[cfg(feature = "serialization")]
    pub fn save_bytecode_to_file(
        &self,
        path: impl AsRef<std::path::Path>,
    ) -> Result<(), ProgramError> {
        let bytecode = self.to_bytecode()?;
        std::fs::write(path, bytecode)?;
        Ok(())
    }
}