Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vkobinski committed Jun 11, 2024
1 parent 9c6fe33 commit 17f9d3f
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 112 deletions.
8 changes: 3 additions & 5 deletions crates/benda/src/benda_ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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::default();
Expand Down
83 changes: 46 additions & 37 deletions crates/benda/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use bend::imp;
use num_traits::ToPrimitive;
use parser::Parser;
use pyo3::{
prelude::*,
types::{PyDict, PyFunction, PyString, PyTuple},
};
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyFunction, PyString, PyTuple};
use rustpython_parser::{parse, Mode};
use types::tree::Tree;
use types::{
extract_type,
tree::{Leaf, Node},
u24::u24,
};
use types::extract_type;
use types::tree::{Leaf, Node, Tree};
use types::u24::u24;
mod benda_ffi;
mod parser;
mod types;
Expand Down Expand Up @@ -49,7 +44,8 @@ impl PyBjit {
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 arg_names =
arg_names_temp.downcast::<PyTuple>().unwrap();
let argcount = code
.getattr("co_argcount")
.unwrap()
Expand All @@ -74,7 +70,9 @@ impl PyBjit {

let mut parsed_types: Vec<(String, imp::Expr)> = vec![];

for (index, arg) in args.downcast::<PyTuple>().unwrap().iter().enumerate() {
for (index, arg) in
args.downcast::<PyTuple>().unwrap().iter().enumerate()
{
parsed_types.push((
arg_list.get(index).unwrap().to_string(),
extract_type(arg).unwrap(),
Expand All @@ -89,12 +87,21 @@ impl PyBjit {
match module {
rustpython_parser::ast::Mod::Module(mods) => {
for (index, stmt) in mods.body.iter().enumerate() {
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(), index, parsed_types.clone());
let return_val = parser.parse(fun_def.name.as_ref(), &[]);
val = Some(PyString::new_bound(py, return_val.as_str()));
let mut parser = Parser::new(
mods.body.clone(),
index,
parsed_types.clone(),
);
let return_val =
parser.parse(fun_def.name.as_ref(), &[]);
val = Some(PyString::new_bound(
py,
return_val.as_str(),
));
break;
}
}
Expand All @@ -111,25 +118,26 @@ impl PyBjit {
fn bjit_test(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();

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!(),
};
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();

(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();
Expand All @@ -149,7 +157,8 @@ fn bjit_test(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);
Expand Down
1 change: 0 additions & 1 deletion crates/benda/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod parser;

use pyo3::prelude::*;

use rustpython_parser::{parse, Mode};

mod benda_ffi;
Expand Down
Loading

0 comments on commit 17f9d3f

Please sign in to comment.