Skip to content

Commit

Permalink
Make ASTNode::m_value and ASTNode::m_returnVal raw pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
zadockmaloba committed Dec 1, 2023
1 parent 11b6c38 commit e6a366f
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 116 deletions.
6 changes: 3 additions & 3 deletions src/naisysmethodhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QVariant>(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<QVariant>(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<QVariant>(m_desirialized._header));
http_headers.setValue(new QVariant(m_desirialized._header));

rt.injectRTDeclarations({
std::make_shared<ServerLang::STNode>(http_body),
Expand Down
6 changes: 3 additions & 3 deletions src/serverlangcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace NaiSys {
namespace ServerLang {

const std::shared_ptr<QVariant> 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()];
Expand All @@ -19,9 +19,9 @@ const std::shared_ptr<QVariant> 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<QVariant>(ret);
return new QVariant(ret);
};
CoreFunctions::functionMap().insert({func->name().toString(), opr});
}
Expand Down
3 changes: 1 addition & 2 deletions src/serverlangcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class Core
Core() = default;

public:
static const std::shared_ptr<QVariant> 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();
};

Expand Down
53 changes: 19 additions & 34 deletions src/serverlangfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -63,15 +62,11 @@ inline const ast_operator CoreFunctions::exec_cmd = []()mutable->value_ptr
tmpList << v.toString();
});

return std::make_shared<QVariant>(
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
Expand All @@ -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<QVariant>(
(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
Expand All @@ -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<QVariant>(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
Expand All @@ -129,11 +119,10 @@ inline const ast_operator CoreFunctions::writefile = []()mutable->value_ptr
.value("data", QByteArray{""})
.toByteArray());
m_file.close();
return std::make_shared<QVariant>(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
Expand All @@ -147,11 +136,10 @@ inline const ast_operator CoreFunctions::dbopen = []()mutable->value_ptr
.value("settings", QJsonObject{})
.toJsonObject())
);
return std::make_shared<QVariant>(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
Expand Down Expand Up @@ -183,11 +171,10 @@ inline const ast_operator CoreFunctions::dbexec = []()mutable->value_ptr

auto ret = dbHandle->json_runSqlQuerry(fmt);

return std::make_shared<QVariant>(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
Expand All @@ -207,11 +194,10 @@ inline const ast_operator CoreFunctions::dbclose = []()mutable->value_ptr

delete dbHandle;

return std::make_shared<QVariant>(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
Expand All @@ -224,11 +210,10 @@ inline const ast_operator CoreFunctions::mkjson = []()mutable->value_ptr
func.parameters().value("fmt", QByteArray{}).toByteArray()
).object();

return std::make_shared<QVariant>(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
Expand All @@ -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<QVariant>(string);
return new QVariant(string);
};

inline std::map<const QString, const ast_operator> CoreFunctions::m_functionMap
Expand Down
15 changes: 6 additions & 9 deletions src/serverlanglexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QVariant>(_body));
node->setValue(new QVariant(_body));
node->setName(QString::number(arc4random())+"://Python::Scope");
break;
}
Expand All @@ -158,9 +158,9 @@ class Lexer
if(temp.startsWith("\"") && temp.endsWith("\"")) {
temp.remove(0,1);
temp.chop(1);
node->setValue(std::make_shared<QVariant>(temp));
node->setValue(new QVariant(temp));
} else {
node->setValue(std::make_shared<QVariant>(node->raw()));
node->setValue(new QVariant(node->raw()));
}
break;
}
Expand Down Expand Up @@ -216,10 +216,7 @@ class Lexer
auto const _name = temp.remove("struct ")
.remove("=").remove(";").trimmed();
node->setName(_name);
node->setValue(
std::make_shared<QVariant>(
QJsonDocument::fromJson(_body.toUtf8()).object())
);
node->setValue(new QVariant(QJsonDocument::fromJson(_body.toUtf8()).object()));
break;
}
case NodeType::FUNCTION: {
Expand Down Expand Up @@ -270,7 +267,7 @@ class Lexer
.remove("=").remove(";").trimmed();
auto spec = _name.split(":");
node->setName(spec.at(0).trimmed());
node->setValue(std::make_shared<QVariant>(_body));
node->setValue(new QVariant(_body));

spec.size() >= 2 ? node->setTypeName("Array::"+spec.at(1).trimmed()) :
node->setTypeName("Array::Variant");
Expand Down Expand Up @@ -315,7 +312,7 @@ class Lexer
.trimmed();
auto spec = _name.split(":");
node->setName(spec.at(0).trimmed());
node->setValue(std::make_shared<QVariant>(_body));
node->setValue(new QVariant(_body));
if(spec.size() >= 2)
{
node->setTypeName(spec.at(1).trimmed());
Expand Down
10 changes: 5 additions & 5 deletions src/serverlangnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions src/serverlangnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ enum class NodeType {
VARIADIC_PARAMETER_LABEL
};

using value_ptr = std::shared_ptr<QVariant>;
using ast_operator = std::function<value_ptr ()>;
using value_ptr = std::unique_ptr<QVariant>;
using value_raw_ptr = QVariant *;
using ast_operator = std::function<value_raw_ptr()>;
using operatorscope = QMap<const QString, ast_operator>;


Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
};

Expand Down
4 changes: 2 additions & 2 deletions src/serverlangruntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QVariant>(_l);
auto const _var = new QVariant(_l);
_v->second()->setValue(_var);
//qDebug() << "Value after call: " << _v->second()->value();
}
Expand Down Expand Up @@ -182,7 +182,7 @@ void RunTime::injectRTDeclarations(const std::vector<STNode::nodeptr> &rtDecls)
{
for(auto &v: rtDecls) {
v->setParentScope(m_BufferAST);
m_BufferAST->add_declaration(v);
m_BufferAST->add_declaration(std::move(v));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/serverlangruntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -96,7 +97,7 @@ class RunTime
_dict.find(_key).value() :
QJsonValue{"__NULL__"};

_v->second()->setValue(std::make_shared<QVariant>(_key_val));
_v->second()->setValue(new QVariant(_key_val));
}
catch(...) {
qWarning() << "WARNING: Cannot find struct field";
Expand Down
Loading

0 comments on commit e6a366f

Please sign in to comment.