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
//! # lambda-throw-cat
//!
//! Lambda calculus with records, prototype chains, ref cells, a tracing GC,
//! and non-local control flow via `throw` and `try`/`catch`. Spike 4 of a
//! comp-cat-rs web-engine reformulation targeting Tauri.
//!
//! ## Quick start
//!
//! ```
//! # fn main() -> Result<(), lambda_throw_cat::error::Error> {
//! use lambda_throw_cat::run;
//!
//! let source = r"
//! try
//! throw (\msg. msg)
//! catch e. e
//! ";
//! let value = run(source).run()?;
//! assert_eq!(format!("{value}"), "\\msg. msg");
//! # Ok(())
//! # }
//! ```
//!
//! ## Grammar
//!
//! ```text
//! expr ::= seq_expr
//! seq_expr ::= right_expr (";" right_expr)*
//! right_expr ::= lambda | let | fix | extend | throw_expr | try_expr | assign_expr
//! lambda ::= "\" ident "." expr
//! let ::= "let" ident "=" expr "in" expr
//! fix ::= "fix" ident "." expr
//! extend ::= "extend" atom object_lit
//! throw_expr ::= "throw" right_expr
//! try_expr ::= "try" right_expr "catch" ident "." right_expr
//! assign_expr ::= app_expr (":=" right_expr)?
//! app_expr ::= atom atom*
//! atom ::= base_atom ("." ident)*
//! base_atom ::= ident | "(" expr ")" | "ref" atom | "!" atom | object_lit
//! object_lit ::= "{" props? "}"
//! props ::= property ("," property)*
//! property ::= ident "=" right_expr
//! ```
use Io;
use crateEnv;
use crateError;
use crateFuel;
use crateHeap;
use crateValue;
/// Default step budget used by [`run`].
pub const DEFAULT_FUEL: u64 = 10_000;
/// Lex, parse, and evaluate `source` with the default fuel budget.
///
/// # Errors
///
/// See [`Error`]. An uncaught `throw` surfaces as [`Error::UncaughtException`].
///
/// [`Error::UncaughtException`]: crate::error::Error::UncaughtException
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), lambda_throw_cat::error::Error> {
/// use lambda_throw_cat::run;
///
/// let value = run(r"try throw (\x. x) catch e. e").run()?;
/// assert_eq!(format!("{value}"), "\\x. x");
/// # Ok(())
/// # }
/// ```
/// Lex, parse, and evaluate `source` with a caller-supplied fuel budget.
///
/// # Errors
///
/// See [`Error`].
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), lambda_throw_cat::error::Error> {
/// use lambda_throw_cat::eval::Fuel;
/// use lambda_throw_cat::run_with_fuel;
///
/// let value = run_with_fuel(r"try throw (\x. x) catch e. e", Fuel::new(100)).run()?;
/// assert_eq!(format!("{value}"), "\\x. x");
/// # Ok(())
/// # }
/// ```
/// Lex, parse, and evaluate `source`, returning both the value and the final
/// heap. Useful for tests that need to verify GC behaviour.
///
/// # Errors
///
/// See [`Error`].
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), lambda_throw_cat::error::Error> {
/// use lambda_throw_cat::eval::Fuel;
/// use lambda_throw_cat::run_inspecting;
///
/// let (_value, heap) = run_inspecting(r"{}", Fuel::new(100)).run()?;
/// assert_eq!(heap.len(), 1);
/// # Ok(())
/// # }
/// ```