1use pyo3::prelude::*;
18use pyo3::wrap_pyfunction;
19
20mod lexer;
21mod parser;
22mod env;
23mod eval;
24mod interp;
25mod arena;
26mod rust_jit;
27mod graph_ir;
28mod checkpoint;
29mod trace;
30mod type_check;
31mod effect_check;
32mod kg;
33
34use eval::Evaluator;
35use interp::{make_env, run_code, print_repr};
36
37#[pyclass(name = "Rusty")]
42pub struct RustyInterp;
43
44#[pymethods]
45impl RustyInterp {
46 #[new]
47 fn new() -> Self { RustyInterp }
48
49 fn eval(&self, code: &str) -> PyResult<String> {
51 let env = make_env();
52 match run_code(code, &env, &Evaluator::new()) {
53 Ok(v) => Ok(print_repr(&v)),
54 Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e)),
55 }
56 }
57
58 fn eval_repr(&self, code: &str) -> PyResult<String> {
60 let env = make_env();
61 match run_code(code, &env, &Evaluator::new()) {
62 Ok(v) => Ok(format!("{}", v)),
63 Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e)),
64 }
65 }
66
67 fn check(&self, code: &str) -> bool {
69 let tokens = lexer::Lexer::new(code).tokenize();
70 !parser::Parser::new(tokens).parse().is_empty()
71 }
72
73 fn __repr__(&self) -> &str { "<Rusty interpreter>" }
74}
75
76#[pyclass]
81pub struct RustySession {
82 history: Vec<String>,
83}
84
85#[pymethods]
86impl RustySession {
87 #[new]
88 fn new() -> Self { RustySession { history: Vec::new() } }
89
90 fn eval(&mut self, code: &str) -> PyResult<String> {
92 let env = make_env();
93 let eval = Evaluator::new();
94 for prev in &self.history {
95 let _ = run_code(prev, &env, &eval);
96 }
97 match run_code(code, &env, &eval) {
98 Ok(v) => {
99 self.history.push(code.to_string());
100 Ok(print_repr(&v))
101 }
102 Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e)),
103 }
104 }
105
106 fn reset(&mut self) { self.history.clear(); }
108
109 fn history_len(&self) -> usize { self.history.len() }
111
112 fn __repr__(&self) -> String {
113 format!("<RustySession history={}>", self.history.len())
114 }
115}
116
117#[pymodule]
120fn rusty(m: &Bound<'_, PyModule>) -> PyResult<()> {
121 m.add_class::<RustyInterp>()?;
122 m.add_class::<RustySession>()?;
123 m.add_function(wrap_pyfunction!(eval_fn, m)?)?;
124 Ok(())
125}
126
127#[pyfunction]
129#[pyo3(name = "eval")]
130fn eval_fn(code: &str) -> PyResult<String> {
131 let env = make_env();
132 match run_code(code, &env, &Evaluator::new()) {
133 Ok(v) => Ok(print_repr(&v)),
134 Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e)),
135 }
136}