1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#ifndef USCRIPT_STATEMENT_H 16fb299fa2Sopenharmony_ci#define USCRIPT_STATEMENT_H 17fb299fa2Sopenharmony_ci 18fb299fa2Sopenharmony_ci#include <vector> 19fb299fa2Sopenharmony_ci#include "script_context.h" 20fb299fa2Sopenharmony_ci#include "script_param.h" 21fb299fa2Sopenharmony_ci 22fb299fa2Sopenharmony_cinamespace Uscript { 23fb299fa2Sopenharmony_ciclass UScriptStatementList; 24fb299fa2Sopenharmony_ciclass UScriptExpression; 25fb299fa2Sopenharmony_ci 26fb299fa2Sopenharmony_ciclass UScriptStatementResult { 27fb299fa2Sopenharmony_cipublic: 28fb299fa2Sopenharmony_ci enum StatementResultType { 29fb299fa2Sopenharmony_ci STATEMENT_RESULT_TYPE_NORMAL = 1, 30fb299fa2Sopenharmony_ci STATEMENT_RESULT_TYPE_BREAK, 31fb299fa2Sopenharmony_ci STATEMENT_RESULT_TYPE_CONTINUE, 32fb299fa2Sopenharmony_ci STATEMENT_RESULT_TYPE_RTN, 33fb299fa2Sopenharmony_ci STATEMENT_RESULT_TYPE_ERROR 34fb299fa2Sopenharmony_ci }; 35fb299fa2Sopenharmony_ci 36fb299fa2Sopenharmony_cipublic: 37fb299fa2Sopenharmony_ci UScriptStatementResult() {} 38fb299fa2Sopenharmony_ci ~UScriptStatementResult() {} 39fb299fa2Sopenharmony_ci 40fb299fa2Sopenharmony_ci UScriptStatementResult(StatementResultType type, UScriptValuePtr value) : type_(type), value_(value) {} 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_ci StatementResultType GetResultType() const 43fb299fa2Sopenharmony_ci { 44fb299fa2Sopenharmony_ci return type_; 45fb299fa2Sopenharmony_ci } 46fb299fa2Sopenharmony_ci 47fb299fa2Sopenharmony_ci void SetResultType(const StatementResultType type) 48fb299fa2Sopenharmony_ci { 49fb299fa2Sopenharmony_ci type_ = type; 50fb299fa2Sopenharmony_ci } 51fb299fa2Sopenharmony_ci 52fb299fa2Sopenharmony_ci UScriptValuePtr GetResultValue() const 53fb299fa2Sopenharmony_ci { 54fb299fa2Sopenharmony_ci return value_; 55fb299fa2Sopenharmony_ci } 56fb299fa2Sopenharmony_ci 57fb299fa2Sopenharmony_ci void SetResultValue(const UScriptValuePtr value) 58fb299fa2Sopenharmony_ci { 59fb299fa2Sopenharmony_ci value_ = value; 60fb299fa2Sopenharmony_ci } 61fb299fa2Sopenharmony_ci 62fb299fa2Sopenharmony_ci void SetError(const int32_t error) 63fb299fa2Sopenharmony_ci { 64fb299fa2Sopenharmony_ci error_ = error; 65fb299fa2Sopenharmony_ci } 66fb299fa2Sopenharmony_ci 67fb299fa2Sopenharmony_ci int32_t GetError() const 68fb299fa2Sopenharmony_ci { 69fb299fa2Sopenharmony_ci return error_; 70fb299fa2Sopenharmony_ci } 71fb299fa2Sopenharmony_ci 72fb299fa2Sopenharmony_ci void UpdateStatementResult(UScriptValuePtr value); 73fb299fa2Sopenharmony_ci static std::string ScriptToString(UScriptStatementResult *result); 74fb299fa2Sopenharmony_ci 75fb299fa2Sopenharmony_ciprivate: 76fb299fa2Sopenharmony_ci StatementResultType type_ = STATEMENT_RESULT_TYPE_NORMAL; 77fb299fa2Sopenharmony_ci UScriptValuePtr value_ = nullptr; 78fb299fa2Sopenharmony_ci int32_t error_ = 0; 79fb299fa2Sopenharmony_ci}; 80fb299fa2Sopenharmony_ci 81fb299fa2Sopenharmony_ciclass UScriptStatement { 82fb299fa2Sopenharmony_cipublic: 83fb299fa2Sopenharmony_ci enum StatementType { 84fb299fa2Sopenharmony_ci STATEMENT_TYPE_EXPRESSION = 1, 85fb299fa2Sopenharmony_ci STATEMENT_TYPE_IF, 86fb299fa2Sopenharmony_ci STATEMENT_TYPE_FOR, 87fb299fa2Sopenharmony_ci STATEMENT_TYPE_WHILE, 88fb299fa2Sopenharmony_ci STATEMENT_TYPE_BREAK, 89fb299fa2Sopenharmony_ci STATEMENT_TYPE_CONTINUE, 90fb299fa2Sopenharmony_ci STATEMENT_TYPE_RTN 91fb299fa2Sopenharmony_ci }; 92fb299fa2Sopenharmony_ci 93fb299fa2Sopenharmony_ci explicit UScriptStatement(UScriptStatement::StatementType type) : type_(type) {} 94fb299fa2Sopenharmony_ci 95fb299fa2Sopenharmony_ci virtual ~UScriptStatement() {} 96fb299fa2Sopenharmony_ci 97fb299fa2Sopenharmony_ci static UScriptStatement* CreateStatement(UScriptStatement::StatementType type); 98fb299fa2Sopenharmony_ci // ExpressionStatement 99fb299fa2Sopenharmony_ci static UScriptStatement* CreateExpressionStatement(UScriptExpression *expression); 100fb299fa2Sopenharmony_ci // IFStatement 101fb299fa2Sopenharmony_ci static UScriptStatement* CreateIfStatement(UScriptExpression *condition, 102fb299fa2Sopenharmony_ci UScriptStatementList *list1, 103fb299fa2Sopenharmony_ci UScriptStatementList *list2 = nullptr, 104fb299fa2Sopenharmony_ci UScriptStatement* nextIfState = nullptr); 105fb299fa2Sopenharmony_ci // FORStatement 106fb299fa2Sopenharmony_ci static UScriptStatement* CreateForStatement(UScriptExpression *before, 107fb299fa2Sopenharmony_ci UScriptExpression *condition, 108fb299fa2Sopenharmony_ci UScriptExpression *after, 109fb299fa2Sopenharmony_ci UScriptStatementList *list); 110fb299fa2Sopenharmony_ci // while 111fb299fa2Sopenharmony_ci static UScriptStatement* CreateWhileStatement(UScriptExpression *condition, UScriptStatementList *list); 112fb299fa2Sopenharmony_ci 113fb299fa2Sopenharmony_ci virtual UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) = 0; 114fb299fa2Sopenharmony_ci 115fb299fa2Sopenharmony_ci StatementType GetType() const 116fb299fa2Sopenharmony_ci { 117fb299fa2Sopenharmony_ci return type_; 118fb299fa2Sopenharmony_ci } 119fb299fa2Sopenharmony_ci 120fb299fa2Sopenharmony_ciprivate: 121fb299fa2Sopenharmony_ci enum StatementType type_; 122fb299fa2Sopenharmony_ci}; 123fb299fa2Sopenharmony_ci 124fb299fa2Sopenharmony_ciclass UScriptStatementCtrl : public UScriptStatement { 125fb299fa2Sopenharmony_cipublic: 126fb299fa2Sopenharmony_ci explicit UScriptStatementCtrl(UScriptStatement::StatementType type) : UScriptStatement(type) {} 127fb299fa2Sopenharmony_ci ~UScriptStatementCtrl() override {} 128fb299fa2Sopenharmony_ci UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 129fb299fa2Sopenharmony_ci}; 130fb299fa2Sopenharmony_ci 131fb299fa2Sopenharmony_ciclass UScriptExpressionStatement : public UScriptStatement { 132fb299fa2Sopenharmony_cipublic: 133fb299fa2Sopenharmony_ci explicit UScriptExpressionStatement(UScriptExpression *expression) 134fb299fa2Sopenharmony_ci : UScriptStatement(STATEMENT_TYPE_EXPRESSION), expression_(expression) {} 135fb299fa2Sopenharmony_ci ~UScriptExpressionStatement() override; 136fb299fa2Sopenharmony_ci UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 137fb299fa2Sopenharmony_ciprivate: 138fb299fa2Sopenharmony_ci UScriptExpression* expression_; 139fb299fa2Sopenharmony_ci}; 140fb299fa2Sopenharmony_ci 141fb299fa2Sopenharmony_ciclass UScriptIfStatement : public UScriptStatement { 142fb299fa2Sopenharmony_cipublic: 143fb299fa2Sopenharmony_ci UScriptIfStatement(UScriptExpression *expression, UScriptStatementList *statements) 144fb299fa2Sopenharmony_ci : UScriptStatement(STATEMENT_TYPE_IF), expression_(expression), trueStatements_(statements) {} 145fb299fa2Sopenharmony_ci ~UScriptIfStatement() override; 146fb299fa2Sopenharmony_ci UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 147fb299fa2Sopenharmony_ci 148fb299fa2Sopenharmony_ci void AddFalseStatementList(UScriptStatementList *statements) 149fb299fa2Sopenharmony_ci { 150fb299fa2Sopenharmony_ci falseStatements_ = statements; 151fb299fa2Sopenharmony_ci } 152fb299fa2Sopenharmony_ci 153fb299fa2Sopenharmony_ci void AddNextStatement(UScriptIfStatement *statement) 154fb299fa2Sopenharmony_ci { 155fb299fa2Sopenharmony_ci nextStatement_ = statement; 156fb299fa2Sopenharmony_ci } 157fb299fa2Sopenharmony_ciprivate: 158fb299fa2Sopenharmony_ci UScriptExpression* expression_ = nullptr; 159fb299fa2Sopenharmony_ci UScriptStatementList* trueStatements_ = nullptr; 160fb299fa2Sopenharmony_ci UScriptStatementList* falseStatements_ = nullptr; 161fb299fa2Sopenharmony_ci UScriptIfStatement* nextStatement_ = nullptr; 162fb299fa2Sopenharmony_ci}; 163fb299fa2Sopenharmony_ci 164fb299fa2Sopenharmony_ciclass UScriptForStatement : public UScriptStatement { 165fb299fa2Sopenharmony_cipublic: 166fb299fa2Sopenharmony_ci UScriptForStatement(UScriptExpression *before, UScriptExpression *condition, UScriptExpression *after, 167fb299fa2Sopenharmony_ci UScriptStatementList *statements) : UScriptStatement(STATEMENT_TYPE_FOR), before_(before), 168fb299fa2Sopenharmony_ci condition_(condition), after_(after), statements_(statements) {} 169fb299fa2Sopenharmony_ci 170fb299fa2Sopenharmony_ci ~UScriptForStatement() override; 171fb299fa2Sopenharmony_ci UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 172fb299fa2Sopenharmony_ci 173fb299fa2Sopenharmony_ciprivate: 174fb299fa2Sopenharmony_ci UScriptExpression* before_ = nullptr; 175fb299fa2Sopenharmony_ci UScriptExpression* condition_ = nullptr; 176fb299fa2Sopenharmony_ci UScriptExpression* after_ = nullptr; 177fb299fa2Sopenharmony_ci UScriptStatementList* statements_ = nullptr; 178fb299fa2Sopenharmony_ci}; 179fb299fa2Sopenharmony_ci 180fb299fa2Sopenharmony_ciclass UScriptWhileStatement : public UScriptStatement { 181fb299fa2Sopenharmony_cipublic: 182fb299fa2Sopenharmony_ci UScriptWhileStatement(UScriptExpression *condition, UScriptStatementList *statements) 183fb299fa2Sopenharmony_ci : UScriptStatement(STATEMENT_TYPE_WHILE), condition_(condition), statements_(statements) {} 184fb299fa2Sopenharmony_ci 185fb299fa2Sopenharmony_ci ~UScriptWhileStatement() override; 186fb299fa2Sopenharmony_ci UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 187fb299fa2Sopenharmony_ci 188fb299fa2Sopenharmony_ciprivate: 189fb299fa2Sopenharmony_ci UScriptExpression *condition_ = nullptr; 190fb299fa2Sopenharmony_ci UScriptStatementList *statements_ = nullptr; 191fb299fa2Sopenharmony_ci}; 192fb299fa2Sopenharmony_ci 193fb299fa2Sopenharmony_ciclass UScriptReturnStatement : public UScriptStatement { 194fb299fa2Sopenharmony_cipublic: 195fb299fa2Sopenharmony_ci UScriptReturnStatement() : UScriptStatement(STATEMENT_TYPE_RTN) {} 196fb299fa2Sopenharmony_ci 197fb299fa2Sopenharmony_ci ~UScriptReturnStatement() override; 198fb299fa2Sopenharmony_ci UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 199fb299fa2Sopenharmony_ci 200fb299fa2Sopenharmony_ci void AddParams(ScriptParams* params) 201fb299fa2Sopenharmony_ci { 202fb299fa2Sopenharmony_ci params_ = params; 203fb299fa2Sopenharmony_ci } 204fb299fa2Sopenharmony_ci 205fb299fa2Sopenharmony_ci static UScriptReturnStatement* CreateStatement(ScriptParams *params); 206fb299fa2Sopenharmony_ci 207fb299fa2Sopenharmony_ciprivate: 208fb299fa2Sopenharmony_ci ScriptParams* params_ {}; 209fb299fa2Sopenharmony_ci}; 210fb299fa2Sopenharmony_ci 211fb299fa2Sopenharmony_ciclass UScriptStatementList { 212fb299fa2Sopenharmony_cipublic: 213fb299fa2Sopenharmony_ci UScriptStatementList() {} 214fb299fa2Sopenharmony_ci 215fb299fa2Sopenharmony_ci ~UScriptStatementList(); 216fb299fa2Sopenharmony_ci 217fb299fa2Sopenharmony_ci static UScriptStatementList* CreateInstance(UScriptStatement *statement); 218fb299fa2Sopenharmony_ci void AddScriptStatement(UScriptStatement *statement); 219fb299fa2Sopenharmony_ci 220fb299fa2Sopenharmony_ci UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context); 221fb299fa2Sopenharmony_ci 222fb299fa2Sopenharmony_ci static UScriptStatementResult DoExecute(ScriptInterpreter &inter, UScriptContextPtr context, 223fb299fa2Sopenharmony_ci UScriptStatementList *statements); 224fb299fa2Sopenharmony_ci 225fb299fa2Sopenharmony_ciprivate: 226fb299fa2Sopenharmony_ci std::vector<UScriptStatement*> statements_ = {}; 227fb299fa2Sopenharmony_ci}; 228fb299fa2Sopenharmony_ci} // namespace Uscript 229fb299fa2Sopenharmony_ci#endif // USCRIPT_STATEMENT_H 230