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_CONTEXT_H 16 #define USCRIPT_CONTEXT_H 17 18 #include <map> 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include "script_instruction.h" 23 #include "script_manager.h" 24 25 namespace Uscript { 26 class ScriptInterpreter; 27 class UScriptValue; 28 class UScriptInterpretContext; 29 30 using UScriptValuePtr = std::shared_ptr<UScriptValue>; 31 using UScriptContextPtr = std::shared_ptr<UScriptInterpretContext>; 32 33 class UScriptValue { 34 public: 35 enum UScriptValueType { 36 VALUE_TYPE_INTEGER = UScriptContext::PARAM_TYPE_INTEGER, 37 VALUE_TYPE_FLOAT = UScriptContext::PARAM_TYPE_FLOAT, 38 VALUE_TYPE_STRING = UScriptContext::PARAM_TYPE_STRING, 39 VALUE_TYPE_ERROR, 40 VALUE_TYPE_LIST, 41 VALUE_TYPE_RETURN, 42 }; 43 UScriptValue(UScriptValueType type)44 explicit UScriptValue(UScriptValueType type) : type_(type) {} ~UScriptValue()45 virtual ~UScriptValue() {} 46 GetValueType()47 UScriptValueType GetValueType() const 48 { 49 return type_; 50 } 51 IsTrue()52 virtual bool IsTrue() const 53 { 54 return false; 55 } 56 57 virtual UScriptValuePtr Computer(int32_t action, UScriptValuePtr value); 58 59 virtual std::string ToString(); 60 61 static std::string ScriptToString(UScriptValuePtr value); 62 63 static UScriptValuePtr GetRightCompluteValue(UScriptValuePtr rightValue); 64 65 private: 66 UScriptValueType type_; 67 }; 68 69 class IntegerValue : public UScriptValue { 70 public: IntegerValue(int32_t value)71 explicit IntegerValue(int32_t value) : UScriptValue(UScriptValue::VALUE_TYPE_INTEGER), value_(value) {} ~IntegerValue()72 ~IntegerValue() override {} 73 IsTrue()74 bool IsTrue() const override 75 { 76 return value_ != 0; 77 } 78 GetValue()79 int32_t GetValue() const 80 { 81 return value_; 82 } 83 84 UScriptValuePtr Computer(int32_t action, UScriptValuePtr value) override; 85 std::string ToString() override; 86 87 private: 88 int32_t value_; 89 }; 90 91 class FloatValue : public UScriptValue { 92 public: FloatValue(float value)93 explicit FloatValue(float value) : UScriptValue(UScriptValue::VALUE_TYPE_FLOAT), value_(value) {} ~FloatValue()94 ~FloatValue() override {} 95 IsTrue()96 bool IsTrue() const override 97 { 98 return value_ != 0; 99 } 100 GetValue()101 float GetValue() const 102 { 103 return value_; 104 } 105 106 UScriptValuePtr Computer(int32_t action, UScriptValuePtr value) override; 107 std::string ToString() override; 108 109 private: 110 bool ComputerEqual(const UScriptValuePtr rightValue); 111 float value_; 112 }; 113 114 class StringValue : public UScriptValue { 115 public: StringValue(std::string value)116 explicit StringValue(std::string value) : UScriptValue(UScriptValue::VALUE_TYPE_STRING), 117 value_(std::move(value)) {} ~StringValue()118 ~StringValue() override {} 119 IsTrue()120 bool IsTrue() const override 121 { 122 return !value_.empty(); 123 } 124 GetValue()125 std::string GetValue() const 126 { 127 return value_; 128 } 129 130 UScriptValuePtr Computer(int32_t action, UScriptValuePtr value) override; 131 132 UScriptValuePtr ComputerReturn(int32_t action, UScriptValuePtr rightValue, UScriptValuePtr defReturn) const; 133 134 std::string ToString() override; 135 136 private: 137 int32_t ComputerLogic(UScriptValuePtr rightValue) const; 138 std::string value_; 139 }; 140 141 class ReturnValue : public UScriptValue { 142 public: ReturnValue()143 ReturnValue() : UScriptValue(UScriptValue::VALUE_TYPE_LIST) {} ~ReturnValue()144 ~ReturnValue() override 145 { 146 values_.clear(); 147 } 148 IsTrue()149 bool IsTrue() const override 150 { 151 return false; 152 } 153 154 UScriptValuePtr Computer(int32_t action, UScriptValuePtr value) override; 155 std::string ToString() override; 156 157 void AddValue(const UScriptValuePtr value); 158 void AddValues(const std::vector<UScriptValuePtr> values); 159 std::vector<UScriptValuePtr> GetValues() const; 160 161 private: 162 std::vector<UScriptValuePtr> values_ {}; 163 }; 164 165 class ErrorValue : public UScriptValue { 166 public: ErrorValue(int32_t retCode)167 explicit ErrorValue(int32_t retCode) : UScriptValue(UScriptValue::VALUE_TYPE_ERROR), retCode_(retCode) {} ~ErrorValue()168 ~ErrorValue() override {} 169 170 // 对于返回值,true表示返回ok IsTrue()171 bool IsTrue() const override 172 { 173 return retCode_ == USCRIPT_SUCCESS; 174 } 175 GetValue()176 int32_t GetValue() const 177 { 178 return retCode_; 179 } 180 SetValue(const int32_t code)181 int32_t SetValue(const int32_t code) 182 { 183 return retCode_ = code; 184 } Computer(int32_t action,UScriptValuePtr value)185 UScriptValuePtr Computer(int32_t action, UScriptValuePtr value) override 186 { 187 return nullptr; 188 } 189 190 std::string ToString() override; 191 192 private: 193 int32_t retCode_ = 0; 194 }; 195 196 /** 197 * 脚本指令上下文,用来在执行脚本指令时,传递输入、输出参数 198 */ 199 class UScriptInstructionContext : public UScriptContext { 200 public: UScriptInstructionContext()201 UScriptInstructionContext() {} 202 ~UScriptInstructionContext()203 virtual ~UScriptInstructionContext() {} 204 205 int32_t PushParam(int32_t value) override; 206 int32_t PushParam(float value) override; 207 int32_t PushParam(const std::string &value) override; 208 int32_t GetParamCount() override; 209 ParamType GetParamType(int32_t index) override; 210 int32_t GetParam(int32_t index, int32_t &value) override; 211 int32_t GetParam(int32_t index, float &value) override; 212 int32_t GetParam(int32_t index, std::string &value) override; 213 214 int32_t AddInputParam(UScriptValuePtr value); 215 GetOutVar()216 std::vector<UScriptValuePtr> GetOutVar() const 217 { 218 return outParam_; 219 } 220 221 private: 222 template<class T, class TWapper> int32_t GetOutputValue(int32_t index, T &value); 223 template<class T, class TWapper> int32_t GetParam(int32_t index, T &value); 224 225 std::vector<UScriptValuePtr> innerParam_ {}; 226 std::vector<UScriptValuePtr> outParam_ {}; 227 }; 228 229 /** 230 * 脚本解析中的上下文,保存每一层的变量 231 */ 232 class UScriptInterpretContext { 233 public: 234 explicit UScriptInterpretContext(bool top = false); 235 ~UScriptInterpretContext()236 virtual ~UScriptInterpretContext() 237 { 238 localVariables_.clear(); 239 } 240 241 UScriptValuePtr FindVariable(const ScriptInterpreter &inter, std::string id); 242 void UpdateVariable(const ScriptInterpreter &inter, std::string id, UScriptValuePtr value); 243 void UpdateVariables(const ScriptInterpreter &inter, 244 UScriptValuePtr value, 245 std::vector<std::string> ids, 246 size_t& startIndex); 247 GetContextId()248 uint32_t GetContextId() const 249 { 250 return contextId_; 251 } 252 IsTop()253 bool IsTop() const 254 { 255 return top_; 256 } 257 258 private: 259 uint32_t contextId_ = 0; 260 bool top_ = false; 261 std::map<std::string, UScriptValuePtr> localVariables_ {}; 262 }; 263 } // namespace Uscript 264 #endif // USCRIPT_CONTEXT_H 265