Skip to content
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

Added Parser #4

Merged
merged 9 commits into from
Jun 12, 2024
Merged

Added Parser #4

merged 9 commits into from
Jun 12, 2024

Conversation

vkobinski
Copy link
Collaborator

Added a lot of Python AST Parsing to Bend Books.

Comment on lines 17 to 18
indexmap = "2.2.3"
interner = "0.2.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments to non-obvious dependencies.

(We are always striving to have fewer dependencies, so it's good to have the non-obvious ones explained)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IndexMap is used by Bend to store their ADTs and constructor definitions, so I needed to add it to be able to construct a new IndexMap when parsing a new ADT.

Comment on lines 116 to 121
impl BendType for PyFloat {
fn to_bend(&self) -> imp::Expr {
let num: f32 = self.extract().unwrap();
imp::Expr::Num { val: Num::F24(num) }
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cast from f32 to F24 is an operation that could fail, so I imagine BendType::to_bend should actually return a Result akin to std::convert::TryInto.

Actually, the whole type BendType could be TryInto<Expr> if it's only method will be to_bend.

Copy link
Collaborator Author

@vkobinski vkobinski Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about some other methods that could help a type implementation in Benda, I will change the return type to a Result for now.
The BendType is useful for Generics, I'm using it in:

pub fn extract_inner<'py, T: BendType + PyTypeCheck + FromPyObject<'py>>(
arg: Bound<'py, PyAny>,
) -> Option<T> {
let inner = arg.downcast::<T>();
if let Ok(inner) = inner {
let inner = <T as FromPyObject>::extract_bound(inner.as_any());
return Some(inner.unwrap());
}
None
}

Node,
}

impl From<String> for BuiltinType {
Copy link
Member

@steinerkelvin steinerkelvin Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this being used anywhere?

I don't think we should be relying on strings like this in the final version.
(if we can rely on in the identity of the types though the Python API and Python itself is not using strings)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll find a way to reference these types in a better way. I don't like this kind of String usage either.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types names are not mapped anywhere in pyo3. I will find a better solution when our subset are better defined.
I use this From here:

let arg_type = BuiltinType::from(name.to_string());

@steinerkelvin steinerkelvin merged commit 5494220 into fglab-tech:master Jun 12, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants