Skip to content

Commit dca076c

Browse files
committed
Refactoring
1 parent 492a0ba commit dca076c

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

crates/benda/src/types/book.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ macro_rules! generate_structs {
136136
#[classattr]
137137
fn __match_args__() -> PyResult<Py<PyAny>> {
138138
Python::with_gil(|py| {
139-
Ok(PyTuple::new_bound(py, vec!["1", "2", "3", "4", "5"])
140-
.into_py(py))
139+
Ok(PyTuple::new_bound(
140+
py,
141+
vec!["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
142+
)
143+
.into_py(py))
141144
})
142145
}
143146

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

513+
println!("{}", b.display_pretty());
510514
let res = benda_ffi::run(
511-
&b.clone(),
515+
&b,
512516
&self.cmd.clone().unwrap_or_default().to_string(),
513517
);
514518

crates/benda/src/types/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ impl From<String> for BuiltinType {
158158
match value.as_str() {
159159
"float" => BuiltinType::F32,
160160
"int" => BuiltinType::U24,
161-
"benda.u24" => BuiltinType::U24,
162-
"u24" => BuiltinType::U24,
161+
"benda.U24" => BuiltinType::U24,
162+
"U24" => BuiltinType::U24,
163163
_ => BuiltinType::UserAdt,
164164
}
165165
}
@@ -184,7 +184,6 @@ impl BendType for f32 {
184184
impl BendType for i32 {
185185
fn to_bend(&self) -> BendResult {
186186
Ok(imp::Expr::Num {
187-
//val: Num::I24(*self),
188187
val: Num::I24(*self),
189188
})
190189
}

crates/benda/src/types/u24.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::ops::{Add, Sub};
22

33
use bend::imp;
4-
use pyo3::{pyclass, pymethods};
4+
use num_traits::ToPrimitive;
5+
use pyo3::basic::CompareOp;
6+
use pyo3::{pyclass, pymethods, PyResult};
57

68
use super::{BendResult, BendType};
79

@@ -91,4 +93,23 @@ impl U24 {
9193
fn __str__(&self) -> String {
9294
self.0.to_string()
9395
}
96+
97+
fn __repr__(&self) -> String {
98+
self.0.to_string()
99+
}
100+
101+
fn __int__(&self) -> i32 {
102+
self.0.to_i32().unwrap()
103+
}
104+
105+
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
106+
match op {
107+
pyclass::CompareOp::Lt => Ok(self < other),
108+
pyclass::CompareOp::Le => Ok(self <= other),
109+
pyclass::CompareOp::Eq => Ok(self == other),
110+
pyclass::CompareOp::Ne => Ok(self != other),
111+
pyclass::CompareOp::Gt => Ok(self > other),
112+
pyclass::CompareOp::Ge => Ok(self >= other),
113+
}
114+
}
94115
}

crates/benda/src/types/user_adt.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
use std::vec;
2121

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

238-
impl<'py> UserAdt<'py> {
237+
impl<'py, 'book> UserAdt<'py, 'book> {
239238
/// Creates a new UserAdt instance
240239
///
241240
/// # Arguments
@@ -251,7 +250,7 @@ impl<'py> UserAdt<'py> {
251250
///
252251
/// This function attempts to create a UserAdt by matching the Python data's `__ctr_type__`
253252
/// attribute with ADT definitions in the provided Bend book.
254-
pub fn new(data: Bound<'py, PyAny>, book: &Book) -> Option<Self> {
253+
pub fn new(data: Bound<'py, PyAny>, book: &'book Book) -> Option<Self> {
255254
if data.is_none() {
256255
return None;
257256
}
@@ -264,18 +263,12 @@ impl<'py> UserAdt<'py> {
264263
if let Ok(binding) = data.getattr("__ctr_type__") {
265264
for (nam, _ctr) in &book.ctrs {
266265
let new_nam = nam.to_string();
267-
let two_names = new_nam.split_once('/').unwrap();
268266

269267
if nam.to_string() == binding.to_string() {
270268
return Some(Self {
271-
book: book.clone(),
269+
book,
272270
data,
273271
full_name: Name::new(new_nam.clone()),
274-
adt: book
275-
.adts
276-
.get(&Name::new(two_names.0.to_string()))
277-
.unwrap()
278-
.clone(),
279272
});
280273
}
281274
}
@@ -295,10 +288,16 @@ impl<'py> UserAdt<'py> {
295288
///
296289
/// This method recursively converts the UserAdt and its fields into a Bend expression,
297290
/// handling nested ADTs and other field types.
298-
impl<'py> BendType for UserAdt<'py> {
291+
impl<'py, 'book> BendType for UserAdt<'py, 'book> {
299292
fn to_bend(&self) -> super::BendResult {
300-
for (nam, fields) in &self.adt.ctrs {
301-
if *nam == self.full_name {
293+
let binding = self.full_name.to_string();
294+
let name = binding.split("/").next().unwrap();
295+
let adt = self.book.adts.get(&Name::new(name)).unwrap();
296+
297+
dbg!(binding);
298+
299+
for (nam, fields) in adt.ctrs.iter() {
300+
if nam.to_string() == self.full_name.to_string() {
302301
let mut adt_fields: Vec<imp::Expr> = vec![];
303302

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

314313
if let Some(t) = extract_type_raw(attr.clone()) {
315314
adt_fields.push(t.to_bend().unwrap());
316-
} else if let Some(adt) = UserAdt::new(attr, &self.book) {
315+
} else if let Some(adt) = UserAdt::new(attr, self.book) {
317316
let new_adt = adt.to_bend();
318317
adt_fields.push(new_adt.unwrap());
319318
} else {

0 commit comments

Comments
 (0)