1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ir/astNode.h" 17 #include "ir/statements/variableDeclarator.h" 18 #include "ir/statements/variableDeclaration.h" 19 #include "ir/expressions/identifier.h" 20 #include "ir/statements/forUpdateStatement.h" 21 #include "ir/statements/blockStatement.h" 22 #include "ir/statements/whileStatement.h" 23 #include "ir/expressions/literals/booleanLiteral.h" 24 #include "ir/expressions/literals/numberLiteral.h" 25 #include "ir/expressions/binaryExpression.h" 26 #include "ir/expressions/updateExpression.h" 27 28 namespace panda::es2panda::gtests { 29 30 class NodeGenerator { 31 public: NodeGenerator(ArenaAllocator * alloc)32 explicit NodeGenerator(ArenaAllocator *alloc) : alloc_(alloc) {} 33 // x = 1 34 ir::VariableDeclaration *CreateVarDecl(bool declare, util::StringView name = "x") 35 { 36 auto varDecl = alloc_->New<ir::VariableDeclarator>(ir::VariableDeclaratorFlag::LET, CreateId(name)); 37 ArenaVector<ir::VariableDeclarator *> tmp {alloc_->Adapter()}; 38 tmp.emplace_back(varDecl); 39 return alloc_->New<ir::VariableDeclaration>(ir::VariableDeclaration::VariableDeclarationKind::LET, alloc_, 40 std::move(tmp), declare); 41 } 42 CreateId(util::StringView x)43 ir::Identifier *CreateId(util::StringView x) 44 { 45 return alloc_->New<ir::Identifier>(x, alloc_); 46 } 47 48 // x = x + 1 49 ir::UpdateExpression *CreateIncrement(util::StringView name = "x", bool isPrefix = false) 50 { 51 return alloc_->New<ir::UpdateExpression>(CreateId(name), lexer::TokenType::PUNCTUATOR_PLUS_PLUS, isPrefix); 52 } 53 54 // x < 10 55 ir::BinaryExpression *CreateLessCmpExpr(util::StringView name = "x") 56 { 57 const int anyLoopLimit = 10; 58 return alloc_->New<ir::BinaryExpression>(CreateId(name), 59 alloc_->New<ir::NumberLiteral>(lexer::Number(anyLoopLimit)), 60 lexer::TokenType::PUNCTUATOR_LESS_THAN); 61 } 62 63 ir::BlockStatement *CreateBlockWithDeclare(util::StringView name = "x") 64 { 65 auto varDecl = CreateVarDecl(true, name); 66 ArenaVector<ir::Statement *> tmp {alloc_->Adapter()}; 67 tmp.emplace_back(varDecl); 68 return alloc_->New<ir::BlockStatement>(alloc_, std::move(tmp)); 69 } 70 CreateForUpdate()71 ir::ForUpdateStatement *CreateForUpdate() 72 { 73 return alloc_->New<ir::ForUpdateStatement>(CreateVarDecl(true), CreateLessCmpExpr(), CreateIncrement(), 74 CreateBlockWithDeclare()); 75 } 76 CreateWhile()77 ir::WhileStatement *CreateWhile() 78 { 79 return alloc_->New<ir::WhileStatement>(CreateLessCmpExpr(), CreateBlockWithDeclare()); 80 } 81 82 private: 83 ArenaAllocator *const alloc_; 84 }; 85 } // namespace panda::es2panda::gtests