From 79859ea763d9c1366e5123def1e31c9b05e3e7ab Mon Sep 17 00:00:00 2001 From: vkobinski Date: Wed, 12 Jun 2024 19:02:40 -0300 Subject: [PATCH 01/11] Updated examples --- .gitignore | 1 + crates/benda/src/parser/mod.rs | 2 +- examples/tree.py | 35 ++++++++++------------------------ 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 6a456fc..19052d1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /.direnv/ /venv/ +/.env/ /debug/ /target/ diff --git a/crates/benda/src/parser/mod.rs b/crates/benda/src/parser/mod.rs index d1e88ab..b6c87d6 100644 --- a/crates/benda/src/parser/mod.rs +++ b/crates/benda/src/parser/mod.rs @@ -946,7 +946,7 @@ impl Parser { self.book.entrypoint = None; - println!("BEND:\n {}", self.book.display_pretty()); + //println!("BEND:\n {}", self.book.display_pretty()); let return_val = run(&self.book); diff --git a/examples/tree.py b/examples/tree.py index 5d4f2d7..2f17f63 100644 --- a/examples/tree.py +++ b/examples/tree.py @@ -1,21 +1,13 @@ -from dataclasses import dataclass -from benda import bjit, u24 - -@dataclass -class Leaf: - value: u24 - - -@dataclass -class Node: - left: 'Tree' - right: 'Tree' - - -Tree = Node | Leaf +from benda import bjit +from benda import Tree, Node, Leaf +def gen_tree(depth, n): + if depth == 0: + return Leaf(n) + else: + return Node(gen_tree(depth-1, n-1), gen_tree(depth-1, n+1)) -# @bjit +@bjit def sum_tree(tree: Tree) -> u24: match tree: case Leaf(value): @@ -23,14 +15,7 @@ def sum_tree(tree: Tree) -> u24: case Node(left, right): return sum_tree(left) + sum_tree(right) - -def gen_tree(depth: int, n: int) -> Tree: - if depth == 0: - return Leaf(value=n) - else: - return Node(left=gen_tree(depth-1, n-1), right=gen_tree(depth-1, n+1)) - - -tree = gen_tree(4, 10) + +tree = gen_tree(10, 10) val = sum_tree(tree) print(val) \ No newline at end of file From 0dba0a929d6c87302bc7d88bd050bdaf077e9e3e Mon Sep 17 00:00:00 2001 From: vkobinski Date: Wed, 12 Jun 2024 19:04:18 -0300 Subject: [PATCH 02/11] Removed import --- crates/benda/src/types/mod.rs | 1 - examples/tree.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/benda/src/types/mod.rs b/crates/benda/src/types/mod.rs index e27f91d..7e51774 100644 --- a/crates/benda/src/types/mod.rs +++ b/crates/benda/src/types/mod.rs @@ -2,7 +2,6 @@ use core::panic; use bend::fun::Num; use bend::imp; -use pyo3::exceptions::PyTypeError; use pyo3::types::{PyAnyMethods, PyFloat, PyTypeMethods}; use pyo3::{Bound, FromPyObject, PyAny, PyErr, PyTypeCheck}; use tree::{Leaf, Node, Tree}; diff --git a/examples/tree.py b/examples/tree.py index 2f17f63..12cee49 100644 --- a/examples/tree.py +++ b/examples/tree.py @@ -1,5 +1,5 @@ from benda import bjit -from benda import Tree, Node, Leaf +from benda import Tree, Node, Leaf, u24 def gen_tree(depth, n): if depth == 0: From 10a3d267d7294584d477d2a3d8f6dd20b7d9fbcf Mon Sep 17 00:00:00 2001 From: vkobinski Date: Wed, 12 Jun 2024 19:12:01 -0300 Subject: [PATCH 03/11] Changed .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 19052d1..6a456fc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ /.direnv/ /venv/ -/.env/ /debug/ /target/ From bfe4ec0b960ce631a9766f8090ed094d4c34f712 Mon Sep 17 00:00:00 2001 From: vkobinski Date: Wed, 12 Jun 2024 20:30:40 -0300 Subject: [PATCH 04/11] Updated examples --- examples/simple.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/simple.py b/examples/simple.py index 1bed79b..02a165b 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -2,13 +2,12 @@ from benda import bjit, u24 +@bjit def simple() -> u24: x = u24(3) y = x - u24(2) return y -if __name__ == "__main__": - translated_simple = bjit(simple) - print(simple()) - print(translated_simple()) +val = simple() +print(val) \ No newline at end of file From 3059e61c263d714228d3bfec94f84d83ed062c7e Mon Sep 17 00:00:00 2001 From: vkobinski Date: Thu, 13 Jun 2024 15:21:59 -0300 Subject: [PATCH 05/11] Fixing examples --- crates/benda/src/lib.rs | 11 +-- crates/benda/src/parser/mod.rs | 114 +++++++++++------------------ crates/benda/src/types/mod.rs | 15 ++-- crates/benda/src/types/user_adt.rs | 33 +++++++++ examples/simple.py | 14 ++-- examples/tree.py | 45 ++++++++---- flake.nix | 3 + rust-toolchain.toml | 2 +- 8 files changed, 131 insertions(+), 106 deletions(-) create mode 100644 crates/benda/src/types/user_adt.rs diff --git a/crates/benda/src/lib.rs b/crates/benda/src/lib.rs index 96fb48e..577ee37 100644 --- a/crates/benda/src/lib.rs +++ b/crates/benda/src/lib.rs @@ -1,10 +1,8 @@ -use bend::imp; use num_traits::ToPrimitive; use parser::Parser; use pyo3::prelude::*; use pyo3::types::{PyDict, PyFunction, PyString, PyTuple}; use rustpython_parser::{parse, Mode}; -use types::extract_type; use types::tree::{Leaf, Node, Tree}; use types::u24::u24; mod benda_ffi; @@ -68,15 +66,14 @@ impl PyBjit { arg_list.push(arg.to_string()); } - let mut parsed_types: Vec<(String, imp::Expr)> = vec![]; + let mut parsed_types: Vec<(String, Bound)> = vec![]; for (index, arg) in args.downcast::().unwrap().iter().enumerate() { - parsed_types.push(( - arg_list.get(index).unwrap().to_string(), - extract_type(arg).unwrap(), - )); + let var_name = arg_list.get(index).unwrap().to_string(); + + parsed_types.push((var_name.clone(), arg)); } let code = std::fs::read_to_string(filename.to_string()).unwrap(); diff --git a/crates/benda/src/parser/mod.rs b/crates/benda/src/parser/mod.rs index b6c87d6..979b551 100644 --- a/crates/benda/src/parser/mod.rs +++ b/crates/benda/src/parser/mod.rs @@ -6,6 +6,7 @@ use bend::fun::{self, Adt, Book, CtrField, Name, Op, Rule, STRINGS}; use bend::imp::{self, Expr, MatchArm, Stmt}; use indexmap::IndexMap; use num_traits::cast::ToPrimitive; +use pyo3::{Bound, PyAny}; use rustpython_parser::ast::{ located, CmpOp as rCmpOp, Expr as rExpr, ExprAttribute, ExprBinOp, Operator as rOperator, Pattern as rPattern, Stmt as rStmt, StmtAssign, @@ -13,6 +14,7 @@ use rustpython_parser::ast::{ }; use crate::benda_ffi::run; +use crate::types::extract_type; #[derive(Clone, Debug)] enum FromExpr { @@ -57,20 +59,20 @@ struct Context { subs: Vec, } -pub struct Parser { +pub struct Parser<'py> { statements: Vec, book: Book, definitions: Vec, ctx: Option, index: usize, - fun_args: Vec<(String, imp::Expr)>, + fun_args: Vec<(String, Bound<'py, PyAny>)>, } -impl Parser { +impl<'py> Parser<'py> { pub fn new( statements: Vec, index: usize, - fun_args: Vec<(String, imp::Expr)>, + fun_args: Vec<(String, Bound<'py, PyAny>)>, ) -> Self { Self { statements, @@ -692,8 +694,8 @@ impl Parser { } // Creates a Bend Definition for each argument for the annotaded function. - fn parse_fun_args(&mut self) { - for (name, expr) in &self.fun_args { + fn parse_fun_args(&mut self, parsed_types: &Vec<(String, imp::Expr)>) { + for (name, expr) in parsed_types { let u_type = expr.clone().to_fun(); let nam = Name::new(name.to_string()); @@ -724,76 +726,44 @@ impl Parser { subs: vec![fun_name.to_string()], }); - self.parse_fun_args(); + let mut parsed_types: Vec<(String, imp::Expr)> = vec![]; - for (index, stmt) in - self.statements.clone().iter().skip(self.index).enumerate() - { - if let rStmt::Assign(assi) = stmt { - if let rExpr::Name(target) = assi.targets.first().unwrap() { - if py_args.contains(target.id.as_ref()) { - let body = - self.parse_vec(&self.statements.clone(), index); - - if let Some(FromExpr::Statement(st)) = body { - return Some(imp::Definition { - name: Name::new("main"), - params: vec![], - body: st, - }); - } - } - } + for arg in self.fun_args.iter() { + parsed_types.push(( + arg.0.clone(), + extract_type(arg.1.clone(), &arg.0).unwrap(), + )); + } - if let rExpr::Call(call) = *assi.clone().value { - if let rExpr::Name(func) = *call.func { - if fun_name == func.id.to_string() { - if call.args.is_empty() { - panic!("The function must have arguments for Bend can run it."); - } else { - let new_body = self - .parse_vec(&self.statements.clone(), index); - - if let Some(FromExpr::Statement(st)) = new_body - { - return Some(imp::Definition { - name: Name::new("main"), - params: vec![], - body: st, - }); - } - - let mut new_args: Vec = vec![]; - - for arg in self.fun_args.clone() { - new_args.push(Expr::Var { - nam: Name::new(arg.0), - }) - } - - let first = Stmt::Return { - term: Box::new(Expr::Call { - fun: Box::new(imp::Expr::Var { - nam: Name::new( - fun_name.to_string(), - ), - }), - args: new_args, - kwargs: vec![], - }), - }; - return Some(imp::Definition { - name: Name::new("main"), - params: vec![], - body: first, - }); - } - } - } - } + self.parse_fun_args(&parsed_types); + + let mut new_args: Vec = vec![]; + + for arg in parsed_types.clone() { + match arg.1 { + imp::Expr::Var { nam } => {} + _ => new_args.push(Expr::Var { + nam: Name::new(arg.0), + }), } } + let first = Stmt::Return { + term: Box::new(Expr::Call { + fun: Box::new(imp::Expr::Var { + nam: Name::new(fun_name.to_string()), + }), + args: new_args, + kwargs: vec![], + }), + }; + + return Some(imp::Definition { + name: Name::new("main"), + params: vec![], + body: first, + }); + None } @@ -946,7 +916,7 @@ impl Parser { self.book.entrypoint = None; - //println!("BEND:\n {}", self.book.display_pretty()); + println!("BEND:\n {}", self.book.display_pretty()); let return_val = run(&self.book); diff --git a/crates/benda/src/types/mod.rs b/crates/benda/src/types/mod.rs index 7e51774..6532105 100644 --- a/crates/benda/src/types/mod.rs +++ b/crates/benda/src/types/mod.rs @@ -1,7 +1,5 @@ -use core::panic; - -use bend::fun::Num; -use bend::imp; +use bend::fun::{Name, Num}; +use bend::imp::{self, Expr}; use pyo3::types::{PyAnyMethods, PyFloat, PyTypeMethods}; use pyo3::{Bound, FromPyObject, PyAny, PyErr, PyTypeCheck}; use tree::{Leaf, Node, Tree}; @@ -10,6 +8,7 @@ pub mod f24; pub mod i24; pub mod tree; pub mod u24; +pub mod user_adt; pub trait BendType { fn to_bend(&self) -> ToBendResult; @@ -36,7 +35,7 @@ pub fn extract_num(arg: Bound, t_type: BuiltinType) -> ToBendResult { } } -pub fn extract_type(arg: Bound) -> ToBendResult { +pub fn extract_type(arg: Bound, var_name: &String) -> ToBendResult { let t_type = arg.get_type(); let name = t_type.name().unwrap(); @@ -49,6 +48,9 @@ pub fn extract_type(arg: Bound) -> ToBendResult { BuiltinType::Tree => extract_inner::(arg).unwrap().to_bend(), BuiltinType::Node => extract_inner::(arg).unwrap().to_bend(), BuiltinType::Leaf => extract_inner::(arg).unwrap().to_bend(), + BuiltinType::UserAdt => Ok(Expr::Var { + nam: Name::new(var_name), + }), } } @@ -60,6 +62,7 @@ pub enum BuiltinType { Tree, Leaf, Node, + UserAdt, } impl From for BuiltinType { @@ -71,7 +74,7 @@ impl From for BuiltinType { "benda.Node" => BuiltinType::Node, "benda.Leaf" => BuiltinType::Leaf, "benda.Tree" => BuiltinType::Tree, - _ => panic!("Unsupported argument type"), + _ => BuiltinType::UserAdt, } } } diff --git a/crates/benda/src/types/user_adt.rs b/crates/benda/src/types/user_adt.rs new file mode 100644 index 0000000..36c6caf --- /dev/null +++ b/crates/benda/src/types/user_adt.rs @@ -0,0 +1,33 @@ +use bend::fun::Adt; +use pyo3::type_object::HasPyGilRef; +use pyo3::types::PyAnyMethods; +use pyo3::{FromPyObject, PyTypeCheck}; + +use super::BendType; + +struct UserAdt { + adt: Adt, +} + +impl<'py> FromPyObject<'py> for UserAdt { + fn extract(ob: &'py pyo3::PyAny) -> pyo3::PyResult { + for field in ob.iter() { + println!("{:?}", field); + } + + //Self::extract_bound(&ob.as_borrowed()) + } + + fn extract_bound( + ob: &pyo3::Bound<'py, pyo3::PyAny>, + ) -> pyo3::PyResult { + Self::extract(ob.clone().into_gil_ref()) + } +} + +impl BendType for UserAdt { + fn to_bend(&self) -> super::ToBendResult { + println!("ADT: {:?}", self.adt); + todo!() + } +} diff --git a/examples/simple.py b/examples/simple.py index 02a165b..857daa2 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -1,13 +1,15 @@ import benda from benda import bjit, u24 - -@bjit -def simple() -> u24: - x = u24(3) +def simple(a) -> u24: + x = u24(a) y = x - u24(2) return y -val = simple() -print(val) \ No newline at end of file +translated_simple = bjit(simple) +val = simple(5) +print(val) + +val_bend = translated_simple(5) +print(val_bend) \ No newline at end of file diff --git a/examples/tree.py b/examples/tree.py index 12cee49..3ee8692 100644 --- a/examples/tree.py +++ b/examples/tree.py @@ -1,21 +1,38 @@ -from benda import bjit -from benda import Tree, Node, Leaf, u24 +from dataclasses import dataclass +from benda import bjit, u24 + +@dataclass +class MyLeaf: + value: u24 + + +@dataclass +class MyNode: + left: 'MyTree' + right: 'MyTree' + + +MyTree = MyNode | MyLeaf -def gen_tree(depth, n): - if depth == 0: - return Leaf(n) - else: - return Node(gen_tree(depth-1, n-1), gen_tree(depth-1, n+1)) @bjit -def sum_tree(tree: Tree) -> u24: +def sum_tree(tree: MyTree) -> u24: match tree: - case Leaf(value): + case MyLeaf(value): return value - case Node(left, right): + case MyNode(left, right): return sum_tree(left) + sum_tree(right) - -tree = gen_tree(10, 10) -val = sum_tree(tree) -print(val) \ No newline at end of file + +def gen_tree(depth: int, n: int) -> MyTree: + if depth == 0: + return MyLeaf(value=n) + else: + return MyNode(left=gen_tree(depth-1, n-1), right=gen_tree(depth-1, n+1)) + + +if __name__ == "__main__": + tree = gen_tree(4, 10) + #print(tree) + print(sum_tree(tree)) + diff --git a/flake.nix b/flake.nix index 9eb9f9c..1b8a357 100644 --- a/flake.nix +++ b/flake.nix @@ -18,6 +18,8 @@ python3 = pkgs.python312; rust_toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + rust_src = pkgs.rust.packages.stable.rustPlatform.rustcSrc; + nativeBuildInputs = [ pkgs.git @@ -29,6 +31,7 @@ python3 ]; + dev_packages = [ pkgs.gnumake # GitHub Actions runner diff --git a/rust-toolchain.toml b/rust-toolchain.toml index f686ef0..a6058a0 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] profile = "minimal" channel = "nightly-2024-05-24" -components = ["rustfmt", "clippy"] +components = ["rustfmt", "clippy", "rust-src"] From 7bd20e72e93936d3b2393de4068e9e34c3b4f4f4 Mon Sep 17 00:00:00 2001 From: vkobinski Date: Thu, 13 Jun 2024 17:16:45 -0300 Subject: [PATCH 06/11] Added UserAdt --- Cargo.lock | 44 +++++++-------- crates/benda/src/lib.rs | 4 +- crates/benda/src/main.rs | 1 + crates/benda/src/parser/mod.rs | 2 +- crates/benda/src/types/f24.rs | 35 ++++++------ crates/benda/src/types/i24.rs | 35 ++++++------ crates/benda/src/types/mod.rs | 40 +++++++++++--- crates/benda/src/types/tree.rs | 6 +- crates/benda/src/types/u24.rs | 35 ++++++------ crates/benda/src/types/user_adt.rs | 88 +++++++++++++++++++++++------- examples/tree.py | 5 +- result | 1 + 12 files changed, 184 insertions(+), 112 deletions(-) create mode 120000 result diff --git a/Cargo.lock b/Cargo.lock index a675a25..5417237 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,9 +127,9 @@ checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" [[package]] name = "cc" -version = "1.0.98" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" +checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" [[package]] name = "cfg-if" @@ -139,9 +139,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.4" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" dependencies = [ "clap_builder", "clap_derive", @@ -149,9 +149,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" dependencies = [ "anstream", "anstyle", @@ -161,9 +161,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -173,9 +173,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "colorchoice" @@ -377,9 +377,9 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "malachite" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ec75c8919dd516ed0ee247046a5950113ebcb06fbd3140f3cefb917d6b50fa5" +checksum = "7f65155c1c6ca06b3182b8c9a92b531eabf83aa2ac23fe1720672e02280d10c3" dependencies = [ "malachite-base", "malachite-nz", @@ -388,9 +388,9 @@ dependencies = [ [[package]] name = "malachite-base" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98ca40106fdb86fe664e5a1697e0246fe5c5461195dbb4f4df91a88283b76a09" +checksum = "a45b2c0c7a8e98471a785106e2eca885f32854287b767266eb6606ae9b80562a" dependencies = [ "hashbrown", "itertools", @@ -413,9 +413,9 @@ dependencies = [ [[package]] name = "malachite-nz" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6117914d26f55be8be3cd874e8ab4c8292c96901613d3dde4381cdd586997062" +checksum = "62c96d099d47d6fedf0b2d51c66488eac37a2b060ea752ecb46b6e8d53dab411" dependencies = [ "itertools", "libm", @@ -424,9 +424,9 @@ dependencies = [ [[package]] name = "malachite-q" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bd3fad92242d433e28335f8d10fec0bfc712b00a2d7e135f8920bbc387dfeaa" +checksum = "d5d8768a4cd2d9bb1fbb48bba1d376fc8b605451d89e455ea71f04d215236023" dependencies = [ "itertools", "malachite-base", @@ -435,9 +435,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.2" +version = "2.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +checksum = "6d0d8b92cd8358e8d229c11df9358decae64d137c5be540952c5ca7b25aea768" [[package]] name = "memoffset" @@ -962,9 +962,9 @@ checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" [[package]] name = "utf8parse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "version_check" diff --git a/crates/benda/src/lib.rs b/crates/benda/src/lib.rs index 577ee37..9a8853d 100644 --- a/crates/benda/src/lib.rs +++ b/crates/benda/src/lib.rs @@ -4,7 +4,7 @@ use pyo3::prelude::*; use pyo3::types::{PyDict, PyFunction, PyString, PyTuple}; use rustpython_parser::{parse, Mode}; use types::tree::{Leaf, Node, Tree}; -use types::u24::u24; +use types::u24; mod benda_ffi; mod parser; mod types; @@ -115,7 +115,7 @@ impl PyBjit { fn benda(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(switch, m)?)?; m.add_class::()?; - m.add_class::()?; + m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/crates/benda/src/main.rs b/crates/benda/src/main.rs index 1a35b13..8d7f20a 100644 --- a/crates/benda/src/main.rs +++ b/crates/benda/src/main.rs @@ -4,6 +4,7 @@ use pyo3::prelude::*; use rustpython_parser::{parse, Mode}; mod benda_ffi; +mod types; fn main() -> PyResult<()> { let filename = String::from("main.py"); diff --git a/crates/benda/src/parser/mod.rs b/crates/benda/src/parser/mod.rs index 979b551..15bd78d 100644 --- a/crates/benda/src/parser/mod.rs +++ b/crates/benda/src/parser/mod.rs @@ -731,7 +731,7 @@ impl<'py> Parser<'py> { for arg in self.fun_args.iter() { parsed_types.push(( arg.0.clone(), - extract_type(arg.1.clone(), &arg.0).unwrap(), + extract_type(arg.1.clone(), &self.book).unwrap(), )); } diff --git a/crates/benda/src/types/f24.rs b/crates/benda/src/types/f24.rs index 0fd1705..534cefe 100644 --- a/crates/benda/src/types/f24.rs +++ b/crates/benda/src/types/f24.rs @@ -6,11 +6,10 @@ use pyo3::{pyclass, pymethods}; use super::{BendType, ToBendResult}; #[pyclass(module = "benda")] -#[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq, PartialOrd)] -pub struct f24(f32); +pub struct F24(f32); -impl BendType for f24 { +impl BendType for F24 { fn to_bend(&self) -> ToBendResult { Ok(imp::Expr::Num { val: bend::fun::Num::F24(self.0), @@ -18,7 +17,7 @@ impl BendType for f24 { } } -impl f24 { +impl F24 { // TODO: Implement a masking for float numbers. pub fn new(value: f32) -> Self { Self(value) @@ -29,43 +28,43 @@ impl f24 { } } -impl std::fmt::Debug for f24 { +impl std::fmt::Debug for F24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl std::fmt::Display for f24 { +impl std::fmt::Display for F24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl From for f24 { +impl From for F24 { fn from(value: f32) -> Self { - f24::new(value) + F24::new(value) } } -impl From for f32 { - fn from(val: f24) -> Self { +impl From for f32 { + fn from(val: F24) -> Self { val.0 } } -impl std::ops::Add for f24 { +impl std::ops::Add for F24 { type Output = Self; fn add(self, other: Self) -> Self { - f24::new(self.0 + other.0) + F24::new(self.0 + other.0) } } -impl std::ops::Sub for f24 { +impl std::ops::Sub for F24 { type Output = Self; fn sub(self, other: Self) -> Self { - f24::new(self.0 - other.0) + F24::new(self.0 - other.0) } } @@ -73,18 +72,18 @@ impl std::ops::Sub for f24 { // TODO: Implement tests for each operation comparing to Bend #[pymethods] -impl f24 { +impl F24 { #[new] fn new_py(value: f32) -> Self { - f24::new(value) + F24::new(value) } fn __add__(&self, other: &Self) -> Self { - f24::add(*self, *other) + F24::add(*self, *other) } fn __sub__(&self, other: &Self) -> Self { - f24::sub(*self, *other) + F24::sub(*self, *other) } fn __str__(&self) -> String { diff --git a/crates/benda/src/types/i24.rs b/crates/benda/src/types/i24.rs index 61cd734..b9e1fb8 100644 --- a/crates/benda/src/types/i24.rs +++ b/crates/benda/src/types/i24.rs @@ -6,11 +6,10 @@ use pyo3::{pyclass, pymethods}; use super::{BendType, ToBendResult}; #[pyclass(module = "benda")] -#[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub struct i24(i32); +pub struct U24(i32); -impl BendType for i24 { +impl BendType for U24 { fn to_bend(&self) -> ToBendResult { Ok(imp::Expr::Num { val: bend::fun::Num::I24(self.0), @@ -18,7 +17,7 @@ impl BendType for i24 { } } -impl i24 { +impl U24 { const MAX: i32 = 0xffffff; // TODO: Check if the masking is working properly @@ -31,43 +30,43 @@ impl i24 { } } -impl std::fmt::Debug for i24 { +impl std::fmt::Debug for U24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl std::fmt::Display for i24 { +impl std::fmt::Display for U24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl From for i24 { +impl From for U24 { fn from(value: i32) -> Self { - i24::new(value) + U24::new(value) } } -impl From for i32 { - fn from(val: i24) -> Self { +impl From for i32 { + fn from(val: U24) -> Self { val.0 } } -impl std::ops::Add for i24 { +impl std::ops::Add for U24 { type Output = Self; fn add(self, other: Self) -> Self { - i24::new(self.0 + other.0) + U24::new(self.0 + other.0) } } -impl std::ops::Sub for i24 { +impl std::ops::Sub for U24 { type Output = Self; fn sub(self, other: Self) -> Self { - i24::new(self.0 - other.0) + U24::new(self.0 - other.0) } } @@ -75,18 +74,18 @@ impl std::ops::Sub for i24 { // TODO: Implement tests for each operation comparing to Bend #[pymethods] -impl i24 { +impl U24 { #[new] fn new_py(value: i32) -> Self { - i24::new(value) + U24::new(value) } fn __add__(&self, other: &Self) -> Self { - i24::add(*self, *other) + U24::add(*self, *other) } fn __sub__(&self, other: &Self) -> Self { - i24::sub(*self, *other) + U24::sub(*self, *other) } fn __str__(&self) -> String { diff --git a/crates/benda/src/types/mod.rs b/crates/benda/src/types/mod.rs index 6532105..de02f86 100644 --- a/crates/benda/src/types/mod.rs +++ b/crates/benda/src/types/mod.rs @@ -1,8 +1,9 @@ -use bend::fun::{Name, Num}; -use bend::imp::{self, Expr}; +use bend::fun::{Book, Num}; +use bend::imp::{self}; use pyo3::types::{PyAnyMethods, PyFloat, PyTypeMethods}; use pyo3::{Bound, FromPyObject, PyAny, PyErr, PyTypeCheck}; use tree::{Leaf, Node, Tree}; +use user_adt::UserAdt; pub mod f24; pub mod i24; @@ -27,6 +28,17 @@ pub fn extract_inner<'py, T: BendType + PyTypeCheck + FromPyObject<'py>>( None } +pub fn extract_num_raw( + arg: Bound, + t_type: BuiltinType, +) -> Box { + match t_type { + BuiltinType::I32 => Box::new(arg.to_string().parse::().unwrap()), + BuiltinType::F32 => Box::new(arg.to_string().parse::().unwrap()), + _ => unreachable!(), + } +} + pub fn extract_num(arg: Bound, t_type: BuiltinType) -> ToBendResult { match t_type { BuiltinType::I32 => arg.to_string().parse::().unwrap().to_bend(), @@ -35,22 +47,36 @@ pub fn extract_num(arg: Bound, t_type: BuiltinType) -> ToBendResult { } } -pub fn extract_type(arg: Bound, var_name: &String) -> ToBendResult { +pub fn extract_type_raw(arg: Bound) -> Option> { + let t_type = arg.get_type(); + let name = t_type.name().unwrap(); + + let arg_type = BuiltinType::from(name.to_string()); + + match arg_type { + BuiltinType::U24 => { + Some(Box::new(extract_inner::(arg).unwrap())) + } + BuiltinType::I32 => Some(extract_num_raw(arg, BuiltinType::I32)), + BuiltinType::F32 => Some(extract_num_raw(arg, BuiltinType::F32)), + _ => None, + } +} + +pub fn extract_type(arg: Bound, book: &Book) -> ToBendResult { let t_type = arg.get_type(); let name = t_type.name().unwrap(); let arg_type = BuiltinType::from(name.to_string()); match arg_type { - BuiltinType::U24 => extract_inner::(arg).unwrap().to_bend(), + BuiltinType::U24 => extract_inner::(arg).unwrap().to_bend(), BuiltinType::I32 => extract_num(arg, BuiltinType::I32), BuiltinType::F32 => extract_num(arg, BuiltinType::F32), BuiltinType::Tree => extract_inner::(arg).unwrap().to_bend(), BuiltinType::Node => extract_inner::(arg).unwrap().to_bend(), BuiltinType::Leaf => extract_inner::(arg).unwrap().to_bend(), - BuiltinType::UserAdt => Ok(Expr::Var { - nam: Name::new(var_name), - }), + BuiltinType::UserAdt => UserAdt::new(arg, book).unwrap().to_bend(), } } diff --git a/crates/benda/src/types/tree.rs b/crates/benda/src/types/tree.rs index e1e828d..d5b1b17 100644 --- a/crates/benda/src/types/tree.rs +++ b/crates/benda/src/types/tree.rs @@ -5,14 +5,14 @@ use bend::{fun, imp}; use pyo3::types::{PyAnyMethods, PyTuple, PyTypeMethods}; use pyo3::{pyclass, pymethods, Bound}; -use super::u24::u24; +use super::u24::U24; use super::{BendType, ToBendResult}; use crate::types::extract_inner; #[derive(Clone, Debug)] #[pyclass(module = "benda", name = "Leaf")] pub struct Leaf { - pub value: u24, + pub value: U24, } impl BendType for Leaf { @@ -26,7 +26,7 @@ impl Leaf { #[new] fn __new__(val: u32) -> Self { Self { - value: u24::new(val), + value: U24::new(val), } } } diff --git a/crates/benda/src/types/u24.rs b/crates/benda/src/types/u24.rs index e98cc6e..2aabce6 100644 --- a/crates/benda/src/types/u24.rs +++ b/crates/benda/src/types/u24.rs @@ -6,11 +6,10 @@ use pyo3::{pyclass, pymethods}; use super::{BendType, ToBendResult}; #[pyclass(module = "benda")] -#[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub struct u24(u32); +pub struct U24(u32); -impl BendType for u24 { +impl BendType for U24 { fn to_bend(&self) -> ToBendResult { Ok(imp::Expr::Num { val: bend::fun::Num::U24(self.0), @@ -18,7 +17,7 @@ impl BendType for u24 { } } -impl u24 { +impl U24 { const MAX: u32 = 0xffffff; // TODO: Check if the masking is working properly @@ -31,43 +30,43 @@ impl u24 { } } -impl std::fmt::Debug for u24 { +impl std::fmt::Debug for U24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl std::fmt::Display for u24 { +impl std::fmt::Display for U24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl From for u24 { +impl From for U24 { fn from(value: u32) -> Self { - u24::new(value) + U24::new(value) } } -impl From for u32 { - fn from(val: u24) -> Self { +impl From for u32 { + fn from(val: U24) -> Self { val.0 } } -impl std::ops::Add for u24 { +impl std::ops::Add for U24 { type Output = Self; fn add(self, other: Self) -> Self { - u24::new(self.0 + other.0) + U24::new(self.0 + other.0) } } -impl std::ops::Sub for u24 { +impl std::ops::Sub for U24 { type Output = Self; fn sub(self, other: Self) -> Self { - u24::new(self.0 - other.0) + U24::new(self.0 - other.0) } } @@ -75,18 +74,18 @@ impl std::ops::Sub for u24 { // TODO: Implement tests for each operation comparing to Bend #[pymethods] -impl u24 { +impl U24 { #[new] fn new_py(value: u32) -> Self { - u24::new(value) + U24::new(value) } fn __add__(&self, other: &Self) -> Self { - u24::add(*self, *other) + U24::add(*self, *other) } fn __sub__(&self, other: &Self) -> Self { - u24::sub(*self, *other) + U24::sub(*self, *other) } fn __str__(&self) -> String { diff --git a/crates/benda/src/types/user_adt.rs b/crates/benda/src/types/user_adt.rs index 36c6caf..79deefb 100644 --- a/crates/benda/src/types/user_adt.rs +++ b/crates/benda/src/types/user_adt.rs @@ -1,33 +1,81 @@ -use bend::fun::Adt; -use pyo3::type_object::HasPyGilRef; -use pyo3::types::PyAnyMethods; -use pyo3::{FromPyObject, PyTypeCheck}; +use bend::fun::{Adt, Book, Name}; +use bend::imp::{self}; +use pyo3::types::{PyAnyMethods, PyString, PyTypeMethods}; +use pyo3::{Bound, PyAny, PyErr}; -use super::BendType; +use super::{extract_type_raw, BendType}; -struct UserAdt { +pub struct UserAdt<'py> { adt: Adt, + entire_nam: Name, + data: Bound<'py, PyAny>, + book: Book, } -impl<'py> FromPyObject<'py> for UserAdt { - fn extract(ob: &'py pyo3::PyAny) -> pyo3::PyResult { - for field in ob.iter() { - println!("{:?}", field); - } +impl<'py> UserAdt<'py> { + pub fn new(data: Bound<'py, PyAny>, book: &Book) -> Option { + let t_type = data.get_type(); - //Self::extract_bound(&ob.as_borrowed()) - } + let binding = t_type.name().unwrap(); + + for (nam, _ctr) in &book.ctrs { + let new_nam = nam.to_string(); + let two_names = new_nam.split_once('/').unwrap(); - fn extract_bound( - ob: &pyo3::Bound<'py, pyo3::PyAny>, - ) -> pyo3::PyResult { - Self::extract(ob.clone().into_gil_ref()) + if two_names.1 == binding { + return Some(Self { + book: book.clone(), + data, + entire_nam: Name::new(new_nam.clone()), + adt: book + .adts + .get(&Name::new(two_names.0.to_string())) + .unwrap() + .clone(), + }); + } + } + + None } } -impl BendType for UserAdt { +impl<'py> BendType for UserAdt<'py> { fn to_bend(&self) -> super::ToBendResult { - println!("ADT: {:?}", self.adt); - todo!() + for (nam, fields) in &self.adt.ctrs { + if *nam == self.entire_nam { + let mut adt_fields: Vec = vec![]; + for field in fields { + let attr_nam = field.nam.clone(); + + let py = self.data.py(); + + let attr = self + .data + .getattr(PyString::new_bound(py, attr_nam.as_ref())) + .unwrap(); + + if let Some(t) = extract_type_raw(attr.clone()) { + return Ok(imp::Expr::Ctr { + name: nam.clone(), + args: vec![t.to_bend().unwrap()], + kwargs: vec![], + }); + } + + if let Some(adt) = UserAdt::new(attr, &self.book) { + let new_adt = adt.to_bend(); + adt_fields.push(new_adt.unwrap()); + } + } + + return Ok(imp::Expr::Ctr { + name: nam.clone(), + args: adt_fields, + kwargs: vec![], + }); + } + } + Err(PyErr::fetch(self.data.py())) } } diff --git a/examples/tree.py b/examples/tree.py index 3ee8692..fb59eca 100644 --- a/examples/tree.py +++ b/examples/tree.py @@ -33,6 +33,5 @@ def gen_tree(depth: int, n: int) -> MyTree: if __name__ == "__main__": tree = gen_tree(4, 10) - #print(tree) - print(sum_tree(tree)) - + print(tree) + print(sum_tree(tree)) \ No newline at end of file diff --git a/result b/result new file mode 120000 index 0000000..d3859dd --- /dev/null +++ b/result @@ -0,0 +1 @@ +/nix/store/rb3lbl2iizrf45f0rgn8g3w2zy6zcqwr-rust-workspace-unknown \ No newline at end of file From 8f4b8154b46b628e5ea237b9b9bbd74580217fdc Mon Sep 17 00:00:00 2001 From: vkobinski Date: Thu, 13 Jun 2024 17:22:26 -0300 Subject: [PATCH 07/11] Fixed type names --- .gitignore | 1 + crates/benda/src/lib.rs | 2 +- crates/benda/src/types/i24.rs | 34 +++++++++++++++++----------------- crates/benda/src/types/mod.rs | 4 ++-- crates/benda/src/types/tree.rs | 6 +++--- crates/benda/src/types/u24.rs | 34 +++++++++++++++++----------------- 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 6a456fc..84446c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/result/ /tmp/ /.direnv/ diff --git a/crates/benda/src/lib.rs b/crates/benda/src/lib.rs index 9a8853d..5f71587 100644 --- a/crates/benda/src/lib.rs +++ b/crates/benda/src/lib.rs @@ -115,7 +115,7 @@ impl PyBjit { fn benda(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(switch, m)?)?; m.add_class::()?; - m.add_class::()?; + m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/crates/benda/src/types/i24.rs b/crates/benda/src/types/i24.rs index b9e1fb8..0af0b96 100644 --- a/crates/benda/src/types/i24.rs +++ b/crates/benda/src/types/i24.rs @@ -7,9 +7,9 @@ use super::{BendType, ToBendResult}; #[pyclass(module = "benda")] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub struct U24(i32); +pub struct i24(i32); -impl BendType for U24 { +impl BendType for i24 { fn to_bend(&self) -> ToBendResult { Ok(imp::Expr::Num { val: bend::fun::Num::I24(self.0), @@ -17,7 +17,7 @@ impl BendType for U24 { } } -impl U24 { +impl i24 { const MAX: i32 = 0xffffff; // TODO: Check if the masking is working properly @@ -30,43 +30,43 @@ impl U24 { } } -impl std::fmt::Debug for U24 { +impl std::fmt::Debug for i24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl std::fmt::Display for U24 { +impl std::fmt::Display for i24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl From for U24 { +impl From for i24 { fn from(value: i32) -> Self { - U24::new(value) + i24::new(value) } } -impl From for i32 { - fn from(val: U24) -> Self { +impl From for i32 { + fn from(val: i24) -> Self { val.0 } } -impl std::ops::Add for U24 { +impl std::ops::Add for i24 { type Output = Self; fn add(self, other: Self) -> Self { - U24::new(self.0 + other.0) + i24::new(self.0 + other.0) } } -impl std::ops::Sub for U24 { +impl std::ops::Sub for i24 { type Output = Self; fn sub(self, other: Self) -> Self { - U24::new(self.0 - other.0) + i24::new(self.0 - other.0) } } @@ -74,18 +74,18 @@ impl std::ops::Sub for U24 { // TODO: Implement tests for each operation comparing to Bend #[pymethods] -impl U24 { +impl i24 { #[new] fn new_py(value: i32) -> Self { - U24::new(value) + i24::new(value) } fn __add__(&self, other: &Self) -> Self { - U24::add(*self, *other) + i24::add(*self, *other) } fn __sub__(&self, other: &Self) -> Self { - U24::sub(*self, *other) + i24::sub(*self, *other) } fn __str__(&self) -> String { diff --git a/crates/benda/src/types/mod.rs b/crates/benda/src/types/mod.rs index de02f86..9c4c778 100644 --- a/crates/benda/src/types/mod.rs +++ b/crates/benda/src/types/mod.rs @@ -55,7 +55,7 @@ pub fn extract_type_raw(arg: Bound) -> Option> { match arg_type { BuiltinType::U24 => { - Some(Box::new(extract_inner::(arg).unwrap())) + Some(Box::new(extract_inner::(arg).unwrap())) } BuiltinType::I32 => Some(extract_num_raw(arg, BuiltinType::I32)), BuiltinType::F32 => Some(extract_num_raw(arg, BuiltinType::F32)), @@ -70,7 +70,7 @@ pub fn extract_type(arg: Bound, book: &Book) -> ToBendResult { let arg_type = BuiltinType::from(name.to_string()); match arg_type { - BuiltinType::U24 => extract_inner::(arg).unwrap().to_bend(), + BuiltinType::U24 => extract_inner::(arg).unwrap().to_bend(), BuiltinType::I32 => extract_num(arg, BuiltinType::I32), BuiltinType::F32 => extract_num(arg, BuiltinType::F32), BuiltinType::Tree => extract_inner::(arg).unwrap().to_bend(), diff --git a/crates/benda/src/types/tree.rs b/crates/benda/src/types/tree.rs index d5b1b17..e1e828d 100644 --- a/crates/benda/src/types/tree.rs +++ b/crates/benda/src/types/tree.rs @@ -5,14 +5,14 @@ use bend::{fun, imp}; use pyo3::types::{PyAnyMethods, PyTuple, PyTypeMethods}; use pyo3::{pyclass, pymethods, Bound}; -use super::u24::U24; +use super::u24::u24; use super::{BendType, ToBendResult}; use crate::types::extract_inner; #[derive(Clone, Debug)] #[pyclass(module = "benda", name = "Leaf")] pub struct Leaf { - pub value: U24, + pub value: u24, } impl BendType for Leaf { @@ -26,7 +26,7 @@ impl Leaf { #[new] fn __new__(val: u32) -> Self { Self { - value: U24::new(val), + value: u24::new(val), } } } diff --git a/crates/benda/src/types/u24.rs b/crates/benda/src/types/u24.rs index 2aabce6..78bacb0 100644 --- a/crates/benda/src/types/u24.rs +++ b/crates/benda/src/types/u24.rs @@ -7,9 +7,9 @@ use super::{BendType, ToBendResult}; #[pyclass(module = "benda")] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub struct U24(u32); +pub struct u24(u32); -impl BendType for U24 { +impl BendType for u24 { fn to_bend(&self) -> ToBendResult { Ok(imp::Expr::Num { val: bend::fun::Num::U24(self.0), @@ -17,7 +17,7 @@ impl BendType for U24 { } } -impl U24 { +impl u24 { const MAX: u32 = 0xffffff; // TODO: Check if the masking is working properly @@ -30,43 +30,43 @@ impl U24 { } } -impl std::fmt::Debug for U24 { +impl std::fmt::Debug for u24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl std::fmt::Display for U24 { +impl std::fmt::Display for u24 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl From for U24 { +impl From for u24 { fn from(value: u32) -> Self { - U24::new(value) + u24::new(value) } } -impl From for u32 { - fn from(val: U24) -> Self { +impl From for u32 { + fn from(val: u24) -> Self { val.0 } } -impl std::ops::Add for U24 { +impl std::ops::Add for u24 { type Output = Self; fn add(self, other: Self) -> Self { - U24::new(self.0 + other.0) + u24::new(self.0 + other.0) } } -impl std::ops::Sub for U24 { +impl std::ops::Sub for u24 { type Output = Self; fn sub(self, other: Self) -> Self { - U24::new(self.0 - other.0) + u24::new(self.0 - other.0) } } @@ -74,18 +74,18 @@ impl std::ops::Sub for U24 { // TODO: Implement tests for each operation comparing to Bend #[pymethods] -impl U24 { +impl u24 { #[new] fn new_py(value: u32) -> Self { - U24::new(value) + u24::new(value) } fn __add__(&self, other: &Self) -> Self { - U24::add(*self, *other) + u24::add(*self, *other) } fn __sub__(&self, other: &Self) -> Self { - U24::sub(*self, *other) + u24::sub(*self, *other) } fn __str__(&self) -> String { From 0c4590ee3d7d2462ef8a165344e3e630a3623064 Mon Sep 17 00:00:00 2001 From: vkobinski Date: Thu, 13 Jun 2024 17:23:49 -0300 Subject: [PATCH 08/11] Removed print --- crates/benda/src/parser/mod.rs | 2 +- result | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/benda/src/parser/mod.rs b/crates/benda/src/parser/mod.rs index 15bd78d..bd7a4d6 100644 --- a/crates/benda/src/parser/mod.rs +++ b/crates/benda/src/parser/mod.rs @@ -916,7 +916,7 @@ impl<'py> Parser<'py> { self.book.entrypoint = None; - println!("BEND:\n {}", self.book.display_pretty()); + //println!("BEND:\n {}", self.book.display_pretty()); let return_val = run(&self.book); diff --git a/result b/result index d3859dd..698d4b6 120000 --- a/result +++ b/result @@ -1 +1 @@ -/nix/store/rb3lbl2iizrf45f0rgn8g3w2zy6zcqwr-rust-workspace-unknown \ No newline at end of file +/nix/store/z9chla9dsjk8q5hk07vz3y0zfy32m80p-rust-workspace-unknown \ No newline at end of file From 7d711fb86986d7aa297b9a677c56f76357861164 Mon Sep 17 00:00:00 2001 From: vkobinski Date: Thu, 13 Jun 2024 18:58:30 -0300 Subject: [PATCH 09/11] Running examples --- crates/benda/src/lib.rs | 2 +- crates/benda/src/parser/mod.rs | 14 +++++--- crates/benda/src/types/mod.rs | 58 ++++++++++++++++++++++++++++++++++ examples/simple.py | 12 +++---- 4 files changed, 73 insertions(+), 13 deletions(-) diff --git a/crates/benda/src/lib.rs b/crates/benda/src/lib.rs index 5f71587..bb662ea 100644 --- a/crates/benda/src/lib.rs +++ b/crates/benda/src/lib.rs @@ -94,7 +94,7 @@ impl PyBjit { parsed_types.clone(), ); let return_val = - parser.parse(fun_def.name.as_ref(), &[]); + parser.parse(name.to_string().as_ref(), &[]); val = Some(PyString::new_bound( py, return_val.as_str(), diff --git a/crates/benda/src/parser/mod.rs b/crates/benda/src/parser/mod.rs index bd7a4d6..ce81831 100644 --- a/crates/benda/src/parser/mod.rs +++ b/crates/benda/src/parser/mod.rs @@ -14,7 +14,7 @@ use rustpython_parser::ast::{ }; use crate::benda_ffi::run; -use crate::types::extract_type; +use crate::types::{extract_type, extract_type_expr}; #[derive(Clone, Debug)] enum FromExpr { @@ -194,11 +194,15 @@ impl<'py> Parser<'py> { } rExpr::Call(c) => { - let fun = c.func; + let fun = c.clone().func; let expr = self.parse_expr_type(*fun); if let Some(FromExpr::Expr(Expr::Var { ref nam })) = expr { + if let Some(var) = extract_type_expr(c.clone()) { + return Some(FromExpr::Expr(var)); + } + let mut args: Vec = vec![]; for arg in c.args { @@ -865,9 +869,9 @@ impl<'py> Parser<'py> { } } - if !is_bjit { - return; - } + //if !is_bjit { + //return; + //} let args = *fun_def.args.clone(); let mut names: Vec = vec![]; diff --git a/crates/benda/src/types/mod.rs b/crates/benda/src/types/mod.rs index 9c4c778..889b7d4 100644 --- a/crates/benda/src/types/mod.rs +++ b/crates/benda/src/types/mod.rs @@ -2,9 +2,18 @@ use bend::fun::{Book, Num}; use bend::imp::{self}; use pyo3::types::{PyAnyMethods, PyFloat, PyTypeMethods}; use pyo3::{Bound, FromPyObject, PyAny, PyErr, PyTypeCheck}; +use rustpython_parser::ast::located::Expr; use tree::{Leaf, Node, Tree}; use user_adt::UserAdt; +use num_traits::cast::ToPrimitive; + +use rustpython_parser::ast::{ + located, CmpOp as rCmpOp, Expr as rExpr, ExprAttribute, ExprBinOp, + ExprCall, Operator as rOperator, Pattern as rPattern, Stmt as rStmt, + StmtAssign, StmtClassDef, StmtExpr, StmtFunctionDef, StmtIf, StmtMatch, +}; + pub mod f24; pub mod i24; pub mod tree; @@ -80,6 +89,54 @@ pub fn extract_type(arg: Bound, book: &Book) -> ToBendResult { } } +pub fn extract_type_expr(call: ExprCall) -> Option { + let name = call.func.as_name_expr().unwrap().id.to_string(); + + let arg = call.args.first().unwrap(); + + let arg_type = BuiltinType::from(name.to_string()); + + match arg_type { + BuiltinType::U24 => Some(imp::Expr::Num { + val: Num::U24( + arg.clone() + .constant_expr() + .unwrap() + .value + .int() + .unwrap() + .to_u32() + .unwrap(), + ), + }), + BuiltinType::I32 => Some(imp::Expr::Num { + val: Num::I24( + arg.clone() + .constant_expr() + .unwrap() + .value + .int() + .unwrap() + .to_i32() + .unwrap(), + ), + }), + BuiltinType::F32 => Some(imp::Expr::Num { + val: Num::F24( + arg.clone() + .constant_expr() + .unwrap() + .value + .int() + .unwrap() + .to_f32() + .unwrap(), + ), + }), + _ => None, + } +} + #[derive(Debug)] pub enum BuiltinType { U24, @@ -97,6 +154,7 @@ impl From for BuiltinType { "float" => BuiltinType::F32, "int" => BuiltinType::I32, "benda.u24" => BuiltinType::U24, + "u24" => BuiltinType::U24, "benda.Node" => BuiltinType::Node, "benda.Leaf" => BuiltinType::Leaf, "benda.Tree" => BuiltinType::Tree, diff --git a/examples/simple.py b/examples/simple.py index 857daa2..41c26e9 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -1,15 +1,13 @@ import benda from benda import bjit, u24 -def simple(a) -> u24: - x = u24(a) + +def simple() -> u24: + x = u24(3) y = x - u24(2) return y translated_simple = bjit(simple) -val = simple(5) -print(val) - -val_bend = translated_simple(5) -print(val_bend) \ No newline at end of file +print(simple()) +print(translated_simple()) \ No newline at end of file From 9378021fee3690cbf786f71c80e59b33f25f28da Mon Sep 17 00:00:00 2001 From: vkobinski Date: Thu, 13 Jun 2024 18:59:34 -0300 Subject: [PATCH 10/11] Fmt --- crates/benda/src/types/mod.rs | 11 ++--------- result | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/crates/benda/src/types/mod.rs b/crates/benda/src/types/mod.rs index 889b7d4..758fda7 100644 --- a/crates/benda/src/types/mod.rs +++ b/crates/benda/src/types/mod.rs @@ -1,19 +1,12 @@ use bend::fun::{Book, Num}; use bend::imp::{self}; +use num_traits::cast::ToPrimitive; use pyo3::types::{PyAnyMethods, PyFloat, PyTypeMethods}; use pyo3::{Bound, FromPyObject, PyAny, PyErr, PyTypeCheck}; -use rustpython_parser::ast::located::Expr; +use rustpython_parser::ast::ExprCall; use tree::{Leaf, Node, Tree}; use user_adt::UserAdt; -use num_traits::cast::ToPrimitive; - -use rustpython_parser::ast::{ - located, CmpOp as rCmpOp, Expr as rExpr, ExprAttribute, ExprBinOp, - ExprCall, Operator as rOperator, Pattern as rPattern, Stmt as rStmt, - StmtAssign, StmtClassDef, StmtExpr, StmtFunctionDef, StmtIf, StmtMatch, -}; - pub mod f24; pub mod i24; pub mod tree; diff --git a/result b/result index 698d4b6..11a79e9 120000 --- a/result +++ b/result @@ -1 +1 @@ -/nix/store/z9chla9dsjk8q5hk07vz3y0zfy32m80p-rust-workspace-unknown \ No newline at end of file +/nix/store/azwmb2sjg9zz83ybcfgh2l4yz2jgx163-rust-workspace-unknown \ No newline at end of file From b9ad7bc3a00a4fde7ea0ff6ea0fca925f1207474 Mon Sep 17 00:00:00 2001 From: vkobinski Date: Thu, 13 Jun 2024 19:04:05 -0300 Subject: [PATCH 11/11] simple.py --- examples/simple.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/simple.py b/examples/simple.py index 41c26e9..ada81ce 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -8,6 +8,7 @@ def simple() -> u24: return y -translated_simple = bjit(simple) -print(simple()) -print(translated_simple()) \ No newline at end of file +if __name__ == "__main__": + translated_simple = bjit(simple) + print(simple()) + print(translated_simple()) \ No newline at end of file