-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: main
Are you sure you want to change the base?
Add C++ translator #270
Conversation
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); |
There was a problem hiding this comment.
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()))) |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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 :)
@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 :) |
Wow this is amazing, I was also thinking if such thing would be possible. |
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. |
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 |
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. 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. |
This adds a preliminary version of a Python to C++ translator. Can be run with