Skip to content

Commit

Permalink
Added UserAdt
Browse files Browse the repository at this point in the history
  • Loading branch information
vkobinski committed Jun 13, 2024
1 parent 3059e61 commit 7bd20e7
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 112 deletions.
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/benda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down 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>()?;
m.add_class::<u24::U24>()?;
m.add_class::<Tree>()?;
m.add_class::<Node>()?;
m.add_class::<Leaf>()?;
Expand Down
1 change: 1 addition & 0 deletions crates/benda/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion crates/benda/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}

Expand Down
35 changes: 17 additions & 18 deletions crates/benda/src/types/f24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ 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),
})
}
}

impl f24 {
impl F24 {
// TODO: Implement a masking for float numbers.
pub fn new(value: f32) -> Self {
Self(value)
Expand All @@ -29,62 +28,62 @@ 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<f32> for f24 {
impl From<f32> for F24 {
fn from(value: f32) -> Self {
f24::new(value)
F24::new(value)
}
}

impl From<f24> for f32 {
fn from(val: f24) -> Self {
impl From<F24> 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)
}
}

// TODO: Check for overflow on the operations
// 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 {
Expand Down
35 changes: 17 additions & 18 deletions crates/benda/src/types/i24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ 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),
})
}
}

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

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

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

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

0 comments on commit 7bd20e7

Please sign in to comment.