-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Add support for do/while loops #768
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
Conversation
Thanks for the pull request and your interest in the project! We have to think about whether introducing do-while-loops is something we want to be part of the language or if they are an unnecessary complication. If you want to work on features that have already been agreed by the community, please talk to a team member in https://gitter.im/ethereum/solidity |
Will change this to re-use the existing while loop ast node. |
Were you going to do that yourself in the next couple of days? I have a road trip coming up tomorrow, and I might have the free evening or two to do that. |
oh wow, even the better :-) |
Yeah, as soon as I saw your comment, I figured that's what you had in mind. I haven't looked at solc since I wrote this (major life changes), but I assume you already have some sort of context variable that gets passed around that I could store the flag in. Most compilers do. |
At the point where the ast node is created in the parser, this flag should be stored inside the ast node as a regular class member. |
Got it. Makes perfect sense. On Fri, Oct 28, 2016 at 9:22 AM chriseth [email protected] wrote:
|
Okay; this appears to be functionally comparable code without the extra class. Also, I saw that Why3 does not appear to support do-while, so I simulate a do-while loop for its output. I'm going to have to look at a rebase next, since the commit from main that this branch is based on has a broken "ast" and "ast-json" output. |
libsolidity/ast/AST.h
Outdated
ASTPointer<Expression> const& _condition, | ||
ASTPointer<Statement> const& _body | ||
ASTPointer<Statement> const& _body, | ||
bool _isDoWhile |
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.
Please use tabs for indentation.
libsolidity/ast/ASTJsonConverter.cpp
Outdated
bool ASTJsonConverter::visit(WhileStatement const& _node) | ||
{ | ||
addJsonNode(_node, "WhileStatement", {}, true); | ||
addJsonNode(_node, |
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.
Code style should be
addJsonNode(
_node,
_node.isDoWhile() ?...,
true
);
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.
Sorry for the style break. Did I miss the style guide somewhere? Or do you follow a well-known one by convention?
libsolidity/parsing/Parser.cpp
Outdated
ASTPointer<Expression> condition = parseExpression(); | ||
expectToken(Token::RParen); | ||
expectToken(Token::Semicolon); | ||
nodeFactory.setEndPositionFromNode(body); |
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.
This should be replaced by a call to nodeFactory.setEndPosition()
right before expecting the semicolon.
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.
Fixed; that certainly explained some funny output I was seeing in the --asm dumps I normally use to confirm the results.
libsolidity/ast/AST.h
Outdated
|
||
Expression const& condition() const { return *m_condition; } | ||
Statement const& body() const { return *m_body; } | ||
bool isDoWhile() const { return m_isDoWhile; } |
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.
Here are still some spaces, probably in more places.
Thanks! The last thing that is missing now is an entry into the changelog file and some mention of this feature in the documentation. I can also do that after merging this PR if you want. |
Yeah, I'll do the pickup merging on grammar.txt. I can get the changelog On Tue, Nov 8, 2016 at 9:07 AM chriseth [email protected] wrote:
|
Oh of course, totally forgot about tests. I think it is enough to add an end to end test, so take a look at the file SolidityEndToEndTest.cpp. You can probably copy an example there. Running the tests is a bit tricky, because you need an |
Since we're mostly done with the CR process, I squashed the commits, updated the Changelog, and added some mention of "do" to the docs, though documentation of the control structures is pretty light. |
Okay. I should be able to add a test. Tonight's a pretty tight schedule, but Wednesday or Thursday should be a little bit better. |
That's great, thanks! |
Okay. I added a basic unit test modeled on the while loop test. I see it runs the test repeatedly with different values of n and checks it against what you expect from C++, and that seems suitable enough. I could get fancier, I suppose, with nesting and all that jazz, if there's a need. |
{ | ||
u256 nfac = 1; | ||
u256 i = 2; | ||
do |
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.
Please don't use spaces for indentation.
do | ||
{ | ||
nfac *= i++; | ||
} while (i <= n); |
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.
Please put the while
on the next line.
This commit adds support for a standard do <statement> while <expr>; form of statement. While loops were already being supported; supporting a do/while loop mostly involves reusing code from while loops but putting the conditional checking last.
Sincerely sorry about the style violations; I thought I checked every line for spaces last night, but it was late. Fixed. |
Wonderful, thanks a lot! |
Thanks for the patience in the review process. solc is pretty easy to work on, so I'm hoping this will not be the last time you'll be seeing a PR from me. |
That's great to hear! The issues marked as "soon" and/or "up-for-grabs" are probably the best to work on. If in doubt, ask on http://gitter.im/ethereum/solidity or contact me at [email protected] |
Fixes #766 |
This commit adds support for a standard do while ;
form of statement. While loops were already being supported; supporting
a do/while loop mostly involves reusing code from while loops but putting
the conditional checking last.
This commit resolves issue #766