• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "script_expression.h"
16 #include "script_function.h"
17 #include "script_interpreter.h"
18 #include "script_utils.h"
19 
20 using namespace std;
21 
22 namespace Uscript {
UScriptExpression(ExpressionType expressType)23 UScriptExpression::UScriptExpression(ExpressionType expressType) : expressType_(expressType) {}
~UScriptExpression()24 UScriptExpression::~UScriptExpression() {}
25 
CreateExpression(const std::string identifier,UScriptExpression * expression)26 UScriptExpression* AssignExpression::CreateExpression(const std::string identifier, UScriptExpression *expression)
27 {
28     return new AssignExpression(identifier, expression);
29 }
AddIdentifier(const std::string & identifier)30 void AssignExpression::AddIdentifier(const std::string &identifier)
31 {
32     multipleIdentifiers_.push_back(identifier);
33 }
34 
AddIdentifier(UScriptExpression * expression,const std::string identifier)35 UScriptExpression* AssignExpression::AddIdentifier(UScriptExpression *expression, const std::string identifier)
36 {
37     auto assign = reinterpret_cast<AssignExpression*>(expression);
38     if (assign != nullptr) {
39         assign->AddIdentifier(identifier);
40     }
41     return assign;
42 }
43 
44 // binary operator
CreateExpression(ExpressionAction action,UScriptExpression * left,UScriptExpression * right)45 UScriptExpression* BinaryExpression::CreateExpression(ExpressionAction action,
46     UScriptExpression *left,
47     UScriptExpression *right)
48 {
49     return new BinaryExpression(action, left, right);
50 }
CreateExpression(const std::string identifier,ScriptParams * params)51 UScriptExpression* FunctionCallExpression::CreateExpression(const std::string identifier, ScriptParams *params)
52 {
53     return new FunctionCallExpression(identifier, params);
54 }
Execute(ScriptInterpreter & inter,UScriptContextPtr local)55 UScriptValuePtr UScriptExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
56 {
57     return std::make_shared<UScriptValue>(UScriptValue::VALUE_TYPE_ERROR);
58 }
Execute(ScriptInterpreter & inter,UScriptContextPtr local)59 UScriptValuePtr IntegerExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
60 {
61     return std::make_shared<IntegerValue>(this->value_);
62 }
Execute(ScriptInterpreter & inter,UScriptContextPtr local)63 UScriptValuePtr FloatExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
64 {
65     return std::make_shared<FloatValue>(this->value_);
66 }
Execute(ScriptInterpreter & inter,UScriptContextPtr local)67 UScriptValuePtr StringExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
68 {
69     return std::make_shared<StringValue>(this->value_);
70 }
Execute(ScriptInterpreter & inter,UScriptContextPtr local)71 UScriptValuePtr IdentifierExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
72 {
73     INTERPRETER_LOGI(inter, local, "Execute statements identifier %s ", identifier_.c_str());
74     UScriptValuePtr variable = inter.FindVariable(local, identifier_);
75     INTERPRETER_LOGI(inter, local, "IdentifierExpression::Execute '%s = %s ' ", identifier_.c_str(),
76         UScriptValue::ScriptToString(variable).c_str());
77     if (variable != nullptr) {
78         return variable;
79     }
80     return std::make_shared<UScriptValue>(UScriptValue::VALUE_TYPE_ERROR);
81 }
82 
GetIdentifierName(UScriptExpression * expression,std::string & name)83 int32_t IdentifierExpression::GetIdentifierName(UScriptExpression *expression, std::string &name)
84 {
85     if (expression->GetExpressType() != EXPRESSION_TYPE_IDENTIFIER) {
86         return USCRIPT_INVALID_PARAM;
87     } else {
88         auto identifier = reinterpret_cast<IdentifierExpression*>(expression);
89         if (identifier != nullptr) {
90             name = identifier->GetIdentifier();
91             return USCRIPT_SUCCESS;
92         }
93     }
94     return USCRIPT_INVALID_PARAM;
95 }
Execute(ScriptInterpreter & inter,UScriptContextPtr local)96 UScriptValuePtr AssignExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
97 {
98     UScriptValuePtr result = expression_->Execute(inter, local);
99     INTERPRETER_LOGI(inter, local, "AssignExpression::Execute update local var '%s = %s ' ", identifier_.c_str(),
100         UScriptValue::ScriptToString(result).c_str());
101     if (result->GetValueType() == UScriptValue::VALUE_TYPE_ERROR) {
102         return result;
103     }
104     UScriptValuePtr var = inter.FindVariable(local, identifier_);
105     if (var != nullptr) {
106         inter.UpdateVariable(local, identifier_, result);
107         return result;
108     }
109 
110     std::vector<std::string> identifiers;
111     identifiers.push_back(identifier_);
112     identifiers.insert(identifiers.begin() + 1, multipleIdentifiers_.begin(), multipleIdentifiers_.end());
113     size_t index = 0;
114     local->UpdateVariables(inter, result, identifiers, index);
115     return result;
116 }
117 
Execute(ScriptInterpreter & inter,UScriptContextPtr local)118 UScriptValuePtr BinaryExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
119 {
120     static std::vector<std::string> opStr = {
121         "", "add", "sub", "mul", "div", ">", ">=", "<", "<=", "==", "!=", "&&", "||"
122     };
123     UScriptValuePtr left;
124     UScriptValuePtr right;
125 
126     INTERPRETER_LOGI(inter, local, "BinaryExpression::Execute ");
127     if (left_ != nullptr) {
128         left = left_->Execute(inter, local);
129     }
130 
131     if (action_ == OR_OPERATOR && left != nullptr && left->IsTrue()) {
132         INTERPRETER_LOGE(inter, local, "BinaryExpression::Execute left:%s %s",
133             UScriptValue::ScriptToString(left).c_str(), opStr[action_].c_str());
134         return std::make_shared<IntegerValue>(1);
135     }
136     if (right_ != nullptr) {
137         right = right_->Execute(inter, local);
138     }
139     if (left != nullptr && right != nullptr) {
140         UScriptValuePtr value = left->Computer(action_, right);
141         INTERPRETER_LOGI(inter, local, "BinaryExpression::Execute left:%s %s right:%s result:%s",
142             UScriptValue::ScriptToString(left).c_str(), opStr[action_].c_str(),
143             UScriptValue::ScriptToString(right).c_str(), UScriptValue::ScriptToString(value).c_str());
144         return value;
145     }
146     return std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
147 }
148 
Execute(ScriptInterpreter & inter,UScriptContextPtr local)149 UScriptValuePtr FunctionCallExpression::Execute(ScriptInterpreter &inter, UScriptContextPtr local)
150 {
151     INTERPRETER_LOGI(inter, local, "FunctionCallExpression::Execute %s ", functionName_.c_str());
152 
153     if (inter.IsNativeFunction(functionName_)) {
154         return inter.ExecuteNativeFunc(local, functionName_, params_);
155     }
156     return inter.ExecuteFunction(local, functionName_, params_);
157 }
158 
~BinaryExpression()159 BinaryExpression::~BinaryExpression()
160 {
161     delete left_;
162     delete right_;
163 }
~AssignExpression()164 AssignExpression::~AssignExpression()
165 {
166     delete this->expression_;
167 }
~FunctionCallExpression()168 FunctionCallExpression::~FunctionCallExpression()
169 {
170     delete params_;
171 }
172 } // namespace Uscript
173