Skip to content

Commit

Permalink
Running tree.py example
Browse files Browse the repository at this point in the history
  • Loading branch information
vkobinski committed Jun 7, 2024
1 parent 2f11aa7 commit 6cbfe17
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 553 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[package]
name = "benda"
version = "0.1.0"
edition = "2021"

[workspace]
resolver = "2"
members = ["crates/*"]
24 changes: 8 additions & 16 deletions crates/benda/src/benda_ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
use bend::diagnostics::{Diagnostics, DiagnosticsConfig};
use bend::fun::{Book, Term};
use bend::{CompileOpts, RunOpts};
use bend::{
diagnostics::{ Diagnostics, DiagnosticsConfig },
fun::{ Book, Term },
CompileOpts,
RunOpts,
};

pub fn run(book: &Book) -> Option<(Term, String, Diagnostics)> {
let run_opts = RunOpts {
linear_readback: false,
pretty: false,
};
let run_opts = RunOpts { linear_readback: false, pretty: false };
let compile_opts = CompileOpts::default();
let diagnostics_cfg = DiagnosticsConfig::default();
let args = None;

bend::run_book(
book.clone(),
run_opts,
compile_opts,
diagnostics_cfg,
args,
"run",
)
.unwrap()
bend::run_book(book.clone(), run_opts, compile_opts, diagnostics_cfg, args, "run-c").unwrap()
}
60 changes: 41 additions & 19 deletions crates/benda/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use num_traits::ToPrimitive;
use parser::Parser;
use pyo3::prelude::*;
use pyo3::types::{PyFunction, PyString};
use rustpython_parser::{parse, Mode};
use pyo3::{ prelude::*, types::{ PyFunction, PyString, PyTuple } };
use rustpython_parser::{ parse, Mode };
use types::u24::u24;
mod benda_ffi;
mod parser;
mod types;
mod parser;
mod benda_ffi;

#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Expand All @@ -18,43 +18,65 @@ fn switch() -> PyResult<String> {
}

#[pyfunction]
fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<PyObject> {
let (name, filename) = match fun.downcast::<PyFunction>() {
fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
let arg_names_temp: Bound<PyAny>;

let (name, filename, arg_names, argcount) = match fun.clone().downcast::<PyFunction>() {
Ok(inner) => {
let name = inner.getattr("__name__");
let filename =
inner.getattr("__code__").unwrap().getattr("co_filename");
let name = inner.getattr("__name__").unwrap();
let code = inner.getattr("__code__").unwrap();
let filename = code.getattr("co_filename").unwrap();

arg_names_temp = code.getattr("co_varnames").unwrap();
let arg_names = arg_names_temp.downcast::<PyTuple>().unwrap();
let argcount = code.getattr("co_argcount").unwrap().to_string().parse::<u32>().unwrap();

(name.unwrap(), filename.unwrap())
(name, filename, arg_names, argcount)
}
Err(_) => todo!(),
};

let code = std::fs::read_to_string(filename.to_string()).unwrap();
let module = parse(code.as_str(), Mode::Module, "main.py").unwrap();

let mut arg_list: Vec<String> = vec!();

for (index, arg) in arg_names.iter().enumerate() {
if index >= argcount.to_usize().unwrap() {
break;
}

arg_list.push(arg.to_string());
}

let mut val: Option<Bound<PyString>> = None;

match module {
rustpython_parser::ast::Mod::Module(mods) => {
for stmt in mods.body.iter() {
if let rustpython_parser::ast::Stmt::FunctionDef(fun_def) = stmt
{
if let rustpython_parser::ast::Stmt::FunctionDef(fun_def) = stmt {
if fun_def.name == name.to_string() {
//let mut parser = Parser::new(mods.body.clone(), 0);

let mut parser = Parser::new(mods.body.clone(), 0);
let return_val = parser.parse(fun_def.name.as_ref());
val =
Some(PyString::new_bound(py, return_val.as_str()));
let return_val = parser.parse(fun_def.name.as_ref(), &arg_list);
val = Some(PyString::new_bound(py, return_val.as_str()));
}
}
}
}
_ => unimplemented!(),
}

Ok(val.unwrap().to_object(py))
let fun: Py<PyAny> = PyModule::from_code_bound(
py,
format!("def test({}):
return {}", "tree", val.clone().unwrap()).as_str(),
"",
""
)?
.getattr("test")?
.into();

Ok(fun)
}

/// A Python module implemented in Rust.
Expand Down
5 changes: 3 additions & 2 deletions crates/benda/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod parser;

use parser::Parser;
use pyo3::prelude::*;
use rustpython_parser::{parse, Mode};

use rustpython_parser::{ parse, Mode };

mod benda_ffi;

Expand All @@ -15,7 +16,7 @@ fn main() -> PyResult<()> {
match module {
rustpython_parser::ast::Mod::Module(mods) => {
let mut parser = Parser::new(mods.body, 0);
parser.parse(&String::from("gen_tree"));
parser.parse(&String::from("sum_tree"), &["tree".to_string()]);
}
_ => todo!(),
}
Expand Down
Loading

0 comments on commit 6cbfe17

Please sign in to comment.