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