Skip to content

Commit

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

[workspace]
resolver = "2"
members = ["crates/*"]
24 changes: 16 additions & 8 deletions crates/benda/src/benda_ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
use bend::{
diagnostics::{ Diagnostics, DiagnosticsConfig },
fun::{ Book, Term },
CompileOpts,
RunOpts,
};
use bend::diagnostics::{Diagnostics, DiagnosticsConfig};
use bend::fun::{Book, Term};
use bend::{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-c").unwrap()
bend::run_book(
book.clone(),
run_opts,
compile_opts,
diagnostics_cfg,
args,
"run-c",
)
.unwrap()
}
65 changes: 40 additions & 25 deletions crates/benda/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use num_traits::ToPrimitive;
use parser::Parser;
use pyo3::{ prelude::*, types::{ PyFunction, PyString, PyTuple } };
use rustpython_parser::{ parse, Mode };
use pyo3::prelude::*;
use pyo3::types::{PyFunction, PyString, PyTuple};
use rustpython_parser::{parse, Mode};
use types::u24::u24;
mod types;
mod parser;
mod benda_ffi;
mod parser;
mod types;

#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Expand All @@ -21,25 +22,31 @@ fn switch() -> PyResult<String> {
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__").unwrap();
let code = inner.getattr("__code__").unwrap();
let filename = code.getattr("co_filename").unwrap();
let (name, filename, arg_names, argcount) =
match fun.clone().downcast::<PyFunction>() {
Ok(inner) => {
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();
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, filename, arg_names, argcount)
}
Err(_) => todo!(),
};
(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!();
let mut arg_list: Vec<String> = vec![];

for (index, arg) in arg_names.iter().enumerate() {
if index >= argcount.to_usize().unwrap() {
Expand All @@ -54,11 +61,14 @@ fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
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 return_val = parser.parse(fun_def.name.as_ref(), &arg_list);
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()));
}
}
}
Expand All @@ -68,13 +78,18 @@ fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {

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

Ok(fun)
}
Expand Down
3 changes: 1 addition & 2 deletions crates/benda/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ mod parser;

use parser::Parser;
use pyo3::prelude::*;

use rustpython_parser::{ parse, Mode };
use rustpython_parser::{parse, Mode};

mod benda_ffi;

Expand Down
Loading

0 comments on commit 311adcf

Please sign in to comment.