boxscript-interperter-c/source/expressions.cpp

258 lines
4.9 KiB
C++
Raw Normal View History

2022-12-01 03:27:30 +00:00
#include "parser.hpp"
namespace BoxScript
{
#pragma region "ConstantNumber"
ConstantNumber::ConstantNumber(string num)
{
this->num=stoll(num);
}
int64_t ConstantNumber::Evaluate(IApplicationState* state)
{
return this->num;
}
string ConstantNumber::GetNodeName()
{
return "ConstantNumber";
}
#pragma endregion
#pragma region "VariableGetValueNode"
string VariableGetValueNode::GetName()
{
return this->var;
}
string VariableGetValueNode::GetNodeName()
{
return "VariableGetValueNode";
}
VariableGetValueNode::VariableGetValueNode(string var)
{
this->var = var;
}
int64_t VariableGetValueNode::Evaluate(IApplicationState* state)
{
return state->GetVariable(this->var);
}
#pragma endregion
#pragma region "StringNode"
string StringNode::GetText()
{
return this->text;
}
string StringNode::GetNodeName()
{
return "StringNode";
}
StringNode::StringNode(string var)
{
this->boxId=-1;
this->text=var;
}
int64_t StringNode::Evaluate(IApplicationState* state)
{
if(boxId > -1)
return boxId;
boxId = state->CreateBoxPermanent(text);
return boxId;
}
#pragma endregion
#pragma region "SetVariableNode"
SetVariableNode::~SetVariableNode()
{
delete this->e;
}
string SetVariableNode::GetNodeName()
{
return "SetVariableNode";
}
SetVariableNode::SetVariableNode(Expression* expression,string name)
{
this->e = expression;
this->vname =name;
}
int64_t SetVariableNode::Evaluate(IApplicationState* state)
{
int64_t v = e->Evaluate(state);
state->SetVariable(vname,v);
return 0;
}
#pragma endregion
#pragma region "FunctionCallNode"
FunctionCallNode::~FunctionCallNode()
{
for(auto node: this->expressions)
{
delete node;
}
}
string FunctionCallNode::GetNodeName()
{
return "FunctionCallNode";
}
FunctionCallNode::FunctionCallNode(string _name,vector<Expression*> exps)
{
this->name = _name;
this->expressions =exps;
}
int64_t FunctionCallNode::Evaluate(IApplicationState* state)
{
if(state->HasRuntimeFunction(name))
{
return state->GetRuntimeFunction(name)(state,expressions);
}
if(state->HasFunction(name))
{
return state->ExecuteFunction(name,expressions);
}
return 0;
}
#pragma endregion
#pragma region "AddNode"
AddNode::~AddNode()
{
delete left;
delete right;
}
string AddNode::GetNodeName()
{
return "AddNode";
}
AddNode::AddNode(Expression* _left,Expression* _right)
{
this->left=_left;
this->right = _right;
}
int64_t AddNode::Evaluate(IApplicationState* state)
{
return this->left->Evaluate(state) + this->right->Evaluate(state);
}
#pragma endregion
#pragma region "MinusNode"
MinusNode::~MinusNode()
{
delete left;
delete right;
}
string MinusNode::GetNodeName()
{
return "MinusNode";
}
MinusNode::MinusNode(Expression* _left,Expression* _right)
{
this->left=_left;
this->right = _right;
}
int64_t MinusNode::Evaluate(IApplicationState* state)
{
return this->left->Evaluate(state) - this->right->Evaluate(state);
}
#pragma endregion
#pragma region "MultiplyNode"
MultiplyNode::~MultiplyNode()
{
delete left;
delete right;
}
string MultiplyNode::GetNodeName()
{
return "MultiplyNode";
}
MultiplyNode::MultiplyNode(Expression* _left,Expression* _right)
{
this->left=_left;
this->right = _right;
}
int64_t MultiplyNode::Evaluate(IApplicationState* state)
{
return this->left->Evaluate(state) * this->right->Evaluate(state);
}
#pragma endregion
#pragma region "DivideNode"
DivideNode::~DivideNode()
{
delete left;
delete right;
}
string DivideNode::GetNodeName()
{
return "DivideNode";
}
DivideNode::DivideNode(Expression* _left,Expression* _right)
{
this->left=_left;
this->right = _right;
}
int64_t DivideNode::Evaluate(IApplicationState* state)
{
return this->left->Evaluate(state) / this->right->Evaluate(state);
}
#pragma endregion
#pragma region "ModulusNode"
ModulusNode::~ModulusNode()
{
delete left;
delete right;
}
string ModulusNode::GetNodeName()
{
return "ModulusNode";
}
ModulusNode::ModulusNode(Expression* _left,Expression* _right)
{
this->left=_left;
this->right = _right;
}
int64_t ModulusNode::Evaluate(IApplicationState* state)
{
return this->left->Evaluate(state) % this->right->Evaluate(state);
}
#pragma endregion
#pragma region "ListNode"
void ListNode::DisableScope()
{
this->root=true;
}
ListNode::~ListNode()
{
for(auto node : nodes)
{
delete node;
}
}
string ListNode::GetNodeName()
{
return "ListNode";
}
int64_t ListNode::Evaluate(IApplicationState* state)
{
int64_t res=0;
IApplicationState* s=state;
if(!root)
{
s=s->NewScope();
s->AddThreadOwner();
}
for(auto node : nodes)
{
state->ExitIfNotRunning();
res= node->Evaluate(state);
}
if(!root)
{
s->RemoveThreadOwner();
}
return res;
}
#pragma endregion
void Expression::DisableScope()
{
}
};