Skip to content

placeholder draft #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions src/py/lazy_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,31 @@ use pyo3::types::{PyBytes, PyTuple};
pub struct LazyNode {
allocator: Rc<Allocator>,
node: NodePtr,
}

impl ToPyObject for LazyNode {
fn to_object(&self, py: Python<'_>) -> PyObject {
let node: &PyCell<LazyNode> = PyCell::new(py, self.clone()).unwrap();
let pa: &PyAny = node;
pa.to_object(py)
}
obj: Option<PyObject>,
}

#[pymethods]
impl LazyNode {
#[getter(pair)]
pub fn pair(&self, py: Python) -> PyResult<Option<PyObject>> {
pub fn pair(&mut self, py: Python) -> PyResult<Option<PyObject>> {
match &self.allocator.sexp(self.node) {
SExp::Pair(p1, p2) => {
let r1 = Self::new(self.allocator.clone(), *p1);
let r2 = Self::new(self.allocator.clone(), *p2);
let v: &PyTuple = PyTuple::new(py, &[r1, r2]);
Ok(Some(v.into()))
SExp::Pair(_p1, _p2) => {
self.populate(py)?;
Ok(self.obj.clone())
}

_ => Ok(None),
}
}

#[getter(atom)]
pub fn atom(&self, py: Python) -> Option<PyObject> {
pub fn atom(&mut self, py: Python) -> PyResult<Option<PyObject>> {
match &self.allocator.sexp(self.node) {
SExp::Atom(atom) => Some(PyBytes::new(py, self.allocator.buf(atom)).into()),
_ => None,
SExp::Atom(_atom) => {
self.populate(py)?;
Ok(self.obj.clone())
}
_ => Ok(None),
}
}
}
Expand All @@ -48,6 +43,40 @@ impl LazyNode {
Self {
allocator: a,
node: n,
obj: None,
}
}

pub fn new_cell(py: Python, a: Rc<Allocator>, n: NodePtr) -> PyResult<&PyCell<Self>> {
PyCell::new(
py,
Self {
allocator: a,
node: n,
obj: None,
},
)
}

pub fn populate(&mut self, py: Python) -> PyResult<()> {
if self.obj.is_none() {
let new_obj: PyObject = {
match &self.allocator.sexp(self.node) {
SExp::Pair(p1, p2) => {
let r1 = Self::new_cell(py, self.allocator.clone(), *p1)?.to_object(py);
let r2 = Self::new_cell(py, self.allocator.clone(), *p2)?.to_object(py);
let v: &PyTuple = PyTuple::new(py, &[r1, r2]);
v.into()
}
SExp::Atom(atom) => PyBytes::new(py, self.allocator.buf(atom)).into(),
}
};
self.obj = Some(new_obj);
}
Ok(())
}

pub fn is_populated(&self) -> bool {
self.obj.is_some()
}
}
2 changes: 1 addition & 1 deletion src/py/run_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub fn run_generator(
Ok((error_code, ret, 0))
}
Err(eval_err) => {
let node = LazyNode::new(Rc::new(allocator), eval_err.0);
let node = LazyNode::new_cell(py, Rc::new(allocator), eval_err.0)?;
let msg = eval_err.1;
let ctx: &PyDict = PyDict::new(py);
ctx.set_item("msg", msg)?;
Expand Down
2 changes: 1 addition & 1 deletion src/py/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub fn deserialize_and_run_program2(
Ok((reduction.0, val))
}
Err(eval_err) => {
let node = LazyNode::new(Rc::new(allocator), eval_err.0);
let node = LazyNode::new_cell(py, Rc::new(allocator), eval_err.0)?;
let msg = eval_err.1;
let ctx: &PyDict = PyDict::new(py);
ctx.set_item("msg", msg)?;
Expand Down