Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bend ADTS in Python #10

Open
wants to merge 71 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
77222a1
Represent Benda in Python
vkobinski Jun 15, 2024
0bdd2ca
Bend ADTS in Python
vkobinski Jun 15, 2024
f5d0c63
Bend Types
vkobinski Jun 15, 2024
8eace62
Bend IO
vkobinski Jun 16, 2024
513cbfb
fmt
vkobinski Jun 16, 2024
807cb9a
fmt
vkobinski Jun 16, 2024
1681edd
fmt
vkobinski Jun 16, 2024
b5479e2
Fixed bug
vkobinski Jun 16, 2024
d5611df
Bend has IO now
vkobinski Jun 17, 2024
4009e0c
__str__ functions
vkobinski Jun 17, 2024
50c9f65
Print Term
vkobinski Jun 18, 2024
ff1a2bb
Merge remote-tracking branch 'upstream/master' into merge
vkobinski Jun 18, 2024
5d8d1ca
Working match with one case
vkobinski Jun 20, 2024
1405203
Remove CtrEnum
vkobinski Jun 20, 2024
65acef4
Running quicksort example
vkobinski Jun 21, 2024
f084a40
Syntax
vkobinski Jun 21, 2024
f037280
Pattern Matching with desestructuring
vkobinski Jun 24, 2024
77cc0d3
fmt
vkobinski Jun 24, 2024
e6454e8
Fixed Term parsing
vkobinski Jun 24, 2024
f64b130
Fixed Term parsing
vkobinski Jun 24, 2024
00037de
Merge branch 'master' of https://github.com/vkobinski/benda-main
vkobinski Jun 24, 2024
46390b6
Nix
vkobinski Jun 24, 2024
656135a
to_list() for HVM result
vkobinski Jun 24, 2024
500d2cb
New example
vkobinski Jun 24, 2024
24cc6f4
Added all sorts from Bend repo
vkobinski Jun 24, 2024
184c179
Sat Solver example
vkobinski Jun 25, 2024
d408a30
HVM output
vkobinski Jun 26, 2024
286d395
TermParser
vkobinski Jun 26, 2024
e6d6e37
Term parser
vkobinski Jun 26, 2024
b4f6a9d
Fixed type
vkobinski Jun 26, 2024
834c50f
Refactoring
vkobinski Jun 26, 2024
3cc5276
Writing FFI documentation
vkobinski Jun 28, 2024
9c8cb8a
Adjusting examples
vkobinski Jun 28, 2024
d9fadb7
Fixed to_list
vkobinski Jun 28, 2024
fcc857d
Docs
vkobinski Jun 28, 2024
30a97f4
Superpositions
vkobinski Jun 28, 2024
01f00ca
Fixed examples
vkobinski Jun 29, 2024
636fa25
Refactor
vkobinski Jun 29, 2024
75375c0
Refactoring
vkobinski Jun 29, 2024
a14d533
Bend algorithm addition
vkobinski Jul 4, 2024
58bc444
Bend dpll
vkobinski Jul 5, 2024
13db0f1
flake update
vkobinski Jul 5, 2024
deaca8b
flake
vkobinski Jul 5, 2024
16f4601
Flake lock
vkobinski Jul 5, 2024
75f1f6b
Solved TODOs
vkobinski Jul 6, 2024
f8593af
Refactoring
vkobinski Jul 6, 2024
7cc5300
Refactoring
vkobinski Jul 6, 2024
7d41d42
Documentation and load_book
vkobinski Jul 7, 2024
3f15ff4
removed result
vkobinski Jul 7, 2024
3d1df1b
Fixed examples
vkobinski Jul 8, 2024
8694b93
Fixed examples
vkobinski Jul 8, 2024
c0fd358
Documentation
vkobinski Jul 8, 2024
f923f90
Documentation
vkobinski Jul 8, 2024
e702f23
Documentation
vkobinski Jul 8, 2024
99f63bd
Update README.md
Jul 8, 2024
6aa07af
Merge branch 'master' of https://github.com/vkobinski/benda-main
vkobinski Jul 8, 2024
686fd71
Documentation
vkobinski Jul 9, 2024
9d08667
Benda package
vkobinski Jul 9, 2024
c3c70cd
Package
vkobinski Jul 9, 2024
b8dd273
Package
vkobinski Jul 9, 2024
4406f91
Package
vkobinski Jul 9, 2024
d01949e
Packaging
vkobinski Jul 9, 2024
ab05d80
Package
vkobinski Jul 9, 2024
e3d642a
Package
vkobinski Jul 9, 2024
1898aad
Package
vkobinski Jul 9, 2024
492a0ba
Updated Bend dependency
vkobinski Jul 12, 2024
dca076c
Refactoring
vkobinski Jul 12, 2024
6722efc
Refactoring
vkobinski Jul 16, 2024
ea2e367
Refactoring
vkobinski Jul 16, 2024
c7a2b31
Refactoring
vkobinski Jul 16, 2024
70cdeae
Writing tests
vkobinski Jul 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ __pycache__
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.vscode/
steinerkelvin marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ rust-fmt:
build:
cd crates/benda; \
maturin develop

