1 /* 2 * Copyright (c) 2021 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 #ifndef USCRIPT_STATEMENT_H 16 #define USCRIPT_STATEMENT_H 17 18 #include <vector> 19 #include "script_context.h" 20 #include "script_param.h" 21 22 namespace Uscript { 23 class UScriptStatementList; 24 class UScriptExpression; 25 26 class UScriptStatementResult { 27 public: 28 enum StatementResultType { 29 STATEMENT_RESULT_TYPE_NORMAL = 1, 30 STATEMENT_RESULT_TYPE_BREAK, 31 STATEMENT_RESULT_TYPE_CONTINUE, 32 STATEMENT_RESULT_TYPE_RTN, 33 STATEMENT_RESULT_TYPE_ERROR 34 }; 35 36 public: UScriptStatementResult()37 UScriptStatementResult() {} ~UScriptStatementResult()38 ~UScriptStatementResult() {} 39 UScriptStatementResult(StatementResultType type,UScriptValuePtr value)40 UScriptStatementResult(StatementResultType type, UScriptValuePtr value) : type_(type), value_(value) {} 41 GetResultType()42 StatementResultType GetResultType() const 43 { 44 return type_; 45 } 46 SetResultType(const StatementResultType type)47 void SetResultType(const StatementResultType type) 48 { 49 type_ = type; 50 } 51 GetResultValue()52 UScriptValuePtr GetResultValue() const 53 { 54 return value_; 55 } 56 SetResultValue(const UScriptValuePtr value)57 void SetResultValue(const UScriptValuePtr value) 58 { 59 value_ = value; 60 } 61 SetError(const int32_t error)62 void SetError(const int32_t error) 63 { 64 error_ = error; 65 } 66 GetError()67 int32_t GetError() const 68 { 69 return error_; 70 } 71 72 void UpdateStatementResult(UScriptValuePtr value); 73 static std::string ScriptToString(UScriptStatementResult *result); 74 75 private: 76 StatementResultType type_ = STATEMENT_RESULT_TYPE_NORMAL; 77 UScriptValuePtr value_ = nullptr; 78 int32_t error_ = 0; 79 }; 80 81 class UScriptStatement { 82 public: 83 enum StatementType { 84 STATEMENT_TYPE_EXPRESSION = 1, 85 STATEMENT_TYPE_IF, 86 STATEMENT_TYPE_FOR, 87 STATEMENT_TYPE_WHILE, 88 STATEMENT_TYPE_BREAK, 89 STATEMENT_TYPE_CONTINUE, 90 STATEMENT_TYPE_RTN 91 }; 92 UScriptStatement(UScriptStatement::StatementType type)93 explicit UScriptStatement(UScriptStatement::StatementType type) : type_(type) {} 94 ~UScriptStatement()95 virtual ~UScriptStatement() {} 96 97 static UScriptStatement* CreateStatement(UScriptStatement::StatementType type); 98 // ExpressionStatement 99 static UScriptStatement* CreateExpressionStatement(UScriptExpression *expression); 100 // IFStatement 101 static UScriptStatement* CreateIfStatement(UScriptExpression *condition, 102 UScriptStatementList *list1, 103 UScriptStatementList *list2 = nullptr, 104 UScriptStatement* nextIfState = nullptr); 105 // FORStatement 106 static UScriptStatement* CreateForStatement(UScriptExpression *before, 107 UScriptExpression *condition, 108 UScriptExpression *after, 109 UScriptStatementList *list); 110 // while 111 static UScriptStatement* CreateWhileStatement(UScriptExpression *condition, UScriptStatementList *list); 112 113 virtual UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) = 0; 114 GetType()115 StatementType GetType() const 116 { 117 return type_; 118 } 119 120 private: 121 enum StatementType type_; 122 }; 123 124 class UScriptStatementCtrl : public UScriptStatement { 125 public: UScriptStatementCtrl(UScriptStatement::StatementType type)126 explicit UScriptStatementCtrl(UScriptStatement::StatementType type) : UScriptStatement(type) {} ~UScriptStatementCtrl()127 ~UScriptStatementCtrl() override {} 128 UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 129 }; 130 131 class UScriptExpressionStatement : public UScriptStatement { 132 public: UScriptExpressionStatement(UScriptExpression * expression)133 explicit UScriptExpressionStatement(UScriptExpression *expression) : 134 UScriptStatement(STATEMENT_TYPE_EXPRESSION), expression_(expression) {} 135 ~UScriptExpressionStatement() override; 136 UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 137 private: 138 UScriptExpression* expression_; 139 }; 140 141 class UScriptIfStatement : public UScriptStatement { 142 public: UScriptIfStatement(UScriptExpression * expression,UScriptStatementList * statements)143 UScriptIfStatement(UScriptExpression *expression, UScriptStatementList *statements) 144 : UScriptStatement(STATEMENT_TYPE_IF), expression_(expression), trueStatements_(statements) {} 145 ~UScriptIfStatement() override; 146 UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 147 AddFalseStatementList(UScriptStatementList * statements)148 void AddFalseStatementList(UScriptStatementList *statements) 149 { 150 falseStatements_ = statements; 151 } 152 AddNextStatement(UScriptIfStatement * statement)153 void AddNextStatement(UScriptIfStatement *statement) 154 { 155 nextStatement_ = statement; 156 } 157 private: 158 UScriptExpression* expression_ = nullptr; 159 UScriptStatementList* trueStatements_ = nullptr; 160 UScriptStatementList* falseStatements_ = nullptr; 161 UScriptIfStatement* nextStatement_ = nullptr; 162 }; 163 164 class UScriptForStatement : public UScriptStatement { 165 public: UScriptForStatement(UScriptExpression * before,UScriptExpression * condition,UScriptExpression * after,UScriptStatementList * statements)166 UScriptForStatement(UScriptExpression *before, UScriptExpression *condition, UScriptExpression *after, 167 UScriptStatementList *statements) : UScriptStatement(STATEMENT_TYPE_FOR), before_(before), 168 condition_(condition), after_(after), statements_(statements) {} 169 170 ~UScriptForStatement() override; 171 UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 172 173 private: 174 UScriptExpression* before_ = nullptr; 175 UScriptExpression* condition_ = nullptr; 176 UScriptExpression* after_ = nullptr; 177 UScriptStatementList* statements_ = nullptr; 178 }; 179 180 class UScriptWhileStatement : public UScriptStatement { 181 public: UScriptWhileStatement(UScriptExpression * condition,UScriptStatementList * statements)182 UScriptWhileStatement(UScriptExpression *condition, UScriptStatementList *statements) : 183 UScriptStatement(STATEMENT_TYPE_WHILE), condition_(condition), statements_(statements) {} 184 185 ~UScriptWhileStatement() override; 186 UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 187 188 private: 189 UScriptExpression *condition_ = nullptr; 190 UScriptStatementList *statements_ = nullptr; 191 }; 192 193 class UScriptReturnStatement : public UScriptStatement { 194 public: UScriptReturnStatement()195 UScriptReturnStatement() : UScriptStatement(STATEMENT_TYPE_RTN) {} 196 197 ~UScriptReturnStatement() override; 198 UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) override; 199 AddParams(ScriptParams * params)200 void AddParams(ScriptParams* params) 201 { 202 params_ = params; 203 } 204 205 static UScriptReturnStatement* CreateStatement(ScriptParams *params); 206 207 private: 208 ScriptParams* params_ {}; 209 }; 210 211 class UScriptStatementList { 212 public: UScriptStatementList()213 UScriptStatementList() {} 214 215 ~UScriptStatementList(); 216 217 static UScriptStatementList* CreateInstance(UScriptStatement *statement); 218 void AddScriptStatement(UScriptStatement *statement); 219 220 UScriptStatementResult Execute(ScriptInterpreter &interpreter, UScriptContextPtr context); 221 222 static UScriptStatementResult DoExecute(ScriptInterpreter &inter, UScriptContextPtr context, 223 UScriptStatementList *statements); 224 225 private: 226 std::vector<UScriptStatement*> statements_ = {}; 227 }; 228 } // namespace Uscript 229 #endif // USCRIPT_STATEMENT_H 230