Skip to content

Commit 9c6fe33

Browse files
committed
Added Type Parsing
1 parent 311adcf commit 9c6fe33

File tree

11 files changed

+798
-179
lines changed

11 files changed

+798
-179
lines changed

Cargo.lock

Lines changed: 40 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/benda/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ name = "benda"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
bend-lang = "=0.2.18"
12+
pyo3 = { version = "0.21.2", features = ["multiple-pymethods"] }
13+
bend-lang = "0.2.33"
1314
num-bigint = "0.4.5"
1415
num-traits = "0.2.19"
15-
pyo3 = "0.21.2"
1616
rustpython-parser = "0.3.1"
1717
indexmap = "2.2.3"
18+
interner = "0.2.1"

crates/benda/src/benda_ffi/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use bend::diagnostics::{Diagnostics, DiagnosticsConfig};
2-
use bend::fun::{Book, Term};
3-
use bend::{CompileOpts, RunOpts};
1+
use bend::{
2+
diagnostics::{Diagnostics, DiagnosticsConfig},
3+
fun::{Book, Term},
4+
CompileOpts, RunOpts,
5+
};
46

57
pub fn run(book: &Book) -> Option<(Term, String, Diagnostics)> {
6-
let run_opts = RunOpts {
7-
linear_readback: false,
8-
pretty: false,
9-
};
10-
let compile_opts = CompileOpts::default();
8+
let run_opts = RunOpts::default();
9+
let compile_opts = CompileOpts::default().set_all();
1110
let diagnostics_cfg = DiagnosticsConfig::default();
1211
let args = None;
1312

@@ -17,7 +16,7 @@ pub fn run(book: &Book) -> Option<(Term, String, Diagnostics)> {
1716
compile_opts,
1817
diagnostics_cfg,
1918
args,
20-
"run-c",
19+
"run",
2120
)
2221
.unwrap()
2322
}

crates/benda/src/lib.rs

Lines changed: 127 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,135 @@
1+
use bend::imp;
12
use num_traits::ToPrimitive;
23
use parser::Parser;
3-
use pyo3::prelude::*;
4-
use pyo3::types::{PyFunction, PyString, PyTuple};
4+
use pyo3::{
5+
prelude::*,
6+
types::{PyDict, PyFunction, PyString, PyTuple},
7+
};
58
use rustpython_parser::{parse, Mode};
6-
use types::u24::u24;
9+
use types::tree::Tree;
10+
use types::{
11+
extract_type,
12+
tree::{Leaf, Node},
13+
u24::u24,
14+
};
715
mod benda_ffi;
816
mod parser;
917
mod types;
1018

11-
#[pyfunction]
12-
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
13-
Ok((a + b).to_string())
14-
}
15-
1619
#[pyfunction]
1720
fn switch() -> PyResult<String> {
1821
Ok("Ok".to_string())
1922
}
2023

24+
#[pyclass(name = "bjit")]
25+
pub struct PyBjit {
26+
wraps: Py<PyAny>,
27+
}
28+
29+
#[pymethods]
30+
impl PyBjit {
31+
#[new]
32+
fn __new__(wraps: Py<PyAny>) -> Self {
33+
PyBjit { wraps }
34+
}
35+
#[pyo3(signature = (*args, **_kwargs))]
36+
fn __call__(
37+
&self,
38+
py: Python<'_>,
39+
args: &Bound<'_, PyTuple>,
40+
_kwargs: Option<&Bound<'_, PyDict>>,
41+
) -> PyResult<Py<PyAny>> {
42+
let arg_names_temp: Bound<PyAny>;
43+
44+
let (name, filename, arg_names, argcount) =
45+
match self.wraps.downcast_bound::<PyFunction>(py) {
46+
Ok(inner) => {
47+
let name = inner.getattr("__name__").unwrap();
48+
let code = inner.getattr("__code__").unwrap();
49+
let filename = code.getattr("co_filename").unwrap();
50+
51+
arg_names_temp = code.getattr("co_varnames").unwrap();
52+
let arg_names = arg_names_temp.downcast::<PyTuple>().unwrap();
53+
let argcount = code
54+
.getattr("co_argcount")
55+
.unwrap()
56+
.to_string()
57+
.parse::<u32>()
58+
.unwrap();
59+
60+
(name, filename, arg_names, argcount)
61+
}
62+
Err(_) => todo!(),
63+
};
64+
65+
let mut arg_list: Vec<String> = vec![];
66+
67+
for (index, arg) in arg_names.iter().enumerate() {
68+
if index >= argcount.to_usize().unwrap() {
69+
break;
70+
}
71+
72+
arg_list.push(arg.to_string());
73+
}
74+
75+
let mut parsed_types: Vec<(String, imp::Expr)> = vec![];
76+
77+
for (index, arg) in args.downcast::<PyTuple>().unwrap().iter().enumerate() {
78+
parsed_types.push((
79+
arg_list.get(index).unwrap().to_string(),
80+
extract_type(arg).unwrap(),
81+
));
82+
}
83+
84+
let code = std::fs::read_to_string(filename.to_string()).unwrap();
85+
let module = parse(code.as_str(), Mode::Module, "main.py").unwrap();
86+
87+
let mut val: Option<Bound<PyString>> = None;
88+
89+
match module {
90+
rustpython_parser::ast::Mod::Module(mods) => {
91+
for (index, stmt) in mods.body.iter().enumerate() {
92+
if let rustpython_parser::ast::Stmt::FunctionDef(fun_def) = stmt {
93+
if fun_def.name == name.to_string() {
94+
let mut parser =
95+
Parser::new(mods.body.clone(), index, parsed_types.clone());
96+
let return_val = parser.parse(fun_def.name.as_ref(), &[]);
97+
val = Some(PyString::new_bound(py, return_val.as_str()));
98+
break;
99+
}
100+
}
101+
}
102+
}
103+
_ => unimplemented!(),
104+
}
105+
106+
Ok(val.unwrap().into())
107+
}
108+
}
109+
21110
#[pyfunction]
22-
fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
111+
fn bjit_test(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
23112
let arg_names_temp: Bound<PyAny>;
24113

25-
let (name, filename, arg_names, argcount) =
26-
match fun.clone().downcast::<PyFunction>() {
27-
Ok(inner) => {
28-
let name = inner.getattr("__name__").unwrap();
29-
let code = inner.getattr("__code__").unwrap();
30-
let filename = code.getattr("co_filename").unwrap();
31-
32-
arg_names_temp = code.getattr("co_varnames").unwrap();
33-
let arg_names = arg_names_temp.downcast::<PyTuple>().unwrap();
34-
let argcount = code
35-
.getattr("co_argcount")
36-
.unwrap()
37-
.to_string()
38-
.parse::<u32>()
39-
.unwrap();
40-
41-
(name, filename, arg_names, argcount)
42-
}
43-
Err(_) => todo!(),
44-
};
114+
let (name, filename, arg_names, argcount) = match fun.clone().downcast::<PyFunction>() {
115+
Ok(inner) => {
116+
let name = inner.getattr("__name__").unwrap();
117+
let code = inner.getattr("__code__").unwrap();
118+
let filename = code.getattr("co_filename").unwrap();
119+
120+
arg_names_temp = code.getattr("co_varnames").unwrap();
121+
let arg_names = arg_names_temp.downcast::<PyTuple>().unwrap();
122+
let argcount = code
123+
.getattr("co_argcount")
124+
.unwrap()
125+
.to_string()
126+
.parse::<u32>()
127+
.unwrap();
128+
129+
(name, filename, arg_names, argcount)
130+
}
131+
Err(_) => todo!(),
132+
};
45133

46134
let code = std::fs::read_to_string(filename.to_string()).unwrap();
47135
let module = parse(code.as_str(), Mode::Module, "main.py").unwrap();
@@ -56,19 +144,16 @@ fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
56144
arg_list.push(arg.to_string());
57145
}
58146

