Skip to content

Commit

Permalink
Cmpop::as_str (#72)
Browse files Browse the repository at this point in the history
* clean up pyo3 generation

* Cmpop::as_str
  • Loading branch information
youknowone authored May 28, 2023
1 parent 4de0cb1 commit 531aeb3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
12 changes: 6 additions & 6 deletions ast-pyo3/src/gen/to_py_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,8 +1653,8 @@ impl ToPyAst for ast::StmtPass<TextRange> {
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();

let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
let Self { range: _range } = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;

Ok(instance)
}
Expand All @@ -1665,8 +1665,8 @@ impl ToPyAst for ast::StmtBreak<TextRange> {
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();

let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
let Self { range: _range } = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;

Ok(instance)
}
Expand All @@ -1677,8 +1677,8 @@ impl ToPyAst for ast::StmtContinue<TextRange> {
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();

let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
let Self { range: _range } = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;

Ok(instance)
}
Expand Down Expand Up @@ -3429,8 +3429,8 @@ impl ToPyAst for ast::StmtPass<SourceRange> {
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();

let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
let Self { range: _range } = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;

let cache = ast_cache();
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
Expand All @@ -3449,8 +3449,8 @@ impl ToPyAst for ast::StmtBreak<SourceRange> {
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();

let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
let Self { range: _range } = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;

let cache = ast_cache();
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
Expand All @@ -3469,8 +3469,8 @@ impl ToPyAst for ast::StmtContinue<SourceRange> {
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();

let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
let Self { range: _range } = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;

let cache = ast_cache();
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
Expand Down
13 changes: 13 additions & 0 deletions ast-pyo3/src/py_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ struct AstCache {
}

impl AstCache {
// fn location_vec<'py>(&'static self, py: Python<'py>, range: &SourceRange) -> &'py PyDict {
// let attributes = PyDict::new(py);
// attributes.set_item(self.lineno.as_ref(py), range.start.row.get()).unwrap();
// attributes.set_item(self.col_offset.as_ref(py), range.start.column.to_zero_indexed()).unwrap();
// if let Some(end) = range.end {
// attributes.set_item(self.end_lineno.as_ref(py), end.row.get()).unwrap();
// attributes.set_item(
// self.end_col_offset.as_ref(py),
// end.column.to_zero_indexed(),
// ).unwrap();
// }
// attributes
// }
#[inline]
fn none_ref<'py>(&'static self, py: Python<'py>) -> &'py PyAny {
Py::<PyAny>::as_ref(&self.none, py)
Expand Down
2 changes: 1 addition & 1 deletion ast-pyo3/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<T: ToPyWrapper> ToPyWrapper for Vec<T> {
}
}

#[pyclass(module = "rustpython_ast", subclass)]
#[pyclass(module = "rustpython_ast", name = "AST", subclass)]
pub struct Ast;

#[pymethods]
Expand Down
23 changes: 7 additions & 16 deletions ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,11 +1078,6 @@ def generics(self):
else:
assert False, self.namespace

@property
def location(self):
# lineno, col_offset
pass

def visitModule(self, mod):
for dfn in mod.dfns:
self.visit(dfn)
Expand Down Expand Up @@ -1133,12 +1128,13 @@ def visitConstructor(self, cons, type):

def emit_to_pyo3_with_fields(self, cons, type, name):
type_info = self.type_info[type.name]

self.emit(
f"""
impl ToPyAst for ast::{name}{self.generics} {{
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
let cache = Self::py_type_cache().get().unwrap();
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
let cache = Self::py_type_cache().get().unwrap();
""",
0,
)
Expand Down Expand Up @@ -1171,12 +1167,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
3,
)
continue
if field.name in (
"lineno",
"col_offset",
"end_lineno",
"end_col_offset",
):
if field.name == "lineno":
self.emit(
f"{rust_field(field.name)}.to_u32().to_object(py),",
3,
Expand All @@ -1192,11 +1183,11 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
)
else:
self.emit(
"let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;",
"let Self { range: _range } = self;",
1,
)
self.emit(
"let Self { range: _range } = self;",
"""let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;""",
1,
)
if type.value.attributes and self.namespace == "located":
Expand All @@ -1210,7 +1201,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
}
""",
1,
0,
)
self.emit(
"""
Expand Down
17 changes: 17 additions & 0 deletions ast/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,21 @@ impl<R> Default for EmptyRange<R> {
}
}

impl Cmpop {
pub fn as_str(&self) -> &'static str {
match self {
Cmpop::Eq => "==",
Cmpop::NotEq => "!=",
Cmpop::Lt => "<",
Cmpop::LtE => "<=",
Cmpop::Gt => ">",
Cmpop::GtE => ">=",
Cmpop::Is => "is",
Cmpop::IsNot => "is not",
Cmpop::In => "in",
Cmpop::NotIn => "not in",
}
}
}

include!("gen/generic.rs");
18 changes: 4 additions & 14 deletions ast/src/unparse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ConversionFlag;
use crate::{Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Expr, Identifier, Operator};
use crate::{Arg, Arguments, Boolop, Comprehension, Constant, Expr, Identifier, Operator};
use std::fmt;

mod precedence {
Expand Down Expand Up @@ -285,19 +285,9 @@ impl<'a> Unparser<'a> {
let new_lvl = precedence::CMP + 1;
self.unparse_expr(left, new_lvl)?;
for (op, cmp) in ops.iter().zip(comparators) {
let op = match op {
Cmpop::Eq => " == ",
Cmpop::NotEq => " != ",
Cmpop::Lt => " < ",
Cmpop::LtE => " <= ",
Cmpop::Gt => " > ",
Cmpop::GtE => " >= ",
Cmpop::Is => " is ",
Cmpop::IsNot => " is not ",
Cmpop::In => " in ",
Cmpop::NotIn => " not in ",
};
self.p(op)?;
self.p(" ")?;
self.p(op.as_str())?;
self.p(" ")?;
self.unparse_expr(cmp, new_lvl)?;
}
})
Expand Down

0 comments on commit 531aeb3

Please sign in to comment.