From 65acef4a3faae65b8d7127e845c55a842c14a998 Mon Sep 17 00:00:00 2001 From: vkobinski Date: Fri, 21 Jun 2024 20:01:22 -0300 Subject: [PATCH] Running quicksort example --- crates/benda/src/types/book.rs | 26 ++++++++--------- examples/quicksort.py | 53 +++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/crates/benda/src/types/book.rs b/crates/benda/src/types/book.rs index 38e5e93..f377a34 100644 --- a/crates/benda/src/types/book.rs +++ b/crates/benda/src/types/book.rs @@ -1,5 +1,4 @@ use std::cell::RefCell; -use std::ffi::CString; use std::vec; use bend::fun::{self, Book as BendBook, Name, Rule}; @@ -7,10 +6,10 @@ use bend::imp::{self, Expr, Stmt}; use indexmap::IndexMap; use num_traits::ToPrimitive; use pyo3::exceptions::PyException; -use pyo3::ffi::PyType_FromSpec; +use pyo3::ffi::{PyMethodDef, PyType_FromSpec}; use pyo3::inspect::types::{ModuleName, TypeInfo}; use pyo3::prelude::*; -use pyo3::types::{PyString, PyTuple, PyType}; +use pyo3::types::{PyList, PyMapping, PyString, PyTuple, PyType}; use pyo3::PyTypeInfo; use super::user_adt::UserAdt; @@ -29,7 +28,7 @@ pub struct Term { term: fun::Term, } -fn get_list(lam: &fun::Term, vals: &mut Vec) { +fn get_list(lam: &fun::Term, vals: &mut Vec) { match lam { fun::Term::Lam { tag, pat, bod } => { get_list(bod, vals); @@ -41,15 +40,15 @@ fn get_list(lam: &fun::Term, vals: &mut Vec) { fun::Term::Num { val } => match val { fun::Num::U24(v) => { if v.to_u32().unwrap() != 1 && v.to_u32().unwrap() != 0 { - vals.push(v.to_string()) + vals.push(v.to_i32().unwrap()) } } fun::Num::I24(v) => { if v.to_u32().unwrap() != 1 && v.to_u32().unwrap() != 0 { - vals.push(v.to_string()) + vals.push(v.to_i32().unwrap()) } } - fun::Num::F24(v) => vals.push(v.to_string()), + fun::Num::F24(v) => vals.push(v.to_i32().unwrap()), }, _ => {} } @@ -64,7 +63,7 @@ impl Term { fn __getattr__(&self, object: Bound) -> PyResult { let py = object.py(); - let mut vals: Vec = vec![]; + let mut vals: Vec = vec![]; get_list(&self.term, &mut vals); Ok(vals.into_py(py)) @@ -75,6 +74,7 @@ impl Term { #[derive(Clone, Debug)] pub struct Ctr { entire_name: String, + name: String, fields: IndexMap>>, } @@ -94,11 +94,6 @@ impl Ctr { }) } - fn __instancecheck__(&self, instance: Bound) -> bool { - dbg!("si"); - todo!() - } - fn __str__(&self) -> String { format!("Bend ADT: {}", self.entire_name) } @@ -129,6 +124,10 @@ impl Ctr { return Ok(PyString::new_bound(py, &self.entire_name).into_py(py)); } + if &object.to_string() == "name" { + return Ok(PyString::new_bound(py, &self.name).into()); + } + if let Ok(val) = object.to_string().parse::() { let return_val = self.fields.get_index(val - 1); if let Some(return_val) = return_val { @@ -348,6 +347,7 @@ impl Book { for (ctr_name, ctr_fields) in bend_adt.ctrs.iter() { let new_name = ctr_name.split('/').last().unwrap().to_string(); let mut new_ctr = Ctr { + name: new_name.clone(), entire_name: ctr_name.to_string(), fields: IndexMap::new(), }; diff --git a/examples/quicksort.py b/examples/quicksort.py index 1f34d0c..6b88505 100644 --- a/examples/quicksort.py +++ b/examples/quicksort.py @@ -1,14 +1,16 @@ from typing import TypeVar import random -from .quicksort_mock import u24, List, List_Cons, List_Nil, mock_sort +from .quicksort_mock import u24, List_Cons, List_Nil, mock_sort +from .quicksort_mock import List as ListMock T = TypeVar("T") -# import benda -# from benda import u24 -# book = benda.load_book_from_file("./quicksort.bend") +import benda +from benda import U24 +book = benda.load_book_from_file("./examples/quicksort.bend") +List = book.adts.List # List_Nil = book.adts.List.Nil # List_Cons = book.adts.List.Cons @@ -21,9 +23,9 @@ def gen_list(n: int, max_value: int = 0xffffff) -> list[u24]: return result -def to_cons_list(xs: list[int]) -> List[u24]: +def to_cons_list(xs: list[int]): """Converts a Python list to a Bend cons-list""" - result = List_Nil() + result = List.Nil() hi = len(xs) if hi == 0: @@ -31,22 +33,33 @@ def to_cons_list(xs: list[int]) -> List[u24]: while hi > 0: hi -= 1 - result = List_Cons(u24(xs[hi]), result) + result = List.Cons(u24(xs[hi]), result) return result - -def from_cons_list(xs: List[u24]) -> list[u24]: +# Ideal Syntax: +#def from_cons_list(xs: List[u24]) -> list[u24]: +# """Converts a Bend cons-list to a Python list""" +# result: list[u24] = [] +# while True: +# match xs: +# case List_Nil(): +# return result +# case List_Cons(value, tail): +# result.append(value) +# xs = tail + +def from_cons_list(xs) -> list[u24]: """Converts a Bend cons-list to a Python list""" result: list[u24] = [] while True: - match xs: - case List_Nil(): + match xs.name: + case List.Nil.name: return result - case List_Cons(value, tail): + case List.Cons.name: + value = xs.head result.append(value) - xs = tail - + xs = xs.tail def main(): data = gen_list(10, 1000) @@ -57,13 +70,13 @@ def main(): cons_list = to_cons_list(data) - # sorted_res = book.defs.Sort(cons_list) - # sorted_arr = from_cons_list(sorted_res) - # print("Result: ", sorted_arr) + sorted_res = book.defs.Sort(cons_list) + sorted_arr = sorted_res.list + print("Result: ", sorted_arr) - mocked_sorted = mock_sort(cons_list) - mocked_sorted_arr = from_cons_list(mocked_sorted) - print("Mocked: ", mocked_sorted_arr) + #mocked_sorted = mock_sort(cons_list) + #mocked_sorted_arr = mocked_from_cons_list(mocked_sorted) + #print("Mocked: ", mocked_sorted_arr) if __name__ == "__main__":