-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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 type declarations to C code printer #10452
Conversation
a, b = symbols("a, b") | ||
|
||
assert ccode(a**2, b, declare="double") == "double b = pow(a, 2);" | ||
assert ccode(a, declare="double") == "double a;" |
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.
Press enter at end of the line.
Can anyone review this PR? |
@@ -298,6 +303,8 @@ def ccode(expr, assign_to=None, **settings): | |||
Setting contract=False will not generate loops, instead the user is | |||
responsible to provide values for the indices in the code. | |||
[default=True]. | |||
declare: string, optional | |||
When given, it specifies the data type of `assign_to` variable. |
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.
The above isn't true for this case: ccode(a, declare="double")
. Please make it generalize to what it actually does.
@moorepants I made the changes! :) Let me know if you require any other changes. |
I'd like to get @asmeurer feedback as we recently decided not to support summation in C. We may not want to extend this too far away from printing simple expressions. |
There is some typing stuff inherited from symcc at #10486 (I haven't ported the code printers yet). I haven't really decided what to do with it, though. Personally type declarations are a lower priority for me. I am afraid of not doing them correctly, so I am a little hesitant on this change. |
No problem @asmeurer . Should I close this PR? |
FYI I removed the datatype stuff from my PR. I'm unclear what the best way to deal with types is in the codegen module, and it's not a top priority for me to fix it out at the moment. @moorepants how useful would the change here be for you? I guess the alternative is to keep track of your variables and declare them separately. |
Right now I have to specify these like: 'double {}'.format(ccode(a**2 + b, assign=z)) Or make a template. I suppose I could subclass the C printer too. This is just a convenience, but in C this is such a common thing to do, it seems like it should be available somehow. |
Fixes #10446