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

Add C++ translator #270

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Add C++ translator #270

wants to merge 14 commits into from

Conversation

pcmoritz
Copy link

@pcmoritz pcmoritz commented Dec 7, 2024

This adds a preliminary version of a Python to C++ translator. Can be run with

cargo run --bin enderpy translate <name of python file>

let l_type = self.infer_expr_type(&b.left);
let r_type = self.infer_expr_type(&b.right);
// let l_type = self.infer_expr_type(&b.left);
self.visit_expr(&b.left);
Copy link
Author

Choose a reason for hiding this comment

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

I believe this was a bug in the original code -- it mean that the subexpressions don't get their types annotated

} else if let PythonType::Class(c) = &called_type {
Ok(called_type)
// This instantiates the class
Ok(PythonType::Instance(types::InstanceType::new(c.clone(), [].to_vec())))
Copy link
Author

Choose a reason for hiding this comment

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

Was this a bug? After the constructur is called, you get an instance of the class and not the class type itself

ast::ConstantValue::Float => self.get_builtin_type("float"),
ast::ConstantValue::Str(_) => self.get_builtin_type("str"),
ast::ConstantValue::Bool(_) => self.get_builtin_type("bool"),
ast::ConstantValue::Int => self.get_builtin_type("int").map(class_type_to_instance_type),
Copy link
Author

Choose a reason for hiding this comment

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

Was this a bug? At the moment literals for these primitive types are class types and not instance types, I believe they should be instance types :)

@pcmoritz
Copy link
Author

pcmoritz commented Dec 7, 2024

@Glyphack I don't think you want to merge this -- I've been starting to hack on a Python -> C++ translator on top of the codebase, maybe you are interested in this :)

@Glyphack
Copy link
Owner

Glyphack commented Dec 8, 2024

Wow this is amazing, I was also thinking if such thing would be possible.

@Glyphack
Copy link
Owner

By the way there is a problem in the parser with operator precedence. I will fix it soon. For the type checker it was not a big issue but for this it will be.

@pcmoritz
Copy link
Author

pcmoritz commented Dec 10, 2024

Thanks, that sounds great!

The thing I've gotten most confused about so far is the difference between class type and instance type -- it seems instance type was introduced in #266, but then lots of things that I would expect to be instance types are still class types (e.g. literal values like strings or ints, and then also the parameters of functions like def f(i: int), in the parameter I could see "int" to be a class type, but "i" should be an instance type, no?). Curious if you can clarify that (I made some adjustments in this PR but they are not passing cargo test) :)

@Glyphack
Copy link
Owner

Glyphack commented Dec 11, 2024

You are right, that should be instance. Actually that was the point I started contributing to Ruff's type checker to understand the python typing better. When I reached that point I realized my knowledge of the typing system is not enough to go further.
Right now I would just refer to this when I'm in doubt about the typing it's pretty complete right now.
Whenever a class is used in the type annotations the inference should be instance of that class.
I think if I go over the types again I fix these mistakes I made earlier.

btw I'm interested in the idea :) What is your goal? Writing a complete python type checker might be very hard but I can focus on a subset of it that can be used for the compiler. Then we can compile a subset of python.

Update: I fixed the operator precedence here #271 will add some more tests and take another look tomorrow.

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