run: build
python3 tmp/quicksort.py
steinerkelvin marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 5 additions & 2 deletions crates/benda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "benda"
crate-type = ["cdylib"]
#crate-type = ["cdylib"]

# Debug purposes
crate-type = ["lib"]

[dependencies]
pyo3 = { version = "0.21.2", features = [] }
pyo3 = { version = "0.21.2", features = ["extension-module"] }
bend-lang = "0.2.33"
num-bigint = "0.4.5"
num-traits = "0.2.19"
Expand Down
31 changes: 27 additions & 4 deletions crates/benda/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::path::Path;

use num_traits::ToPrimitive;
use parser::Parser;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyFunction, PyString, PyTuple};
use rustpython_parser::{parse, Mode};
use types::book::Book;
use types::tree::{Leaf, Node, Tree};
use types::u24;
use types::u24::U24;
mod benda_ffi;
mod parser;
mod types;
Expand All @@ -14,6 +17,26 @@ fn switch() -> PyResult<String> {
Ok("Ok".to_string())
}

#[pyfunction]
fn load_book_from_file(py: Python, path: Py<PyString>) -> PyResult<Py<Book>> {
let binding = path.to_string();
let new_path = Path::new(&binding);
let bend_book = bend::load_file_to_book(new_path);

//let code = std::fs::read_to_string(new_path)
steinerkelvin marked this conversation as resolved.
Show resolved Hide resolved
// .map_err(|e| e.to_string())
// .unwrap();
//let bend_book = bend::fun::load_book::do_parse_book(
// &code,
// new_path,
// BendBook::default(),
//);

let book = Book::new(bend_book.unwrap());

Ok(Py::new(py, book).unwrap())
}

#[pyclass(name = "bjit")]
pub struct PyBjit {
wraps: Py<PyAny>,
Expand Down Expand Up @@ -83,14 +106,13 @@ impl PyBjit {

match module {
rustpython_parser::ast::Mod::Module(mods) => {
for (index, stmt) in mods.body.iter().enumerate() {
for stmt in mods.body.iter() {
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 =
Expand All @@ -114,8 +136,9 @@ impl PyBjit {
#[pymodule]
fn benda(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(switch, m)?)?;
m.add_function(wrap_pyfunction!(load_book_from_file, m)?)?;
m.add_class::<PyBjit>()?;
m.add_class::<u24::u24>()?;
m.add_class::<U24>()?;
m.add_class::<Tree>()?;
m.add_class::<Node>()?;
m.add_class::<Leaf>()?;
Expand Down
25 changes: 0 additions & 25 deletions crates/benda/src/main.rs

This file was deleted.

52 changes: 7 additions & 45 deletions crates/benda/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ enum FromExpr {
}

impl FromExpr {
pub fn to_expr(&self) -> Option<Expr> {
if let FromExpr::Expr(exp) = self {
return Some(exp.clone());
}
None
}

pub fn to_stmt(&self) -> Option<Stmt> {
if let FromExpr::Statement(stmt) = self {
return Some(stmt.clone());
}
None
}

pub fn get_var_name(&self) -> Option<Name> {
if let FromExpr::Expr(Expr::Var { nam }) = self {
Some(nam.clone())
Expand All @@ -64,21 +50,18 @@ pub struct Parser<'py> {
book: Book,
definitions: Vec<imp::Definition>,
ctx: Option<Context>,
index: usize,
fun_args: Vec<(String, Bound<'py, PyAny>)>,
}

impl<'py> Parser<'py> {
pub fn new(
statements: Vec<rStmt>,
index: usize,
fun_args: Vec<(String, Bound<'py, PyAny>)>,
) -> Self {
Self {
statements,
book: bend::fun::Book::builtins(),
definitions: vec![],
index,
ctx: None,
fun_args,
}
Expand Down Expand Up @@ -488,7 +471,7 @@ impl<'py> Parser<'py> {
}
}

if ctx.now == CurContext::Main && !ctx.vars.contains(&name) {
if ctx.now == CurContext::Main && !ctx.vars.contains(name) {
return self.parse_vec(stmts, index + 1);
}
}
Expand Down Expand Up @@ -530,12 +513,7 @@ impl<'py> Parser<'py> {
}
}

fn parse_stmt_expr(
&mut self,
expr: &StmtExpr,
stmts: &Vec<rStmt>,
index: usize,
) -> Option<FromExpr> {
fn parse_stmt_expr(&mut self, expr: &StmtExpr) -> Option<FromExpr> {
if let Some(ctx) = &self.ctx {
if ctx.now == CurContext::Main {
let val = self.parse_expr_type(*expr.value.clone());
Expand Down Expand Up @@ -649,7 +627,7 @@ impl<'py> Parser<'py> {
}
None => None,
},
rStmt::Expr(expr) => self.parse_stmt_expr(expr, stmts, index),
rStmt::Expr(expr) => self.parse_stmt_expr(expr),
rStmt::Match(m) => {
if let Some(val) = self.parse_match(m, stmts, &index) {
return Some(FromExpr::Statement(val));
Expand Down Expand Up @@ -745,7 +723,7 @@ impl<'py> Parser<'py> {

for arg in parsed_types.clone() {
match arg.1 {
imp::Expr::Var { nam } => {}
imp::Expr::Var { nam: _ } => {}
_ => new_args.push(Expr::Var {
nam: Name::new(arg.0),
}),
Expand All @@ -762,13 +740,11 @@ impl<'py> Parser<'py> {
}),
};

return Some(imp::Definition {
Some(imp::Definition {
name: Name::new("main"),
params: vec![],
body: first,
});

None
})
}

fn parse_class_def(&mut self, class: &StmtClassDef) {
Expand Down Expand Up @@ -859,20 +835,6 @@ impl<'py> Parser<'py> {
}

fn parse_function_def(&mut self, fun_def: &StmtFunctionDef) {
let mut is_bjit = false;

for dec in &fun_def.decorator_list {
if let rExpr::Name(nam) = dec {
if nam.id.to_string() == "bjit" {
is_bjit = true;
}
}
}

//if !is_bjit {
//return;
//}

let args = *fun_def.args.clone();
let mut names: Vec<Name> = vec![];

Expand All @@ -899,7 +861,7 @@ impl<'py> Parser<'py> {
rStmt::FunctionDef(fun_def) => {
self.parse_function_def(&fun_def)
}
// Treats a type alias, example: Type = A | B
steinerkelvin marked this conversation as resolved.
Show resolved Hide resolved
// Treats an type alias, example: Type = A | B
rStmt::Assign(assign) => self.parse_type_alias(&assign),
rStmt::ClassDef(class) => self.parse_class_def(&class),
_ => {}
Expand Down
26 changes: 26 additions & 0 deletions crates/benda/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
mod parser;

use std::path::{self, Path};

use pyo3::prelude::*;
use rustpython_parser::{parse, Mode};
use types::book::Book;

mod benda_ffi;
mod types;

fn main() {
let new_path = Path::new("./examples/quicksort.bend");
let bend_book = bend::load_file_to_book(new_path);

//let code = std::fs::read_to_string(new_path)
// .map_err(|e| e.to_string())
// .unwrap();
//let bend_book = bend::fun::load_book::do_parse_book(
// &code,
// new_path,
// BendBook::default(),
//);

let book = Book::new(bend_book.unwrap());
}
Loading