59-
let mut val: Option<Bound<PyString>> = None;
147+
let val: Option<Bound<PyString>> = None;
60148

61149
match module {
62150
rustpython_parser::ast::Mod::Module(mods) => {
63151
for stmt in mods.body.iter() {
64-
if let rustpython_parser::ast::Stmt::FunctionDef(fun_def) = stmt
65-
{
152+
if let rustpython_parser::ast::Stmt::FunctionDef(fun_def) = stmt {
66153
if fun_def.name == name.to_string() {
67-
let mut parser = Parser::new(mods.body.clone(), 0);
68-
let return_val =
69-
parser.parse(fun_def.name.as_ref(), &arg_list);
70-
val =
71-
Some(PyString::new_bound(py, return_val.as_str()));
154+
//let mut parser = Parser::new(mods.body.clone(), 0);
155+
//let return_val = parser.parse(fun_def.name.as_ref(), &arg_list);
156+
//val = Some(PyString::new_bound(py, return_val.as_str()));
72157
}
73158
}
74159
}
@@ -94,12 +179,14 @@ fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
94179
Ok(fun)
95180
}
96181

97-
/// A Python module implemented in Rust.
98182
#[pymodule]
99183
fn benda(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
100-
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
101-
m.add_function(wrap_pyfunction!(bjit, m)?)?;
184+
m.add_function(wrap_pyfunction!(bjit_test, m)?)?;
102185
m.add_function(wrap_pyfunction!(switch, m)?)?;
186+
m.add_class::<PyBjit>()?;
103187
m.add_class::<u24>()?;
188+
m.add_class::<Tree>()?;
189+
m.add_class::<Node>()?;
190+
m.add_class::<Leaf>()?;
104191
Ok(())
105192
}

crates/benda/src/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
mod parser;
22

3-
use parser::Parser;
43
use pyo3::prelude::*;
4+
55
use rustpython_parser::{parse, Mode};
66

77
mod benda_ffi;
88

99
fn main() -> PyResult<()> {
1010
let filename = String::from("main.py");
1111

12-
let code = std::fs::read_to_string(&filename).unwrap();
12+
let code = std::fs::read_to_string(filename).unwrap();
1313
let module = parse(code.as_str(), Mode::Module, "main.py").unwrap();
1414

1515
match module {
16-
rustpython_parser::ast::Mod::Module(mods) => {
17-
let mut parser = Parser::new(mods.body, 0);
18-
parser.parse(&String::from("sum_tree"), &["tree".to_string()]);
16+
rustpython_parser::ast::Mod::Module(_mods) => {
17+
//let mut parser = Parser::new(mods.body, 0);
18+
//let val = parser.parse(&String::from("sum_tree"), &["tree".to_string()]);
19+
//println!("Return {:?}", val);
1920
}
2021
_ => todo!(),
2122
}

0 commit comments

Comments
 (0)