From e6a366f9d2d07cc99faac14aed610fbb02c08e63 Mon Sep 17 00:00:00 2001 From: Zadock Maloba Date: Fri, 1 Dec 2023 13:55:29 +0300 Subject: [PATCH] Make ASTNode::m_value and ASTNode::m_returnVal raw pointers. --- src/naisysmethodhandler.cpp | 6 +-- src/serverlangcore.cpp | 6 +-- src/serverlangcore.h | 3 +- src/serverlangfunctions.h | 53 ++++++++-------------- src/serverlanglexer.h | 15 +++---- src/serverlangnode.cpp | 10 ++--- src/serverlangnode.h | 22 +++++---- src/serverlangruntime.cpp | 4 +- src/serverlangruntime.h | 7 +-- src/serverlangtypes.h | 89 ++++++++++++++++++------------------- 10 files changed, 99 insertions(+), 116 deletions(-) diff --git a/src/naisysmethodhandler.cpp b/src/naisysmethodhandler.cpp index 24a5dc0..a484bd1 100644 --- a/src/naisysmethodhandler.cpp +++ b/src/naisysmethodhandler.cpp @@ -37,15 +37,15 @@ const NaiSysHttpResponse MethodHandler::get() ServerLang::String http_body(m_desirialized._body); http_body.setName("RUNTIME_HTTP_BODY"); - http_body.setValue(std::make_shared(m_desirialized._body)); + http_body.setValue(new QVariant(m_desirialized._body)); ServerLang::Struct http_params; http_params.setName("RUNTIME_HTTP_PARAMS"); - http_params.setValue(std::make_shared(m_parameters.url_dict)); + http_params.setValue(new QVariant(m_parameters.url_dict)); ServerLang::Struct http_headers; http_headers.setName("RUNTIME_HTTP_HEADERS"); - http_headers.setValue(std::make_shared(m_desirialized._header)); + http_headers.setValue(new QVariant(m_desirialized._header)); rt.injectRTDeclarations({ std::make_shared(http_body), diff --git a/src/serverlangcore.cpp b/src/serverlangcore.cpp index 749b812..7d1caa2 100644 --- a/src/serverlangcore.cpp +++ b/src/serverlangcore.cpp @@ -7,7 +7,7 @@ namespace NaiSys { namespace ServerLang { -const std::shared_ptr Core::exec(QStringView symbol, const QVariantMap &args) throw() +const value_raw_ptr Core::exec(QStringView symbol, const QVariantMap &args) throw() { if (auto _mp = CoreFunctions::functionMap(); _mp.find(symbol.toString()) != _mp.end()) { auto func = _mp[symbol.toString()]; @@ -19,9 +19,9 @@ const std::shared_ptr Core::exec(QStringView symbol, const QVariantMap const void Core::define(const STNode::nodeptr &func) throw() { - ast_operator opr = [func]()mutable->value_ptr { + ast_operator opr = [func]()mutable->value_raw_ptr { auto const ret = QVariant::fromValue(func); - return std::make_shared(ret); + return new QVariant(ret); }; CoreFunctions::functionMap().insert({func->name().toString(), opr}); } diff --git a/src/serverlangcore.h b/src/serverlangcore.h index 929d135..78799fc 100644 --- a/src/serverlangcore.h +++ b/src/serverlangcore.h @@ -45,8 +45,7 @@ class Core Core() = default; public: - static const std::shared_ptr exec(QStringView symbol, - const QVariantMap &args = {}) throw(); + static const value_raw_ptr exec(QStringView symbol, const QVariantMap &args = {}) throw(); static const void define(const STNode::nodeptr &func = {}) throw(); }; diff --git a/src/serverlangfunctions.h b/src/serverlangfunctions.h index 10c1e30..1ac70c0 100644 --- a/src/serverlangfunctions.h +++ b/src/serverlangfunctions.h @@ -49,8 +49,7 @@ class CoreFunctions inline QVariantMap CoreFunctions::args_reg = {}; inline QVariantMap CoreFunctions::params_reg = {}; -inline const ast_operator CoreFunctions::exec_cmd = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::exec_cmd = []() mutable -> value_raw_ptr { Function func; func.setArguments({"exec", "args"}); //NOTE: Always set params after args @@ -63,15 +62,11 @@ inline const ast_operator CoreFunctions::exec_cmd = []()mutable->value_ptr tmpList << v.toString(); }); - return std::make_shared( - QProcess::execute(func.parameters() - .value("fmt", QString{""}).toString(), - tmpList) - ); + return new QVariant( + QProcess::execute(func.parameters().value("fmt", QString{""}).toString(), tmpList)); }; -inline const ast_operator CoreFunctions::println = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::println = []() mutable -> value_raw_ptr { Function func; func.setArguments({"fmt", "args"}); //NOTE: Always set params after args @@ -90,14 +85,10 @@ inline const ast_operator CoreFunctions::println = []()mutable->value_ptr fmt.replace("%{"+QString::number(i)+"}", tmpList.at(i)); } - return std::make_shared( - (int)StreamIO::println(QSTRING_TO_CSTR(fmt)) - ); - + return new QVariant((int) StreamIO::println(QSTRING_TO_CSTR(fmt))); }; -inline const ast_operator CoreFunctions::readfile = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::readfile = []() mutable -> value_raw_ptr { Function func; func.setArguments({"file"}); //NOTE: Always set params after args @@ -110,11 +101,10 @@ inline const ast_operator CoreFunctions::readfile = []()mutable->value_ptr m_file.open(QIODevice::ReadOnly); auto const ret = m_file.read(m_file.bytesAvailable()); m_file.close(); - return std::make_shared(ret); + return new QVariant(ret); }; -inline const ast_operator CoreFunctions::writefile = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::writefile = []() mutable -> value_raw_ptr { Function func; func.setArguments({"file", "data"}); //NOTE: Always set params after args @@ -129,11 +119,10 @@ inline const ast_operator CoreFunctions::writefile = []()mutable->value_ptr .value("data", QByteArray{""}) .toByteArray()); m_file.close(); - return std::make_shared(ret); + return new QVariant(ret); }; -inline const ast_operator CoreFunctions::dbopen = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::dbopen = []() mutable -> value_raw_ptr { Function func; func.setArguments({"settigs"}); //NOTE: Always set params after args @@ -147,11 +136,10 @@ inline const ast_operator CoreFunctions::dbopen = []()mutable->value_ptr .value("settings", QJsonObject{}) .toJsonObject()) ); - return std::make_shared(ret); + return new QVariant(ret); }; -inline const ast_operator CoreFunctions::dbexec = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::dbexec = []() mutable -> value_raw_ptr { Function func; func.setArguments({"handle", "fmt", "args"}); //NOTE: Always set params after args @@ -183,11 +171,10 @@ inline const ast_operator CoreFunctions::dbexec = []()mutable->value_ptr auto ret = dbHandle->json_runSqlQuerry(fmt); - return std::make_shared(ret); + return new QVariant(ret); }; -inline const ast_operator CoreFunctions::dbclose = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::dbclose = []() mutable -> value_raw_ptr { Function func; func.setArguments({"handle"}); //NOTE: Always set params after args @@ -207,11 +194,10 @@ inline const ast_operator CoreFunctions::dbclose = []()mutable->value_ptr delete dbHandle; - return std::make_shared(true); + return new QVariant(true); }; -inline const ast_operator CoreFunctions::mkjson = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::mkjson = []() mutable -> value_raw_ptr { Function func; func.setArguments({"fmt"}); //NOTE: Always set params after args @@ -224,11 +210,10 @@ inline const ast_operator CoreFunctions::mkjson = []()mutable->value_ptr func.parameters().value("fmt", QByteArray{}).toByteArray() ).object(); - return std::make_shared(obj); + return new QVariant(obj); }; -inline const ast_operator CoreFunctions::jsnstringify = []()mutable->value_ptr -{ +inline const ast_operator CoreFunctions::jsnstringify = []() mutable -> value_raw_ptr { Function func; func.setArguments({"object"}); //NOTE: Always set params after args @@ -240,7 +225,7 @@ inline const ast_operator CoreFunctions::jsnstringify = []()mutable->value_ptr auto obj = func.parameters().value("object", QJsonObject{}).toJsonObject(); auto string = QJsonDocument(obj).toJson(); - return std::make_shared(string); + return new QVariant(string); }; inline std::map CoreFunctions::m_functionMap diff --git a/src/serverlanglexer.h b/src/serverlanglexer.h index 6df1e56..50f70e0 100644 --- a/src/serverlanglexer.h +++ b/src/serverlanglexer.h @@ -148,7 +148,7 @@ class Lexer auto temp = QString(node->raw()); auto const _body = scope_capture .match(temp).captured().remove(0, 1).chopped(1); - node->setValue(std::make_shared(_body)); + node->setValue(new QVariant(_body)); node->setName(QString::number(arc4random())+"://Python::Scope"); break; } @@ -158,9 +158,9 @@ class Lexer if(temp.startsWith("\"") && temp.endsWith("\"")) { temp.remove(0,1); temp.chop(1); - node->setValue(std::make_shared(temp)); + node->setValue(new QVariant(temp)); } else { - node->setValue(std::make_shared(node->raw())); + node->setValue(new QVariant(node->raw())); } break; } @@ -216,10 +216,7 @@ class Lexer auto const _name = temp.remove("struct ") .remove("=").remove(";").trimmed(); node->setName(_name); - node->setValue( - std::make_shared( - QJsonDocument::fromJson(_body.toUtf8()).object()) - ); + node->setValue(new QVariant(QJsonDocument::fromJson(_body.toUtf8()).object())); break; } case NodeType::FUNCTION: { @@ -270,7 +267,7 @@ class Lexer .remove("=").remove(";").trimmed(); auto spec = _name.split(":"); node->setName(spec.at(0).trimmed()); - node->setValue(std::make_shared(_body)); + node->setValue(new QVariant(_body)); spec.size() >= 2 ? node->setTypeName("Array::"+spec.at(1).trimmed()) : node->setTypeName("Array::Variant"); @@ -315,7 +312,7 @@ class Lexer .trimmed(); auto spec = _name.split(":"); node->setName(spec.at(0).trimmed()); - node->setValue(std::make_shared(_body)); + node->setValue(new QVariant(_body)); if(spec.size() >= 2) { node->setTypeName(spec.at(1).trimmed()); diff --git a/src/serverlangnode.cpp b/src/serverlangnode.cpp index 904bf0b..0834107 100644 --- a/src/serverlangnode.cpp +++ b/src/serverlangnode.cpp @@ -42,12 +42,12 @@ void STNode::setParentScope(const nodeptr &newParentScope) m_parentScope = newParentScope; } -value_ptr STNode::value() const +value_raw_ptr STNode::value() const { return m_value; } -void STNode::setValue(const value_ptr &newValue) +void STNode::setValue(const value_raw_ptr newValue) { m_value = std::move(newValue); } @@ -106,14 +106,14 @@ void STNode::setOperand(const nodeptr &newOperand) m_operand = newOperand; } -value_ptr STNode::returnval() const +value_raw_ptr STNode::returnval() const { return m_returnval; } -void STNode::setReturnval(const value_ptr &newReturnval) +void STNode::setReturnval(const value_raw_ptr newReturnval) { - m_returnval = newReturnval; + m_returnval = std::move(newReturnval); } QByteArray STNode::raw() const diff --git a/src/serverlangnode.h b/src/serverlangnode.h index e3de9d7..bc99504 100644 --- a/src/serverlangnode.h +++ b/src/serverlangnode.h @@ -40,8 +40,9 @@ enum class NodeType { VARIADIC_PARAMETER_LABEL }; -using value_ptr = std::shared_ptr; -using ast_operator = std::function; +using value_ptr = std::unique_ptr; +using value_raw_ptr = QVariant *; +using ast_operator = std::function; using operatorscope = QMap; @@ -53,7 +54,11 @@ class STNode public: STNode() = default; - ~STNode(){} + ~STNode() + { + delete m_value; + delete m_returnval; + } public: // helper methods const nodeptr check_for_declaration(QStringView name); @@ -67,8 +72,8 @@ class STNode nodeptr parentScope() const; void setParentScope(const nodeptr &newParentScope); - value_ptr value() const; - void setValue(const value_ptr &newValue); + value_raw_ptr value() const; + void setValue(const value_raw_ptr newValue); nodeptr innerScope() const; void setInnerScope(const nodeptr &newInnerScope); @@ -85,8 +90,8 @@ class STNode nodeptr operand() const; void setOperand(const nodeptr &newOperand); - value_ptr returnval() const; - void setReturnval(const value_ptr &newReturnval); + value_raw_ptr returnval() const; + void setReturnval(const value_raw_ptr newReturnval); QByteArray raw() const; void setRaw(const QByteArray &newRaw); @@ -114,8 +119,7 @@ class STNode QVariantMap m_parametersMap; QByteArray m_raw; QString m_name, m_typeName, m_referencedId; - value_ptr m_value = value_ptr{new QVariant}, - m_returnval = value_ptr{new QVariant}; + value_raw_ptr m_value, m_returnval; NodeType m_type; }; diff --git a/src/serverlangruntime.cpp b/src/serverlangruntime.cpp index 756cccb..567119e 100644 --- a/src/serverlangruntime.cpp +++ b/src/serverlangruntime.cpp @@ -77,7 +77,7 @@ void RunTime::interprate(const STNode::nodeptr &ast) _l.insert(QString::number(tmp_indx), *_t->second()->value()); tmp_indx += 1; } - auto const _var = std::make_shared(_l); + auto const _var = new QVariant(_l); _v->second()->setValue(_var); //qDebug() << "Value after call: " << _v->second()->value(); } @@ -182,7 +182,7 @@ void RunTime::injectRTDeclarations(const std::vector &rtDecls) { for(auto &v: rtDecls) { v->setParentScope(m_BufferAST); - m_BufferAST->add_declaration(v); + m_BufferAST->add_declaration(std::move(v)); } } diff --git a/src/serverlangruntime.h b/src/serverlangruntime.h index bcd7700..cf7a8ac 100644 --- a/src/serverlangruntime.h +++ b/src/serverlangruntime.h @@ -59,8 +59,9 @@ class RunTime break; } } - inline const value_ptr get_rhs_value(const STNode::nodeptr &rhs) { - value_ptr ret = {}; + inline const value_raw_ptr get_rhs_value(const STNode::nodeptr &rhs) + { + value_raw_ptr ret = {}; for(auto &_v : rhs->declarationMap()) { if(_v->second()->type() == NodeType::VARIABLE_EXPRESSION) { try { @@ -96,7 +97,7 @@ class RunTime _dict.find(_key).value() : QJsonValue{"__NULL__"}; - _v->second()->setValue(std::make_shared(_key_val)); + _v->second()->setValue(new QVariant(_key_val)); } catch(...) { qWarning() << "WARNING: Cannot find struct field"; diff --git a/src/serverlangtypes.h b/src/serverlangtypes.h index a0648fb..fe8464c 100644 --- a/src/serverlangtypes.h +++ b/src/serverlangtypes.h @@ -38,32 +38,32 @@ struct I16 : STNode }); } private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setValue(operand()->value()); return {}; }; - const ast_operator add = [this]()mutable->value_ptr{ + const ast_operator add = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toInt(); auto const rhs = this->operand()->value()->toInt(); - this->setReturnval(value_ptr{new QVariant(lhs+rhs)}); + this->setReturnval(new QVariant(lhs + rhs)); return {}; }; - const ast_operator sub = [this]()mutable->value_ptr{ + const ast_operator sub = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toInt(); auto const rhs = this->operand()->value()->toInt(); - this->setReturnval(value_ptr{new QVariant(lhs-rhs)}); + this->setReturnval(new QVariant(lhs - rhs)); return {}; }; - const ast_operator mul = [this]()mutable->value_ptr{ + const ast_operator mul = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toInt(); auto const rhs = this->operand()->value()->toInt(); - this->setReturnval(value_ptr{new QVariant(lhs*rhs)}); + this->setReturnval(new QVariant(lhs * rhs)); return {}; }; - const ast_operator div = [this]()mutable->value_ptr{ + const ast_operator div = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toInt(); auto const rhs = this->operand()->value()->toInt(); - this->setReturnval(value_ptr{new QVariant(lhs/rhs)}); + this->setReturnval(new QVariant(lhs / rhs)); return {}; }; }; @@ -84,32 +84,32 @@ struct Double : STNode }); } private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setValue(operand()->value()); return {}; }; - const ast_operator add = [this]()mutable->value_ptr{ + const ast_operator add = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toDouble(); auto const rhs = this->operand()->value()->toDouble(); - this->setReturnval(value_ptr{new QVariant(lhs+rhs)}); + this->setReturnval(new QVariant(lhs + rhs)); return {}; }; - const ast_operator sub = [this]()mutable->value_ptr{ + const ast_operator sub = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toDouble(); auto const rhs = this->operand()->value()->toDouble(); - this->setReturnval(value_ptr{new QVariant(lhs-rhs)}); + this->setReturnval(new QVariant(lhs - rhs)); return {}; }; - const ast_operator mul = [this]()mutable->value_ptr{ + const ast_operator mul = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toDouble(); auto const rhs = this->operand()->value()->toDouble(); - this->setReturnval(value_ptr{new QVariant(lhs*rhs)}); + this->setReturnval(new QVariant(lhs * rhs)); return {}; }; - const ast_operator div = [this]()mutable->value_ptr{ + const ast_operator div = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toDouble(); auto const rhs = this->operand()->value()->toDouble(); - this->setReturnval(value_ptr{new QVariant(lhs/rhs)}); + this->setReturnval(new QVariant(lhs / rhs)); return {}; }; }; @@ -130,32 +130,32 @@ struct Float : STNode }); } private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setValue(operand()->value()); return {}; }; - const ast_operator add = [this]()mutable->value_ptr{ + const ast_operator add = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toFloat(); auto const rhs = this->operand()->value()->toFloat(); - this->setReturnval(value_ptr{new QVariant(lhs+rhs)}); + this->setReturnval(new QVariant(lhs + rhs)); return {}; }; - const ast_operator sub = [this]()mutable->value_ptr{ + const ast_operator sub = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toFloat(); auto const rhs = this->operand()->value()->toFloat(); - this->setReturnval(value_ptr{new QVariant(lhs-rhs)}); + this->setReturnval(new QVariant(lhs - rhs)); return {}; }; - const ast_operator mul = [this]()mutable->value_ptr{ + const ast_operator mul = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toFloat(); auto const rhs = this->operand()->value()->toFloat(); - this->setReturnval(value_ptr{new QVariant(lhs*rhs)}); + this->setReturnval(new QVariant(lhs * rhs)); return {}; }; - const ast_operator div = [this]()mutable->value_ptr{ + const ast_operator div = [this]() mutable -> value_raw_ptr { auto const lhs = this->value()->toFloat(); auto const rhs = this->operand()->value()->toFloat(); - this->setReturnval(value_ptr{new QVariant(lhs/rhs)}); + this->setReturnval(new QVariant(lhs / rhs)); return {}; }; }; @@ -173,13 +173,12 @@ struct ByteArray : STNode }); } private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setValue(operand()->value()); return {}; }; - const ast_operator concat = [this]()mutable->value_ptr{ - this->setValue(value_ptr{new QVariant(value()->toByteArray() - +operand()->value()->toByteArray())}); + const ast_operator concat = [this]() mutable -> value_raw_ptr { + this->setValue(new QVariant(value()->toByteArray() + operand()->value()->toByteArray())); return {}; }; }; @@ -197,13 +196,12 @@ struct String : STNode }); } private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setReturnval(operand()->value()); return {}; }; - const ast_operator concat = [this]()mutable->value_ptr{ - this->setReturnval(value_ptr{ new QVariant(value()->toString() - +operand()->value()->toString())}); + const ast_operator concat = [this]() mutable -> value_raw_ptr { + this->setReturnval(new QVariant(value()->toString() + operand()->value()->toString())); return {}; }; }; @@ -256,13 +254,12 @@ struct Variant : STNode QString m_inferredTypeName; private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setReturnval(operand()->value()); return {}; }; - const ast_operator concat = [this]()mutable->value_ptr{ - this->setReturnval(value_ptr{ new QVariant(value()->toString() - +operand()->value()->toString())}); + const ast_operator concat = [this]() mutable -> value_raw_ptr { + this->setReturnval(new QVariant(value()->toString() + operand()->value()->toString())); return {}; }; }; @@ -290,15 +287,15 @@ struct Struct : STNode QJsonObject m_data; private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setValue(operand()->value()); return {}; }; - const ast_operator access = [this]()mutable->value_ptr{ - this->setReturnval(value_ptr{ new QVariant(checkFor(operand()->name()))}); + const ast_operator access = [this]() mutable -> value_raw_ptr { + this->setReturnval(new QVariant(checkFor(operand()->name()))); return {}; }; - const ast_operator insert = [this]()mutable->value_ptr{ + const ast_operator insert = [this]() mutable -> value_raw_ptr { m_data.insert(operand()->name(), operand()->value()->toString()); return {}; }; @@ -317,12 +314,12 @@ struct Class : STNode }); } private: - const ast_operator assign = [this]()mutable->value_ptr{ + const ast_operator assign = [this]() mutable -> value_raw_ptr { this->setValue(operand()->value()); return {}; }; - const ast_operator access = [this]() mutable -> value_ptr { - this->setReturnval(value_ptr{new QVariant(operand()->name().toString())}); + const ast_operator access = [this]() mutable -> value_raw_ptr { + this->setReturnval(new QVariant(operand()->name().toString())); return {}; }; };