Skip to content

Commit 874ce76

Browse files
committed
Added Parser
1 parent f9bf829 commit 874ce76

File tree

9 files changed

+2114
-0
lines changed

9 files changed

+2114
-0
lines changed

Cargo.lock

Lines changed: 1064 additions & 0 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "benda"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
name = "benda"
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
bend-lang = "=0.2.18"
13+
num-bigint = "0.4.5"
14+
num-traits = "0.2.19"
15+
pyo3 = "0.21.2"
16+
rustpython-parser = "0.3.1"
17+
indexmap = "2.2.3"

crates/benda/src/benda_ffi/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use bend::{
2+
diagnostics::{ Diagnostics, DiagnosticsConfig },
3+
fun::{ Book, Term },
4+
CompileOpts,
5+
RunOpts,
6+
};
7+
8+
pub fn run(book: &Book) -> Option<(Term, String, Diagnostics)> {
9+
let run_opts = RunOpts { linear_readback: false, pretty: false };
10+
let compile_opts = CompileOpts::default();
11+
let diagnostics_cfg = DiagnosticsConfig::default();
12+
let args = None;
13+
14+
bend::run_book(book.clone(), run_opts, compile_opts, diagnostics_cfg, args, "run").unwrap()
15+
}

crates/benda/src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use parser::Parser;
2+
use pyo3::{ prelude::*, types::{ PyFunction, PyString } };
3+
use rustpython_parser::{ parse, Mode };
4+
use types::u24::u24;
5+
mod types;
6+
mod parser;
7+
mod benda_ffi;
8+
9+
#[pyfunction]
10+
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
11+
Ok((a + b).to_string())
12+
}
13+
14+
#[pyfunction]
15+
fn switch() -> PyResult<String> {
16+
Ok("Ok".to_string())
17+
}
18+
19+
#[pyfunction]
20+
fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<PyObject> {
21+
let (name, filename) = match fun.downcast::<PyFunction>() {
22+
Ok(inner) => {
23+
let name = inner.getattr("__name__");
24+
let filename = inner.getattr("__code__").unwrap().getattr("co_filename");
25+
26+
(name.unwrap(), filename.unwrap())
27+
}
28+
Err(_) => todo!(),
29+
};
30+
31+
let code = std::fs::read_to_string(filename.to_string()).unwrap();
32+
let module = parse(code.as_str(), Mode::Module, "main.py").unwrap();
33+
34+
let mut val: Option<Bound<PyString>> = None;
35+
36+
match module {
37+
rustpython_parser::ast::Mod::Module(mods) => {
38+
for stmt in mods.body.iter() {
39+
if let rustpython_parser::ast::Stmt::FunctionDef(fun_def) = stmt {
40+
if fun_def.name == name.to_string() {
41+
//let mut parser = Parser::new(mods.body.clone(), 0);
42+
43+
let mut parser = Parser::new(mods.body.clone(), 0);
44+
let return_val = parser.parse(fun_def.name.as_ref());
45+
val = Some(PyString::new_bound(py, return_val.as_str()));
46+
}
47+
}
48+
}
49+
}
50+
_ => unimplemented!(),
51+
}
52+
53+
Ok(val.unwrap().to_object(py))
54+
}
55+
56+
/// A Python module implemented in Rust.
57+
#[pymodule]
58+
fn benda(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
59+
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
60+
m.add_function(wrap_pyfunction!(bjit, m)?)?;
61+
m.add_function(wrap_pyfunction!(switch, m)?)?;
62+
m.add_class::<u24>()?;
63+
Ok(())
64+
}

crates/benda/src/main.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
mod parser;
2+
3+
use parser::Parser;
4+
use pyo3::prelude::*;
5+
6+
use rustpython_parser::{ parse, Mode };
7+
8+
mod benda_ffi;
9+
10+
fn main() -> PyResult<()> {
11+
let filename = String::from("main.py");
12+
13+
let code = std::fs::read_to_string(&filename).unwrap();
14+
let module = parse(code.as_str(), Mode::Module, "main.py").unwrap();
15+
16+
match module {
17+
rustpython_parser::ast::Mod::Module(mods) => {
18+
let mut parser = Parser::new(mods.body, 0);
19+
parser.parse(&String::from("gen_tree"));
20+
}
21+
_ => todo!(),
22+
}
23+
24+
Ok(())
25+
}

crates/benda/src/parser/builtins.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub enum BuiltIn {
2+
Switch,
3+
}

0 commit comments

Comments
 (0)