Skip to content

Commit

Permalink
Fixed type names
Browse files Browse the repository at this point in the history
  • Loading branch information
vkobinski committed Jun 13, 2024
1 parent 7bd20e7 commit 8f4b815
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/result/
/tmp/
/.direnv/

Expand Down
2 changes: 1 addition & 1 deletion crates/benda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl PyBjit {
fn benda(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(switch, m)?)?;
m.add_class::<PyBjit>()?;
m.add_class::<u24::U24>()?;
m.add_class::<u24::u24>()?;
m.add_class::<Tree>()?;
m.add_class::<Node>()?;
m.add_class::<Leaf>()?;
Expand Down
34 changes: 17 additions & 17 deletions crates/benda/src/types/i24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ 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),
})
}
}

impl U24 {
impl i24 {
const MAX: i32 = 0xffffff;

// TODO: Check if the masking is working properly
Expand All @@ -30,62 +30,62 @@ 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<i32> for U24 {
impl From<i32> for i24 {
fn from(value: i32) -> Self {
U24::new(value)
i24::new(value)
}
}

impl From<U24> for i32 {
fn from(val: U24) -> Self {
impl From<i24> 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)
}
}

// TODO: Check for overflow on the operations
// 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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/benda/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn extract_type_raw(arg: Bound<PyAny>) -> Option<Box<dyn BendType>> {

match arg_type {
BuiltinType::U24 => {
Some(Box::new(extract_inner::<u24::U24>(arg).unwrap()))
Some(Box::new(extract_inner::<u24::u24>(arg).unwrap()))
}
BuiltinType::I32 => Some(extract_num_raw(arg, BuiltinType::I32)),
BuiltinType::F32 => Some(extract_num_raw(arg, BuiltinType::F32)),
Expand All @@ -70,7 +70,7 @@ pub fn extract_type(arg: Bound<PyAny>, book: &Book) -> ToBendResult {
let arg_type = BuiltinType::from(name.to_string());

match arg_type {
BuiltinType::U24 => extract_inner::<u24::U24>(arg).unwrap().to_bend(),
BuiltinType::U24 => extract_inner::<u24::u24>(arg).unwrap().to_bend(),
BuiltinType::I32 => extract_num(arg, BuiltinType::I32),
BuiltinType::F32 => extract_num(arg, BuiltinType::F32),
BuiltinType::Tree => extract_inner::<Tree>(arg).unwrap().to_bend(),
Expand Down
6 changes: 3 additions & 3 deletions crates/benda/src/types/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,7 +26,7 @@ impl Leaf {
#[new]
fn __new__(val: u32) -> Self {
Self {
value: U24::new(val),
value: u24::new(val),
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions crates/benda/src/types/u24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ 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),
})
}
}

impl U24 {
impl u24 {
const MAX: u32 = 0xffffff;

// TODO: Check if the masking is working properly
Expand All @@ -30,62 +30,62 @@ 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<u32> for U24 {
impl From<u32> for u24 {
fn from(value: u32) -> Self {
U24::new(value)
u24::new(value)
}
}

impl From<U24> for u32 {
fn from(val: U24) -> Self {
impl From<u24> 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)
}
}

// TODO: Check for overflow on the operations
// 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 {
Expand Down

0 comments on commit 8f4b815

Please sign in to comment.