Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vkobinski committed Jul 12, 2024
1 parent 492a0ba commit dca076c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
10 changes: 7 additions & 3 deletions crates/benda/src/types/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ macro_rules! generate_structs {
#[classattr]
fn __match_args__() -> PyResult<Py<PyAny>> {
Python::with_gil(|py| {
Ok(PyTuple::new_bound(py, vec!["1", "2", "3", "4", "5"])
.into_py(py))
Ok(PyTuple::new_bound(
py,
vec!["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
)
.into_py(py))
})
}

Expand Down Expand Up @@ -507,8 +510,9 @@ impl Definition {
b.defs
.insert(Name::new("main"), main_def.to_fun(true).unwrap());

println!("{}", b.display_pretty());
let res = benda_ffi::run(
&b.clone(),
&b,
&self.cmd.clone().unwrap_or_default().to_string(),
);

Expand Down
5 changes: 2 additions & 3 deletions crates/benda/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ impl From<String> for BuiltinType {
match value.as_str() {
"float" => BuiltinType::F32,
"int" => BuiltinType::U24,
"benda.u24" => BuiltinType::U24,
"u24" => BuiltinType::U24,
"benda.U24" => BuiltinType::U24,
"U24" => BuiltinType::U24,
_ => BuiltinType::UserAdt,
}
}
Expand All @@ -184,7 +184,6 @@ impl BendType for f32 {
impl BendType for i32 {
fn to_bend(&self) -> BendResult {
Ok(imp::Expr::Num {
//val: Num::I24(*self),
val: Num::I24(*self),
})
}
Expand Down
23 changes: 22 additions & 1 deletion crates/benda/src/types/u24.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::ops::{Add, Sub};

use bend::imp;
use pyo3::{pyclass, pymethods};
use num_traits::ToPrimitive;
use pyo3::basic::CompareOp;
use pyo3::{pyclass, pymethods, PyResult};

use super::{BendResult, BendType};

Expand Down Expand Up @@ -91,4 +93,23 @@ impl U24 {
fn __str__(&self) -> String {
self.0.to_string()
}

fn __repr__(&self) -> String {
self.0.to_string()
}

fn __int__(&self) -> i32 {
self.0.to_i32().unwrap()
}

fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
match op {
pyclass::CompareOp::Lt => Ok(self < other),
pyclass::CompareOp::Le => Ok(self <= other),
pyclass::CompareOp::Eq => Ok(self == other),
pyclass::CompareOp::Ne => Ok(self != other),
pyclass::CompareOp::Gt => Ok(self > other),
pyclass::CompareOp::Ge => Ok(self >= other),
}
}
}
33 changes: 16 additions & 17 deletions crates/benda/src/types/user_adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use std::vec;

use bend::fun::{Adt as BAdt, Book, Name, Num, Term as BTerm};
use bend::fun::{Book, Name, Num, Term as BTerm};
use bend::imp::{self};
use num_traits::ToPrimitive;
use pyo3::types::{PyAnyMethods, PyString, PyTuple};
Expand Down Expand Up @@ -228,14 +228,13 @@ pub fn from_term_into_adt(term: &BTerm, def_adts: &Ctrs) -> Option<TermParse> {
/// * `data` - The Python data associated with this ADT instance
/// * `book` - The Bend book containing ADT definitions
#[derive(Debug, Clone)]
pub struct UserAdt<'py> {
adt: BAdt,
pub struct UserAdt<'py, 'book> {
book: &'book Book,
full_name: Name,
data: Bound<'py, PyAny>,
book: Book,
}

impl<'py> UserAdt<'py> {
impl<'py, 'book> UserAdt<'py, 'book> {
/// Creates a new UserAdt instance
///
/// # Arguments
Expand All @@ -251,7 +250,7 @@ impl<'py> UserAdt<'py> {
///
/// This function attempts to create a UserAdt by matching the Python data's `__ctr_type__`
/// attribute with ADT definitions in the provided Bend book.
pub fn new(data: Bound<'py, PyAny>, book: &Book) -> Option<Self> {
pub fn new(data: Bound<'py, PyAny>, book: &'book Book) -> Option<Self> {
if data.is_none() {
return None;
}
Expand All @@ -264,18 +263,12 @@ impl<'py> UserAdt<'py> {
if let Ok(binding) = data.getattr("__ctr_type__") {
for (nam, _ctr) in &book.ctrs {
let new_nam = nam.to_string();
let two_names = new_nam.split_once('/').unwrap();

if nam.to_string() == binding.to_string() {
return Some(Self {
book: book.clone(),
book,
data,
full_name: Name::new(new_nam.clone()),
adt: book
.adts
.get(&Name::new(two_names.0.to_string()))
.unwrap()
.clone(),
});
}
}
Expand All @@ -295,10 +288,16 @@ impl<'py> UserAdt<'py> {
///
/// This method recursively converts the UserAdt and its fields into a Bend expression,
/// handling nested ADTs and other field types.
impl<'py> BendType for UserAdt<'py> {
impl<'py, 'book> BendType for UserAdt<'py, 'book> {
fn to_bend(&self) -> super::BendResult {
for (nam, fields) in &self.adt.ctrs {
if *nam == self.full_name {
let binding = self.full_name.to_string();
let name = binding.split("/").next().unwrap();
let adt = self.book.adts.get(&Name::new(name)).unwrap();

dbg!(binding);

for (nam, fields) in adt.ctrs.iter() {
if nam.to_string() == self.full_name.to_string() {
let mut adt_fields: Vec<imp::Expr> = vec![];

for field in fields {
Expand All @@ -313,7 +312,7 @@ impl<'py> BendType for UserAdt<'py> {

if let Some(t) = extract_type_raw(attr.clone()) {
adt_fields.push(t.to_bend().unwrap());
} else if let Some(adt) = UserAdt::new(attr, &self.book) {
} else if let Some(adt) = UserAdt::new(attr, self.book) {
let new_adt = adt.to_bend();
adt_fields.push(new_adt.unwrap());
} else {
Expand Down

0 comments on commit dca076c

Please sign in to comment.