forked from chapel-lang/chapel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request chapel-lang#17804 from dlongnecke-cray/next-add-while
next: Parse While and DoWhile loops This is for `compiler/next`. Add nodes for `While` and `DoWhile` loops, and wire up the parser actions for them. Reviewed by @lydia-duncan and @mppf. Thanks! TESTING: - [x] All `compiler/next` tests pass Signed-off-by: David Longnecker <[email protected]>
- Loading branch information
Showing
13 changed files
with
730 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2021 Hewlett Packard Enterprise Development LP | ||
* Other additional copyright holders may be indicated within. | ||
* | ||
* The entirety of this work is licensed under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef CHPL_UAST_DOWHILE_H | ||
#define CHPL_UAST_DOWHILE_H | ||
|
||
#include "chpl/queries/Location.h" | ||
#include "chpl/uast/Loop.h" | ||
|
||
namespace chpl { | ||
namespace uast { | ||
|
||
|
||
/** | ||
This class represents a do-while loop. For example: | ||
\rst | ||
.. code-block:: chapel | ||
// Example 1: | ||
var i = 0; | ||
do { | ||
writeln(i); | ||
i += 1; | ||
} while i < 5; | ||
\endrst | ||
*/ | ||
class DoWhile final : public Loop { | ||
private: | ||
DoWhile(ASTList children, int loopBodyChildNum, int numLoopBodyStmts, | ||
int conditionChildNum, | ||
bool isBodyBlock) | ||
: Loop(asttags::DoWhile, std::move(children), | ||
loopBodyChildNum, | ||
numLoopBodyStmts, | ||
/*usesDo*/ true), | ||
conditionChildNum_(conditionChildNum), | ||
isBodyBlock_(isBodyBlock) { | ||
assert(isExpressionASTList(children_)); | ||
assert(condition()); | ||
} | ||
|
||
bool contentsMatchInner(const ASTNode* other) const override; | ||
|
||
void markUniqueStringsInner(Context* context) const override { | ||
loopMarkUniqueStringsInner(context); | ||
} | ||
|
||
int conditionChildNum_; | ||
bool isBodyBlock_; | ||
|
||
public: | ||
~DoWhile() override = default; | ||
|
||
/** | ||
Create and return a do-while loop. | ||
*/ | ||
static owned<DoWhile> build(Builder* builder, Location loc, | ||
ASTList stmts, | ||
owned<Expression> condition, | ||
bool isBodyBlock); | ||
|
||
/** | ||
Return the condition of this do-while loop. | ||
*/ | ||
const Expression* condition() const { | ||
auto ret = child(conditionChildNum_); | ||
assert(ret->isExpression()); | ||
return (const Expression*)ret; | ||
} | ||
|
||
/** | ||
Returns true if this do-while loop body is a block. | ||
*/ | ||
bool isBodyBlock() const { | ||
return isBodyBlock_; | ||
} | ||
|
||
}; | ||
|
||
|
||
} // end namespace uast | ||
} // end namespace chpl | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2021 Hewlett Packard Enterprise Development LP | ||
* Other additional copyright holders may be indicated within. | ||
* | ||
* The entirety of this work is licensed under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef CHPL_UAST_WHILE_H | ||
#define CHPL_UAST_WHILE_H | ||
|
||
#include "chpl/queries/Location.h" | ||
#include "chpl/uast/Loop.h" | ||
|
||
namespace chpl { | ||
namespace uast { | ||
|
||
|
||
/** | ||
This class represents a while loop. For example: | ||
\rst | ||
.. code-block:: chapel | ||
// Example 1: | ||
var i = 0; | ||
while i < 5 { | ||
writeln(i); | ||
i += 1; | ||
} | ||
\endrst | ||
*/ | ||
class While final : public Loop { | ||
private: | ||
While(ASTList children, int8_t conditionChildNum, | ||
int loopBodyChildNum, | ||
int numLoopBodyStmts, | ||
bool usesDo) | ||
: Loop(asttags::While, std::move(children), loopBodyChildNum, | ||
numLoopBodyStmts, | ||
usesDo), | ||
conditionChildNum_(conditionChildNum) { | ||
assert(isExpressionASTList(children_)); | ||
assert(condition()); | ||
} | ||
|
||
bool contentsMatchInner(const ASTNode* other) const override; | ||
|
||
void markUniqueStringsInner(Context* context) const override { | ||
loopMarkUniqueStringsInner(context); | ||
} | ||
|
||
int8_t conditionChildNum_; | ||
|
||
public: | ||
~While() override = default; | ||
|
||
/** | ||
Create and return a while loop. | ||
*/ | ||
static owned<While> build(Builder* builder, Location loc, | ||
owned<Expression> condition, | ||
ASTList stmts, | ||
bool usesDo); | ||
|
||
/** | ||
Return the condition of this while loop. | ||
*/ | ||
const Expression* condition() const { | ||
auto ret = child(conditionChildNum_); | ||
assert(ret->isExpression()); | ||
return (const Expression*)ret; | ||
} | ||
|
||
}; | ||
|
||
|
||
} // end namespace uast | ||
} // end namespace chpl | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.