Apply formating to all files. CLeanup
This commit is contained in:
parent
43e374f269
commit
744c01c5d0
928 changed files with 14515 additions and 21480 deletions
|
|
@ -1,49 +1,48 @@
|
|||
|
||||
#include "Compiler/Compiler.h"
|
||||
|
||||
#include "primitives/primitives.h"
|
||||
#include "MethodObject/MethodObject.h"
|
||||
#include "log.h"
|
||||
|
||||
using namespace osc;
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
tp::alloc_init();
|
||||
string::Initialize();
|
||||
Logger::init();
|
||||
|
||||
objects_init();
|
||||
primitives_define_types();
|
||||
MethodObject::Initialize();
|
||||
|
||||
{
|
||||
Compiler compiler;
|
||||
|
||||
tp::string script;
|
||||
|
||||
RelAssert(argc == 2 && "Invalid Arguments");
|
||||
|
||||
script.loadFile(argv[1]);
|
||||
|
||||
compiler.compile(script);
|
||||
|
||||
GLog->write("oscc : compilation finished", 1);
|
||||
}
|
||||
|
||||
try {
|
||||
MethodObject::UnInitialize();
|
||||
objects_finalize();
|
||||
primitives_uninitialize();
|
||||
|
||||
Logger::deinit();
|
||||
string::UnInitialize();
|
||||
tp::alloc_uninit();
|
||||
|
||||
}
|
||||
catch (...) {
|
||||
tp::terminate();
|
||||
}
|
||||
|
||||
#include "Compiler/Compiler.h"
|
||||
|
||||
#include "MethodObject/MethodObject.h"
|
||||
#include "log.h"
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
using namespace osc;
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
tp::alloc_init();
|
||||
string::Initialize();
|
||||
Logger::init();
|
||||
|
||||
objects_init();
|
||||
primitives_define_types();
|
||||
MethodObject::Initialize();
|
||||
|
||||
{
|
||||
Compiler compiler;
|
||||
|
||||
tp::string script;
|
||||
|
||||
RelAssert(argc == 2 && "Invalid Arguments");
|
||||
|
||||
script.loadFile(argv[1]);
|
||||
|
||||
compiler.compile(script);
|
||||
|
||||
GLog->write("oscc : compilation finished", 1);
|
||||
}
|
||||
|
||||
try {
|
||||
MethodObject::UnInitialize();
|
||||
objects_finalize();
|
||||
primitives_uninitialize();
|
||||
|
||||
Logger::deinit();
|
||||
string::UnInitialize();
|
||||
tp::alloc_uninit();
|
||||
|
||||
} catch (...) {
|
||||
tp::terminate();
|
||||
}
|
||||
}
|
||||
|
|
@ -20,9 +20,7 @@ namespace ImGui {
|
|||
bool ishovered = false;
|
||||
tp::Vec2F p1;
|
||||
tp::Vec2F p2;
|
||||
operator bool() {
|
||||
return opened;
|
||||
}
|
||||
operator bool() { return opened; }
|
||||
};
|
||||
|
||||
PopupData HoverPopupBegin(const char* id, tp::Vec2F size, tp::Vec2F pos = tp::Vec2F(-1), int flags = 0);
|
||||
|
|
@ -40,8 +38,7 @@ ImGui::PopupData ImGui::HoverPopupBegin(const char* str_id, tp::Vec2F size, tp::
|
|||
|
||||
if (pos_p == tp::Vec2F(-1)) {
|
||||
pos = GImGui->CurrentWindow->DC.CursorPos;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
pos.x = pos_p.x;
|
||||
pos.y = pos_p.y;
|
||||
}
|
||||
|
|
@ -62,7 +59,6 @@ ImGui::PopupData ImGui::HoverPopupBegin(const char* str_id, tp::Vec2F size, tp::
|
|||
out.p1 = { pos.x, pos.y };
|
||||
out.p2 = out.p1;
|
||||
out.p2.x += ImGui::GetWindowWidth();
|
||||
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
@ -87,7 +83,6 @@ void ImGui::HoverPopupEnd(ImGui::PopupData& in) {
|
|||
EndPopup();
|
||||
}
|
||||
|
||||
|
||||
obj::ObjectsGUI::~ObjectsGUI() {
|
||||
if (mClipboard) {
|
||||
obj::NDO->destroy(mClipboard);
|
||||
|
|
@ -109,9 +104,7 @@ void obj::ObjectsGUI::setClipboard(obj::Object* obj) {
|
|||
}
|
||||
}
|
||||
|
||||
obj::Object* obj::ObjectsGUI::getClipboard() {
|
||||
return mClipboard;
|
||||
}
|
||||
obj::Object* obj::ObjectsGUI::getClipboard() { return mClipboard; }
|
||||
|
||||
obj::Object* imgui_object_create_menu(obj::TypeGroups* type_group = NULL) {
|
||||
|
||||
|
|
@ -134,8 +127,7 @@ obj::Object* imgui_object_create_menu(obj::TypeGroups* type_group = NULL) {
|
|||
if (ImGui::Button((childo->key).read(), { 100, 0 })) {
|
||||
if (childo->key == "null") {
|
||||
newo = NDO_NULL;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
newo = obj::NDO->create(childo->key);
|
||||
}
|
||||
}
|
||||
|
|
@ -144,9 +136,7 @@ obj::Object* imgui_object_create_menu(obj::TypeGroups* type_group = NULL) {
|
|||
return newo;
|
||||
}
|
||||
|
||||
obj::ObjectsGUI::ObjectsGUI() {
|
||||
assert(obj::NDO && "Objects library is not initialized");
|
||||
}
|
||||
obj::ObjectsGUI::ObjectsGUI() { assert(obj::NDO && "Objects library is not initialized"); }
|
||||
|
||||
void obj::ObjectsGUI::cd(obj::Object* child, tp::String name) {
|
||||
mActive = child;
|
||||
|
|
@ -178,8 +168,7 @@ void obj::ObjectsGUI::preview(obj::Object* obj) {
|
|||
|
||||
if (link_lookup.presents(tp::alni(link_iter))) {
|
||||
no_link_preview = true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
link_lookup.put(tp::alni(link_iter), 0);
|
||||
}
|
||||
no_link_preview |= max_depth < depth_count;
|
||||
|
|
@ -190,22 +179,18 @@ void obj::ObjectsGUI::preview(obj::Object* obj) {
|
|||
ImGui::Text("=> ");
|
||||
ImGui::SameLine();
|
||||
preview(linko->getLink());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ImGui::Text("Max preview depth exceeded");
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ImGui::Text("Link Is Null");
|
||||
}
|
||||
}
|
||||
else if (NDO_CAST(obj::IntObject, obj)) {
|
||||
} else if (NDO_CAST(obj::IntObject, obj)) {
|
||||
tp::alni& val = NDO_CAST(obj::IntObject, obj)->val;
|
||||
int gui_val = int(val);
|
||||
ImGui::InputInt(" ", &gui_val);
|
||||
val = tp::alni(gui_val);
|
||||
}
|
||||
else if (NDO_CAST(obj::StringObject, obj)) {
|
||||
} else if (NDO_CAST(obj::StringObject, obj)) {
|
||||
NDO_CASTV(obj::StringObject, obj, stringo);
|
||||
static char val[2048] = { " " };
|
||||
if (stringo->val != val) {
|
||||
|
|
@ -216,25 +201,21 @@ void obj::ObjectsGUI::preview(obj::Object* obj) {
|
|||
if (stringo->val != val) {
|
||||
stringo->val = val;
|
||||
}
|
||||
}
|
||||
else if (NDO_CAST(obj::BoolObject, obj)) {
|
||||
boolView((obj::BoolObject*)obj);
|
||||
}
|
||||
else if (NDO_CAST(obj::FloatObject, obj)) {
|
||||
} else if (NDO_CAST(obj::BoolObject, obj)) {
|
||||
boolView((obj::BoolObject*) obj);
|
||||
} else if (NDO_CAST(obj::FloatObject, obj)) {
|
||||
tp::alnf& val = NDO_CAST(obj::FloatObject, obj)->val;
|
||||
auto gui_val = float(val);
|
||||
ImGui::InputFloat(" ", &gui_val);
|
||||
val = tp::alnf(gui_val);
|
||||
|
||||
}
|
||||
else if (NDO_CAST(obj::EnumObject, obj)) {
|
||||
enumView((obj::EnumObject*)obj);
|
||||
}
|
||||
else if (NDO_CAST(obj::ColorObject, obj)) {
|
||||
colorView((obj::ColorObject*)obj);
|
||||
}
|
||||
else {
|
||||
ImGui::Text(obj->type->name); ImGui::SameLine();
|
||||
} else if (NDO_CAST(obj::EnumObject, obj)) {
|
||||
enumView((obj::EnumObject*) obj);
|
||||
} else if (NDO_CAST(obj::ColorObject, obj)) {
|
||||
colorView((obj::ColorObject*) obj);
|
||||
} else {
|
||||
ImGui::Text(obj->type->name);
|
||||
ImGui::SameLine();
|
||||
ImGui::Text(" (No Preview) ");
|
||||
}
|
||||
}
|
||||
|
|
@ -261,12 +242,15 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::interpreterView(obj::Interpreter
|
|||
}
|
||||
|
||||
// running state
|
||||
BeginChild("dockable"); {
|
||||
BeginChild("dockable");
|
||||
{
|
||||
auto id = ImGui::GetCurrentWindow()->ID;
|
||||
DockSpace(id);
|
||||
} EndChild();
|
||||
}
|
||||
EndChild();
|
||||
|
||||
Begin("ByteCodeView"); {
|
||||
Begin("ByteCodeView");
|
||||
{
|
||||
|
||||
if (Button("Step")) {
|
||||
interp.stepBytecode();
|
||||
|
|
@ -299,21 +283,21 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::interpreterView(obj::Interpreter
|
|||
const auto bytecode = interp.mCallStack.getBytecode();
|
||||
tp::halni idx = 0;
|
||||
|
||||
for (auto ip = 0; ip < bytecode->mInstructions.size(); ) {
|
||||
for (auto ip = 0; ip < bytecode->mInstructions.size();) {
|
||||
auto opcode = bytecode->mInstructions[ip];
|
||||
auto info = obj::gOpcodeInfos.fetch(opcode);
|
||||
PushID(info.name);
|
||||
|
||||
TextColored(ImVec4(1.f, 1.f, 1.f, 0.5f), "%i", idx); SameLine();
|
||||
TextColored(ImVec4(1.f, 1.f, 1.f, 0.5f), "%i", idx);
|
||||
SameLine();
|
||||
if (bytecode->mInstructionIdx == ip) {
|
||||
TextColored(ImVec4(1.f, 0.5f, 0.5f, 1.f), info.name);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Text(info.name);
|
||||
}
|
||||
|
||||
// INFO
|
||||
auto hd = HoverPopupBegin(tp::String((tp::alni)ip).read(), { 400, 250 });
|
||||
auto hd = HoverPopupBegin(tp::String((tp::alni) ip).read(), { 400, 250 });
|
||||
if (hd) {
|
||||
Text(info.desc);
|
||||
if (info.operands.len) {
|
||||
|
|
@ -339,43 +323,45 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::interpreterView(obj::Interpreter
|
|||
}
|
||||
|
||||
EndChild();
|
||||
} End();
|
||||
}
|
||||
End();
|
||||
|
||||
Begin("SourceCodeView"); {
|
||||
stringView(interp.mCallStack.mStack.last().mMethod->mScript->mReadable);
|
||||
} End();
|
||||
Begin("SourceCodeView");
|
||||
{ stringView(interp.mCallStack.mStack.last().mMethod->mScript->mReadable); }
|
||||
End();
|
||||
|
||||
Begin("OperandStack"); {
|
||||
Begin("OperandStack");
|
||||
{
|
||||
if (!interp.mOperandsStack.mIdx) {
|
||||
Text("No Operands");
|
||||
}
|
||||
for (auto i = 0; i < interp.mOperandsStack.mIdx; i++) {
|
||||
auto operand = interp.mOperandsStack.mBuff[i];
|
||||
tp::alni operand_val = (tp::alni)operand;
|
||||
tp::alni operand_val = (tp::alni) operand;
|
||||
|
||||
if (operand_val == NULL) {
|
||||
Text("NULL");
|
||||
}
|
||||
else if (tp::uint2(operand_val) == NULL) {
|
||||
} else if (tp::uint2(operand_val) == NULL) {
|
||||
Text("2 BYTE VAL : %i", tp::uint2(operand_val));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (Selectable(operand->type->name, false, 0, ImVec2(100, 0))) {
|
||||
End();
|
||||
return { operand, "operand" };
|
||||
}
|
||||
SameLine();
|
||||
|
||||
PushID((tp::alni)operand);
|
||||
PushID((tp::alni) operand);
|
||||
preview(operand);
|
||||
PopID();
|
||||
}
|
||||
}
|
||||
} End();
|
||||
}
|
||||
End();
|
||||
|
||||
Begin("ScopeStack"); {
|
||||
Begin("ScopeStack");
|
||||
{
|
||||
for (auto i = 0; i < interp.mScopeStack.mIdx; i++) {
|
||||
if (TreeNode(tp::String((tp::alni)i).read())) {
|
||||
if (TreeNode(tp::String((tp::alni) i).read())) {
|
||||
|
||||
auto& scope = interp.mScopeStack.mBuff[i];
|
||||
|
||||
|
|
@ -412,9 +398,11 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::interpreterView(obj::Interpreter
|
|||
TreePop();
|
||||
}
|
||||
}
|
||||
} End();
|
||||
}
|
||||
End();
|
||||
|
||||
Begin("CallStack"); {
|
||||
Begin("CallStack");
|
||||
{
|
||||
for (auto i = 0; i < interp.mCallStack.mStack.size(); i++) {
|
||||
auto method = interp.mCallStack.mStack[i].mMethod;
|
||||
PushID(i);
|
||||
|
|
@ -425,9 +413,11 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::interpreterView(obj::Interpreter
|
|||
}
|
||||
PopID();
|
||||
}
|
||||
} End();
|
||||
}
|
||||
End();
|
||||
|
||||
Begin("ConstPool"); {
|
||||
Begin("ConstPool");
|
||||
{
|
||||
auto bc = interp.mCallStack.getBytecode();
|
||||
tp::alni idx = 0;
|
||||
for (auto const_obj : bc->mConstants) {
|
||||
|
|
@ -435,14 +425,15 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::interpreterView(obj::Interpreter
|
|||
if (Button(const_obj.data()->type->name)) {
|
||||
End();
|
||||
PopID();
|
||||
return {const_obj.data(), tp::String(idx)};
|
||||
return { const_obj.data(), tp::String(idx) };
|
||||
}
|
||||
SameLine();
|
||||
preview(const_obj.data());
|
||||
PopID();
|
||||
idx++;
|
||||
}
|
||||
} End();
|
||||
}
|
||||
End();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
|
@ -452,7 +443,6 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::colorView(obj::ColorObject* in)
|
|||
return {};
|
||||
}
|
||||
|
||||
|
||||
obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::enumView(obj::EnumObject* obj) {
|
||||
|
||||
if (!obj->entries) {
|
||||
|
|
@ -504,8 +494,7 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::linkoView(obj::LinkObject* obj)
|
|||
if (ImGui::Selectable("View")) {
|
||||
return { obj->getLink(), "adress" };
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ImGui::Text("Link Is Null");
|
||||
}
|
||||
|
||||
|
|
@ -524,10 +513,11 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::linkoView(obj::LinkObject* obj)
|
|||
}
|
||||
|
||||
obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::intoView(obj::IntObject* obj) {
|
||||
ImGui::Text("Int Value: "); ImGui::SameLine();
|
||||
int val = (int)obj->val;
|
||||
ImGui::Text("Int Value: ");
|
||||
ImGui::SameLine();
|
||||
int val = (int) obj->val;
|
||||
ImGui::InputInt(" ", &val);
|
||||
obj->val = (tp::alni)val;
|
||||
obj->val = (tp::alni) val;
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
@ -581,20 +571,18 @@ void obj::ObjectsGUI::dictViewEdit(obj::DictObject* dict, tp::String key, obj::O
|
|||
|
||||
if (key.size() > 100) {
|
||||
ImGui::Text("name is too large");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
|
||||
if (mLastNameEditObject != obj) {
|
||||
tp::memCopy(mNameEdit, key.read(), key.size() + 1);
|
||||
mLastNameEditObject = obj;
|
||||
}
|
||||
|
||||
if (ImGui::InputTextEx(" ", "new name", mNameEdit, 100, { 140 , 30 }, ImGuiInputTextFlags_EnterReturnsTrue)) {
|
||||
if (ImGui::InputTextEx(" ", "new name", mNameEdit, 100, { 140, 30 }, ImGuiInputTextFlags_EnterReturnsTrue)) {
|
||||
auto idx = dict->presents(mNameEdit);
|
||||
if (bool(idx)) {
|
||||
//Notify("Object with such name Already Exists");
|
||||
}
|
||||
else {
|
||||
// Notify("Object with such name Already Exists");
|
||||
} else {
|
||||
obj::NDO->refinc(obj);
|
||||
dict->remove(key);
|
||||
auto id = tp::String(mNameEdit);
|
||||
|
|
@ -620,7 +608,8 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::dictView(obj::DictObject* obj) {
|
|||
bool popup = false;
|
||||
auto table_flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable;
|
||||
|
||||
ImGui::BeginChild("frame", { 0, 0 }, 0, ImGuiWindowFlags_NoBackground); {
|
||||
ImGui::BeginChild("frame", { 0, 0 }, 0, ImGuiWindowFlags_NoBackground);
|
||||
{
|
||||
if (!obj->getItems().size()) {
|
||||
ImGui::Text("Dictinary Is Empty. ");
|
||||
}
|
||||
|
|
@ -629,27 +618,27 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::dictView(obj::DictObject* obj) {
|
|||
ImGui::TableSetupColumn("name");
|
||||
ImGui::TableSetupColumn("info");
|
||||
for (auto childo : obj->getItems()) {
|
||||
|
||||
|
||||
ImGui::TableNextRow(0, 30);
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
|
||||
if (ImGui::Selectable(childo->key.read())) {
|
||||
ImGui::EndTable();
|
||||
ImGui::EndChild();
|
||||
return { childo->val, { tp::String(childo->val->type->name) + tp::String(" ") + childo->key } };
|
||||
return { childo->val, { tp::String(childo->val->type->name) + tp::String(" ") + childo->key } };
|
||||
}
|
||||
|
||||
|
||||
dictViewEdit(obj, childo->key, childo->val, popup);
|
||||
|
||||
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::PushID((int)childo->key.read()[0]);
|
||||
ImGui::PushID((int) childo->key.read()[0]);
|
||||
preview(childo->val);
|
||||
ImGui::PopID();
|
||||
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
} ImGui::EndChild();
|
||||
}
|
||||
ImGui::EndChild();
|
||||
|
||||
if (!popup) {
|
||||
dictViewDrawCreate(obj);
|
||||
|
|
@ -678,7 +667,7 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::listView(obj::ListObject* obj) {
|
|||
|
||||
ImGui::TableSetColumnIndex(0);
|
||||
if (ImGui::Selectable(tp::String(idx).read())) {
|
||||
out = ViewStackNode(childo.data(), { tp::String(childo->type->name) + tp::String("at") + tp::String(idx) } );
|
||||
out = ViewStackNode(childo.data(), { tp::String(childo->type->name) + tp::String("at") + tp::String(idx) });
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -718,7 +707,7 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::listView(obj::ListObject* obj) {
|
|||
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
|
||||
ImGui::PushID((int)idx);
|
||||
ImGui::PushID((int) idx);
|
||||
preview(childo.data());
|
||||
ImGui::PopID();
|
||||
|
||||
|
|
@ -758,7 +747,7 @@ void drawStrView(tp::String& in) {
|
|||
}
|
||||
|
||||
ImGui::BeginChild("str_edit");
|
||||
ImGui::InputTextMultiline(" ", val, 2048, { ImGui::GetWindowWidth() , ImGui::GetWindowHeight() });
|
||||
ImGui::InputTextMultiline(" ", val, 2048, { ImGui::GetWindowWidth(), ImGui::GetWindowHeight() });
|
||||
ImGui::EndChild();
|
||||
|
||||
if (in != val) {
|
||||
|
|
@ -811,16 +800,15 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::classView(obj::ClassObject* self
|
|||
ImGui::EndTable();
|
||||
ImGui::TreePop();
|
||||
ImGui::EndChild();
|
||||
return { childo->val, { tp::String(childo->val->type->name) + childo->key } };
|
||||
return { childo->val, { tp::String(childo->val->type->name) + childo->key } };
|
||||
}
|
||||
|
||||
dictViewEdit(dict, childo->key, childo->val, popup);
|
||||
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
ImGui::PushID((int)idx);
|
||||
ImGui::PushID((int) idx);
|
||||
preview(childo->val);
|
||||
ImGui::PopID();
|
||||
|
||||
}
|
||||
ImGui::EndTable();
|
||||
|
||||
|
|
@ -829,13 +817,13 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::classView(obj::ClassObject* self
|
|||
ImGui::TreePop();
|
||||
}
|
||||
|
||||
if (n_methods && ImGui::TreeNodeBehavior(window->GetID("Methods"), tree_flags, "Methods", 0) ) {
|
||||
if (n_methods && ImGui::TreeNodeBehavior(window->GetID("Methods"), tree_flags, "Methods", 0)) {
|
||||
for (auto childo : dict->getItems()) {
|
||||
if (childo->val->type == &obj::MethodObject::TypeData) {
|
||||
if (ImGui::Selectable(childo->key.read())) {
|
||||
ImGui::TreePop();
|
||||
ImGui::EndChild();
|
||||
return { childo->val, { tp::String(childo->val->type->name) + childo->key } };
|
||||
return { childo->val, { tp::String(childo->val->type->name) + childo->key } };
|
||||
}
|
||||
dictViewEdit(dict, childo->key, childo->val, popup);
|
||||
}
|
||||
|
|
@ -865,8 +853,7 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::methodView(obj::MethodObject* in
|
|||
interp.execAll(in);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (ImGui::Button("Compile")) {
|
||||
in->compile();
|
||||
}
|
||||
|
|
@ -897,13 +884,14 @@ void obj::ObjectsGUI::explorer() {
|
|||
|
||||
tp::alni idx = 0;
|
||||
for (auto childo = rev_path.last(); childo; childo = childo->prev) {
|
||||
ImGui::PushID((int)idx);
|
||||
ImGui::PushID((int) idx);
|
||||
bool go_back = false;
|
||||
if (childo == rev_path.last()) {
|
||||
go_back = ImGui::Button(childo->data->id.read()); ImGui::SameLine();
|
||||
}
|
||||
else {
|
||||
go_back = ImGui::Button((childo->data->id).read()); ImGui::SameLine();
|
||||
go_back = ImGui::Button(childo->data->id.read());
|
||||
ImGui::SameLine();
|
||||
} else {
|
||||
go_back = ImGui::Button((childo->data->id).read());
|
||||
ImGui::SameLine();
|
||||
}
|
||||
ImGui::PopID();
|
||||
|
||||
|
|
@ -919,7 +907,7 @@ void obj::ObjectsGUI::explorer() {
|
|||
|
||||
if (ImGui::Selectable("Instantiate ")) {
|
||||
setClipboard(obj::NDO->instatiate(curretn_object));
|
||||
//Notify("Object copied to clipboard");
|
||||
// Notify("Object copied to clipboard");
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
|
@ -929,22 +917,22 @@ void obj::ObjectsGUI::explorer() {
|
|||
ImGui::InputTextEx(" ", "save path", path_str, 100, { 100, 30 }, 0);
|
||||
|
||||
bool save_object = ImGui::Button("Save Object");
|
||||
ImGui::SameLine(); ImGui::Checkbox("Compressed", &compressed);
|
||||
ImGui::SameLine();
|
||||
ImGui::Checkbox("Compressed", &compressed);
|
||||
bool load_object = ImGui::Button("Load Object");
|
||||
|
||||
if (save_object) {
|
||||
obj::NDO->save(curretn_object, path_str, compressed);
|
||||
//Notify("Object saved");
|
||||
// Notify("Object saved");
|
||||
}
|
||||
|
||||
if (load_object) {
|
||||
obj::Object* loadedo = obj::NDO->load(path_str);
|
||||
if (loadedo) {
|
||||
setClipboard(loadedo);
|
||||
//Notify("Object copied to clipboard");
|
||||
}
|
||||
else {
|
||||
//Notify("Can't load Object");
|
||||
// Notify("Object copied to clipboard");
|
||||
} else {
|
||||
// Notify("Can't load Object");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -968,45 +956,32 @@ void obj::ObjectsGUI::explorer() {
|
|||
|
||||
ViewStackNode new_active;
|
||||
if (NDO_CAST(obj::NullObject, mActive)) {
|
||||
new_active = nullView((obj::NullObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::LinkObject, mActive)) {
|
||||
new_active = linkoView((obj::LinkObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::IntObject, mActive)) {
|
||||
new_active = intoView((obj::IntObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::StringObject, mActive)) {
|
||||
new_active = stringView((obj::StringObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::ListObject, mActive)) {
|
||||
new_active = listView((obj::ListObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::DictObject, mActive)) {
|
||||
new_active = dictView((obj::DictObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::InterpreterObject, mActive)) {
|
||||
new_active = interpreterView((obj::InterpreterObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::ClassObject, mActive)) {
|
||||
new_active = classView((obj::ClassObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::BoolObject, mActive)) {
|
||||
new_active = boolView((obj::BoolObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::FloatObject, mActive)) {
|
||||
new_active = floatView((obj::FloatObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::EnumObject, mActive)) {
|
||||
new_active = enumView((obj::EnumObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::MethodObject, mActive)) {
|
||||
new_active = methodView((obj::MethodObject*)mActive);
|
||||
}
|
||||
else if (NDO_CAST(obj::ColorObject, mActive)) {
|
||||
new_active = colorView((obj::ColorObject*)mActive);
|
||||
}
|
||||
else {
|
||||
new_active = nullView((obj::NullObject*) mActive);
|
||||
} else if (NDO_CAST(obj::LinkObject, mActive)) {
|
||||
new_active = linkoView((obj::LinkObject*) mActive);
|
||||
} else if (NDO_CAST(obj::IntObject, mActive)) {
|
||||
new_active = intoView((obj::IntObject*) mActive);
|
||||
} else if (NDO_CAST(obj::StringObject, mActive)) {
|
||||
new_active = stringView((obj::StringObject*) mActive);
|
||||
} else if (NDO_CAST(obj::ListObject, mActive)) {
|
||||
new_active = listView((obj::ListObject*) mActive);
|
||||
} else if (NDO_CAST(obj::DictObject, mActive)) {
|
||||
new_active = dictView((obj::DictObject*) mActive);
|
||||
} else if (NDO_CAST(obj::InterpreterObject, mActive)) {
|
||||
new_active = interpreterView((obj::InterpreterObject*) mActive);
|
||||
} else if (NDO_CAST(obj::ClassObject, mActive)) {
|
||||
new_active = classView((obj::ClassObject*) mActive);
|
||||
} else if (NDO_CAST(obj::BoolObject, mActive)) {
|
||||
new_active = boolView((obj::BoolObject*) mActive);
|
||||
} else if (NDO_CAST(obj::FloatObject, mActive)) {
|
||||
new_active = floatView((obj::FloatObject*) mActive);
|
||||
} else if (NDO_CAST(obj::EnumObject, mActive)) {
|
||||
new_active = enumView((obj::EnumObject*) mActive);
|
||||
} else if (NDO_CAST(obj::MethodObject, mActive)) {
|
||||
new_active = methodView((obj::MethodObject*) mActive);
|
||||
} else if (NDO_CAST(obj::ColorObject, mActive)) {
|
||||
new_active = colorView((obj::ColorObject*) mActive);
|
||||
} else {
|
||||
ImGui::Text("Preview is Unavaliable");
|
||||
}
|
||||
|
||||
|
|
@ -1028,8 +1003,7 @@ void obj::ObjectsGUI::properties(const obj::ObjectType* type, bool top_of_tree_v
|
|||
if (ImGui::TreeNode("Description ")) {
|
||||
if (type->description) {
|
||||
ImGui::Text(type->description);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ImGui::Text("Description is not given");
|
||||
}
|
||||
ImGui::TreePop();
|
||||
|
|
@ -1052,7 +1026,7 @@ void obj::ObjectsGUI::properties(const obj::ObjectType* type, bool top_of_tree_v
|
|||
|
||||
ImGui::TreePop();
|
||||
}
|
||||
//if (ImGui::TreeNode("Full Type Hierarchy")) {
|
||||
// if (ImGui::TreeNode("Full Type Hierarchy")) {
|
||||
|
||||
ImGui::Text("RAM : ");
|
||||
ImGui::Indent();
|
||||
|
|
@ -1067,16 +1041,15 @@ void obj::ObjectsGUI::properties(const obj::ObjectType* type, bool top_of_tree_v
|
|||
ImGui::Text("With Object Links : %i bytes", obj::NDO->objsize_file_recursive(mActive));
|
||||
ImGui::Unindent();
|
||||
|
||||
//ImGui::TreePop();
|
||||
//}
|
||||
// ImGui::TreePop();
|
||||
//}
|
||||
ImGui::TreePop();
|
||||
}
|
||||
|
||||
if (ImGui::TreeNode("Base Type")) {
|
||||
if (type->base) {
|
||||
properties(type->base, false);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ImGui::Text("No base type");
|
||||
}
|
||||
ImGui::TreePop();
|
||||
|
|
@ -1099,8 +1072,7 @@ void obj::ObjectsGUI::draw() {
|
|||
}
|
||||
ImGui::End();
|
||||
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ImGui::Text("ViewStack is Empty");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,9 +28,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
~GUIWindow() {
|
||||
Window::destroyWindow(window);
|
||||
}
|
||||
~GUIWindow() { Window::destroyWindow(window); }
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
|
|
|||
|
|
@ -1,58 +1,58 @@
|
|||
|
||||
|
||||
#include "MethodObject/methodobject.h"
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "Interpreter/Interpreter.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
using namespace osc;
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
tp::alloc_init();
|
||||
string::Initialize();
|
||||
Logger::init();
|
||||
|
||||
objects_init();
|
||||
primitives_define_types();
|
||||
MethodObject::Initialize();
|
||||
|
||||
{
|
||||
time_ms load_start = get_time();
|
||||
RelAssert(argc == 2 && "Invalid Arguments");
|
||||
auto obj = NDO->load(argv[1]);
|
||||
RelAssert(obj && "Failed to Load the File");
|
||||
RelAssert(obj->type == &MethodObject::TypeData && "Innvalid file content");
|
||||
|
||||
Interpreter interp;
|
||||
NDO_CASTV(MethodObject, obj, method);
|
||||
|
||||
RelAssert(method->mScript && "Innvalid file content");
|
||||
time_ms load_end = get_time();
|
||||
|
||||
GLog->write("\noscvm - execution started\n");
|
||||
time_ms exec_start = get_time();
|
||||
interp.exec(&method->mScript->mBytecode);
|
||||
time_ms exec_end = get_time();
|
||||
|
||||
auto exec_time = (exec_end - exec_start);
|
||||
auto load_time = (load_end - load_start);
|
||||
GLog->write(sfmt("\noscvm - execution finished (Load : %f Exec : %f)", load_time, exec_time));
|
||||
}
|
||||
|
||||
try {
|
||||
MethodObject::UnInitialize();
|
||||
objects_finalize();
|
||||
primitives_uninitialize();
|
||||
|
||||
Logger::deinit();
|
||||
string::UnInitialize();
|
||||
tp::alloc_uninit();
|
||||
|
||||
} catch (...) {
|
||||
tp::terminate();
|
||||
}
|
||||
|
||||
|
||||
#include "MethodObject/methodobject.h"
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "Interpreter/Interpreter.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
using namespace osc;
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
tp::alloc_init();
|
||||
string::Initialize();
|
||||
Logger::init();
|
||||
|
||||
objects_init();
|
||||
primitives_define_types();
|
||||
MethodObject::Initialize();
|
||||
|
||||
{
|
||||
time_ms load_start = get_time();
|
||||
RelAssert(argc == 2 && "Invalid Arguments");
|
||||
auto obj = NDO->load(argv[1]);
|
||||
RelAssert(obj && "Failed to Load the File");
|
||||
RelAssert(obj->type == &MethodObject::TypeData && "Innvalid file content");
|
||||
|
||||
Interpreter interp;
|
||||
NDO_CASTV(MethodObject, obj, method);
|
||||
|
||||
RelAssert(method->mScript && "Innvalid file content");
|
||||
time_ms load_end = get_time();
|
||||
|
||||
GLog->write("\noscvm - execution started\n");
|
||||
time_ms exec_start = get_time();
|
||||
interp.exec(&method->mScript->mBytecode);
|
||||
time_ms exec_end = get_time();
|
||||
|
||||
auto exec_time = (exec_end - exec_start);
|
||||
auto load_time = (load_end - load_start);
|
||||
GLog->write(sfmt("\noscvm - execution finished (Load : %f Exec : %f)", load_time, exec_time));
|
||||
}
|
||||
|
||||
try {
|
||||
MethodObject::UnInitialize();
|
||||
objects_finalize();
|
||||
primitives_uninitialize();
|
||||
|
||||
Logger::deinit();
|
||||
string::UnInitialize();
|
||||
tp::alloc_uninit();
|
||||
|
||||
} catch (...) {
|
||||
tp::terminate();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,87 +1,89 @@
|
|||
|
||||
|
||||
#include "primitives/methodobject.h"
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
#include "compiler/function.h"
|
||||
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
void TestCompile(ByteCode& bytecode) {
|
||||
using namespace BCgen;
|
||||
|
||||
Genereate(bytecode,
|
||||
StmScope({
|
||||
|
||||
StmDefFunc("main", {}, {
|
||||
StmDefLocal("i1", ExprConst(1)),
|
||||
StmDefLocal("i2", ExprConst(2)),
|
||||
|
||||
//StmDefFunc("add", {"first", "second"}, {
|
||||
// StmReturn(ExprAdd(ExprLocal("first"), ExprLocal("second")))
|
||||
//}),
|
||||
|
||||
StmPrint(ExprLocal("i1")),
|
||||
StmPrint(ExprConst(" + ")),
|
||||
StmPrint(ExprLocal("i2")),
|
||||
StmPrint(ExprConst(" = ")),
|
||||
|
||||
StmPrint(ExprFunc("add")->ExprCall({ ExprLocal("i1"), ExprLocal("i2") })),
|
||||
|
||||
StmPrint(ExprConst("\n")),
|
||||
}),
|
||||
|
||||
StmPrint(ExprBoolNot(ExprConst(0))),
|
||||
|
||||
StmWhile(ExprConst(false), StmScope({
|
||||
StmIgnore(ExprFunc("main")->ExprCall({}))
|
||||
})
|
||||
),
|
||||
|
||||
StmIf(ExprConst(true),
|
||||
StmScope({
|
||||
//StmIgnore(ExprFunc("main")->ExprCall({})),
|
||||
//StmPrint(ExprConst("true")),
|
||||
}),
|
||||
StmScope({
|
||||
//StmPrint(ExprConst("false")),
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
tp::alloc_init();
|
||||
string::Initialize();
|
||||
Logger::init();
|
||||
|
||||
objects_init();
|
||||
primitives_define_types();
|
||||
MethodObject::Initialize();
|
||||
|
||||
{
|
||||
Interpreter interp;
|
||||
|
||||
ByteCode bytecode;
|
||||
TestCompile(bytecode);
|
||||
|
||||
GLog->write(" >> Exec: start \n");
|
||||
auto start = get_time();
|
||||
interp.exec(&bytecode);
|
||||
auto end = get_time();
|
||||
GLog->write(sfmt("\n >> Exec time %i ms", end - start));
|
||||
}
|
||||
|
||||
MethodObject::UnInitialize();
|
||||
objects_finalize();
|
||||
primitives_uninitialize();
|
||||
|
||||
Logger::deinit();
|
||||
string::UnInitialize();
|
||||
tp::alloc_uninit();
|
||||
|
||||
|
||||
#include "primitives/methodobject.h"
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
#include "compiler/function.h"
|
||||
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
void TestCompile(ByteCode& bytecode) {
|
||||
using namespace BCgen;
|
||||
|
||||
Genereate(
|
||||
bytecode,
|
||||
StmScope({
|
||||
|
||||
StmDefFunc(
|
||||
"main",
|
||||
{},
|
||||
{
|
||||
StmDefLocal("i1", ExprConst(1)),
|
||||
StmDefLocal("i2", ExprConst(2)),
|
||||
|
||||
// StmDefFunc("add", {"first", "second"}, {
|
||||
// StmReturn(ExprAdd(ExprLocal("first"), ExprLocal("second")))
|
||||
//}),
|
||||
|
||||
StmPrint(ExprLocal("i1")),
|
||||
StmPrint(ExprConst(" + ")),
|
||||
StmPrint(ExprLocal("i2")),
|
||||
StmPrint(ExprConst(" = ")),
|
||||
|
||||
StmPrint(ExprFunc("add")->ExprCall({ ExprLocal("i1"), ExprLocal("i2") })),
|
||||
|
||||
StmPrint(ExprConst("\n")),
|
||||
}
|
||||
),
|
||||
|
||||
StmPrint(ExprBoolNot(ExprConst(0))),
|
||||
|
||||
StmWhile(ExprConst(false), StmScope({ StmIgnore(ExprFunc("main")->ExprCall({})) })),
|
||||
|
||||
StmIf(
|
||||
ExprConst(true),
|
||||
StmScope({
|
||||
// StmIgnore(ExprFunc("main")->ExprCall({})),
|
||||
// StmPrint(ExprConst("true")),
|
||||
}),
|
||||
StmScope({
|
||||
// StmPrint(ExprConst("false")),
|
||||
})
|
||||
) })
|
||||
);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
tp::alloc_init();
|
||||
string::Initialize();
|
||||
Logger::init();
|
||||
|
||||
objects_init();
|
||||
primitives_define_types();
|
||||
MethodObject::Initialize();
|
||||
|
||||
{
|
||||
Interpreter interp;
|
||||
|
||||
ByteCode bytecode;
|
||||
TestCompile(bytecode);
|
||||
|
||||
GLog->write(" >> Exec: start \n");
|
||||
auto start = get_time();
|
||||
interp.exec(&bytecode);
|
||||
auto end = get_time();
|
||||
GLog->write(sfmt("\n >> Exec time %i ms", end - start));
|
||||
}
|
||||
|
||||
MethodObject::UnInitialize();
|
||||
objects_finalize();
|
||||
primitives_uninitialize();
|
||||
|
||||
Logger::deinit();
|
||||
string::UnInitialize();
|
||||
tp::alloc_uninit();
|
||||
}
|
||||
|
|
@ -1,75 +1,68 @@
|
|||
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "compiler/function.h"
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
#include "CmdArgParser.h"
|
||||
|
||||
void compile(char argc, const char* argv[]) {
|
||||
tp::CmdArgParser args = {
|
||||
{ "script", tp::CmdArgParser::Arg::FILE_IN },
|
||||
{ "out", "out.mo" }
|
||||
};
|
||||
|
||||
if (!args.parse(argc, argv, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& script_file = args.getFile("script");
|
||||
auto outpath = args.getString("out");
|
||||
|
||||
tp::string script;
|
||||
script.loadFile(&script_file);
|
||||
|
||||
auto method = obj::MethodObject::create(script);
|
||||
|
||||
obj::NDO->save(method, outpath);
|
||||
}
|
||||
|
||||
void execute(char argc, const char* argv[]) {
|
||||
tp::CmdArgParser args = {
|
||||
{ "object_path", tp::CmdArgParser::Arg::STR },
|
||||
{ "debug", false }
|
||||
};
|
||||
|
||||
if (!args.parse(argc, argv, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto object_path = args.getString("object_path");
|
||||
auto debug = args.getBool("debug");
|
||||
|
||||
auto obj = obj::NDO->load(object_path);
|
||||
if (!obj) {
|
||||
printf("Invalid Object File\n");
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(obj::MethodObject, obj, method);
|
||||
if (!method) {
|
||||
printf("Object Is Not A Method\n");
|
||||
return;
|
||||
}
|
||||
|
||||
obj::Interpreter interpreter;
|
||||
interpreter.execAll(method);
|
||||
}
|
||||
|
||||
|
||||
int main(char argc, const char* argv[]) {
|
||||
|
||||
tp::ModuleManifest* ModuleDependencies[] = { &obj::gModuleObjects, NULL };
|
||||
tp::ModuleManifest TestModule("Test", NULL, NULL, ModuleDependencies);
|
||||
if (!TestModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef OBC
|
||||
compile(argc, argv);
|
||||
#elif OBVM
|
||||
execute(argc, argv);
|
||||
#endif // OBC
|
||||
|
||||
TestModule.deinitialize();
|
||||
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "compiler/function.h"
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
#include "CmdArgParser.h"
|
||||
|
||||
void compile(char argc, const char* argv[]) {
|
||||
tp::CmdArgParser args = { { "script", tp::CmdArgParser::Arg::FILE_IN }, { "out", "out.mo" } };
|
||||
|
||||
if (!args.parse(argc, argv, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& script_file = args.getFile("script");
|
||||
auto outpath = args.getString("out");
|
||||
|
||||
tp::string script;
|
||||
script.loadFile(&script_file);
|
||||
|
||||
auto method = obj::MethodObject::create(script);
|
||||
|
||||
obj::NDO->save(method, outpath);
|
||||
}
|
||||
|
||||
void execute(char argc, const char* argv[]) {
|
||||
tp::CmdArgParser args = { { "object_path", tp::CmdArgParser::Arg::STR }, { "debug", false } };
|
||||
|
||||
if (!args.parse(argc, argv, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto object_path = args.getString("object_path");
|
||||
auto debug = args.getBool("debug");
|
||||
|
||||
auto obj = obj::NDO->load(object_path);
|
||||
if (!obj) {
|
||||
printf("Invalid Object File\n");
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(obj::MethodObject, obj, method);
|
||||
if (!method) {
|
||||
printf("Object Is Not A Method\n");
|
||||
return;
|
||||
}
|
||||
|
||||
obj::Interpreter interpreter;
|
||||
interpreter.execAll(method);
|
||||
}
|
||||
|
||||
int main(char argc, const char* argv[]) {
|
||||
|
||||
tp::ModuleManifest* ModuleDependencies[] = { &obj::gModuleObjects, NULL };
|
||||
tp::ModuleManifest TestModule("Test", NULL, NULL, ModuleDependencies);
|
||||
if (!TestModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef OBC
|
||||
compile(argc, argv);
|
||||
#elif OBVM
|
||||
execute(argc, argv);
|
||||
#endif // OBC
|
||||
|
||||
TestModule.deinitialize();
|
||||
}
|
||||
|
|
@ -1,148 +1,145 @@
|
|||
#include "NewPlacement.hpp"
|
||||
#include "compiler/constants.h"
|
||||
#include "primitives/primitives.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
using namespace BCgen;
|
||||
|
||||
ConstObject::ConstObject() {}
|
||||
ConstObject::ConstObject(obj::Object* mObj) : mObj(mObj) {}
|
||||
|
||||
ConstObjectsPool::~ConstObjectsPool() {
|
||||
if (mDelete) {
|
||||
for (auto obj : mStrings) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
for (auto obj : mIntegers) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
for (auto obj : mFloats) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
for (auto obj : mMethods) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
if (mBoolFalse.mObj) {
|
||||
obj::NDO->destroy(mBoolFalse.mObj);
|
||||
}
|
||||
if (mBoolTrue.mObj) {
|
||||
obj::NDO->destroy(mBoolTrue.mObj);
|
||||
}
|
||||
}
|
||||
for (auto obj : mStrings) {
|
||||
delete obj->val;
|
||||
}
|
||||
for (auto obj : mIntegers) {
|
||||
delete obj->val;
|
||||
}
|
||||
for (auto obj : mFloats) {
|
||||
delete obj->val;
|
||||
}
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::registerObject(obj::Object* obj) {
|
||||
ConstObject* out = new ConstObject(obj);
|
||||
mTotalObjects++;
|
||||
return out;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(tp::alni val) {
|
||||
auto idx = mIntegers.presents(val);
|
||||
ConstObject* const_obj = NULL;
|
||||
if (idx) {
|
||||
const_obj = mIntegers.getSlotVal(idx);
|
||||
}
|
||||
else {
|
||||
const_obj = registerObject(IntObject::create(val));
|
||||
mIntegers.put(val, const_obj);
|
||||
}
|
||||
return const_obj;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(tp::String val) {
|
||||
auto idx = mStrings.presents(val);
|
||||
ConstObject* const_obj = NULL;
|
||||
if (idx) {
|
||||
const_obj = mStrings.getSlotVal(idx);
|
||||
}
|
||||
else {
|
||||
const_obj = registerObject(StringObject::create(val));
|
||||
mStrings.put(val, const_obj);
|
||||
}
|
||||
return const_obj;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(tp::alnf val) {
|
||||
auto idx = mFloats.presents(val);
|
||||
ConstObject* const_obj = NULL;
|
||||
if (idx) {
|
||||
const_obj = mFloats.getSlotVal(idx);
|
||||
}
|
||||
else {
|
||||
const_obj = registerObject(FloatObject::create(val));
|
||||
mFloats.put(val, const_obj);
|
||||
}
|
||||
return const_obj;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(bool val) {
|
||||
if (val) {
|
||||
if (!mBoolTrue.mObj) {
|
||||
mBoolTrue.mObj = BoolObject::create(val);
|
||||
mTotalObjects++;
|
||||
}
|
||||
return &mBoolTrue;
|
||||
}
|
||||
else {
|
||||
if (!mBoolFalse.mObj) {
|
||||
mBoolFalse.mObj = BoolObject::create(val);
|
||||
mTotalObjects++;
|
||||
}
|
||||
return &mBoolFalse;
|
||||
}
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::addMethod(tp::String method_id, obj::Object* method) {
|
||||
ASSERT(NDO_CAST(MethodObject, method) && "Object is not a method object");
|
||||
ASSERT(!mMethods.presents(method_id) && "Method Redefinition");
|
||||
auto out = registerObject(method);
|
||||
mMethods.put(method_id, out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void ConstObjectsPool::save(tp::Buffer<ConstData>& out) {
|
||||
out.reserve(mTotalObjects);
|
||||
tp::alni data_idx = 0;
|
||||
for (auto obj : mMethods) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
for (auto obj : mStrings) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
for (auto obj : mIntegers) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
for (auto obj : mFloats) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
if (mBoolFalse.mObj) {
|
||||
out[data_idx] = mBoolFalse.mObj;
|
||||
mBoolFalse.mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
if (mBoolTrue.mObj) {
|
||||
out[data_idx] = mBoolTrue.mObj;
|
||||
mBoolTrue.mConstIdx = data_idx;
|
||||
}
|
||||
mDelete = false;
|
||||
#include "compiler/constants.h"
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/methodobject.h"
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
using namespace BCgen;
|
||||
|
||||
ConstObject::ConstObject() {}
|
||||
ConstObject::ConstObject(obj::Object* mObj) :
|
||||
mObj(mObj) {}
|
||||
|
||||
ConstObjectsPool::~ConstObjectsPool() {
|
||||
if (mDelete) {
|
||||
for (auto obj : mStrings) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
for (auto obj : mIntegers) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
for (auto obj : mFloats) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
for (auto obj : mMethods) {
|
||||
obj::NDO->destroy(obj->val->mObj);
|
||||
}
|
||||
if (mBoolFalse.mObj) {
|
||||
obj::NDO->destroy(mBoolFalse.mObj);
|
||||
}
|
||||
if (mBoolTrue.mObj) {
|
||||
obj::NDO->destroy(mBoolTrue.mObj);
|
||||
}
|
||||
}
|
||||
for (auto obj : mStrings) {
|
||||
delete obj->val;
|
||||
}
|
||||
for (auto obj : mIntegers) {
|
||||
delete obj->val;
|
||||
}
|
||||
for (auto obj : mFloats) {
|
||||
delete obj->val;
|
||||
}
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::registerObject(obj::Object* obj) {
|
||||
ConstObject* out = new ConstObject(obj);
|
||||
mTotalObjects++;
|
||||
return out;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(tp::alni val) {
|
||||
auto idx = mIntegers.presents(val);
|
||||
ConstObject* const_obj = NULL;
|
||||
if (idx) {
|
||||
const_obj = mIntegers.getSlotVal(idx);
|
||||
} else {
|
||||
const_obj = registerObject(IntObject::create(val));
|
||||
mIntegers.put(val, const_obj);
|
||||
}
|
||||
return const_obj;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(tp::String val) {
|
||||
auto idx = mStrings.presents(val);
|
||||
ConstObject* const_obj = NULL;
|
||||
if (idx) {
|
||||
const_obj = mStrings.getSlotVal(idx);
|
||||
} else {
|
||||
const_obj = registerObject(StringObject::create(val));
|
||||
mStrings.put(val, const_obj);
|
||||
}
|
||||
return const_obj;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(tp::alnf val) {
|
||||
auto idx = mFloats.presents(val);
|
||||
ConstObject* const_obj = NULL;
|
||||
if (idx) {
|
||||
const_obj = mFloats.getSlotVal(idx);
|
||||
} else {
|
||||
const_obj = registerObject(FloatObject::create(val));
|
||||
mFloats.put(val, const_obj);
|
||||
}
|
||||
return const_obj;
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::get(bool val) {
|
||||
if (val) {
|
||||
if (!mBoolTrue.mObj) {
|
||||
mBoolTrue.mObj = BoolObject::create(val);
|
||||
mTotalObjects++;
|
||||
}
|
||||
return &mBoolTrue;
|
||||
} else {
|
||||
if (!mBoolFalse.mObj) {
|
||||
mBoolFalse.mObj = BoolObject::create(val);
|
||||
mTotalObjects++;
|
||||
}
|
||||
return &mBoolFalse;
|
||||
}
|
||||
}
|
||||
|
||||
ConstObject* ConstObjectsPool::addMethod(tp::String method_id, obj::Object* method) {
|
||||
ASSERT(NDO_CAST(MethodObject, method) && "Object is not a method object");
|
||||
ASSERT(!mMethods.presents(method_id) && "Method Redefinition");
|
||||
auto out = registerObject(method);
|
||||
mMethods.put(method_id, out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void ConstObjectsPool::save(tp::Buffer<ConstData>& out) {
|
||||
out.reserve(mTotalObjects);
|
||||
tp::alni data_idx = 0;
|
||||
for (auto obj : mMethods) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
for (auto obj : mStrings) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
for (auto obj : mIntegers) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
for (auto obj : mFloats) {
|
||||
out[data_idx] = obj->val->mObj;
|
||||
obj->val->mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
if (mBoolFalse.mObj) {
|
||||
out[data_idx] = mBoolFalse.mObj;
|
||||
mBoolFalse.mConstIdx = data_idx;
|
||||
data_idx++;
|
||||
}
|
||||
if (mBoolTrue.mObj) {
|
||||
out[data_idx] = mBoolTrue.mObj;
|
||||
mBoolTrue.mConstIdx = data_idx;
|
||||
}
|
||||
mDelete = false;
|
||||
}
|
||||
|
|
@ -1,70 +1,93 @@
|
|||
#include "NewPlacement.hpp"
|
||||
#include "compiler/expression.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace BCgen;
|
||||
|
||||
Expression::Expression() {}
|
||||
Expression::Expression(Type type) : mType(type) {}
|
||||
|
||||
ExpressionChild* Expression::ExprChild(tp::String id) {
|
||||
return new ExpressionChild(this, id);
|
||||
}
|
||||
|
||||
ExpressionCall* Expression::ExprCall(tp::InitialierList<Expression*> args) {
|
||||
return new ExpressionCall(this, args);
|
||||
}
|
||||
|
||||
ExpressionLocal* obj::BCgen::ExprLocal(tp::String id) {
|
||||
return new ExpressionLocal(id);
|
||||
}
|
||||
|
||||
ExpressionSelf* obj::BCgen::ExprSelf() {
|
||||
return new ExpressionSelf();
|
||||
}
|
||||
|
||||
ExpressionNew* obj::BCgen::ExprNew(tp::String type) {
|
||||
return new ExpressionNew(type);
|
||||
}
|
||||
|
||||
ExpressionAriphm* obj::BCgen::ExprAriphm(Expression* left, Expression* right, OpCode type) {
|
||||
return new ExpressionAriphm(left, right, type);
|
||||
}
|
||||
|
||||
ExpressionFunc* obj::BCgen::ExprFunc(tp::String id) {
|
||||
return new ExpressionFunc(id);
|
||||
}
|
||||
|
||||
ExpressionBoolean* obj::BCgen::ExprBool(Expression* left, Expression* right, obj::OpCode type) {
|
||||
return new ExpressionBoolean(left, right, ExpressionBoolean::BoolType(type));
|
||||
}
|
||||
|
||||
ExpressionBoolean* obj::BCgen::ExprBoolNot(Expression* invert) {
|
||||
return new ExpressionBoolean(invert);
|
||||
}
|
||||
|
||||
ExpressionBoolean::ExpressionBoolean(Expression* left, Expression* right, BoolType type)
|
||||
: Expression(Type::BOOLEAN), mLeft(left), mRight(right), mBoolType(type) {
|
||||
}
|
||||
|
||||
ExpressionBoolean::ExpressionBoolean(Expression* invert)
|
||||
: Expression(Type::BOOLEAN), mLeft(invert), mBoolType(BoolType::NOT){
|
||||
}
|
||||
|
||||
ExpressionCall::ExpressionCall(Expression * mParent, tp::InitialierList<Expression*> args) : Expression(Type::CALL), mParent(mParent) {
|
||||
mArgs = args;
|
||||
}
|
||||
|
||||
ExpressionNew::ExpressionNew(tp::String type) : Expression(Type::NEW), mNewType(type) {}
|
||||
ExpressionLocal::ExpressionLocal(tp::String id) : Expression(Type::LOCAL), mLocalId(id) {}
|
||||
ExpressionSelf::ExpressionSelf() : Expression(Type::SELF) {}
|
||||
ExpressionChild::ExpressionChild(Expression* mParent, tp::String id) : Expression(Type::CHILD), mParent(mParent), mLocalId(id) {}
|
||||
ExpressionAriphm::ExpressionAriphm(Expression* left, Expression* right, OpCode type) : Expression(Type::ARIPHM), mLeft(left), mRight(right), mOpType(type) {}
|
||||
ExpressionFunc::ExpressionFunc(tp::String id) : Expression(Type::FUNC), mFuncId(id) {}
|
||||
ExpressionConst::ExpressionConst(tp::String val) : Expression(Type::CONST), mConstType(STR), str(val) {}
|
||||
ExpressionConst::ExpressionConst(const char* val) : Expression(Type::CONST), mConstType(STR), str(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::int4 val) : Expression(Type::CONST), mConstType(INT), integer(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::flt4 val) : Expression(Type::CONST), mConstType(FLT), floating(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::alni val) : Expression(Type::CONST), mConstType(INT), integer(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::alnf val) : Expression(Type::CONST), mConstType(FLT), floating(val) {}
|
||||
ExpressionConst::ExpressionConst(bool val) : Expression(Type::CONST), mConstType(BOOL), boolean(val) {}
|
||||
#include "compiler/expression.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace obj;
|
||||
using namespace BCgen;
|
||||
|
||||
Expression::Expression() {}
|
||||
Expression::Expression(Type type) :
|
||||
mType(type) {}
|
||||
|
||||
ExpressionChild* Expression::ExprChild(tp::String id) { return new ExpressionChild(this, id); }
|
||||
|
||||
ExpressionCall* Expression::ExprCall(tp::InitialierList<Expression*> args) { return new ExpressionCall(this, args); }
|
||||
|
||||
ExpressionLocal* obj::BCgen::ExprLocal(tp::String id) { return new ExpressionLocal(id); }
|
||||
|
||||
ExpressionSelf* obj::BCgen::ExprSelf() { return new ExpressionSelf(); }
|
||||
|
||||
ExpressionNew* obj::BCgen::ExprNew(tp::String type) { return new ExpressionNew(type); }
|
||||
|
||||
ExpressionAriphm* obj::BCgen::ExprAriphm(Expression* left, Expression* right, OpCode type) { return new ExpressionAriphm(left, right, type); }
|
||||
|
||||
ExpressionFunc* obj::BCgen::ExprFunc(tp::String id) { return new ExpressionFunc(id); }
|
||||
|
||||
ExpressionBoolean* obj::BCgen::ExprBool(Expression* left, Expression* right, obj::OpCode type) { return new ExpressionBoolean(left, right, ExpressionBoolean::BoolType(type)); }
|
||||
|
||||
ExpressionBoolean* obj::BCgen::ExprBoolNot(Expression* invert) { return new ExpressionBoolean(invert); }
|
||||
|
||||
ExpressionBoolean::ExpressionBoolean(Expression* left, Expression* right, BoolType type) :
|
||||
Expression(Type::BOOLEAN),
|
||||
mLeft(left),
|
||||
mRight(right),
|
||||
mBoolType(type) {}
|
||||
|
||||
ExpressionBoolean::ExpressionBoolean(Expression* invert) :
|
||||
Expression(Type::BOOLEAN),
|
||||
mLeft(invert),
|
||||
mBoolType(BoolType::NOT) {}
|
||||
|
||||
ExpressionCall::ExpressionCall(Expression* mParent, tp::InitialierList<Expression*> args) :
|
||||
Expression(Type::CALL),
|
||||
mParent(mParent) {
|
||||
mArgs = args;
|
||||
}
|
||||
|
||||
ExpressionNew::ExpressionNew(tp::String type) :
|
||||
Expression(Type::NEW),
|
||||
mNewType(type) {}
|
||||
ExpressionLocal::ExpressionLocal(tp::String id) :
|
||||
Expression(Type::LOCAL),
|
||||
mLocalId(id) {}
|
||||
ExpressionSelf::ExpressionSelf() :
|
||||
Expression(Type::SELF) {}
|
||||
ExpressionChild::ExpressionChild(Expression* mParent, tp::String id) :
|
||||
Expression(Type::CHILD),
|
||||
mParent(mParent),
|
||||
mLocalId(id) {}
|
||||
ExpressionAriphm::ExpressionAriphm(Expression* left, Expression* right, OpCode type) :
|
||||
Expression(Type::ARIPHM),
|
||||
mLeft(left),
|
||||
mRight(right),
|
||||
mOpType(type) {}
|
||||
ExpressionFunc::ExpressionFunc(tp::String id) :
|
||||
Expression(Type::FUNC),
|
||||
mFuncId(id) {}
|
||||
ExpressionConst::ExpressionConst(tp::String val) :
|
||||
Expression(Type::CONST),
|
||||
mConstType(STR),
|
||||
str(val) {}
|
||||
ExpressionConst::ExpressionConst(const char* val) :
|
||||
Expression(Type::CONST),
|
||||
mConstType(STR),
|
||||
str(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::int4 val) :
|
||||
Expression(Type::CONST),
|
||||
mConstType(INT),
|
||||
integer(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::flt4 val) :
|
||||
Expression(Type::CONST),
|
||||
mConstType(FLT),
|
||||
floating(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::alni val) :
|
||||
Expression(Type::CONST),
|
||||
mConstType(INT),
|
||||
integer(val) {}
|
||||
ExpressionConst::ExpressionConst(tp::alnf val) :
|
||||
Expression(Type::CONST),
|
||||
mConstType(FLT),
|
||||
floating(val) {}
|
||||
ExpressionConst::ExpressionConst(bool val) :
|
||||
Expression(Type::CONST),
|
||||
mConstType(BOOL),
|
||||
boolean(val) {}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,30 +1,44 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "compiler/instruction.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
using namespace BCgen;
|
||||
|
||||
Instruction::Instruction(OpCode op) : mOp(op), mArgType(ArgType::NO_ARG), mInstType(InstType::EXEC) {}
|
||||
|
||||
Instruction::Instruction() : mInstType(InstType::NONE) {}
|
||||
|
||||
Instruction::Instruction(ConstObject* constData)
|
||||
: mConstData(constData), mInstType(InstType::PURE_CONST) {}
|
||||
|
||||
Instruction::Instruction(OpCode op, ConstObject* constData)
|
||||
: mOp(op), mConstData(constData), mArgType(ArgType::CONST), mInstType(InstType::EXEC) {
|
||||
}
|
||||
|
||||
Instruction::Instruction(OpCode op, ConstObject* constData, ConstObject* constData2)
|
||||
: mOp(op), mConstData(constData), mConstData2(constData2), mArgType(ArgType::CONST), mInstType(InstType::EXEC) {
|
||||
}
|
||||
|
||||
Instruction::Instruction(OpCode op, tp::alni param, tp::alni nBytes)
|
||||
: mOp(op), mParam(param), mArgType(ArgType::PARAM), mParamBytes(nBytes), mInstType(InstType::EXEC) {
|
||||
}
|
||||
|
||||
Instruction::Instruction(Instruction* inst, InstType jump_type): mInstTarget(inst) {
|
||||
mInstType = jump_type;
|
||||
|
||||
#include "compiler/instruction.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
using namespace BCgen;
|
||||
|
||||
Instruction::Instruction(OpCode op) :
|
||||
mOp(op),
|
||||
mArgType(ArgType::NO_ARG),
|
||||
mInstType(InstType::EXEC) {}
|
||||
|
||||
Instruction::Instruction() :
|
||||
mInstType(InstType::NONE) {}
|
||||
|
||||
Instruction::Instruction(ConstObject* constData) :
|
||||
mConstData(constData),
|
||||
mInstType(InstType::PURE_CONST) {}
|
||||
|
||||
Instruction::Instruction(OpCode op, ConstObject* constData) :
|
||||
mOp(op),
|
||||
mConstData(constData),
|
||||
mArgType(ArgType::CONST),
|
||||
mInstType(InstType::EXEC) {}
|
||||
|
||||
Instruction::Instruction(OpCode op, ConstObject* constData, ConstObject* constData2) :
|
||||
mOp(op),
|
||||
mConstData(constData),
|
||||
mConstData2(constData2),
|
||||
mArgType(ArgType::CONST),
|
||||
mInstType(InstType::EXEC) {}
|
||||
|
||||
Instruction::Instruction(OpCode op, tp::alni param, tp::alni nBytes) :
|
||||
mOp(op),
|
||||
mParam(param),
|
||||
mArgType(ArgType::PARAM),
|
||||
mParamBytes(nBytes),
|
||||
mInstType(InstType::EXEC) {}
|
||||
|
||||
Instruction::Instruction(Instruction* inst, InstType jump_type) :
|
||||
mInstTarget(inst) {
|
||||
mInstType = jump_type;
|
||||
}
|
||||
|
|
@ -1,116 +1,102 @@
|
|||
#include "NewPlacement.hpp"
|
||||
#include "compiler/statement.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace BCgen;
|
||||
|
||||
|
||||
StatementFuncDef::StatementFuncDef(tp::String function_id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> statements) {
|
||||
mType = Type::DEF_FUNC;
|
||||
mArgs = args;
|
||||
mStatements = statements;
|
||||
mFunctionId = function_id;
|
||||
}
|
||||
|
||||
StatementLocalDef::StatementLocalDef(tp::String id, Expression* value) : mLocalId(id), mNewExpr(value) {
|
||||
mType = Type::DEF_LOCAL;
|
||||
}
|
||||
|
||||
StatementLocalDef::StatementLocalDef(tp::String id, ExpressionConst* value) : mLocalId(id), mConstExpr(value), mIsConstExpr(true) {
|
||||
mType = Type::DEF_LOCAL;
|
||||
}
|
||||
|
||||
StatementCopy::StatementCopy(Expression* left, Expression* right) : mLeft(left), mRight(right) {
|
||||
mType = Type::COPY;
|
||||
}
|
||||
|
||||
StatementReturn::StatementReturn() {
|
||||
mType = Type::RET;
|
||||
}
|
||||
|
||||
StatementReturn::StatementReturn(Expression* ret) : mRet(ret) {
|
||||
mType = Type::RET;
|
||||
}
|
||||
|
||||
StatementPrint::StatementPrint(Expression* target) : mTarget(target) {
|
||||
mType = Type::PRINT;
|
||||
}
|
||||
|
||||
StatementScope::StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack) {
|
||||
mType = Type::SCOPE;
|
||||
mStatements = statements;
|
||||
mPushToScopeStack = aPushToScopeStack;
|
||||
}
|
||||
|
||||
StatementIf::StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) {
|
||||
mType = Type::IF;
|
||||
mOnFalse = on_false;
|
||||
mOnTrue = on_true;
|
||||
mCondition = condition;
|
||||
}
|
||||
|
||||
StatementWhile::StatementWhile(Expression* condition, StatementScope* scope) {
|
||||
mType = Type::WHILE;
|
||||
mScope = scope;
|
||||
mCondition = condition;
|
||||
}
|
||||
|
||||
StatementIgnore::StatementIgnore(Expression* expr) {
|
||||
mType = Type::IGNORE;
|
||||
mExpr = expr;
|
||||
}
|
||||
|
||||
StatementClassDef::StatementClassDef(tp::String class_id, StatementScope* scope) {
|
||||
mClassId = class_id;
|
||||
mScope = scope;
|
||||
mType = Type::CLASS_DEF;
|
||||
}
|
||||
// helpers
|
||||
|
||||
StatementFuncDef* obj::BCgen::StmDefFunc(tp::String id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> stms) {
|
||||
return new StatementFuncDef(id, args, stms);
|
||||
}
|
||||
|
||||
StatementLocalDef* obj::BCgen::StmDefLocal(tp::String id, Expression* value) {
|
||||
if (value->mType == Expression::Type::CONST) {
|
||||
return new StatementLocalDef(id, (ExpressionConst*)value);
|
||||
}
|
||||
|
||||
return new StatementLocalDef(id, value);
|
||||
}
|
||||
|
||||
StatementCopy* obj::BCgen::StmCopy(Expression* left, Expression* right) {
|
||||
return new StatementCopy(left, right);
|
||||
}
|
||||
|
||||
StatementPrint* obj::BCgen::StmPrint(Expression* target) {
|
||||
return new StatementPrint(target);
|
||||
}
|
||||
|
||||
StatementReturn* obj::BCgen::StmReturn() {
|
||||
return new StatementReturn();
|
||||
}
|
||||
|
||||
StatementReturn* obj::BCgen::StmReturn(Expression* obj) {
|
||||
return new StatementReturn(obj);
|
||||
}
|
||||
|
||||
StatementIf* obj::BCgen::StmIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) {
|
||||
return new StatementIf(condition, on_true, on_false);
|
||||
}
|
||||
|
||||
StatementScope* obj::BCgen::StmScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack = false) {
|
||||
return new StatementScope(statements, aPushToScopeStack);
|
||||
}
|
||||
|
||||
StatementWhile* obj::BCgen::StmWhile(Expression* condition, StatementScope* scope) {
|
||||
return new StatementWhile(condition, scope);
|
||||
}
|
||||
|
||||
StatementIgnore* obj::BCgen::StmIgnore(Expression* expr) {
|
||||
return new StatementIgnore(expr);
|
||||
}
|
||||
|
||||
StatementClassDef* obj::BCgen::StmClassDef(tp::String id, StatementScope* scope) {
|
||||
return new StatementClassDef(id, scope);
|
||||
}
|
||||
#include "compiler/statement.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace obj;
|
||||
using namespace BCgen;
|
||||
|
||||
StatementFuncDef::StatementFuncDef(tp::String function_id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> statements) {
|
||||
mType = Type::DEF_FUNC;
|
||||
mArgs = args;
|
||||
mStatements = statements;
|
||||
mFunctionId = function_id;
|
||||
}
|
||||
|
||||
StatementLocalDef::StatementLocalDef(tp::String id, Expression* value) :
|
||||
mLocalId(id),
|
||||
mNewExpr(value) {
|
||||
mType = Type::DEF_LOCAL;
|
||||
}
|
||||
|
||||
StatementLocalDef::StatementLocalDef(tp::String id, ExpressionConst* value) :
|
||||
mLocalId(id),
|
||||
mConstExpr(value),
|
||||
mIsConstExpr(true) {
|
||||
mType = Type::DEF_LOCAL;
|
||||
}
|
||||
|
||||
StatementCopy::StatementCopy(Expression* left, Expression* right) :
|
||||
mLeft(left),
|
||||
mRight(right) {
|
||||
mType = Type::COPY;
|
||||
}
|
||||
|
||||
StatementReturn::StatementReturn() { mType = Type::RET; }
|
||||
|
||||
StatementReturn::StatementReturn(Expression* ret) :
|
||||
mRet(ret) {
|
||||
mType = Type::RET;
|
||||
}
|
||||
|
||||
StatementPrint::StatementPrint(Expression* target) :
|
||||
mTarget(target) {
|
||||
mType = Type::PRINT;
|
||||
}
|
||||
|
||||
StatementScope::StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack) {
|
||||
mType = Type::SCOPE;
|
||||
mStatements = statements;
|
||||
mPushToScopeStack = aPushToScopeStack;
|
||||
}
|
||||
|
||||
StatementIf::StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) {
|
||||
mType = Type::IF;
|
||||
mOnFalse = on_false;
|
||||
mOnTrue = on_true;
|
||||
mCondition = condition;
|
||||
}
|
||||
|
||||
StatementWhile::StatementWhile(Expression* condition, StatementScope* scope) {
|
||||
mType = Type::WHILE;
|
||||
mScope = scope;
|
||||
mCondition = condition;
|
||||
}
|
||||
|
||||
StatementIgnore::StatementIgnore(Expression* expr) {
|
||||
mType = Type::IGNORE;
|
||||
mExpr = expr;
|
||||
}
|
||||
|
||||
StatementClassDef::StatementClassDef(tp::String class_id, StatementScope* scope) {
|
||||
mClassId = class_id;
|
||||
mScope = scope;
|
||||
mType = Type::CLASS_DEF;
|
||||
}
|
||||
// helpers
|
||||
|
||||
StatementFuncDef* obj::BCgen::StmDefFunc(tp::String id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> stms) { return new StatementFuncDef(id, args, stms); }
|
||||
|
||||
StatementLocalDef* obj::BCgen::StmDefLocal(tp::String id, Expression* value) {
|
||||
if (value->mType == Expression::Type::CONST) {
|
||||
return new StatementLocalDef(id, (ExpressionConst*) value);
|
||||
}
|
||||
|
||||
return new StatementLocalDef(id, value);
|
||||
}
|
||||
|
||||
StatementCopy* obj::BCgen::StmCopy(Expression* left, Expression* right) { return new StatementCopy(left, right); }
|
||||
|
||||
StatementPrint* obj::BCgen::StmPrint(Expression* target) { return new StatementPrint(target); }
|
||||
|
||||
StatementReturn* obj::BCgen::StmReturn() { return new StatementReturn(); }
|
||||
|
||||
StatementReturn* obj::BCgen::StmReturn(Expression* obj) { return new StatementReturn(obj); }
|
||||
|
||||
StatementIf* obj::BCgen::StmIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) { return new StatementIf(condition, on_true, on_false); }
|
||||
|
||||
StatementScope* obj::BCgen::StmScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack = false) { return new StatementScope(statements, aPushToScopeStack); }
|
||||
|
||||
StatementWhile* obj::BCgen::StmWhile(Expression* condition, StatementScope* scope) { return new StatementWhile(condition, scope); }
|
||||
|
||||
StatementIgnore* obj::BCgen::StmIgnore(Expression* expr) { return new StatementIgnore(expr); }
|
||||
|
||||
StatementClassDef* obj::BCgen::StmClassDef(tp::String id, StatementScope* scope) { return new StatementClassDef(id, scope); }
|
||||
|
|
@ -1,92 +1,91 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "Tokenizer.hpp"
|
||||
|
||||
#include "compiler/function.h"
|
||||
|
||||
#include "primitives/boolobject.h"
|
||||
#include "primitives/classobject.h"
|
||||
#include "primitives/colorobject.h"
|
||||
#include "primitives/dictobject.h"
|
||||
#include "primitives/enumobject.h"
|
||||
#include "primitives/floatobject.h"
|
||||
#include "primitives/interpreterobject.h"
|
||||
#include "primitives/intobject.h"
|
||||
#include "primitives/linkobject.h"
|
||||
#include "primitives/listobject.h"
|
||||
#include "primitives/methodobject.h"
|
||||
#include "primitives/nullobject.h"
|
||||
#include "primitives/stringobject.h"
|
||||
#include "primitives/typeobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
static void defineTypes() {
|
||||
NDO->define(&DictObject::TypeData);
|
||||
NDO->define(&IntObject::TypeData);
|
||||
NDO->define(&LinkObject::TypeData);
|
||||
NDO->define(&ListObject::TypeData);
|
||||
NDO->define(&NullObject::TypeData);
|
||||
NDO->define(&StringObject::TypeData);
|
||||
NDO->define(&BoolObject::TypeData);
|
||||
NDO->define(&FloatObject::TypeData);
|
||||
NDO->define(&EnumObject::TypeData);
|
||||
NDO->define(&ClassObject::TypeData);
|
||||
NDO->define(&ColorObject::TypeData);
|
||||
NDO->define(&InterpreterObject::TypeData);
|
||||
NDO->define(&TypeObject::TypeData);
|
||||
}
|
||||
|
||||
static void defineGroups() {
|
||||
NDO->type_groups.addType(&DictObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&IntObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&LinkObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&ListObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&NullObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&StringObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&BoolObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&FloatObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&EnumObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&ClassObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&ColorObject::TypeData, {"Primitives"});
|
||||
NDO->type_groups.addType(&InterpreterObject::TypeData, { "scripting" });
|
||||
}
|
||||
|
||||
|
||||
static bool init(const tp::ModuleManifest*) {
|
||||
if (!NDO) NDO = new objects_api();
|
||||
obj::BCgen::init();
|
||||
|
||||
MethodObject::Initialize();
|
||||
|
||||
defineTypes();
|
||||
defineGroups();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void deinit(const tp::ModuleManifest*) {
|
||||
|
||||
NullObject::uninit();
|
||||
MethodObject::UnInitialize();
|
||||
|
||||
obj::BCgen::deinit();
|
||||
|
||||
assertNoLeaks();
|
||||
|
||||
delete NDO;
|
||||
NDO = nullptr;
|
||||
}
|
||||
|
||||
static tp::ModuleManifest* sModuleDependencies[] = {
|
||||
// &tp::gModuleCompressor,
|
||||
&tp::gModuleMath,
|
||||
&tp::gModuleStrings,
|
||||
&tp::gModuleTokenizer,
|
||||
&tp::gModuleConnection,
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "Tokenizer.hpp"
|
||||
|
||||
#include "compiler/function.h"
|
||||
|
||||
#include "primitives/boolobject.h"
|
||||
#include "primitives/classobject.h"
|
||||
#include "primitives/colorobject.h"
|
||||
#include "primitives/dictobject.h"
|
||||
#include "primitives/enumobject.h"
|
||||
#include "primitives/floatobject.h"
|
||||
#include "primitives/interpreterobject.h"
|
||||
#include "primitives/intobject.h"
|
||||
#include "primitives/linkobject.h"
|
||||
#include "primitives/listobject.h"
|
||||
#include "primitives/methodobject.h"
|
||||
#include "primitives/nullobject.h"
|
||||
#include "primitives/stringobject.h"
|
||||
#include "primitives/typeobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
static void defineTypes() {
|
||||
NDO->define(&DictObject::TypeData);
|
||||
NDO->define(&IntObject::TypeData);
|
||||
NDO->define(&LinkObject::TypeData);
|
||||
NDO->define(&ListObject::TypeData);
|
||||
NDO->define(&NullObject::TypeData);
|
||||
NDO->define(&StringObject::TypeData);
|
||||
NDO->define(&BoolObject::TypeData);
|
||||
NDO->define(&FloatObject::TypeData);
|
||||
NDO->define(&EnumObject::TypeData);
|
||||
NDO->define(&ClassObject::TypeData);
|
||||
NDO->define(&ColorObject::TypeData);
|
||||
NDO->define(&InterpreterObject::TypeData);
|
||||
NDO->define(&TypeObject::TypeData);
|
||||
}
|
||||
|
||||
static void defineGroups() {
|
||||
NDO->type_groups.addType(&DictObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&IntObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&LinkObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&ListObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&NullObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&StringObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&BoolObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&FloatObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&EnumObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&ClassObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&ColorObject::TypeData, { "Primitives" });
|
||||
NDO->type_groups.addType(&InterpreterObject::TypeData, { "scripting" });
|
||||
}
|
||||
|
||||
static bool init(const tp::ModuleManifest*) {
|
||||
if (!NDO) NDO = new objects_api();
|
||||
obj::BCgen::init();
|
||||
|
||||
MethodObject::Initialize();
|
||||
|
||||
defineTypes();
|
||||
defineGroups();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void deinit(const tp::ModuleManifest*) {
|
||||
|
||||
NullObject::uninit();
|
||||
MethodObject::UnInitialize();
|
||||
|
||||
obj::BCgen::deinit();
|
||||
|
||||
assertNoLeaks();
|
||||
|
||||
delete NDO;
|
||||
NDO = nullptr;
|
||||
}
|
||||
|
||||
static tp::ModuleManifest* sModuleDependencies[] = {
|
||||
// &tp::gModuleCompressor,
|
||||
&tp::gModuleMath,
|
||||
&tp::gModuleStrings,
|
||||
&tp::gModuleTokenizer,
|
||||
&tp::gModuleConnection,
|
||||
NULL
|
||||
};
|
||||
|
||||
tp::ModuleManifest obj::gModuleObjects = tp::ModuleManifest("Objects", init, deinit, sModuleDependencies);
|
||||
|
|
@ -1,230 +1,227 @@
|
|||
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "core/object.h"
|
||||
|
||||
#include "primitives/nullobject.h"
|
||||
#include "primitives/classobject.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
#include "interpreter/interpreter.h"
|
||||
#include "compiler/function.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
Object* ObjectMemAllocate(const ObjectType* type);
|
||||
void ObjectMemDeallocate(Object* in);
|
||||
|
||||
objects_api* NDO = NULL;
|
||||
|
||||
void hierarchy_copy(Object* self, const Object* in, const ObjectType* type);
|
||||
void hierarchy_construct(Object* self, const ObjectType* type);
|
||||
|
||||
objects_api::objects_api() {
|
||||
tp::memSetVal(sl_callbacks, SAVE_LOAD_MAX_CALLBACK_SLOTS * sizeof(save_load_callbacks*), 0);
|
||||
interp = new Interpreter();
|
||||
}
|
||||
|
||||
objects_api::~objects_api() {
|
||||
delete interp;
|
||||
}
|
||||
|
||||
void objects_api::define(ObjectType* type) {
|
||||
MODULE_SANITY_CHECK(gModuleObjects);
|
||||
|
||||
DEBUG_ASSERT(NDO && "using uninitialize objects api");
|
||||
DEBUG_ASSERT(!types.presents(type->name) && "Type Redefinition");
|
||||
types.put(type->name, type);
|
||||
|
||||
type->type_methods.init();
|
||||
}
|
||||
|
||||
Object* objects_api::create(const tp::String& name) {
|
||||
MODULE_SANITY_CHECK(gModuleObjects);
|
||||
|
||||
const ObjectType* type = types.get(name);
|
||||
|
||||
Object* obj_instance = ObjectMemAllocate(type);
|
||||
|
||||
if (!obj_instance) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hierarchy_construct(obj_instance, obj_instance->type);
|
||||
|
||||
setrefc(obj_instance, 0);
|
||||
|
||||
NDO_CASTV(ClassObject, obj_instance, classobj);
|
||||
if (classobj) {
|
||||
auto idx = classobj->members->presents("__init__");
|
||||
DEBUG_ASSERT(idx);
|
||||
if (idx) {
|
||||
auto constructor_obj = classobj->members->getSlotVal(idx);
|
||||
NDO_CASTV(MethodObject, constructor_obj, constructor_method);
|
||||
if (constructor_method) {
|
||||
interp->execAll(constructor_method, classobj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj_instance;
|
||||
}
|
||||
|
||||
Object* objects_api::copy(Object* self, const Object* in) {
|
||||
if (self->type != in->type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hierarchy_copy(self, in, self->type);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
bool objects_api::compare(Object* first, Object* second) {
|
||||
if (first->type == second->type) {
|
||||
if (first->type->comparison) {
|
||||
return first->type->comparison(first, second);
|
||||
}
|
||||
|
||||
// raw data comparison
|
||||
return tp::memCompare(first, second, first->type->size) == 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Object* objects_api::instatiate(Object* in) {
|
||||
obj::Object* obj = NDO->create(in->type->name);
|
||||
NDO->copy(obj, in);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void objects_api::set(Object* self, tp::alni val) {
|
||||
if (self->type->convesions && self->type->convesions->from_int) {
|
||||
self->type->convesions->from_int(self, val);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void objects_api::set(Object* self, tp::alnf val) {
|
||||
if (self->type->convesions && self->type->convesions->from_float) {
|
||||
self->type->convesions->from_float(self, val);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void objects_api::set(Object* self, tp::String val) {
|
||||
if (self->type->convesions && self->type->convesions->from_string) {
|
||||
self->type->convesions->from_string(self, val);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
tp::alni objects_api::toInt(Object* self) {
|
||||
DEBUG_ASSERT(self->type->convesions && self->type->convesions->to_int);
|
||||
return self->type->convesions->to_int(self);
|
||||
}
|
||||
|
||||
tp::alnf objects_api::toFloat(Object* self) {
|
||||
DEBUG_ASSERT(self->type->convesions && self->type->convesions->to_float);
|
||||
return self->type->convesions->to_float(self);
|
||||
}
|
||||
|
||||
bool objects_api::toBool(Object* self) {
|
||||
if (self->type->convesions && self->type->convesions->to_int) {
|
||||
return (bool) self->type->convesions->to_int(self);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
tp::String objects_api::toString(Object* self) {
|
||||
DEBUG_ASSERT(self->type->convesions && self->type->convesions->to_string);
|
||||
return self->type->convesions->to_string(self);
|
||||
}
|
||||
|
||||
void objects_api::destroy(Object* in) {
|
||||
|
||||
if (!in) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
if (mh->refc > 1) {
|
||||
mh->refc--;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
NDO_CASTV(ClassObject, in, classobj);
|
||||
if (classobj) {
|
||||
auto idx = classobj->members->presents("__del__");
|
||||
DEBUG_ASSERT(idx);
|
||||
if (idx) {
|
||||
auto constructor_obj = classobj->members->getSlotVal(idx);
|
||||
NDO_CASTV(MethodObject, constructor_obj, constructor_method);
|
||||
if (constructor_method) {
|
||||
interp->execAll(constructor_method, classobj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const ObjectType* iter = in->type; iter; iter = iter->base) {
|
||||
if (iter->destructor) {
|
||||
iter->destructor(in);
|
||||
}
|
||||
}
|
||||
|
||||
ObjectMemDeallocate(in);
|
||||
}
|
||||
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
tp::halni objects_api::getrefc(Object* in) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
return (tp::halni)mh->refc;
|
||||
}
|
||||
|
||||
void objects_api::refinc(Object* in) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
mh->refc++;
|
||||
}
|
||||
|
||||
void objects_api::setrefc(Object* in, tp::halni refc) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
mh->refc = refc;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void hierarchy_copy(Object* self, const Object* in, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
hierarchy_copy(self, in, type->base);
|
||||
}
|
||||
|
||||
if (type->copy) {
|
||||
type->copy(self, in);
|
||||
}
|
||||
}
|
||||
|
||||
void hierarchy_construct(Object* self, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
hierarchy_construct(self, type->base);
|
||||
}
|
||||
|
||||
if (type->constructor) {
|
||||
type->constructor(self);
|
||||
}
|
||||
}
|
||||
|
||||
Object* ndo_cast(const Object* in, const ObjectType* to_type) {
|
||||
const ObjectType* typeiter = in->type;
|
||||
while (typeiter) {
|
||||
if (typeiter == to_type) {
|
||||
return (Object*) in;
|
||||
}
|
||||
typeiter = typeiter->base;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "core/object.h"
|
||||
|
||||
#include "primitives/classobject.h"
|
||||
#include "primitives/methodobject.h"
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
#include "compiler/function.h"
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
Object* ObjectMemAllocate(const ObjectType* type);
|
||||
void ObjectMemDeallocate(Object* in);
|
||||
|
||||
objects_api* NDO = NULL;
|
||||
|
||||
void hierarchy_copy(Object* self, const Object* in, const ObjectType* type);
|
||||
void hierarchy_construct(Object* self, const ObjectType* type);
|
||||
|
||||
objects_api::objects_api() {
|
||||
tp::memSetVal(sl_callbacks, SAVE_LOAD_MAX_CALLBACK_SLOTS * sizeof(save_load_callbacks*), 0);
|
||||
interp = new Interpreter();
|
||||
}
|
||||
|
||||
objects_api::~objects_api() { delete interp; }
|
||||
|
||||
void objects_api::define(ObjectType* type) {
|
||||
MODULE_SANITY_CHECK(gModuleObjects);
|
||||
|
||||
DEBUG_ASSERT(NDO && "using uninitialize objects api");
|
||||
DEBUG_ASSERT(!types.presents(type->name) && "Type Redefinition");
|
||||
types.put(type->name, type);
|
||||
|
||||
type->type_methods.init();
|
||||
}
|
||||
|
||||
Object* objects_api::create(const tp::String& name) {
|
||||
MODULE_SANITY_CHECK(gModuleObjects);
|
||||
|
||||
const ObjectType* type = types.get(name);
|
||||
|
||||
Object* obj_instance = ObjectMemAllocate(type);
|
||||
|
||||
if (!obj_instance) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hierarchy_construct(obj_instance, obj_instance->type);
|
||||
|
||||
setrefc(obj_instance, 0);
|
||||
|
||||
NDO_CASTV(ClassObject, obj_instance, classobj);
|
||||
if (classobj) {
|
||||
auto idx = classobj->members->presents("__init__");
|
||||
DEBUG_ASSERT(idx);
|
||||
if (idx) {
|
||||
auto constructor_obj = classobj->members->getSlotVal(idx);
|
||||
NDO_CASTV(MethodObject, constructor_obj, constructor_method);
|
||||
if (constructor_method) {
|
||||
interp->execAll(constructor_method, classobj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj_instance;
|
||||
}
|
||||
|
||||
Object* objects_api::copy(Object* self, const Object* in) {
|
||||
if (self->type != in->type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hierarchy_copy(self, in, self->type);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
bool objects_api::compare(Object* first, Object* second) {
|
||||
if (first->type == second->type) {
|
||||
if (first->type->comparison) {
|
||||
return first->type->comparison(first, second);
|
||||
}
|
||||
|
||||
// raw data comparison
|
||||
return tp::memCompare(first, second, first->type->size) == 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Object* objects_api::instatiate(Object* in) {
|
||||
obj::Object* obj = NDO->create(in->type->name);
|
||||
NDO->copy(obj, in);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void objects_api::set(Object* self, tp::alni val) {
|
||||
if (self->type->convesions && self->type->convesions->from_int) {
|
||||
self->type->convesions->from_int(self, val);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void objects_api::set(Object* self, tp::alnf val) {
|
||||
if (self->type->convesions && self->type->convesions->from_float) {
|
||||
self->type->convesions->from_float(self, val);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void objects_api::set(Object* self, tp::String val) {
|
||||
if (self->type->convesions && self->type->convesions->from_string) {
|
||||
self->type->convesions->from_string(self, val);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
tp::alni objects_api::toInt(Object* self) {
|
||||
DEBUG_ASSERT(self->type->convesions && self->type->convesions->to_int);
|
||||
return self->type->convesions->to_int(self);
|
||||
}
|
||||
|
||||
tp::alnf objects_api::toFloat(Object* self) {
|
||||
DEBUG_ASSERT(self->type->convesions && self->type->convesions->to_float);
|
||||
return self->type->convesions->to_float(self);
|
||||
}
|
||||
|
||||
bool objects_api::toBool(Object* self) {
|
||||
if (self->type->convesions && self->type->convesions->to_int) {
|
||||
return (bool) self->type->convesions->to_int(self);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
tp::String objects_api::toString(Object* self) {
|
||||
DEBUG_ASSERT(self->type->convesions && self->type->convesions->to_string);
|
||||
return self->type->convesions->to_string(self);
|
||||
}
|
||||
|
||||
void objects_api::destroy(Object* in) {
|
||||
|
||||
if (!in) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
if (mh->refc > 1) {
|
||||
mh->refc--;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
NDO_CASTV(ClassObject, in, classobj);
|
||||
if (classobj) {
|
||||
auto idx = classobj->members->presents("__del__");
|
||||
DEBUG_ASSERT(idx);
|
||||
if (idx) {
|
||||
auto constructor_obj = classobj->members->getSlotVal(idx);
|
||||
NDO_CASTV(MethodObject, constructor_obj, constructor_method);
|
||||
if (constructor_method) {
|
||||
interp->execAll(constructor_method, classobj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const ObjectType* iter = in->type; iter; iter = iter->base) {
|
||||
if (iter->destructor) {
|
||||
iter->destructor(in);
|
||||
}
|
||||
}
|
||||
|
||||
ObjectMemDeallocate(in);
|
||||
}
|
||||
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
tp::halni objects_api::getrefc(Object* in) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
return (tp::halni) mh->refc;
|
||||
}
|
||||
|
||||
void objects_api::refinc(Object* in) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
mh->refc++;
|
||||
}
|
||||
|
||||
void objects_api::setrefc(Object* in, tp::halni refc) {
|
||||
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
|
||||
mh->refc = refc;
|
||||
}
|
||||
#endif
|
||||
|
||||
void hierarchy_copy(Object* self, const Object* in, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
hierarchy_copy(self, in, type->base);
|
||||
}
|
||||
|
||||
if (type->copy) {
|
||||
type->copy(self, in);
|
||||
}
|
||||
}
|
||||
|
||||
void hierarchy_construct(Object* self, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
hierarchy_construct(self, type->base);
|
||||
}
|
||||
|
||||
if (type->constructor) {
|
||||
type->constructor(self);
|
||||
}
|
||||
}
|
||||
|
||||
Object* ndo_cast(const Object* in, const ObjectType* to_type) {
|
||||
const ObjectType* typeiter = in->type;
|
||||
while (typeiter) {
|
||||
if (typeiter == to_type) {
|
||||
return (Object*) in;
|
||||
}
|
||||
typeiter = typeiter->base;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
};
|
||||
|
|
@ -1,444 +1,435 @@
|
|||
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "core/object.h"
|
||||
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
#include "HeapAllocatorGlobal.hpp"
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
namespace obj {
|
||||
|
||||
ObjectMemHead* bottom = nullptr;
|
||||
|
||||
struct ObjectsFileHeader {
|
||||
char name[10] = {0};
|
||||
char version[10] = {0};
|
||||
|
||||
ObjectsFileHeader(bool default_val = true) {
|
||||
if (default_val) {
|
||||
tp::memCopy(&name, "objects", tp::String::Logic::calcLength("objects") + 1);
|
||||
tp::memCopy(&version, "0", tp::String::Logic::calcLength("0") + 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Object* ObjectMemAllocate(const ObjectType* type) {
|
||||
ObjectMemHead* memh = (ObjectMemHead*) tp::HeapAllocGlobal::allocate(type->size + sizeof(ObjectMemHead));
|
||||
if (!memh) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memh->down = NULL;
|
||||
memh->flags = 0;
|
||||
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
memh->refc = (tp::alni) 1;
|
||||
#endif
|
||||
|
||||
if (bottom) {
|
||||
bottom->down = memh;
|
||||
}
|
||||
memh->up = bottom;
|
||||
bottom = memh;
|
||||
|
||||
NDO_FROM_MEMH(memh)->type = type;
|
||||
return NDO_FROM_MEMH(memh);
|
||||
}
|
||||
|
||||
void ObjectMemDeallocate(Object* in) {
|
||||
ObjectMemHead* memh = ((ObjectMemHead*) in) - 1;
|
||||
|
||||
if (memh->up) {
|
||||
memh->up->down = memh->down;
|
||||
}
|
||||
|
||||
if (memh->down) {
|
||||
memh->down->up = memh->up;
|
||||
} else {
|
||||
bottom = memh->up;
|
||||
}
|
||||
|
||||
tp::HeapAllocGlobal::deallocate(memh);
|
||||
}
|
||||
|
||||
void logTypeData(const ObjectType* type) {
|
||||
printf("type - %s\n", type->name);
|
||||
if (type->base) {
|
||||
printf("Based on ");
|
||||
logTypeData(type->base);
|
||||
}
|
||||
}
|
||||
|
||||
void assertNoLeaks() {
|
||||
if (bottom) {
|
||||
printf("ERROR : not all objects are destroyed\n");
|
||||
tp::ualni idx = 0;
|
||||
for (ObjectMemHead* memh = bottom; memh; memh = memh->up) {
|
||||
printf(" ===== Object - %i. Ref count - %i ===== \n", idx, memh->refc);
|
||||
logTypeData(NDO_FROM_MEMH(memh)->type);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct ObjectFileHead {
|
||||
Object* load_head_adress = 0;
|
||||
tp::halni refc = 0;
|
||||
};
|
||||
|
||||
tp::int1* loaded_file = nullptr;
|
||||
|
||||
|
||||
void objects_api::clear_object_flags() {
|
||||
// clear all object flags
|
||||
for (ObjectMemHead* iter = bottom; iter; iter = iter->up) {
|
||||
iter->flags = -1;
|
||||
}
|
||||
}
|
||||
|
||||
tp::alni& getObjectFlags(Object* in) {
|
||||
return NDO_MEMH_FROM_NDO(in)->flags;
|
||||
}
|
||||
|
||||
tp::alni objsize_ram_util(Object* self, const ObjectType* type) {
|
||||
tp::alni out = 0;
|
||||
|
||||
if (type->allocated_size) {
|
||||
out += type->allocated_size(self);
|
||||
} else {
|
||||
out += type->size - sizeof(ObjectType*);
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_ram_util(self, type->base);
|
||||
} else {
|
||||
out += sizeof(ObjectType*);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_ram(Object* self) {
|
||||
return objsize_ram_util(self, self->type);
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object) {
|
||||
tp::alni out = 0;
|
||||
|
||||
if (different_object) {
|
||||
if (getObjectFlags(self) == 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
getObjectFlags(self) = 1;
|
||||
}
|
||||
|
||||
if (type->allocated_size_recursive) {
|
||||
out += type->allocated_size_recursive(self);
|
||||
} else {
|
||||
if (type->allocated_size) {
|
||||
out += type->allocated_size(self);
|
||||
} else {
|
||||
out += type->size - sizeof(ObjectType*);
|
||||
}
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_ram_recursive_util(self, type->base, false);
|
||||
} else {
|
||||
out += sizeof(ObjectType*);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_ram_recursive(Object* self) {
|
||||
clear_object_flags();
|
||||
return objsize_ram_recursive_util(self, self->type);
|
||||
}
|
||||
|
||||
tp::alni objsize_file_util(Object* self, const ObjectType* type) {
|
||||
tp::alni out = 0;
|
||||
|
||||
if (type->save_size) {
|
||||
out += type->save_size(self);
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_file_util(self, type->base);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_file(Object* self) {
|
||||
return objsize_file_util(self, self->type);
|
||||
}
|
||||
|
||||
tp::alni objsize_file_recursive_util(Object* self, const ObjectType* type) {
|
||||
tp::alni out = 0;
|
||||
|
||||
getObjectFlags(self) = 1;
|
||||
|
||||
if (type->save_size) {
|
||||
out += type->save_size(self);
|
||||
}
|
||||
|
||||
if (type->childs_retrival) {
|
||||
tp::Buffer<Object*> childs = type->childs_retrival(self);
|
||||
for (auto child : childs) {
|
||||
if (getObjectFlags(child.data()) != 1) {
|
||||
out += objsize_file_recursive_util(child.data(), child.data()->type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_file_recursive_util(self, type->base);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_file_recursive(Object* self) {
|
||||
clear_object_flags();
|
||||
return objsize_file_recursive_util(self, self->type);
|
||||
}
|
||||
|
||||
void object_recursive_save(ArchiverOut& ndf, Object* self, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
object_recursive_save(ndf, self, type->base);
|
||||
}
|
||||
|
||||
// automatically offsets for parent type to write
|
||||
if (type->save) {
|
||||
type->save(self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
tp::alni objects_api::save(ArchiverOut& ndf, Object* in) {
|
||||
// if already saved return file_adress
|
||||
if (NDO_MEMH_FROM_NDO(in)->flags != -1) {
|
||||
return NDO_MEMH_FROM_NDO(in)->flags;
|
||||
}
|
||||
|
||||
// save write adress for parent save function call
|
||||
tp::alni tmp_adress = ndf.getAddress();
|
||||
|
||||
// save requested object to first available adress
|
||||
tp::alni save_adress = ndf.getFreeAddress();
|
||||
|
||||
// save file_adress in memhead
|
||||
NDO_MEMH_FROM_NDO(in)->flags = save_adress;
|
||||
|
||||
// update write adress
|
||||
ndf.setAddress(save_adress);
|
||||
|
||||
// save file object header
|
||||
ObjectFileHead ofh = { 0, getrefc(in) };
|
||||
ndf << ofh;
|
||||
ndf << tp::String(in->type->name);
|
||||
|
||||
// allocate for object file header
|
||||
ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + tp::SaveSizeCounter::calc(tp::String(in->type->name)));
|
||||
|
||||
// calc max size needed for saving all hierarchy of types
|
||||
tp::alni file_alloc_size = objsize_file_util(in, in->type);
|
||||
|
||||
// offes first available adress
|
||||
ndf.setFreeAddress(ndf.getFreeAddress() + file_alloc_size);
|
||||
|
||||
object_recursive_save(ndf, in, in->type);
|
||||
|
||||
// restore adress for parent save function
|
||||
ndf.setAddress(tmp_adress);
|
||||
|
||||
// return addres of saved object in file space
|
||||
return save_adress;
|
||||
}
|
||||
|
||||
void object_recursive_load(ArchiverIn& ndf, Object* out, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
object_recursive_load(ndf, out, type->base);
|
||||
}
|
||||
|
||||
// automatically offsets for parent type to read
|
||||
if (type->load) {
|
||||
type->load(ndf, out);
|
||||
}
|
||||
}
|
||||
|
||||
Object* objects_api::load(ArchiverIn& ndf, tp::alni file_adress) {
|
||||
// check if already saved
|
||||
if (((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress) {
|
||||
return ((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress;
|
||||
}
|
||||
|
||||
// save read address
|
||||
tp::alni parent_file_adress = ndf.getAddress();
|
||||
|
||||
// set read address
|
||||
ndf.setAddress(file_adress);
|
||||
|
||||
ObjectFileHead ofh;
|
||||
ndf >> ofh;
|
||||
|
||||
tp::String type_name;
|
||||
ndf >> type_name;
|
||||
|
||||
const ObjectType* object_type = NDO->types.get(type_name);
|
||||
Object* out = ObjectMemAllocate(object_type);
|
||||
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
setrefc(out, 0);
|
||||
|
||||
// check for null object
|
||||
if (out->type == &NullObject::TypeData) {
|
||||
ObjectMemDeallocate(out);
|
||||
out = NdoNull_globalInstance;
|
||||
}
|
||||
|
||||
// save heap adress in "loaded_file"
|
||||
((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress = out;
|
||||
|
||||
// loads recursively
|
||||
object_recursive_load(ndf, out, object_type);
|
||||
|
||||
// restore read address for parent call to continue
|
||||
ndf.setAddress(parent_file_adress);
|
||||
|
||||
// return heap memory adress
|
||||
return out;
|
||||
}
|
||||
|
||||
bool objects_api::save(Object* in, tp::String path, bool compressed) {
|
||||
ArchiverOut ndf(path.read());
|
||||
|
||||
if (!ndf.isOpened()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
clear_object_flags();
|
||||
|
||||
// save version info
|
||||
ObjectsFileHeader header;
|
||||
ndf << header;
|
||||
|
||||
ndf.setFreeAddress(ndf.getAddress());
|
||||
|
||||
// pre allocate
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i]) {
|
||||
DEBUG_ASSERT(sl_callbacks[i]->size);
|
||||
ndf.setFreeAddress(ndf.getFreeAddress() + sl_callbacks[i]->size(sl_callbacks[i]->self, ndf));
|
||||
}
|
||||
}
|
||||
|
||||
// pre-save
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->pre_save) {
|
||||
sl_callbacks[i]->pre_save(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
save(ndf, in);
|
||||
|
||||
// post-save
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->post_save) {
|
||||
sl_callbacks[i]->post_save(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO : add compression
|
||||
/*
|
||||
if (compressed) {
|
||||
auto temp = path + ".bz2";
|
||||
tp::compressF2F(path.read(), temp.cstr());
|
||||
|
||||
File::remove(path.read());
|
||||
File::rename(temp.cstr(), path.read());
|
||||
}
|
||||
*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Object* objects_api::load(tp::String path) {
|
||||
/*
|
||||
auto temp_file_name = path + ".unz";
|
||||
bool unz_res = tp::decompressF2F(path.cstr(), temp_file_name.cstr());
|
||||
|
||||
if (!unz_res) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
File ndf(temp_file_name.cstr(), tp::osfile_openflags::LOAD);
|
||||
*/
|
||||
|
||||
ArchiverIn ndf(path.read());
|
||||
|
||||
if (!ndf.isOpened()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check for compatibility
|
||||
ObjectsFileHeader current_header;
|
||||
ObjectsFileHeader loaded_header(false);
|
||||
|
||||
ndf >> loaded_header;
|
||||
|
||||
if (!tp::memEqual(¤t_header, &loaded_header, sizeof(ObjectsFileHeader))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ndf.setAddress(0);
|
||||
|
||||
const auto fileSize = ndf.getSize();
|
||||
loaded_file = (tp::int1*) malloc(fileSize);
|
||||
tp::memSetVal(loaded_file, fileSize, 0);
|
||||
ndf.readBytes(loaded_file, fileSize);
|
||||
|
||||
ndf.setAddress(sizeof(ObjectsFileHeader));
|
||||
|
||||
// preload
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->pre_load) {
|
||||
sl_callbacks[i]->pre_load(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
Object* out = load(ndf, ndf.getAddress());
|
||||
|
||||
// post
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->post_save) {
|
||||
sl_callbacks[i]->post_load(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
free(loaded_file);
|
||||
|
||||
/*
|
||||
ndf.close();
|
||||
|
||||
if (unz_res == NULL) {
|
||||
File::remove(temp_file_name.cstr());
|
||||
}
|
||||
*/
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void objects_api::add_sl_callbacks(save_load_callbacks* in) {
|
||||
sl_callbacks[sl_callbacks_load_idx] = in;
|
||||
sl_callbacks_load_idx++;
|
||||
}
|
||||
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "core/object.h"
|
||||
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
#include "HeapAllocatorGlobal.hpp"
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
namespace obj {
|
||||
|
||||
ObjectMemHead* bottom = nullptr;
|
||||
|
||||
struct ObjectsFileHeader {
|
||||
char name[10] = { 0 };
|
||||
char version[10] = { 0 };
|
||||
|
||||
ObjectsFileHeader(bool default_val = true) {
|
||||
if (default_val) {
|
||||
tp::memCopy(&name, "objects", tp::String::Logic::calcLength("objects") + 1);
|
||||
tp::memCopy(&version, "0", tp::String::Logic::calcLength("0") + 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Object* ObjectMemAllocate(const ObjectType* type) {
|
||||
ObjectMemHead* memh = (ObjectMemHead*) tp::HeapAllocGlobal::allocate(type->size + sizeof(ObjectMemHead));
|
||||
if (!memh) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memh->down = NULL;
|
||||
memh->flags = 0;
|
||||
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
memh->refc = (tp::alni) 1;
|
||||
#endif
|
||||
|
||||
if (bottom) {
|
||||
bottom->down = memh;
|
||||
}
|
||||
memh->up = bottom;
|
||||
bottom = memh;
|
||||
|
||||
NDO_FROM_MEMH(memh)->type = type;
|
||||
return NDO_FROM_MEMH(memh);
|
||||
}
|
||||
|
||||
void ObjectMemDeallocate(Object* in) {
|
||||
ObjectMemHead* memh = ((ObjectMemHead*) in) - 1;
|
||||
|
||||
if (memh->up) {
|
||||
memh->up->down = memh->down;
|
||||
}
|
||||
|
||||
if (memh->down) {
|
||||
memh->down->up = memh->up;
|
||||
} else {
|
||||
bottom = memh->up;
|
||||
}
|
||||
|
||||
tp::HeapAllocGlobal::deallocate(memh);
|
||||
}
|
||||
|
||||
void logTypeData(const ObjectType* type) {
|
||||
printf("type - %s\n", type->name);
|
||||
if (type->base) {
|
||||
printf("Based on ");
|
||||
logTypeData(type->base);
|
||||
}
|
||||
}
|
||||
|
||||
void assertNoLeaks() {
|
||||
if (bottom) {
|
||||
printf("ERROR : not all objects are destroyed\n");
|
||||
tp::ualni idx = 0;
|
||||
for (ObjectMemHead* memh = bottom; memh; memh = memh->up) {
|
||||
printf(" ===== Object - %i. Ref count - %i ===== \n", idx, memh->refc);
|
||||
logTypeData(NDO_FROM_MEMH(memh)->type);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ObjectFileHead {
|
||||
Object* load_head_adress = 0;
|
||||
tp::halni refc = 0;
|
||||
};
|
||||
|
||||
tp::int1* loaded_file = nullptr;
|
||||
|
||||
void objects_api::clear_object_flags() {
|
||||
// clear all object flags
|
||||
for (ObjectMemHead* iter = bottom; iter; iter = iter->up) {
|
||||
iter->flags = -1;
|
||||
}
|
||||
}
|
||||
|
||||
tp::alni& getObjectFlags(Object* in) { return NDO_MEMH_FROM_NDO(in)->flags; }
|
||||
|
||||
tp::alni objsize_ram_util(Object* self, const ObjectType* type) {
|
||||
tp::alni out = 0;
|
||||
|
||||
if (type->allocated_size) {
|
||||
out += type->allocated_size(self);
|
||||
} else {
|
||||
out += type->size - sizeof(ObjectType*);
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_ram_util(self, type->base);
|
||||
} else {
|
||||
out += sizeof(ObjectType*);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_ram(Object* self) { return objsize_ram_util(self, self->type); }
|
||||
|
||||
tp::alni objects_api::objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object) {
|
||||
tp::alni out = 0;
|
||||
|
||||
if (different_object) {
|
||||
if (getObjectFlags(self) == 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
getObjectFlags(self) = 1;
|
||||
}
|
||||
|
||||
if (type->allocated_size_recursive) {
|
||||
out += type->allocated_size_recursive(self);
|
||||
} else {
|
||||
if (type->allocated_size) {
|
||||
out += type->allocated_size(self);
|
||||
} else {
|
||||
out += type->size - sizeof(ObjectType*);
|
||||
}
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_ram_recursive_util(self, type->base, false);
|
||||
} else {
|
||||
out += sizeof(ObjectType*);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_ram_recursive(Object* self) {
|
||||
clear_object_flags();
|
||||
return objsize_ram_recursive_util(self, self->type);
|
||||
}
|
||||
|
||||
tp::alni objsize_file_util(Object* self, const ObjectType* type) {
|
||||
tp::alni out = 0;
|
||||
|
||||
if (type->save_size) {
|
||||
out += type->save_size(self);
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_file_util(self, type->base);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_file(Object* self) { return objsize_file_util(self, self->type); }
|
||||
|
||||
tp::alni objsize_file_recursive_util(Object* self, const ObjectType* type) {
|
||||
tp::alni out = 0;
|
||||
|
||||
getObjectFlags(self) = 1;
|
||||
|
||||
if (type->save_size) {
|
||||
out += type->save_size(self);
|
||||
}
|
||||
|
||||
if (type->childs_retrival) {
|
||||
tp::Buffer<Object*> childs = type->childs_retrival(self);
|
||||
for (auto child : childs) {
|
||||
if (getObjectFlags(child.data()) != 1) {
|
||||
out += objsize_file_recursive_util(child.data(), child.data()->type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type->base) {
|
||||
out += objsize_file_recursive_util(self, type->base);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
tp::alni objects_api::objsize_file_recursive(Object* self) {
|
||||
clear_object_flags();
|
||||
return objsize_file_recursive_util(self, self->type);
|
||||
}
|
||||
|
||||
void object_recursive_save(ArchiverOut& ndf, Object* self, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
object_recursive_save(ndf, self, type->base);
|
||||
}
|
||||
|
||||
// automatically offsets for parent type to write
|
||||
if (type->save) {
|
||||
type->save(self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
tp::alni objects_api::save(ArchiverOut& ndf, Object* in) {
|
||||
// if already saved return file_adress
|
||||
if (NDO_MEMH_FROM_NDO(in)->flags != -1) {
|
||||
return NDO_MEMH_FROM_NDO(in)->flags;
|
||||
}
|
||||
|
||||
// save write adress for parent save function call
|
||||
tp::alni tmp_adress = ndf.getAddress();
|
||||
|
||||
// save requested object to first available adress
|
||||
tp::alni save_adress = ndf.getFreeAddress();
|
||||
|
||||
// save file_adress in memhead
|
||||
NDO_MEMH_FROM_NDO(in)->flags = save_adress;
|
||||
|
||||
// update write adress
|
||||
ndf.setAddress(save_adress);
|
||||
|
||||
// save file object header
|
||||
ObjectFileHead ofh = { 0, getrefc(in) };
|
||||
ndf << ofh;
|
||||
ndf << tp::String(in->type->name);
|
||||
|
||||
// allocate for object file header
|
||||
ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + tp::SaveSizeCounter::calc(tp::String(in->type->name)));
|
||||
|
||||
// calc max size needed for saving all hierarchy of types
|
||||
tp::alni file_alloc_size = objsize_file_util(in, in->type);
|
||||
|
||||
// offes first available adress
|
||||
ndf.setFreeAddress(ndf.getFreeAddress() + file_alloc_size);
|
||||
|
||||
object_recursive_save(ndf, in, in->type);
|
||||
|
||||
// restore adress for parent save function
|
||||
ndf.setAddress(tmp_adress);
|
||||
|
||||
// return addres of saved object in file space
|
||||
return save_adress;
|
||||
}
|
||||
|
||||
void object_recursive_load(ArchiverIn& ndf, Object* out, const ObjectType* type) {
|
||||
if (type->base) {
|
||||
object_recursive_load(ndf, out, type->base);
|
||||
}
|
||||
|
||||
// automatically offsets for parent type to read
|
||||
if (type->load) {
|
||||
type->load(ndf, out);
|
||||
}
|
||||
}
|
||||
|
||||
Object* objects_api::load(ArchiverIn& ndf, tp::alni file_adress) {
|
||||
// check if already saved
|
||||
if (((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress) {
|
||||
return ((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress;
|
||||
}
|
||||
|
||||
// save read address
|
||||
tp::alni parent_file_adress = ndf.getAddress();
|
||||
|
||||
// set read address
|
||||
ndf.setAddress(file_adress);
|
||||
|
||||
ObjectFileHead ofh;
|
||||
ndf >> ofh;
|
||||
|
||||
tp::String type_name;
|
||||
ndf >> type_name;
|
||||
|
||||
const ObjectType* object_type = NDO->types.get(type_name);
|
||||
Object* out = ObjectMemAllocate(object_type);
|
||||
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
setrefc(out, 0);
|
||||
|
||||
// check for null object
|
||||
if (out->type == &NullObject::TypeData) {
|
||||
ObjectMemDeallocate(out);
|
||||
out = NdoNull_globalInstance;
|
||||
}
|
||||
|
||||
// save heap adress in "loaded_file"
|
||||
((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress = out;
|
||||
|
||||
// loads recursively
|
||||
object_recursive_load(ndf, out, object_type);
|
||||
|
||||
// restore read address for parent call to continue
|
||||
ndf.setAddress(parent_file_adress);
|
||||
|
||||
// return heap memory adress
|
||||
return out;
|
||||
}
|
||||
|
||||
bool objects_api::save(Object* in, tp::String path, bool compressed) {
|
||||
ArchiverOut ndf(path.read());
|
||||
|
||||
if (!ndf.isOpened()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
clear_object_flags();
|
||||
|
||||
// save version info
|
||||
ObjectsFileHeader header;
|
||||
ndf << header;
|
||||
|
||||
ndf.setFreeAddress(ndf.getAddress());
|
||||
|
||||
// pre allocate
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i]) {
|
||||
DEBUG_ASSERT(sl_callbacks[i]->size);
|
||||
ndf.setFreeAddress(ndf.getFreeAddress() + sl_callbacks[i]->size(sl_callbacks[i]->self, ndf));
|
||||
}
|
||||
}
|
||||
|
||||
// pre-save
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->pre_save) {
|
||||
sl_callbacks[i]->pre_save(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
save(ndf, in);
|
||||
|
||||
// post-save
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->post_save) {
|
||||
sl_callbacks[i]->post_save(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO : add compression
|
||||
/*
|
||||
if (compressed) {
|
||||
auto temp = path + ".bz2";
|
||||
tp::compressF2F(path.read(), temp.cstr());
|
||||
|
||||
File::remove(path.read());
|
||||
File::rename(temp.cstr(), path.read());
|
||||
}
|
||||
*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Object* objects_api::load(tp::String path) {
|
||||
/*
|
||||
auto temp_file_name = path + ".unz";
|
||||
bool unz_res = tp::decompressF2F(path.cstr(), temp_file_name.cstr());
|
||||
|
||||
if (!unz_res) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
File ndf(temp_file_name.cstr(), tp::osfile_openflags::LOAD);
|
||||
*/
|
||||
|
||||
ArchiverIn ndf(path.read());
|
||||
|
||||
if (!ndf.isOpened()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check for compatibility
|
||||
ObjectsFileHeader current_header;
|
||||
ObjectsFileHeader loaded_header(false);
|
||||
|
||||
ndf >> loaded_header;
|
||||
|
||||
if (!tp::memEqual(¤t_header, &loaded_header, sizeof(ObjectsFileHeader))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ndf.setAddress(0);
|
||||
|
||||
const auto fileSize = ndf.getSize();
|
||||
loaded_file = (tp::int1*) malloc(fileSize);
|
||||
tp::memSetVal(loaded_file, fileSize, 0);
|
||||
ndf.readBytes(loaded_file, fileSize);
|
||||
|
||||
ndf.setAddress(sizeof(ObjectsFileHeader));
|
||||
|
||||
// preload
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->pre_load) {
|
||||
sl_callbacks[i]->pre_load(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
Object* out = load(ndf, ndf.getAddress());
|
||||
|
||||
// post
|
||||
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
|
||||
if (sl_callbacks[i] && sl_callbacks[i]->post_save) {
|
||||
sl_callbacks[i]->post_load(sl_callbacks[i]->self, ndf);
|
||||
}
|
||||
}
|
||||
|
||||
free(loaded_file);
|
||||
|
||||
/*
|
||||
ndf.close();
|
||||
|
||||
if (unz_res == NULL) {
|
||||
File::remove(temp_file_name.cstr());
|
||||
}
|
||||
*/
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void objects_api::add_sl_callbacks(save_load_callbacks* in) {
|
||||
sl_callbacks[sl_callbacks_load_idx] = in;
|
||||
sl_callbacks_load_idx++;
|
||||
}
|
||||
};
|
||||
|
|
@ -1,268 +1,255 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "HeapAllocatorGlobal.hpp"
|
||||
|
||||
#include "core/scriptsection.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
ScriptSection* gScriptSection = NULL;
|
||||
|
||||
struct script_data_head {
|
||||
tp::alni refc = 0;
|
||||
tp::alni store_adress = 0;
|
||||
};
|
||||
|
||||
void set_script_head_store_adress(Script* in, tp::alni address) {
|
||||
((script_data_head*)in - 1)->store_adress = address;
|
||||
}
|
||||
|
||||
tp::alni get_script_head_store_adress(Script* in) {
|
||||
return ((script_data_head*)in - 1)->store_adress;
|
||||
}
|
||||
|
||||
Script* ScriptSection::createScript() {
|
||||
auto sdhead = (script_data_head*)tp::HeapAllocGlobal::allocate(sizeof(script_data_head) + sizeof(Script));
|
||||
auto out = (Script*)(sdhead + 1);
|
||||
|
||||
if (!sdhead) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sdhead->refc = 1;
|
||||
sdhead->store_adress = -1;
|
||||
|
||||
mScripts.pushBack(out);
|
||||
|
||||
new (out) Script();
|
||||
out->mReadable = obj::StringObject::create("");
|
||||
obj::NDO->refinc(out->mReadable);
|
||||
return out;
|
||||
}
|
||||
|
||||
void ScriptSection::delete_script(Script* script) {
|
||||
script->~Script();
|
||||
obj::NDO->destroy(script->mReadable);
|
||||
|
||||
tp::HeapAllocGlobal::deallocate((((script_data_head*)script) - 1));
|
||||
}
|
||||
|
||||
void ScriptSection::reference_script(Script* script) {
|
||||
(((script_data_head*)script) - 1)->refc++;
|
||||
}
|
||||
|
||||
void ScriptSection::abandonScript(Script* script) {
|
||||
if ((((script_data_head*)script) - 1)->refc > 1) {
|
||||
(((script_data_head*)script) - 1)->refc--;
|
||||
}
|
||||
else {
|
||||
// ISSUE : on delete list structure is outdated
|
||||
// FIXME : use pool alloc with ref count
|
||||
// forwarding to global destruction
|
||||
//delete_script(script);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptSection::changeScript(Script** current_script, Script** new_script) {
|
||||
abandonScript(*current_script);
|
||||
reference_script(*new_script);
|
||||
|
||||
*current_script = *new_script;
|
||||
}
|
||||
|
||||
tp::alni ScriptSection::get_script_file_adress(Script* in) {
|
||||
return get_script_head_store_adress(in);
|
||||
}
|
||||
|
||||
tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file) {
|
||||
tp::alni size = 0;
|
||||
size += 5; // header
|
||||
size += sizeof(tp::alni); // scripts length
|
||||
|
||||
for (auto iter : self->mScripts) {
|
||||
|
||||
set_script_head_store_adress(iter.data(), file.getAddress() + size);
|
||||
|
||||
size += sizeof(tp::alni); // scripts string obj ref
|
||||
size += sizeof(tp::alni); // constants length
|
||||
|
||||
for (auto const_obj : iter->mBytecode.mConstants) {
|
||||
size += sizeof(tp::alni); // constant object addres
|
||||
}
|
||||
|
||||
// instructions
|
||||
size += tp::SaveSizeCounter::calc(iter->mBytecode.mInstructions);
|
||||
}
|
||||
|
||||
size += sizeof(tp::alni); // objects mem offset
|
||||
size += 5; // header
|
||||
return size;
|
||||
}
|
||||
|
||||
void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut& file) {
|
||||
// header
|
||||
file.writeBytes("/scr/", 5);
|
||||
|
||||
// scripts length
|
||||
tp::alni scripts_count = self->mScripts.length();
|
||||
file << scripts_count;
|
||||
|
||||
for (auto iter : self->mScripts) {
|
||||
|
||||
// scripts string obj ref
|
||||
auto obj_addres = obj::NDO->save(file, iter->mReadable);
|
||||
file << obj_addres;
|
||||
|
||||
// constants length
|
||||
tp::alni consts_count = iter->mBytecode.mConstants.size();
|
||||
file << consts_count;
|
||||
|
||||
for (auto const_obj : iter->mBytecode.mConstants) {
|
||||
// constant object addres
|
||||
auto obj_addres = obj::NDO->save(file, const_obj.data());
|
||||
file << obj_addres;
|
||||
}
|
||||
|
||||
// mInstructions
|
||||
file << iter->mBytecode.mInstructions;
|
||||
}
|
||||
|
||||
// header
|
||||
file.writeBytes("/scr/", 5);
|
||||
|
||||
tp::alni objects_mem_offset = file.getFreeAddress();
|
||||
file << objects_mem_offset;
|
||||
}
|
||||
|
||||
void load_constants(ScriptSection* self, ArchiverIn& file, tp::alni start_addr) {
|
||||
auto addres = file.getAddress();
|
||||
|
||||
file.setAddress(start_addr);
|
||||
file.setAddress( start_addr + 5); // header
|
||||
|
||||
tp::alni scripts_count;
|
||||
file >> scripts_count;
|
||||
|
||||
for (tp::alni i = 0; i < scripts_count; i++) {
|
||||
|
||||
auto script = self->get_scritp_from_file_adress(file.getAddress());
|
||||
|
||||
// script text
|
||||
tp::alni str_addr;
|
||||
file >> str_addr;
|
||||
|
||||
NDO->destroy(script->mReadable); // we already have string object in the script when creating script
|
||||
script->mReadable = NDO_CAST(obj::StringObject, obj::NDO->load(file, str_addr));
|
||||
|
||||
file.setAddress(file.getAddress() + sizeof(tp::alni)); // constants length
|
||||
|
||||
for (auto const_obj : script->mBytecode.mConstants) {
|
||||
tp::alni consts_addr;
|
||||
file >> consts_addr;
|
||||
const_obj.data() = obj::NDO->load(file, consts_addr);
|
||||
}
|
||||
|
||||
// instructions
|
||||
file.setAddress(file.getAddress() + tp::SaveSizeCounter::calc(script->mBytecode.mInstructions));
|
||||
}
|
||||
|
||||
file.setAddress(addres);
|
||||
}
|
||||
|
||||
void ScriptSection::load_script_table_from_file(ScriptSection* self, ArchiverIn& file) {
|
||||
for (auto iter : self->mScripts) {
|
||||
set_script_head_store_adress(iter.data(), -1);
|
||||
}
|
||||
|
||||
auto section_start_addr = file.getAddress();
|
||||
|
||||
// header
|
||||
file.setAddress(file.getAddress() + 5);
|
||||
|
||||
// scripts length
|
||||
tp::alni scripts_count;
|
||||
file >> scripts_count;
|
||||
|
||||
for (tp::alni i = 0; i < scripts_count; i++) {
|
||||
|
||||
auto new_script = self->createScript();
|
||||
set_script_head_store_adress(new_script, file.getAddress());
|
||||
|
||||
// scripts text
|
||||
file.setAddress(file.getAddress() + sizeof(tp::alni));
|
||||
|
||||
// constants length
|
||||
tp::alni consts_count;
|
||||
file >> consts_count;
|
||||
|
||||
new_script->mBytecode.mConstants.reserve(consts_count);
|
||||
|
||||
for (tp::alni j = 0; j < consts_count; j++) {
|
||||
// constant object addres
|
||||
tp::alni consts_addr;
|
||||
file >> consts_addr;
|
||||
|
||||
// skiping
|
||||
//new_script->mBytecode.mConstants[j] = obj::NDO->load(file, consts_addr);
|
||||
}
|
||||
|
||||
// mInstructions
|
||||
file >> new_script->mBytecode.mInstructions;
|
||||
}
|
||||
|
||||
load_constants(self, file, section_start_addr);
|
||||
|
||||
// header
|
||||
file.setAddress(file.getAddress() + 5);
|
||||
|
||||
tp::alni objects_mem_offset;
|
||||
file >> objects_mem_offset;
|
||||
|
||||
file.setAddress(objects_mem_offset);
|
||||
}
|
||||
|
||||
Script* ScriptSection::get_scritp_from_file_adress(tp::alni file_adress) {
|
||||
for (auto iter : mScripts) {
|
||||
if (get_script_head_store_adress(iter.data()) == file_adress) {
|
||||
return iter.data();
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ScriptSection::~ScriptSection() {
|
||||
for (auto iter : mScripts) {
|
||||
delete_script(iter.data());
|
||||
}
|
||||
}
|
||||
|
||||
obj::save_load_callbacks ScriptSection::slcb_script_section = {
|
||||
.self = gScriptSection,
|
||||
.pre_save = (obj::pre_save_callback*)ScriptSection::save_script_table_to_file,
|
||||
.size = (obj::slcb_size_callback*)ScriptSection::save_script_table_to_file_size,
|
||||
.pre_load = (obj::pre_load_callback*)ScriptSection::load_script_table_from_file,
|
||||
.post_save = NULL,
|
||||
.post_load = NULL,
|
||||
};
|
||||
|
||||
|
||||
void ScriptSection::initialize() {
|
||||
ASSERT(!gScriptSection);
|
||||
gScriptSection = new ScriptSection();
|
||||
|
||||
ASSERT(obj::NDO && "Forgot to initialize objects?");
|
||||
|
||||
slcb_script_section.self = gScriptSection;
|
||||
obj::NDO->add_sl_callbacks(&slcb_script_section);
|
||||
}
|
||||
|
||||
void ScriptSection::uninitialize() {
|
||||
ASSERT(gScriptSection);
|
||||
delete gScriptSection;
|
||||
gScriptSection = nullptr;
|
||||
}
|
||||
|
||||
ScriptSection* ScriptSection::globalHandle() {
|
||||
return gScriptSection;
|
||||
}
|
||||
|
||||
|
||||
#include "HeapAllocatorGlobal.hpp"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "core/scriptsection.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
ScriptSection* gScriptSection = NULL;
|
||||
|
||||
struct script_data_head {
|
||||
tp::alni refc = 0;
|
||||
tp::alni store_adress = 0;
|
||||
};
|
||||
|
||||
void set_script_head_store_adress(Script* in, tp::alni address) { ((script_data_head*) in - 1)->store_adress = address; }
|
||||
|
||||
tp::alni get_script_head_store_adress(Script* in) { return ((script_data_head*) in - 1)->store_adress; }
|
||||
|
||||
Script* ScriptSection::createScript() {
|
||||
auto sdhead = (script_data_head*) tp::HeapAllocGlobal::allocate(sizeof(script_data_head) + sizeof(Script));
|
||||
auto out = (Script*) (sdhead + 1);
|
||||
|
||||
if (!sdhead) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sdhead->refc = 1;
|
||||
sdhead->store_adress = -1;
|
||||
|
||||
mScripts.pushBack(out);
|
||||
|
||||
new (out) Script();
|
||||
out->mReadable = obj::StringObject::create("");
|
||||
obj::NDO->refinc(out->mReadable);
|
||||
return out;
|
||||
}
|
||||
|
||||
void ScriptSection::delete_script(Script* script) {
|
||||
script->~Script();
|
||||
obj::NDO->destroy(script->mReadable);
|
||||
|
||||
tp::HeapAllocGlobal::deallocate((((script_data_head*) script) - 1));
|
||||
}
|
||||
|
||||
void ScriptSection::reference_script(Script* script) { (((script_data_head*) script) - 1)->refc++; }
|
||||
|
||||
void ScriptSection::abandonScript(Script* script) {
|
||||
if ((((script_data_head*) script) - 1)->refc > 1) {
|
||||
(((script_data_head*) script) - 1)->refc--;
|
||||
} else {
|
||||
// ISSUE : on delete list structure is outdated
|
||||
// FIXME : use pool alloc with ref count
|
||||
// forwarding to global destruction
|
||||
// delete_script(script);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptSection::changeScript(Script** current_script, Script** new_script) {
|
||||
abandonScript(*current_script);
|
||||
reference_script(*new_script);
|
||||
|
||||
*current_script = *new_script;
|
||||
}
|
||||
|
||||
tp::alni ScriptSection::get_script_file_adress(Script* in) { return get_script_head_store_adress(in); }
|
||||
|
||||
tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file) {
|
||||
tp::alni size = 0;
|
||||
size += 5; // header
|
||||
size += sizeof(tp::alni); // scripts length
|
||||
|
||||
for (auto iter : self->mScripts) {
|
||||
|
||||
set_script_head_store_adress(iter.data(), file.getAddress() + size);
|
||||
|
||||
size += sizeof(tp::alni); // scripts string obj ref
|
||||
size += sizeof(tp::alni); // constants length
|
||||
|
||||
for (auto const_obj : iter->mBytecode.mConstants) {
|
||||
size += sizeof(tp::alni); // constant object addres
|
||||
}
|
||||
|
||||
// instructions
|
||||
size += tp::SaveSizeCounter::calc(iter->mBytecode.mInstructions);
|
||||
}
|
||||
|
||||
size += sizeof(tp::alni); // objects mem offset
|
||||
size += 5; // header
|
||||
return size;
|
||||
}
|
||||
|
||||
void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut& file) {
|
||||
// header
|
||||
file.writeBytes("/scr/", 5);
|
||||
|
||||
// scripts length
|
||||
tp::alni scripts_count = self->mScripts.length();
|
||||
file << scripts_count;
|
||||
|
||||
for (auto iter : self->mScripts) {
|
||||
|
||||
// scripts string obj ref
|
||||
auto obj_addres = obj::NDO->save(file, iter->mReadable);
|
||||
file << obj_addres;
|
||||
|
||||
// constants length
|
||||
tp::alni consts_count = iter->mBytecode.mConstants.size();
|
||||
file << consts_count;
|
||||
|
||||
for (auto const_obj : iter->mBytecode.mConstants) {
|
||||
// constant object addres
|
||||
auto obj_addres = obj::NDO->save(file, const_obj.data());
|
||||
file << obj_addres;
|
||||
}
|
||||
|
||||
// mInstructions
|
||||
file << iter->mBytecode.mInstructions;
|
||||
}
|
||||
|
||||
// header
|
||||
file.writeBytes("/scr/", 5);
|
||||
|
||||
tp::alni objects_mem_offset = file.getFreeAddress();
|
||||
file << objects_mem_offset;
|
||||
}
|
||||
|
||||
void load_constants(ScriptSection* self, ArchiverIn& file, tp::alni start_addr) {
|
||||
auto addres = file.getAddress();
|
||||
|
||||
file.setAddress(start_addr);
|
||||
file.setAddress(start_addr + 5); // header
|
||||
|
||||
tp::alni scripts_count;
|
||||
file >> scripts_count;
|
||||
|
||||
for (tp::alni i = 0; i < scripts_count; i++) {
|
||||
|
||||
auto script = self->get_scritp_from_file_adress(file.getAddress());
|
||||
|
||||
// script text
|
||||
tp::alni str_addr;
|
||||
file >> str_addr;
|
||||
|
||||
NDO->destroy(script->mReadable); // we already have string object in the script when creating script
|
||||
script->mReadable = NDO_CAST(obj::StringObject, obj::NDO->load(file, str_addr));
|
||||
|
||||
file.setAddress(file.getAddress() + sizeof(tp::alni)); // constants length
|
||||
|
||||
for (auto const_obj : script->mBytecode.mConstants) {
|
||||
tp::alni consts_addr;
|
||||
file >> consts_addr;
|
||||
const_obj.data() = obj::NDO->load(file, consts_addr);
|
||||
}
|
||||
|
||||
// instructions
|
||||
file.setAddress(file.getAddress() + tp::SaveSizeCounter::calc(script->mBytecode.mInstructions));
|
||||
}
|
||||
|
||||
file.setAddress(addres);
|
||||
}
|
||||
|
||||
void ScriptSection::load_script_table_from_file(ScriptSection* self, ArchiverIn& file) {
|
||||
for (auto iter : self->mScripts) {
|
||||
set_script_head_store_adress(iter.data(), -1);
|
||||
}
|
||||
|
||||
auto section_start_addr = file.getAddress();
|
||||
|
||||
// header
|
||||
file.setAddress(file.getAddress() + 5);
|
||||
|
||||
// scripts length
|
||||
tp::alni scripts_count;
|
||||
file >> scripts_count;
|
||||
|
||||
for (tp::alni i = 0; i < scripts_count; i++) {
|
||||
|
||||
auto new_script = self->createScript();
|
||||
set_script_head_store_adress(new_script, file.getAddress());
|
||||
|
||||
// scripts text
|
||||
file.setAddress(file.getAddress() + sizeof(tp::alni));
|
||||
|
||||
// constants length
|
||||
tp::alni consts_count;
|
||||
file >> consts_count;
|
||||
|
||||
new_script->mBytecode.mConstants.reserve(consts_count);
|
||||
|
||||
for (tp::alni j = 0; j < consts_count; j++) {
|
||||
// constant object addres
|
||||
tp::alni consts_addr;
|
||||
file >> consts_addr;
|
||||
|
||||
// skiping
|
||||
// new_script->mBytecode.mConstants[j] = obj::NDO->load(file, consts_addr);
|
||||
}
|
||||
|
||||
// mInstructions
|
||||
file >> new_script->mBytecode.mInstructions;
|
||||
}
|
||||
|
||||
load_constants(self, file, section_start_addr);
|
||||
|
||||
// header
|
||||
file.setAddress(file.getAddress() + 5);
|
||||
|
||||
tp::alni objects_mem_offset;
|
||||
file >> objects_mem_offset;
|
||||
|
||||
file.setAddress(objects_mem_offset);
|
||||
}
|
||||
|
||||
Script* ScriptSection::get_scritp_from_file_adress(tp::alni file_adress) {
|
||||
for (auto iter : mScripts) {
|
||||
if (get_script_head_store_adress(iter.data()) == file_adress) {
|
||||
return iter.data();
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ScriptSection::~ScriptSection() {
|
||||
for (auto iter : mScripts) {
|
||||
delete_script(iter.data());
|
||||
}
|
||||
}
|
||||
|
||||
obj::save_load_callbacks ScriptSection::slcb_script_section = {
|
||||
.self = gScriptSection,
|
||||
.pre_save = (obj::pre_save_callback*) ScriptSection::save_script_table_to_file,
|
||||
.size = (obj::slcb_size_callback*) ScriptSection::save_script_table_to_file_size,
|
||||
.pre_load = (obj::pre_load_callback*) ScriptSection::load_script_table_from_file,
|
||||
.post_save = NULL,
|
||||
.post_load = NULL,
|
||||
};
|
||||
|
||||
void ScriptSection::initialize() {
|
||||
ASSERT(!gScriptSection);
|
||||
gScriptSection = new ScriptSection();
|
||||
|
||||
ASSERT(obj::NDO && "Forgot to initialize objects?");
|
||||
|
||||
slcb_script_section.self = gScriptSection;
|
||||
obj::NDO->add_sl_callbacks(&slcb_script_section);
|
||||
}
|
||||
|
||||
void ScriptSection::uninitialize() {
|
||||
ASSERT(gScriptSection);
|
||||
delete gScriptSection;
|
||||
gScriptSection = nullptr;
|
||||
}
|
||||
|
||||
ScriptSection* ScriptSection::globalHandle() { return gScriptSection; }
|
||||
|
|
|
|||
|
|
@ -1,81 +1,83 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "core/typegroups.h"
|
||||
|
||||
#include "core/object.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
obj::TypeGroups::TypeGroups() : is_group(true) {
|
||||
new (&childs) Dict();
|
||||
}
|
||||
|
||||
obj::TypeGroups::TypeGroups(bool is_group) : is_group(is_group) {
|
||||
if (is_group) {
|
||||
new (&childs) Dict();
|
||||
} else {
|
||||
type = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool obj::TypeGroups::isGroup() { return is_group; }
|
||||
|
||||
obj::TypeGroups::Dict* obj::TypeGroups::getChilds() {
|
||||
DEBUG_ASSERT(is_group);
|
||||
return &childs;
|
||||
}
|
||||
|
||||
void obj::TypeGroups::setType(ObjectType* type) {
|
||||
DEBUG_ASSERT(!is_group);
|
||||
this->type = type;
|
||||
}
|
||||
|
||||
void obj::TypeGroups::addType(ObjectType* type, tp::InitialierList<const char*> path, tp::alni cur_dir_idx) {
|
||||
DEBUG_ASSERT(is_group);
|
||||
|
||||
tp::alni dir_len = (tp::alni) path.size();
|
||||
|
||||
if (dir_len == cur_dir_idx) {
|
||||
TypeGroups* type_ref = new TypeGroups(false);
|
||||
type_ref->setType(type);
|
||||
childs.put(type->name, type_ref);
|
||||
return;
|
||||
}
|
||||
|
||||
TypeGroups* group = NULL;
|
||||
tp::alni index = 0;
|
||||
|
||||
for (auto dir : path) {
|
||||
if (index != cur_dir_idx) {
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto child_idx = childs.presents(dir);
|
||||
if (child_idx) {
|
||||
group = childs.getSlotVal(child_idx);
|
||||
} else {
|
||||
group = new TypeGroups(true);
|
||||
childs.put(dir, group);
|
||||
}
|
||||
|
||||
group->addType(type, path, cur_dir_idx + 1);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const obj::ObjectType* obj::TypeGroups::getType() {
|
||||
DEBUG_ASSERT(!is_group);
|
||||
return type;
|
||||
}
|
||||
|
||||
obj::TypeGroups::~TypeGroups() {
|
||||
if (is_group) {
|
||||
for (auto& child : childs) {
|
||||
delete child->val;
|
||||
}
|
||||
childs.~Map();
|
||||
}
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "core/typegroups.h"
|
||||
|
||||
#include "core/object.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
obj::TypeGroups::TypeGroups() :
|
||||
is_group(true) {
|
||||
new (&childs) Dict();
|
||||
}
|
||||
|
||||
obj::TypeGroups::TypeGroups(bool is_group) :
|
||||
is_group(is_group) {
|
||||
if (is_group) {
|
||||
new (&childs) Dict();
|
||||
} else {
|
||||
type = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool obj::TypeGroups::isGroup() { return is_group; }
|
||||
|
||||
obj::TypeGroups::Dict* obj::TypeGroups::getChilds() {
|
||||
DEBUG_ASSERT(is_group);
|
||||
return &childs;
|
||||
}
|
||||
|
||||
void obj::TypeGroups::setType(ObjectType* type) {
|
||||
DEBUG_ASSERT(!is_group);
|
||||
this->type = type;
|
||||
}
|
||||
|
||||
void obj::TypeGroups::addType(ObjectType* type, tp::InitialierList<const char*> path, tp::alni cur_dir_idx) {
|
||||
DEBUG_ASSERT(is_group);
|
||||
|
||||
tp::alni dir_len = (tp::alni) path.size();
|
||||
|
||||
if (dir_len == cur_dir_idx) {
|
||||
TypeGroups* type_ref = new TypeGroups(false);
|
||||
type_ref->setType(type);
|
||||
childs.put(type->name, type_ref);
|
||||
return;
|
||||
}
|
||||
|
||||
TypeGroups* group = NULL;
|
||||
tp::alni index = 0;
|
||||
|
||||
for (auto dir : path) {
|
||||
if (index != cur_dir_idx) {
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto child_idx = childs.presents(dir);
|
||||
if (child_idx) {
|
||||
group = childs.getSlotVal(child_idx);
|
||||
} else {
|
||||
group = new TypeGroups(true);
|
||||
childs.put(dir, group);
|
||||
}
|
||||
|
||||
group->addType(type, path, cur_dir_idx + 1);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const obj::ObjectType* obj::TypeGroups::getType() {
|
||||
DEBUG_ASSERT(!is_group);
|
||||
return type;
|
||||
}
|
||||
|
||||
obj::TypeGroups::~TypeGroups() {
|
||||
if (is_group) {
|
||||
for (auto& child : childs) {
|
||||
delete child->val;
|
||||
}
|
||||
childs.~Map();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,138 +1,134 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "core/typemethods.h"
|
||||
|
||||
#include "primitives/nullobject.h"
|
||||
#include "primitives/typeobject.h"
|
||||
#include "primitives/floatobject.h"
|
||||
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
|
||||
using namespace obj;
|
||||
|
||||
TypeMethod obj::gDefaultTypeMethods[] = {
|
||||
{
|
||||
.nameid = "type",
|
||||
.descr = "retrieves typeobject",
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
tm->ret.obj = TypeObject::create(tm->self->type);
|
||||
},
|
||||
.ret = { "typeobject", NULL },
|
||||
},
|
||||
{
|
||||
.nameid = "to_str",
|
||||
.descr = "converts to string",
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
if (tm->self->type->convesions && tm->self->type->convesions->to_string) {
|
||||
tm->ret.obj = StringObject::create(NDO->toString(tm->self));
|
||||
}
|
||||
},
|
||||
.ret = { "string object", NULL },
|
||||
},
|
||||
{
|
||||
.nameid = "to_float",
|
||||
.descr = "converts to float",
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
if (tm->self->type->convesions && tm->self->type->convesions->to_float) {
|
||||
tm->ret.obj = FloatObject::create(NDO->toFloat(tm->self));
|
||||
}
|
||||
},
|
||||
.ret = { "string object", NULL },
|
||||
},
|
||||
};
|
||||
|
||||
tp::int2 TypeMethods::presents(tp::String id) const {
|
||||
for (tp::int2 idx = 0; idx < mNMethods; idx++) {
|
||||
if (id == methods[idx]->nameid) {
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
|
||||
tp::int2 idx = 0;
|
||||
for (auto& tm : gDefaultTypeMethods) {
|
||||
if (id == tm.nameid) {
|
||||
return { tp::int2(idx + MAX_TYPE_METHODS) };
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
TypeMethods::LookupKey TypeMethods::presents(const ObjectType* type, tp::String id) {
|
||||
|
||||
tp::int2 depth = 0;
|
||||
tp::int2 idx = 0;
|
||||
|
||||
for (auto iter_type = type; iter_type; iter_type = iter_type->base) {
|
||||
idx = iter_type->type_methods.presents(id);
|
||||
if (idx != -1) {
|
||||
break;
|
||||
}
|
||||
depth++;
|
||||
}
|
||||
|
||||
return { idx, depth};
|
||||
}
|
||||
|
||||
const TypeMethod* TypeMethods::getMethod(tp::int2 key) const {
|
||||
if (key < MAX_TYPE_METHODS) {
|
||||
return methods[key];
|
||||
}
|
||||
return &gDefaultTypeMethods[key - MAX_TYPE_METHODS];
|
||||
}
|
||||
|
||||
const TypeMethod* TypeMethods::getMethod(const ObjectType* type, LookupKey key) {
|
||||
auto type_iter = type;
|
||||
for (auto idx = 0; idx < key.type_depth; idx++) {
|
||||
type_iter = type_iter->base;
|
||||
}
|
||||
return type_iter->type_methods.getMethod(key.key);
|
||||
}
|
||||
|
||||
void TypeMethods::init() {
|
||||
while (methods[mNMethods]) {
|
||||
mNMethods++;
|
||||
}
|
||||
|
||||
for (tp::int1 i = 0; i < mNMethods; i++) {
|
||||
methods[i]->init();
|
||||
}
|
||||
|
||||
// initialize and use finate state automata to lookup methods
|
||||
}
|
||||
|
||||
tp::halni TypeMethods::nMethods() const {
|
||||
return mNMethods;
|
||||
}
|
||||
|
||||
void TypeMethod::operator()(Interpreter* interp) const{
|
||||
for (auto i = 1; i <= mNargs; i++) {
|
||||
args[mNargs - i].obj = interp->mOperandsStack.getOperand();
|
||||
//NDO->refinc(args[i].obj);
|
||||
ASSERT(args[mNargs - i].obj && "expected an argument");
|
||||
}
|
||||
|
||||
ASSERT(!interp->mOperandsStack.getOperand() && "args remained");
|
||||
interp->mOperandsStack.getOperand();
|
||||
|
||||
self = interp->mLastParent;
|
||||
|
||||
exec(this);
|
||||
|
||||
if (ret.obj) {
|
||||
interp->mOperandsStack.push(ret.obj);
|
||||
interp->mScopeStack.addTempReturn(ret.obj);
|
||||
}
|
||||
else {
|
||||
interp->mOperandsStack.push(NDO_NULL_REF);
|
||||
}
|
||||
}
|
||||
|
||||
void TypeMethod::init() {
|
||||
while (args[mNargs].descr) {
|
||||
mNargs++;
|
||||
}
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "core/typemethods.h"
|
||||
|
||||
#include "primitives/floatobject.h"
|
||||
#include "primitives/nullobject.h"
|
||||
#include "primitives/typeobject.h"
|
||||
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
TypeMethod obj::gDefaultTypeMethods[] = {
|
||||
{
|
||||
.nameid = "type",
|
||||
.descr = "retrieves typeobject",
|
||||
.exec = [](const TypeMethod* tm) { tm->ret.obj = TypeObject::create(tm->self->type); },
|
||||
.ret = { "typeobject", NULL },
|
||||
},
|
||||
{
|
||||
.nameid = "to_str",
|
||||
.descr = "converts to string",
|
||||
.exec =
|
||||
[](const TypeMethod* tm) {
|
||||
if (tm->self->type->convesions && tm->self->type->convesions->to_string) {
|
||||
tm->ret.obj = StringObject::create(NDO->toString(tm->self));
|
||||
}
|
||||
},
|
||||
.ret = { "string object", NULL },
|
||||
},
|
||||
{
|
||||
.nameid = "to_float",
|
||||
.descr = "converts to float",
|
||||
.exec =
|
||||
[](const TypeMethod* tm) {
|
||||
if (tm->self->type->convesions && tm->self->type->convesions->to_float) {
|
||||
tm->ret.obj = FloatObject::create(NDO->toFloat(tm->self));
|
||||
}
|
||||
},
|
||||
.ret = { "string object", NULL },
|
||||
},
|
||||
};
|
||||
|
||||
tp::int2 TypeMethods::presents(tp::String id) const {
|
||||
for (tp::int2 idx = 0; idx < mNMethods; idx++) {
|
||||
if (id == methods[idx]->nameid) {
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
|
||||
tp::int2 idx = 0;
|
||||
for (auto& tm : gDefaultTypeMethods) {
|
||||
if (id == tm.nameid) {
|
||||
return { tp::int2(idx + MAX_TYPE_METHODS) };
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
TypeMethods::LookupKey TypeMethods::presents(const ObjectType* type, tp::String id) {
|
||||
|
||||
tp::int2 depth = 0;
|
||||
tp::int2 idx = 0;
|
||||
|
||||
for (auto iter_type = type; iter_type; iter_type = iter_type->base) {
|
||||
idx = iter_type->type_methods.presents(id);
|
||||
if (idx != -1) {
|
||||
break;
|
||||
}
|
||||
depth++;
|
||||
}
|
||||
|
||||
return { idx, depth };
|
||||
}
|
||||
|
||||
const TypeMethod* TypeMethods::getMethod(tp::int2 key) const {
|
||||
if (key < MAX_TYPE_METHODS) {
|
||||
return methods[key];
|
||||
}
|
||||
return &gDefaultTypeMethods[key - MAX_TYPE_METHODS];
|
||||
}
|
||||
|
||||
const TypeMethod* TypeMethods::getMethod(const ObjectType* type, LookupKey key) {
|
||||
auto type_iter = type;
|
||||
for (auto idx = 0; idx < key.type_depth; idx++) {
|
||||
type_iter = type_iter->base;
|
||||
}
|
||||
return type_iter->type_methods.getMethod(key.key);
|
||||
}
|
||||
|
||||
void TypeMethods::init() {
|
||||
while (methods[mNMethods]) {
|
||||
mNMethods++;
|
||||
}
|
||||
|
||||
for (tp::int1 i = 0; i < mNMethods; i++) {
|
||||
methods[i]->init();
|
||||
}
|
||||
|
||||
// initialize and use finate state automata to lookup methods
|
||||
}
|
||||
|
||||
tp::halni TypeMethods::nMethods() const { return mNMethods; }
|
||||
|
||||
void TypeMethod::operator()(Interpreter* interp) const {
|
||||
for (auto i = 1; i <= mNargs; i++) {
|
||||
args[mNargs - i].obj = interp->mOperandsStack.getOperand();
|
||||
// NDO->refinc(args[i].obj);
|
||||
ASSERT(args[mNargs - i].obj && "expected an argument");
|
||||
}
|
||||
|
||||
ASSERT(!interp->mOperandsStack.getOperand() && "args remained");
|
||||
interp->mOperandsStack.getOperand();
|
||||
|
||||
self = interp->mLastParent;
|
||||
|
||||
exec(this);
|
||||
|
||||
if (ret.obj) {
|
||||
interp->mOperandsStack.push(ret.obj);
|
||||
interp->mScopeStack.addTempReturn(ret.obj);
|
||||
} else {
|
||||
interp->mOperandsStack.push(NDO_NULL_REF);
|
||||
}
|
||||
}
|
||||
|
||||
void TypeMethod::init() {
|
||||
while (args[mNargs].descr) {
|
||||
mNargs++;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +1,36 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "interpreter/callstack.h"
|
||||
|
||||
#include "interpreter/bytecode.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
void CallStack::enter(const CallStack::CallFrame& frame) {
|
||||
if (mStack.size()) {
|
||||
auto& last_frame = mStack.last();
|
||||
last_frame.mIp = last_frame.mMethod->mScript->mBytecode.mInstructionIdx;
|
||||
}
|
||||
|
||||
frame.mMethod->mScript->mBytecode.mArgumentsLoaded = 0;
|
||||
frame.mMethod->mScript->mBytecode.mInstructionIdx = 0;
|
||||
obj::NDO->refinc(frame.mMethod);
|
||||
mStack.append(frame);
|
||||
}
|
||||
|
||||
void CallStack::leave() {
|
||||
auto frame = mStack.last();
|
||||
obj::NDO->destroy(frame.mMethod);
|
||||
mStack.pop();
|
||||
|
||||
if (mStack.size()) {
|
||||
auto& last_frame = mStack.last();
|
||||
last_frame.mMethod->mScript->mBytecode.mInstructionIdx = last_frame.mIp;
|
||||
}
|
||||
}
|
||||
|
||||
ByteCode* CallStack::getBytecode() {
|
||||
return &mStack.last().mMethod->mScript->mBytecode;
|
||||
}
|
||||
|
||||
tp::halni CallStack::len() const {
|
||||
return mStack.size();
|
||||
}
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "interpreter/callstack.h"
|
||||
|
||||
#include "interpreter/bytecode.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
void CallStack::enter(const CallStack::CallFrame& frame) {
|
||||
if (mStack.size()) {
|
||||
auto& last_frame = mStack.last();
|
||||
last_frame.mIp = last_frame.mMethod->mScript->mBytecode.mInstructionIdx;
|
||||
}
|
||||
|
||||
frame.mMethod->mScript->mBytecode.mArgumentsLoaded = 0;
|
||||
frame.mMethod->mScript->mBytecode.mInstructionIdx = 0;
|
||||
obj::NDO->refinc(frame.mMethod);
|
||||
mStack.append(frame);
|
||||
}
|
||||
|
||||
void CallStack::leave() {
|
||||
auto frame = mStack.last();
|
||||
obj::NDO->destroy(frame.mMethod);
|
||||
mStack.pop();
|
||||
|
||||
if (mStack.size()) {
|
||||
auto& last_frame = mStack.last();
|
||||
last_frame.mMethod->mScript->mBytecode.mInstructionIdx = last_frame.mIp;
|
||||
}
|
||||
}
|
||||
|
||||
ByteCode* CallStack::getBytecode() { return &mStack.last().mMethod->mScript->mBytecode; }
|
||||
|
||||
tp::halni CallStack::len() const { return mStack.size(); }
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,384 +1,371 @@
|
|||
|
||||
#include "Utils.hpp"
|
||||
|
||||
#include "interpreter/opcodes.h"
|
||||
|
||||
namespace obj {
|
||||
OpcodeInfos gOpcodeInfos;
|
||||
};
|
||||
|
||||
using namespace obj;
|
||||
|
||||
#define CONST_IDX_BYTES 2
|
||||
|
||||
#define OP(opcode, name, desc, ops, params) \
|
||||
add(opcode, { #name, #desc, ops, params } );
|
||||
|
||||
OpcodeInfos::OperandsInfo::OperandsInfo() {}
|
||||
OpcodeInfos::OperandsInfo::OperandsInfo(tp::InitialierList<Operand> list) {
|
||||
DEBUG_ASSERT(MAX_OPERANDS >= list.size());
|
||||
for (auto item : list) {
|
||||
buff[len] = item;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
||||
OpcodeInfos::ParamsInfo::ParamsInfo() {}
|
||||
OpcodeInfos::ParamsInfo::ParamsInfo(tp::InitialierList<Param> list) {
|
||||
DEBUG_ASSERT(MAX_PARAMS >= list.size());
|
||||
for (auto item : list) {
|
||||
buff[len] = item;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
||||
tp::uint1 OpcodeInfos::OpInfo::opsize() {
|
||||
tp::uint1 out = 1;
|
||||
for (auto i = 0; i < params.len; i++) {
|
||||
out += params.buff[i].bytes;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
OpcodeInfos::OpcodeInfos() {
|
||||
|
||||
add(OpCode::NONE, { "NONE", "Does Nothing" });
|
||||
add(OpCode::HALT, { "HALT", "Halts for 3 sec" });
|
||||
add(OpCode::TERMINATE, { "TERMINATE", "Terminates the process" });
|
||||
add(OpCode::DEF_LOCAL, {
|
||||
.name = "DEF LOCAL",
|
||||
.desc = "Adds object to the locals",
|
||||
.operands = {
|
||||
{ "str", "local id" },
|
||||
{ "any", "object to be local" }
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::LOAD_CONST, {
|
||||
.name = "LOAD CONST",
|
||||
.desc = "Loads const object from the const pool",
|
||||
.params = {
|
||||
{ "const obj idx", CONST_IDX_BYTES },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::LOAD_LOCAL, {
|
||||
.name = "LOAD LOCAL",
|
||||
.desc = "Loads local object from the locals",
|
||||
.params = {
|
||||
{ "idx of const StringObject - represents name id", CONST_IDX_BYTES },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::IGNORE, {
|
||||
.name = "IGNORE",
|
||||
.desc = "Ignores returned object by destroying it",
|
||||
}
|
||||
);
|
||||
add(OpCode::SCOPE_IN, {
|
||||
.name = "SCOPE IN",
|
||||
.desc = "Enters new scope",
|
||||
}
|
||||
);
|
||||
add(OpCode::SCOPE_OUT, {
|
||||
.name = "SCOPE OUT",
|
||||
.desc = "Leaves current scope",
|
||||
}
|
||||
);
|
||||
add(OpCode::CALL, {
|
||||
.name = "CALL",
|
||||
.desc = "Leaves current scope",
|
||||
.operands = {
|
||||
{ "method", "method object" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::RETURN_OBJ, {
|
||||
.name = "RETURN OBJ",
|
||||
.desc = "Returns Operand",
|
||||
.operands = {
|
||||
{ "any", "object to be returned" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::RETURN, {
|
||||
.name = "RETURN",
|
||||
.desc = "Returns Null Object",
|
||||
}
|
||||
);
|
||||
add(OpCode::CHILD, {
|
||||
.name = "CHILD",
|
||||
.desc = "Retrieves child from class",
|
||||
.operands = {
|
||||
{ "str", "child id" },
|
||||
{ "class", "parent class" },
|
||||
},
|
||||
.params = {
|
||||
{ "is method ?", 1 }
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::PUSH_ARGS, {
|
||||
.name = "PUSH ARGS",
|
||||
.desc = "Pushes Separator on the OperandsStack",
|
||||
.params = {
|
||||
{ "length to chech number of args", 1 }
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::SAVE_ARGS, {
|
||||
.name = "SAVE ARGS",
|
||||
.desc = "Pushes operands to locals",
|
||||
.params = {
|
||||
{ "number of arguments in function defenition", 1 }
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_CREATE_LOCAL, {
|
||||
.name = "CREATE LOCAL OBJ",
|
||||
.desc = "creates object of given type and adds it to the locals",
|
||||
.operands = {
|
||||
{ "str", "types" },
|
||||
{ "str", "name id" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_CREATE, {
|
||||
.name = "CREATE OBJ",
|
||||
.desc = "creates object of given type",
|
||||
.operands = {
|
||||
{ "str", "types" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_COPY, {
|
||||
.name = "COPY",
|
||||
.desc = "Copies objects",
|
||||
.operands = {
|
||||
{ "any", "self" },
|
||||
{ "any", "target" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_SAVE, {
|
||||
.name = "SAVE",
|
||||
.desc = "Saves object to a file",
|
||||
.operands = {
|
||||
{ "str", "path" },
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_LOAD, {
|
||||
.name = "LOAD",
|
||||
.desc = "loads object from a file",
|
||||
.operands = {
|
||||
{ "str", "path" },
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_ADD, {
|
||||
.name = "ADD",
|
||||
.desc = "Adds objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_SUB, {
|
||||
.name = "SUB",
|
||||
.desc = "Subtruts objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_MUL, {
|
||||
.name = "MUL",
|
||||
.desc = "Multiplies objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_DIV, {
|
||||
.name = "DIV",
|
||||
.desc = "Divides objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::AND, {
|
||||
.name = "AND",
|
||||
.desc = "Logical AND of objects of the same type that supports boolean ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OR, {
|
||||
.name = "OR",
|
||||
.desc = "Logical OR of objects of the same type that supports boolean ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::EQUAL, {
|
||||
.name = "EQUAL",
|
||||
.desc = "Compares objects",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::NOT_EQUAL, {
|
||||
.name = "NOT EQUAL",
|
||||
.desc = "Compares objects and inverts the resault",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::MORE, {
|
||||
.name = "MORE",
|
||||
.desc = " > ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::LESS, {
|
||||
.name = "LESS",
|
||||
.desc = " < ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::EQUAL_OR_MORE, {
|
||||
.name = "GRATER OR EQUAL",
|
||||
.desc = " >= ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::EQUAL_OR_LESS, {
|
||||
.name = "LESS OR EQUAL",
|
||||
.desc = " <= ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::NOT, {
|
||||
.name = "NOT",
|
||||
.desc = "Inverts the object bolean representation",
|
||||
.operands = {
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP, {
|
||||
.name = "JUMP",
|
||||
.desc = "Offsets the instruction pointer",
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_R, {
|
||||
.name = "JUMP R",
|
||||
.desc = "Offsets the instruction pointer in reversed direction",
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF, {
|
||||
.name = "JUMP IF",
|
||||
.desc = "Offsets the instruction pointer if condition is met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF_R, {
|
||||
.name = "JUMP IF R",
|
||||
.desc = "Offsets the instruction pointer in reversed direction if condition is met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF_NOT, {
|
||||
.name = "JUMP IF R",
|
||||
.desc = "Offsets the instruction pointer if condition is NOT met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF_NOT_R, {
|
||||
.name = "JUMP IF R",
|
||||
.desc = "Offsets the instruction pointer in reversed direction if condition is NOT met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::PRINT, {
|
||||
.name = "PRINT",
|
||||
.desc = "Prints the object string representation",
|
||||
.operands = {
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::CLASS_CONSTRUCT, {
|
||||
.name = "CLASS CONSTRUCT",
|
||||
.desc = "Creates class from the function execution state",
|
||||
}
|
||||
);
|
||||
add(OpCode::SELF, {
|
||||
.name = "SELF",
|
||||
.desc = "retrieves parent class of the current method",
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
OpcodeInfos::OpInfo OpcodeInfos::fetch(OpCode code) {
|
||||
DEBUG_ASSERT((tp::alni)code >= 0 && (tp::alni)code < (tp::alni)OpCode::END_OPCODES);
|
||||
return buff[(tp::alni)code];
|
||||
}
|
||||
|
||||
void OpcodeInfos::add(OpCode code, const OpInfo& info) {
|
||||
buff[(tp::alni)code] = info;
|
||||
}
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
#include "interpreter/opcodes.h"
|
||||
|
||||
namespace obj {
|
||||
OpcodeInfos gOpcodeInfos;
|
||||
};
|
||||
|
||||
using namespace obj;
|
||||
|
||||
#define CONST_IDX_BYTES 2
|
||||
|
||||
#define OP(opcode, name, desc, ops, params) add(opcode, { #name, #desc, ops, params });
|
||||
|
||||
OpcodeInfos::OperandsInfo::OperandsInfo() {}
|
||||
OpcodeInfos::OperandsInfo::OperandsInfo(tp::InitialierList<Operand> list) {
|
||||
DEBUG_ASSERT(MAX_OPERANDS >= list.size());
|
||||
for (auto item : list) {
|
||||
buff[len] = item;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
||||
OpcodeInfos::ParamsInfo::ParamsInfo() {}
|
||||
OpcodeInfos::ParamsInfo::ParamsInfo(tp::InitialierList<Param> list) {
|
||||
DEBUG_ASSERT(MAX_PARAMS >= list.size());
|
||||
for (auto item : list) {
|
||||
buff[len] = item;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
||||
tp::uint1 OpcodeInfos::OpInfo::opsize() {
|
||||
tp::uint1 out = 1;
|
||||
for (auto i = 0; i < params.len; i++) {
|
||||
out += params.buff[i].bytes;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
OpcodeInfos::OpcodeInfos() {
|
||||
|
||||
add(OpCode::NONE, { "NONE", "Does Nothing" });
|
||||
add(OpCode::HALT, { "HALT", "Halts for 3 sec" });
|
||||
add(OpCode::TERMINATE, { "TERMINATE", "Terminates the process" });
|
||||
add(OpCode::DEF_LOCAL, { .name = "DEF LOCAL", .desc = "Adds object to the locals", .operands = { { "str", "local id" }, { "any", "object to be local" } } });
|
||||
add(OpCode::LOAD_CONST, {
|
||||
.name = "LOAD CONST",
|
||||
.desc = "Loads const object from the const pool",
|
||||
.params = {
|
||||
{ "const obj idx", CONST_IDX_BYTES },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::LOAD_LOCAL, {
|
||||
.name = "LOAD LOCAL",
|
||||
.desc = "Loads local object from the locals",
|
||||
.params = {
|
||||
{ "idx of const StringObject - represents name id", CONST_IDX_BYTES },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(
|
||||
OpCode::IGNORE,
|
||||
{
|
||||
.name = "IGNORE",
|
||||
.desc = "Ignores returned object by destroying it",
|
||||
}
|
||||
);
|
||||
add(
|
||||
OpCode::SCOPE_IN,
|
||||
{
|
||||
.name = "SCOPE IN",
|
||||
.desc = "Enters new scope",
|
||||
}
|
||||
);
|
||||
add(
|
||||
OpCode::SCOPE_OUT,
|
||||
{
|
||||
.name = "SCOPE OUT",
|
||||
.desc = "Leaves current scope",
|
||||
}
|
||||
);
|
||||
add(OpCode::CALL, {
|
||||
.name = "CALL",
|
||||
.desc = "Leaves current scope",
|
||||
.operands = {
|
||||
{ "method", "method object" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::RETURN_OBJ, {
|
||||
.name = "RETURN OBJ",
|
||||
.desc = "Returns Operand",
|
||||
.operands = {
|
||||
{ "any", "object to be returned" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(
|
||||
OpCode::RETURN,
|
||||
{
|
||||
.name = "RETURN",
|
||||
.desc = "Returns Null Object",
|
||||
}
|
||||
);
|
||||
add(OpCode::CHILD, {
|
||||
.name = "CHILD",
|
||||
.desc = "Retrieves child from class",
|
||||
.operands = {
|
||||
{ "str", "child id" },
|
||||
{ "class", "parent class" },
|
||||
},
|
||||
.params = {
|
||||
{ "is method ?", 1 }
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::PUSH_ARGS, { .name = "PUSH ARGS", .desc = "Pushes Separator on the OperandsStack", .params = { { "length to chech number of args", 1 } } });
|
||||
add(OpCode::SAVE_ARGS, { .name = "SAVE ARGS", .desc = "Pushes operands to locals", .params = { { "number of arguments in function defenition", 1 } } });
|
||||
add(OpCode::OBJ_CREATE_LOCAL, {
|
||||
.name = "CREATE LOCAL OBJ",
|
||||
.desc = "creates object of given type and adds it to the locals",
|
||||
.operands = {
|
||||
{ "str", "types" },
|
||||
{ "str", "name id" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_CREATE, {
|
||||
.name = "CREATE OBJ",
|
||||
.desc = "creates object of given type",
|
||||
.operands = {
|
||||
{ "str", "types" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_COPY, {
|
||||
.name = "COPY",
|
||||
.desc = "Copies objects",
|
||||
.operands = {
|
||||
{ "any", "self" },
|
||||
{ "any", "target" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_SAVE, {
|
||||
.name = "SAVE",
|
||||
.desc = "Saves object to a file",
|
||||
.operands = {
|
||||
{ "str", "path" },
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_LOAD, {
|
||||
.name = "LOAD",
|
||||
.desc = "loads object from a file",
|
||||
.operands = {
|
||||
{ "str", "path" },
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_ADD, {
|
||||
.name = "ADD",
|
||||
.desc = "Adds objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_SUB, {
|
||||
.name = "SUB",
|
||||
.desc = "Subtruts objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_MUL, {
|
||||
.name = "MUL",
|
||||
.desc = "Multiplies objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OBJ_DIV, {
|
||||
.name = "DIV",
|
||||
.desc = "Divides objects of the same type that supports ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::AND, {
|
||||
.name = "AND",
|
||||
.desc = "Logical AND of objects of the same type that supports boolean ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::OR, {
|
||||
.name = "OR",
|
||||
.desc = "Logical OR of objects of the same type that supports boolean ariphmetics",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::EQUAL, {
|
||||
.name = "EQUAL",
|
||||
.desc = "Compares objects",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::NOT_EQUAL, {
|
||||
.name = "NOT EQUAL",
|
||||
.desc = "Compares objects and inverts the resault",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::MORE, {
|
||||
.name = "MORE",
|
||||
.desc = " > ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::LESS, {
|
||||
.name = "LESS",
|
||||
.desc = " < ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::EQUAL_OR_MORE, {
|
||||
.name = "GRATER OR EQUAL",
|
||||
.desc = " >= ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::EQUAL_OR_LESS, {
|
||||
.name = "LESS OR EQUAL",
|
||||
.desc = " <= ",
|
||||
.operands = {
|
||||
{ "any", "left" },
|
||||
{ "any", "right" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::NOT, {
|
||||
.name = "NOT",
|
||||
.desc = "Inverts the object bolean representation",
|
||||
.operands = {
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP, {
|
||||
.name = "JUMP",
|
||||
.desc = "Offsets the instruction pointer",
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_R, {
|
||||
.name = "JUMP R",
|
||||
.desc = "Offsets the instruction pointer in reversed direction",
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF, {
|
||||
.name = "JUMP IF",
|
||||
.desc = "Offsets the instruction pointer if condition is met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF_R, {
|
||||
.name = "JUMP IF R",
|
||||
.desc = "Offsets the instruction pointer in reversed direction if condition is met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF_NOT, {
|
||||
.name = "JUMP IF R",
|
||||
.desc = "Offsets the instruction pointer if condition is NOT met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::JUMP_IF_NOT_R, {
|
||||
.name = "JUMP IF R",
|
||||
.desc = "Offsets the instruction pointer in reversed direction if condition is NOT met",
|
||||
.operands = {
|
||||
{ "any", "condition object" },
|
||||
},
|
||||
.params = {
|
||||
{ "ip offset unsigned", 2 },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(OpCode::PRINT, {
|
||||
.name = "PRINT",
|
||||
.desc = "Prints the object string representation",
|
||||
.operands = {
|
||||
{ "any", "self" },
|
||||
}
|
||||
}
|
||||
);
|
||||
add(
|
||||
OpCode::CLASS_CONSTRUCT,
|
||||
{
|
||||
.name = "CLASS CONSTRUCT",
|
||||
.desc = "Creates class from the function execution state",
|
||||
}
|
||||
);
|
||||
add(
|
||||
OpCode::SELF,
|
||||
{
|
||||
.name = "SELF",
|
||||
.desc = "retrieves parent class of the current method",
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
OpcodeInfos::OpInfo OpcodeInfos::fetch(OpCode code) {
|
||||
DEBUG_ASSERT((tp::alni) code >= 0 && (tp::alni) code < (tp::alni) OpCode::END_OPCODES);
|
||||
return buff[(tp::alni) code];
|
||||
}
|
||||
|
||||
void OpcodeInfos::add(OpCode code, const OpInfo& info) { buff[(tp::alni) code] = info; }
|
||||
|
|
@ -1,32 +1,30 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "interpreter/operandsstack.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
OperandStack::OperandStack() {
|
||||
mBuff = new Operand[MAX_STACK_SIZE];
|
||||
mIdx = 0;
|
||||
}
|
||||
|
||||
void OperandStack::push(Operand operand) {
|
||||
ASSERT(MAX_STACK_SIZE - 1 > mIdx && "stack overflow");
|
||||
mBuff[mIdx] = operand;
|
||||
mIdx++;
|
||||
}
|
||||
|
||||
void OperandStack::pop() {
|
||||
ASSERT(mIdx != NULL && "stack overflow");
|
||||
mIdx--;
|
||||
}
|
||||
|
||||
Operand OperandStack::getOperand() {
|
||||
auto ret = mBuff[mIdx - 1];
|
||||
mIdx--;
|
||||
return ret;
|
||||
}
|
||||
|
||||
OperandStack::~OperandStack() {
|
||||
delete[] mBuff;
|
||||
}
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "interpreter/operandsstack.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
OperandStack::OperandStack() {
|
||||
mBuff = new Operand[MAX_STACK_SIZE];
|
||||
mIdx = 0;
|
||||
}
|
||||
|
||||
void OperandStack::push(Operand operand) {
|
||||
ASSERT(MAX_STACK_SIZE - 1 > mIdx && "stack overflow");
|
||||
mBuff[mIdx] = operand;
|
||||
mIdx++;
|
||||
}
|
||||
|
||||
void OperandStack::pop() {
|
||||
ASSERT(mIdx != NULL && "stack overflow");
|
||||
mIdx--;
|
||||
}
|
||||
|
||||
Operand OperandStack::getOperand() {
|
||||
auto ret = mBuff[mIdx - 1];
|
||||
mIdx--;
|
||||
return ret;
|
||||
}
|
||||
|
||||
OperandStack::~OperandStack() { delete[] mBuff; }
|
||||
|
|
|
|||
|
|
@ -1,90 +1,85 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "interpreter/scopestack.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
Scope::~Scope() {
|
||||
for (auto local : mLocals) {
|
||||
obj::NDO->destroy(local->val);
|
||||
}
|
||||
for (auto tmp : mTemps) {
|
||||
obj::NDO->destroy(tmp.data());
|
||||
}
|
||||
}
|
||||
|
||||
obj::Object* ScopeStack::getLocalUtil(Scope& scope, tp::String* id) {
|
||||
|
||||
auto idx = scope.mLocals.presents(*id);
|
||||
|
||||
if (idx) {
|
||||
return scope.mLocals.getSlotVal(idx);
|
||||
}
|
||||
else {
|
||||
mIterIdx--;
|
||||
Scope& parent_scope = mBuff[mIterIdx];
|
||||
ASSERT(parent_scope.mChildReachable && "Undefined Local Reference");
|
||||
return getLocalUtil(parent_scope, id);
|
||||
}
|
||||
}
|
||||
|
||||
ScopeStack::ScopeStack() {
|
||||
mBuff = (Scope*) tp::HeapAllocGlobal::allocate(sizeof(Scope) * MAX_STACK_SIZE);
|
||||
mIdx = 0;
|
||||
mIterIdx = 0;
|
||||
}
|
||||
|
||||
void ScopeStack::enterScope(bool aChildReachable) {
|
||||
ASSERT(MAX_STACK_SIZE - 1 > mIdx && "stack overflow");
|
||||
new (&mBuff[mIdx]) Scope();
|
||||
mBuff[mIdx].mChildReachable = aChildReachable;
|
||||
mIdx++;
|
||||
}
|
||||
|
||||
void ScopeStack::leaveScope() {
|
||||
ASSERT(mIdx != NULL && "stack overflow");
|
||||
mBuff[mIdx - 1].~Scope();
|
||||
mIdx--;
|
||||
}
|
||||
|
||||
void ScopeStack::addTemp(obj::Object* tmp) {
|
||||
obj::NDO->refinc(tmp);
|
||||
mBuff[mIdx - 1].mTemps.pushBack(tmp);
|
||||
}
|
||||
|
||||
void ScopeStack::popTemp() {
|
||||
obj::NDO->destroy(mBuff[mIdx - 1].mTemps.last()->data);
|
||||
mBuff[mIdx - 1].mTemps.popBack();
|
||||
}
|
||||
|
||||
void ScopeStack::addTempReturn(obj::Object* ret) {
|
||||
if (mIdx >= 2) {
|
||||
obj::NDO->refinc(ret);
|
||||
mBuff[mIdx - 2].mTemps.pushBack(ret);
|
||||
}
|
||||
}
|
||||
|
||||
void ScopeStack::addLocal(obj::Object* local, tp::String id) {
|
||||
DEBUG_ASSERT(mIdx != 0 && "No scope given");
|
||||
Scope::ObjDict& locals = mBuff[mIdx - 1].mLocals;
|
||||
auto idx = locals.presents(id);
|
||||
if (idx) {
|
||||
obj::NDO->destroy(locals.getSlotVal(idx));
|
||||
}
|
||||
obj::NDO->refinc(local);
|
||||
locals.put(id, local);
|
||||
}
|
||||
|
||||
obj::Object* ScopeStack::getLocal(tp::String& str) {
|
||||
mIterIdx = mIdx - 1;
|
||||
return getLocalUtil(mBuff[mIdx - 1], &str);
|
||||
}
|
||||
|
||||
Scope* ScopeStack::getCurrentScope() {
|
||||
return &mBuff[mIdx - 1];
|
||||
}
|
||||
|
||||
ScopeStack::~ScopeStack() {
|
||||
tp::HeapAllocGlobal::deallocate(mBuff);
|
||||
}
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "interpreter/scopestack.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
Scope::~Scope() {
|
||||
for (auto local : mLocals) {
|
||||
obj::NDO->destroy(local->val);
|
||||
}
|
||||
for (auto tmp : mTemps) {
|
||||
obj::NDO->destroy(tmp.data());
|
||||
}
|
||||
}
|
||||
|
||||
obj::Object* ScopeStack::getLocalUtil(Scope& scope, tp::String* id) {
|
||||
|
||||
auto idx = scope.mLocals.presents(*id);
|
||||
|
||||
if (idx) {
|
||||
return scope.mLocals.getSlotVal(idx);
|
||||
} else {
|
||||
mIterIdx--;
|
||||
Scope& parent_scope = mBuff[mIterIdx];
|
||||
ASSERT(parent_scope.mChildReachable && "Undefined Local Reference");
|
||||
return getLocalUtil(parent_scope, id);
|
||||
}
|
||||
}
|
||||
|
||||
ScopeStack::ScopeStack() {
|
||||
mBuff = (Scope*) tp::HeapAllocGlobal::allocate(sizeof(Scope) * MAX_STACK_SIZE);
|
||||
mIdx = 0;
|
||||
mIterIdx = 0;
|
||||
}
|
||||
|
||||
void ScopeStack::enterScope(bool aChildReachable) {
|
||||
ASSERT(MAX_STACK_SIZE - 1 > mIdx && "stack overflow");
|
||||
new (&mBuff[mIdx]) Scope();
|
||||
mBuff[mIdx].mChildReachable = aChildReachable;
|
||||
mIdx++;
|
||||
}
|
||||
|
||||
void ScopeStack::leaveScope() {
|
||||
ASSERT(mIdx != NULL && "stack overflow");
|
||||
mBuff[mIdx - 1].~Scope();
|
||||
mIdx--;
|
||||
}
|
||||
|
||||
void ScopeStack::addTemp(obj::Object* tmp) {
|
||||
obj::NDO->refinc(tmp);
|
||||
mBuff[mIdx - 1].mTemps.pushBack(tmp);
|
||||
}
|
||||
|
||||
void ScopeStack::popTemp() {
|
||||
obj::NDO->destroy(mBuff[mIdx - 1].mTemps.last()->data);
|
||||
mBuff[mIdx - 1].mTemps.popBack();
|
||||
}
|
||||
|
||||
void ScopeStack::addTempReturn(obj::Object* ret) {
|
||||
if (mIdx >= 2) {
|
||||
obj::NDO->refinc(ret);
|
||||
mBuff[mIdx - 2].mTemps.pushBack(ret);
|
||||
}
|
||||
}
|
||||
|
||||
void ScopeStack::addLocal(obj::Object* local, tp::String id) {
|
||||
DEBUG_ASSERT(mIdx != 0 && "No scope given");
|
||||
Scope::ObjDict& locals = mBuff[mIdx - 1].mLocals;
|
||||
auto idx = locals.presents(id);
|
||||
if (idx) {
|
||||
obj::NDO->destroy(locals.getSlotVal(idx));
|
||||
}
|
||||
obj::NDO->refinc(local);
|
||||
locals.put(id, local);
|
||||
}
|
||||
|
||||
obj::Object* ScopeStack::getLocal(tp::String& str) {
|
||||
mIterIdx = mIdx - 1;
|
||||
return getLocalUtil(mBuff[mIdx - 1], &str);
|
||||
}
|
||||
|
||||
Scope* ScopeStack::getCurrentScope() { return &mBuff[mIdx - 1]; }
|
||||
|
||||
ScopeStack::~ScopeStack() { tp::HeapAllocGlobal::deallocate(mBuff); }
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,78 +1,56 @@
|
|||
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/boolobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void BoolObject::constructor(BoolObject* self) {
|
||||
self->val = false;
|
||||
}
|
||||
|
||||
void BoolObject::copy(BoolObject* self, const BoolObject* in) {
|
||||
self->val = in->val;
|
||||
}
|
||||
|
||||
BoolObject* BoolObject::create(bool in) {
|
||||
NDO_CASTV(BoolObject, NDO->create("bool"), out)->val = alni(in);
|
||||
return out;
|
||||
}
|
||||
|
||||
void BoolObject::from_int(BoolObject* self, alni in) {
|
||||
self->val = in;
|
||||
}
|
||||
|
||||
void BoolObject::from_float(BoolObject* self, alnf in) {
|
||||
self->val = alni(bool(in));
|
||||
}
|
||||
|
||||
void BoolObject::from_string(BoolObject* self, String in) {
|
||||
self->val = alni(bool(in));
|
||||
}
|
||||
|
||||
String BoolObject::to_string(BoolObject* self) {
|
||||
return String(bool(self->val));
|
||||
}
|
||||
|
||||
alni BoolObject::to_int(BoolObject* self) {
|
||||
return self->val;
|
||||
}
|
||||
|
||||
alnf BoolObject::to_float(BoolObject* self) {
|
||||
return alnf(bool(self->val));
|
||||
}
|
||||
|
||||
static alni save_size(BoolObject* self) {
|
||||
return sizeof(alni);
|
||||
}
|
||||
|
||||
static void save(BoolObject* self, ArchiverOut& file_self) {
|
||||
file_self << self->val;
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, BoolObject* self) {
|
||||
file_self >> self->val;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions BoolObjectTypeConversions = {
|
||||
.from_int = (object_from_int) BoolObject::from_int,
|
||||
.from_float = (object_from_float) BoolObject::from_float,
|
||||
.from_string = (object_from_string) BoolObject::from_string,
|
||||
.to_string = (object_to_string) BoolObject::to_string,
|
||||
.to_int = (object_to_int) BoolObject::to_int,
|
||||
.to_float = (object_to_float) BoolObject::to_float,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::BoolObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) BoolObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) BoolObject::copy,
|
||||
.size = sizeof(BoolObject),
|
||||
.name = "bool",
|
||||
.convesions = &BoolObjectTypeConversions,
|
||||
.save_size = (object_save_size)save_size,
|
||||
.save = (object_save)save,
|
||||
.load = (object_load)load,
|
||||
|
||||
|
||||
#include "primitives/boolobject.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void BoolObject::constructor(BoolObject* self) { self->val = false; }
|
||||
|
||||
void BoolObject::copy(BoolObject* self, const BoolObject* in) { self->val = in->val; }
|
||||
|
||||
BoolObject* BoolObject::create(bool in) {
|
||||
NDO_CASTV(BoolObject, NDO->create("bool"), out)->val = alni(in);
|
||||
return out;
|
||||
}
|
||||
|
||||
void BoolObject::from_int(BoolObject* self, alni in) { self->val = in; }
|
||||
|
||||
void BoolObject::from_float(BoolObject* self, alnf in) { self->val = alni(bool(in)); }
|
||||
|
||||
void BoolObject::from_string(BoolObject* self, String in) { self->val = alni(bool(in)); }
|
||||
|
||||
String BoolObject::to_string(BoolObject* self) { return String(bool(self->val)); }
|
||||
|
||||
alni BoolObject::to_int(BoolObject* self) { return self->val; }
|
||||
|
||||
alnf BoolObject::to_float(BoolObject* self) { return alnf(bool(self->val)); }
|
||||
|
||||
static alni save_size(BoolObject* self) { return sizeof(alni); }
|
||||
|
||||
static void save(BoolObject* self, ArchiverOut& file_self) { file_self << self->val; }
|
||||
|
||||
static void load(ArchiverIn& file_self, BoolObject* self) { file_self >> self->val; }
|
||||
|
||||
struct ObjectTypeConversions BoolObjectTypeConversions = {
|
||||
.from_int = (object_from_int) BoolObject::from_int,
|
||||
.from_float = (object_from_float) BoolObject::from_float,
|
||||
.from_string = (object_from_string) BoolObject::from_string,
|
||||
.to_string = (object_to_string) BoolObject::to_string,
|
||||
.to_int = (object_to_int) BoolObject::to_int,
|
||||
.to_float = (object_to_float) BoolObject::to_float,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::BoolObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) BoolObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) BoolObject::copy,
|
||||
.size = sizeof(BoolObject),
|
||||
.name = "bool",
|
||||
.convesions = &BoolObjectTypeConversions,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
};
|
||||
|
|
@ -1,82 +1,72 @@
|
|||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/classobject.h"
|
||||
#include "primitives/dictobject.h"
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void ClassObject::constructor(ClassObject* self) {
|
||||
self->members = NDO_CAST(DictObject, NDO->create("dict"));
|
||||
|
||||
self->addMember(NDO_NULL, "__init__");
|
||||
self->addMember(NDO_NULL, "__del__");
|
||||
}
|
||||
|
||||
void ClassObject::copy(ClassObject* self, const ClassObject* blueprint) {
|
||||
NDO->copy(self->members, blueprint->members);
|
||||
}
|
||||
|
||||
void ClassObject::destructor(ClassObject* self) {
|
||||
NDO->destroy(self->members);
|
||||
}
|
||||
|
||||
void ClassObject::addMember(Object* obj, tp::String id) {
|
||||
members->put(id, obj);
|
||||
}
|
||||
|
||||
void ClassObject::createMember(tp::String type, tp::String id) {
|
||||
auto newo = NDO->create(type);
|
||||
members->put(id, newo);
|
||||
}
|
||||
|
||||
alni ClassObject::save_size(ClassObject* self) {
|
||||
return sizeof(alni); // dict object adress
|
||||
}
|
||||
|
||||
void ClassObject::save(ClassObject* self, ArchiverOut& file_self) {
|
||||
// save dictobject
|
||||
alni ndo_object_adress = NDO->save(file_self, self->members);
|
||||
file_self << ndo_object_adress;
|
||||
}
|
||||
|
||||
void ClassObject::load(ArchiverIn& file_self, ClassObject* self) {
|
||||
alni ndo_object_adress;
|
||||
file_self >> ndo_object_adress;
|
||||
self->members = NDO_CAST(DictObject, NDO->load(file_self, ndo_object_adress));
|
||||
NDO->refinc(self->members);
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> childs_retrival(ClassObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
out.append(self->members);
|
||||
return out;
|
||||
}
|
||||
|
||||
alni allocated_size(ClassObject* self) {
|
||||
return sizeof(DictObject*);
|
||||
}
|
||||
|
||||
alni allocated_size_recursive(ClassObject* self) {
|
||||
alni out = sizeof(DictObject*);
|
||||
out += NDO->objsize_ram_recursive_util(self->members, self->members->type);
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ObjectType ClassObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) ClassObject::constructor,
|
||||
.destructor = (object_destructor) ClassObject::destructor,
|
||||
.copy = (object_copy) ClassObject::copy,
|
||||
.size = sizeof(ClassObject),
|
||||
.name = "class",
|
||||
.save_size = (object_save_size)save_size,
|
||||
.save = (object_save)save,
|
||||
.load = (object_load)load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival) childs_retrival,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive) allocated_size_recursive
|
||||
};
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/classobject.h"
|
||||
#include "primitives/dictobject.h"
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void ClassObject::constructor(ClassObject* self) {
|
||||
self->members = NDO_CAST(DictObject, NDO->create("dict"));
|
||||
|
||||
self->addMember(NDO_NULL, "__init__");
|
||||
self->addMember(NDO_NULL, "__del__");
|
||||
}
|
||||
|
||||
void ClassObject::copy(ClassObject* self, const ClassObject* blueprint) { NDO->copy(self->members, blueprint->members); }
|
||||
|
||||
void ClassObject::destructor(ClassObject* self) { NDO->destroy(self->members); }
|
||||
|
||||
void ClassObject::addMember(Object* obj, tp::String id) { members->put(id, obj); }
|
||||
|
||||
void ClassObject::createMember(tp::String type, tp::String id) {
|
||||
auto newo = NDO->create(type);
|
||||
members->put(id, newo);
|
||||
}
|
||||
|
||||
alni ClassObject::save_size(ClassObject* self) {
|
||||
return sizeof(alni); // dict object adress
|
||||
}
|
||||
|
||||
void ClassObject::save(ClassObject* self, ArchiverOut& file_self) {
|
||||
// save dictobject
|
||||
alni ndo_object_adress = NDO->save(file_self, self->members);
|
||||
file_self << ndo_object_adress;
|
||||
}
|
||||
|
||||
void ClassObject::load(ArchiverIn& file_self, ClassObject* self) {
|
||||
alni ndo_object_adress;
|
||||
file_self >> ndo_object_adress;
|
||||
self->members = NDO_CAST(DictObject, NDO->load(file_self, ndo_object_adress));
|
||||
NDO->refinc(self->members);
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> childs_retrival(ClassObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
out.append(self->members);
|
||||
return out;
|
||||
}
|
||||
|
||||
alni allocated_size(ClassObject* self) { return sizeof(DictObject*); }
|
||||
|
||||
alni allocated_size_recursive(ClassObject* self) {
|
||||
alni out = sizeof(DictObject*);
|
||||
out += NDO->objsize_ram_recursive_util(self->members, self->members->type);
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ObjectType ClassObject::TypeData = { .base = NULL,
|
||||
.constructor = (object_constructor) ClassObject::constructor,
|
||||
.destructor = (object_destructor) ClassObject::destructor,
|
||||
.copy = (object_copy) ClassObject::copy,
|
||||
.size = sizeof(ClassObject),
|
||||
.name = "class",
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival) childs_retrival,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive) allocated_size_recursive };
|
||||
|
|
|
|||
|
|
@ -1,84 +1,66 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/colorobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void ColorObject::constructor(Object* self) {
|
||||
NDO_CAST(ColorObject, self)->mCol = tp::RGBA(1.f);
|
||||
}
|
||||
|
||||
void ColorObject::copy(ColorObject* self, const ColorObject* in) {
|
||||
self->mCol = in->mCol;
|
||||
}
|
||||
|
||||
ColorObject* ColorObject::create(tp::RGBA in) {
|
||||
NDO_CASTV(ColorObject, NDO->create("RGBA"), out)->mCol = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
void ColorObject::from_int(ColorObject* self, alni in) {
|
||||
self->mCol = tp::RGBA((tp::halnf)tp::clamp(in, (tp::alni) 0, (tp::alni) 1));
|
||||
}
|
||||
|
||||
void ColorObject::from_float(ColorObject* self, alnf in) {
|
||||
NDO_CAST(ColorObject, self)->mCol = tp::RGBA(tp::clamp((tp::halnf)in, 0.f, 1.f));
|
||||
}
|
||||
|
||||
String ColorObject::to_string(ColorObject* self) {
|
||||
// auto &col = NDO_CAST(ColorObject, self)->mCol;
|
||||
// return tp::sfmt("%i:%i:%i:%i", (tp::alni) (col.r * 255), (tp::alni)(col.g * 255), (tp::alni)(col.b * 255), (tp::alni)(col.a * 255));
|
||||
// TODO : implement
|
||||
return {};
|
||||
}
|
||||
|
||||
static alni save_size(ColorObject* self) {
|
||||
return sizeof(tp::RGBA);
|
||||
}
|
||||
|
||||
static void save(ColorObject* self, ArchiverOut& file_self) {
|
||||
file_self << self->mCol;
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, ColorObject* self) {
|
||||
file_self >> self->mCol;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions ColorObjectTypeConversions = {
|
||||
.from_int = (object_from_int) ColorObject::from_int,
|
||||
.from_float = (object_from_float) ColorObject::from_float,
|
||||
.from_string = NULL,
|
||||
.to_string = (object_to_string) ColorObject::to_string,
|
||||
.to_int = NULL,
|
||||
.to_float = NULL,
|
||||
};
|
||||
|
||||
void sub(ColorObject* self, ColorObject* in) {
|
||||
self->mCol = in->mCol - self->mCol;
|
||||
}
|
||||
|
||||
void add(ColorObject* self, ColorObject* in) {
|
||||
self->mCol = in->mCol + self->mCol;
|
||||
}
|
||||
|
||||
struct ObjectTypeAriphmetics ColorObject::TypeAriphm = {
|
||||
.add = (object_add) add,
|
||||
.sub = (object_sub) sub,
|
||||
.mul = (object_mul) NULL,
|
||||
.div = (object_div) NULL,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::ColorObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = ColorObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) ColorObject::copy,
|
||||
.size = sizeof(ColorObject),
|
||||
.name = "RGBA",
|
||||
.convesions = &ColorObjectTypeConversions,
|
||||
.ariphmetics = &ColorObject::TypeAriphm,
|
||||
.save_size = (object_save_size)save_size,
|
||||
.save = (object_save)save,
|
||||
.load = (object_load)load,
|
||||
|
||||
#include "primitives/colorobject.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void ColorObject::constructor(Object* self) { NDO_CAST(ColorObject, self)->mCol = tp::RGBA(1.f); }
|
||||
|
||||
void ColorObject::copy(ColorObject* self, const ColorObject* in) { self->mCol = in->mCol; }
|
||||
|
||||
ColorObject* ColorObject::create(tp::RGBA in) {
|
||||
NDO_CASTV(ColorObject, NDO->create("RGBA"), out)->mCol = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
void ColorObject::from_int(ColorObject* self, alni in) { self->mCol = tp::RGBA((tp::halnf) tp::clamp(in, (tp::alni) 0, (tp::alni) 1)); }
|
||||
|
||||
void ColorObject::from_float(ColorObject* self, alnf in) { NDO_CAST(ColorObject, self)->mCol = tp::RGBA(tp::clamp((tp::halnf) in, 0.f, 1.f)); }
|
||||
|
||||
String ColorObject::to_string(ColorObject* self) {
|
||||
// auto &col = NDO_CAST(ColorObject, self)->mCol;
|
||||
// return tp::sfmt("%i:%i:%i:%i", (tp::alni) (col.r * 255), (tp::alni)(col.g * 255), (tp::alni)(col.b * 255), (tp::alni)(col.a * 255));
|
||||
// TODO : implement
|
||||
return {};
|
||||
}
|
||||
|
||||
static alni save_size(ColorObject* self) { return sizeof(tp::RGBA); }
|
||||
|
||||
static void save(ColorObject* self, ArchiverOut& file_self) { file_self << self->mCol; }
|
||||
|
||||
static void load(ArchiverIn& file_self, ColorObject* self) { file_self >> self->mCol; }
|
||||
|
||||
struct ObjectTypeConversions ColorObjectTypeConversions = {
|
||||
.from_int = (object_from_int) ColorObject::from_int,
|
||||
.from_float = (object_from_float) ColorObject::from_float,
|
||||
.from_string = NULL,
|
||||
.to_string = (object_to_string) ColorObject::to_string,
|
||||
.to_int = NULL,
|
||||
.to_float = NULL,
|
||||
};
|
||||
|
||||
void sub(ColorObject* self, ColorObject* in) { self->mCol = in->mCol - self->mCol; }
|
||||
|
||||
void add(ColorObject* self, ColorObject* in) { self->mCol = in->mCol + self->mCol; }
|
||||
|
||||
struct ObjectTypeAriphmetics ColorObject::TypeAriphm = {
|
||||
.add = (object_add) add,
|
||||
.sub = (object_sub) sub,
|
||||
.mul = (object_mul) NULL,
|
||||
.div = (object_div) NULL,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::ColorObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = ColorObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) ColorObject::copy,
|
||||
.size = sizeof(ColorObject),
|
||||
.name = "RGBA",
|
||||
.convesions = &ColorObjectTypeConversions,
|
||||
.ariphmetics = &ColorObject::TypeAriphm,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
};
|
||||
|
|
@ -1,234 +1,217 @@
|
|||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/dictobject.h"
|
||||
#include "primitives/stringobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void DictObject::constructor(Object* self) {
|
||||
NDO_CASTV(DictObject, self, dict);
|
||||
|
||||
new (&dict->items) Map<String, Object*>();
|
||||
}
|
||||
|
||||
void DictObject::copy(Object* in, const Object* target) {
|
||||
NDO_CASTV(DictObject, in, self);
|
||||
NDO_CASTV(DictObject, target, src);
|
||||
|
||||
destructor(self);
|
||||
constructor(self);
|
||||
|
||||
for (auto item : src->items) {
|
||||
auto instance = NDO->instatiate(item->val);
|
||||
self->items.put(item->key, instance);
|
||||
}
|
||||
}
|
||||
|
||||
void DictObject::destructor(Object* self) {
|
||||
NDO_CASTV(DictObject, self, dict);
|
||||
for (auto item : dict->items) {
|
||||
NDO->destroy(item->val);
|
||||
}
|
||||
dict->items.~Map();
|
||||
}
|
||||
|
||||
alni DictObject::save_size(DictObject* self) {
|
||||
// calculate size needed
|
||||
alni save_size = 0;
|
||||
|
||||
// number on entries
|
||||
save_size += sizeof(alni);
|
||||
|
||||
for (auto item : self->items) {
|
||||
// string length
|
||||
save_size += tp::SaveSizeCounter::calc(item->key);
|
||||
|
||||
// object file adress
|
||||
save_size += sizeof(alni);
|
||||
}
|
||||
|
||||
return save_size;
|
||||
}
|
||||
|
||||
void DictObject::save(DictObject* self, ArchiverOut& file_self) {
|
||||
|
||||
// write size
|
||||
alni len = self->items.size();
|
||||
file_self << len;
|
||||
|
||||
// save hashmap pairs
|
||||
for (auto item : self->items) {
|
||||
// item val
|
||||
alni ndo_object_adress = NDO->save(file_self, item->val);
|
||||
file_self << ndo_object_adress;
|
||||
|
||||
// item key
|
||||
file_self << item->key;
|
||||
}
|
||||
}
|
||||
|
||||
void DictObject::load(ArchiverIn& file_self, DictObject* self) {
|
||||
new (&self->items) tp::Map<tp::String, Object*>();
|
||||
|
||||
alni len;
|
||||
file_self >> len;
|
||||
|
||||
for (alni i = 0; i < len; i++) {
|
||||
|
||||
// read val
|
||||
alni ndo_object_adress;
|
||||
file_self >> ndo_object_adress;
|
||||
Object* val = NDO->load(file_self, ndo_object_adress);
|
||||
|
||||
// read key value
|
||||
String key;
|
||||
file_self >> key;
|
||||
|
||||
// add to dictinary
|
||||
self->put(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> DictObject::childs_retrival(DictObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
out.reserve(self->items.size());
|
||||
ualni i = 0;
|
||||
for (auto item : self->items) {
|
||||
out[i] = item->val;
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
alni DictObject::allocated_size(DictObject* self) {
|
||||
ASSERT(false)
|
||||
// alni out = self->items.sizeAllocatedMem();
|
||||
for (auto item : self->items) {
|
||||
// out += item->key.sizeAllocatedMem();
|
||||
}
|
||||
// return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
alni DictObject::allocated_size_recursive(DictObject* self) {
|
||||
alni out = allocated_size(self);
|
||||
for (auto item : self->items) {
|
||||
out += NDO->objsize_ram_recursive_util(item->val, item->val->type);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void DictObject::put(tp::String str, Object* obj) {
|
||||
DEBUG_ASSERT(obj);
|
||||
NDO->refinc(obj);
|
||||
items.put(str, obj);
|
||||
}
|
||||
|
||||
void DictObject::remove(tp::String str) {
|
||||
auto idx = items.presents(str);
|
||||
if (idx) {
|
||||
NDO->destroy(items.getSlotVal(idx));
|
||||
items.remove(str);
|
||||
}
|
||||
}
|
||||
|
||||
Object* DictObject::get(tp::String str) {
|
||||
return items.get(str);
|
||||
}
|
||||
|
||||
tp::Map<tp::String, Object*>::Idx DictObject::presents(tp::String str) {
|
||||
return items.presents(str);
|
||||
}
|
||||
|
||||
Object* DictObject::getSlotVal(tp::Map<tp::String, Object*>::Idx idx) {
|
||||
return items.getSlotVal(idx);
|
||||
}
|
||||
|
||||
const tp::Map<tp::String, Object*>& DictObject::getItems() const {
|
||||
return items;
|
||||
}
|
||||
|
||||
static auto tm_get = TypeMethod{
|
||||
.nameid = "get",
|
||||
.descr = "gets the object",
|
||||
.args = {
|
||||
{ "str key", NULL }
|
||||
},
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
auto const self = (DictObject*)tm->self;
|
||||
auto str_key = tm->args[0].obj;
|
||||
|
||||
NDO_CASTV(StringObject, str_key, key);
|
||||
ASSERT(key);
|
||||
|
||||
auto idx = self->presents(key->val);
|
||||
if (idx) {
|
||||
tm->ret.obj = self->getSlotVal(idx);
|
||||
}
|
||||
},
|
||||
.ret = { "object", NULL }
|
||||
};
|
||||
|
||||
static auto tm_put = TypeMethod{
|
||||
.nameid = "put",
|
||||
.descr = "puts the object into the dictinary",
|
||||
.args = {
|
||||
{ "key", NULL },
|
||||
{ "object", NULL }
|
||||
},
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
auto const self = (DictObject*)tm->self;
|
||||
|
||||
auto str_key = tm->args[0].obj;
|
||||
auto obj = tm->args[1].obj;
|
||||
|
||||
NDO_CASTV(StringObject, str_key, key);
|
||||
ASSERT(key);
|
||||
|
||||
self->put(key->val, obj);
|
||||
},
|
||||
};
|
||||
|
||||
static auto tm_remove = TypeMethod{
|
||||
.nameid = "remove",
|
||||
.descr = "remove the object from the dictinary",
|
||||
.args = {
|
||||
{ "key", NULL },
|
||||
},
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
auto const self = (DictObject*)tm->self;
|
||||
|
||||
auto str_key = tm->args[0].obj;
|
||||
|
||||
NDO_CASTV(StringObject, str_key, key);
|
||||
ASSERT(key);
|
||||
|
||||
self->remove(key->val);
|
||||
},
|
||||
};
|
||||
|
||||
struct obj::ObjectType DictObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = DictObject::constructor,
|
||||
.destructor = DictObject::destructor,
|
||||
.copy = DictObject::copy,
|
||||
.size = sizeof(DictObject),
|
||||
.name = "dict",
|
||||
.save_size = (object_save_size)DictObject::save_size,
|
||||
.save = (object_save)DictObject::save,
|
||||
.load = (object_load)DictObject::load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival)DictObject::childs_retrival,
|
||||
.allocated_size = (object_allocated_size)DictObject::allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive)DictObject::allocated_size_recursive,
|
||||
|
||||
.type_methods = {
|
||||
.methods = {
|
||||
&tm_put,
|
||||
&tm_remove,
|
||||
&tm_get,
|
||||
}
|
||||
}
|
||||
};
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/dictobject.h"
|
||||
#include "primitives/stringobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void DictObject::constructor(Object* self) {
|
||||
NDO_CASTV(DictObject, self, dict);
|
||||
|
||||
new (&dict->items) Map<String, Object*>();
|
||||
}
|
||||
|
||||
void DictObject::copy(Object* in, const Object* target) {
|
||||
NDO_CASTV(DictObject, in, self);
|
||||
NDO_CASTV(DictObject, target, src);
|
||||
|
||||
destructor(self);
|
||||
constructor(self);
|
||||
|
||||
for (auto item : src->items) {
|
||||
auto instance = NDO->instatiate(item->val);
|
||||
self->items.put(item->key, instance);
|
||||
}
|
||||
}
|
||||
|
||||
void DictObject::destructor(Object* self) {
|
||||
NDO_CASTV(DictObject, self, dict);
|
||||
for (auto item : dict->items) {
|
||||
NDO->destroy(item->val);
|
||||
}
|
||||
dict->items.~Map();
|
||||
}
|
||||
|
||||
alni DictObject::save_size(DictObject* self) {
|
||||
// calculate size needed
|
||||
alni save_size = 0;
|
||||
|
||||
// number on entries
|
||||
save_size += sizeof(alni);
|
||||
|
||||
for (auto item : self->items) {
|
||||
// string length
|
||||
save_size += tp::SaveSizeCounter::calc(item->key);
|
||||
|
||||
// object file adress
|
||||
save_size += sizeof(alni);
|
||||
}
|
||||
|
||||
return save_size;
|
||||
}
|
||||
|
||||
void DictObject::save(DictObject* self, ArchiverOut& file_self) {
|
||||
|
||||
// write size
|
||||
alni len = self->items.size();
|
||||
file_self << len;
|
||||
|
||||
// save hashmap pairs
|
||||
for (auto item : self->items) {
|
||||
// item val
|
||||
alni ndo_object_adress = NDO->save(file_self, item->val);
|
||||
file_self << ndo_object_adress;
|
||||
|
||||
// item key
|
||||
file_self << item->key;
|
||||
}
|
||||
}
|
||||
|
||||
void DictObject::load(ArchiverIn& file_self, DictObject* self) {
|
||||
new (&self->items) tp::Map<tp::String, Object*>();
|
||||
|
||||
alni len;
|
||||
file_self >> len;
|
||||
|
||||
for (alni i = 0; i < len; i++) {
|
||||
|
||||
// read val
|
||||
alni ndo_object_adress;
|
||||
file_self >> ndo_object_adress;
|
||||
Object* val = NDO->load(file_self, ndo_object_adress);
|
||||
|
||||
// read key value
|
||||
String key;
|
||||
file_self >> key;
|
||||
|
||||
// add to dictinary
|
||||
self->put(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> DictObject::childs_retrival(DictObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
out.reserve(self->items.size());
|
||||
ualni i = 0;
|
||||
for (auto item : self->items) {
|
||||
out[i] = item->val;
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
alni DictObject::allocated_size(DictObject* self) {
|
||||
ASSERT(false)
|
||||
// alni out = self->items.sizeAllocatedMem();
|
||||
for (auto item : self->items) {
|
||||
// out += item->key.sizeAllocatedMem();
|
||||
}
|
||||
// return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
alni DictObject::allocated_size_recursive(DictObject* self) {
|
||||
alni out = allocated_size(self);
|
||||
for (auto item : self->items) {
|
||||
out += NDO->objsize_ram_recursive_util(item->val, item->val->type);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void DictObject::put(tp::String str, Object* obj) {
|
||||
DEBUG_ASSERT(obj);
|
||||
NDO->refinc(obj);
|
||||
items.put(str, obj);
|
||||
}
|
||||
|
||||
void DictObject::remove(tp::String str) {
|
||||
auto idx = items.presents(str);
|
||||
if (idx) {
|
||||
NDO->destroy(items.getSlotVal(idx));
|
||||
items.remove(str);
|
||||
}
|
||||
}
|
||||
|
||||
Object* DictObject::get(tp::String str) { return items.get(str); }
|
||||
|
||||
tp::Map<tp::String, Object*>::Idx DictObject::presents(tp::String str) { return items.presents(str); }
|
||||
|
||||
Object* DictObject::getSlotVal(tp::Map<tp::String, Object*>::Idx idx) { return items.getSlotVal(idx); }
|
||||
|
||||
const tp::Map<tp::String, Object*>& DictObject::getItems() const { return items; }
|
||||
|
||||
static auto tm_get = TypeMethod{ .nameid = "get",
|
||||
.descr = "gets the object",
|
||||
.args = { { "str key", NULL } },
|
||||
.exec =
|
||||
[](const TypeMethod* tm) {
|
||||
auto const self = (DictObject*) tm->self;
|
||||
auto str_key = tm->args[0].obj;
|
||||
|
||||
NDO_CASTV(StringObject, str_key, key);
|
||||
ASSERT(key);
|
||||
|
||||
auto idx = self->presents(key->val);
|
||||
if (idx) {
|
||||
tm->ret.obj = self->getSlotVal(idx);
|
||||
}
|
||||
},
|
||||
.ret = { "object", NULL } };
|
||||
|
||||
static auto tm_put = TypeMethod{
|
||||
.nameid = "put",
|
||||
.descr = "puts the object into the dictinary",
|
||||
.args = { { "key", NULL }, { "object", NULL } },
|
||||
.exec =
|
||||
[](const TypeMethod* tm) {
|
||||
auto const self = (DictObject*) tm->self;
|
||||
|
||||
auto str_key = tm->args[0].obj;
|
||||
auto obj = tm->args[1].obj;
|
||||
|
||||
NDO_CASTV(StringObject, str_key, key);
|
||||
ASSERT(key);
|
||||
|
||||
self->put(key->val, obj);
|
||||
},
|
||||
};
|
||||
|
||||
static auto tm_remove = TypeMethod{
|
||||
.nameid = "remove",
|
||||
.descr = "remove the object from the dictinary",
|
||||
.args = {
|
||||
{ "key", NULL },
|
||||
},
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
auto const self = (DictObject*)tm->self;
|
||||
|
||||
auto str_key = tm->args[0].obj;
|
||||
|
||||
NDO_CASTV(StringObject, str_key, key);
|
||||
ASSERT(key);
|
||||
|
||||
self->remove(key->val);
|
||||
},
|
||||
};
|
||||
|
||||
struct obj::ObjectType DictObject::TypeData = { .base = NULL,
|
||||
.constructor = DictObject::constructor,
|
||||
.destructor = DictObject::destructor,
|
||||
.copy = DictObject::copy,
|
||||
.size = sizeof(DictObject),
|
||||
.name = "dict",
|
||||
.save_size = (object_save_size) DictObject::save_size,
|
||||
.save = (object_save) DictObject::save,
|
||||
.load = (object_load) DictObject::load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival) DictObject::childs_retrival,
|
||||
.allocated_size = (object_allocated_size) DictObject::allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive) DictObject::allocated_size_recursive,
|
||||
|
||||
.type_methods = { .methods = {
|
||||
&tm_put,
|
||||
&tm_remove,
|
||||
&tm_get,
|
||||
} } };
|
||||
|
|
|
|||
|
|
@ -1,182 +1,177 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/enumobject.h"
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void EnumObject::constructor(EnumObject* self) {
|
||||
self->active = 0;
|
||||
self->nentries = 0;
|
||||
self->entries = NULL;
|
||||
}
|
||||
|
||||
void obj::EnumObject::destructor(EnumObject* self) {
|
||||
if (self->entries) free(self->entries);
|
||||
}
|
||||
|
||||
void EnumObject::copy(EnumObject* self, const EnumObject* in) {
|
||||
if (self->entries) free(self->entries);
|
||||
self->active = in->active;
|
||||
self->nentries = in->nentries;
|
||||
|
||||
self->entries = (alni*)malloc(self->nentries * ENV_ALNI_SIZE_B);
|
||||
tp::memCopy(self->entries, in->entries, self->nentries * ENV_ALNI_SIZE_B);
|
||||
}
|
||||
|
||||
void obj::EnumObject::init(tp::InitialierList<const char*> list) {
|
||||
|
||||
if (entries) free(entries);
|
||||
|
||||
active = 0;
|
||||
nentries = (uhalni) list.size();
|
||||
entries = (alni*) malloc(nentries * ENV_ALNI_SIZE_B);
|
||||
tp::memSetVal(entries, nentries * ENV_ALNI_SIZE_B, 0);
|
||||
|
||||
alni* entry = entries;
|
||||
for (auto elem : list) {
|
||||
|
||||
alni len = tp::String::Logic::calcLength(elem);
|
||||
if (len > ENV_ALNI_SIZE_B - 1) len = ENV_ALNI_SIZE_B - 1;
|
||||
|
||||
tp::memCopy(entry, elem, len);
|
||||
|
||||
for (alni* chech_entry = entries; chech_entry != entry; chech_entry++) {
|
||||
DEBUG_ASSERT(tp::memCompare(chech_entry, entry, ENV_ALNI_SIZE_B) != 0);
|
||||
}
|
||||
|
||||
entry++;
|
||||
}
|
||||
}
|
||||
|
||||
const char* obj::EnumObject::getActiveName() {
|
||||
return getItemName(active);
|
||||
}
|
||||
|
||||
const char* obj::EnumObject::getItemName(tp::uhalni idx) {
|
||||
DEBUG_ASSERT(entries && idx >= 0 && idx < nentries);
|
||||
return (const char*) (entries + idx);
|
||||
}
|
||||
|
||||
|
||||
void EnumObject::from_int(EnumObject* self, alni in) {
|
||||
if (self->entries && in >= 0 && in < self->nentries) {
|
||||
self->active = uhalni(in);
|
||||
}
|
||||
}
|
||||
|
||||
void EnumObject::from_float(EnumObject* self, alnf in) {
|
||||
if (self->entries && in >= 0 && in < self->nentries) {
|
||||
self->active = uhalni(in);
|
||||
}
|
||||
}
|
||||
|
||||
void EnumObject::from_string(EnumObject* self, String in) {
|
||||
if (self->entries) {
|
||||
alni* entry = self->entries;
|
||||
for (uhalni i = 0; i < self->nentries; i++) {
|
||||
if (tp::String::Logic::isEqualLogic((const char*)entry, in.read())) {
|
||||
self->active = i;
|
||||
}
|
||||
entry += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String EnumObject::to_string(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return tp::String();
|
||||
}
|
||||
auto val = (const char*)(&self->entries[self->active]);
|
||||
return String(val);
|
||||
}
|
||||
|
||||
alni EnumObject::to_int(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return -1;
|
||||
}
|
||||
return alni(self->active);
|
||||
}
|
||||
|
||||
alnf EnumObject::to_float(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return -1;
|
||||
}
|
||||
return alnf(self->active);
|
||||
}
|
||||
|
||||
static alni save_size(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return sizeof(uhalni);
|
||||
}
|
||||
return sizeof(uhalni) + sizeof(uhalni) + sizeof(alni) * self->nentries;
|
||||
}
|
||||
|
||||
static void save(EnumObject* self, ArchiverOut& file_self) {
|
||||
if (!self->entries) {
|
||||
uhalni empty_code = -1;
|
||||
file_self << empty_code;
|
||||
return;
|
||||
}
|
||||
file_self << self->active;
|
||||
file_self << self->nentries;
|
||||
file_self.writeBytes((tp::int1*) self->entries, self->nentries * ENV_ALNI_SIZE_B);
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, EnumObject* self) {
|
||||
file_self >> self->active;
|
||||
if (self->active == -1) {
|
||||
self->nentries = 0;
|
||||
self->entries = NULL;
|
||||
return;
|
||||
}
|
||||
file_self >> self->nentries;
|
||||
self->entries = (alni*) malloc(self->nentries * ENV_ALNI_SIZE_B);
|
||||
file_self.readBytes((tp::int1*) self->entries, self->nentries * ENV_ALNI_SIZE_B);
|
||||
}
|
||||
|
||||
bool obj::EnumObject::compare(EnumObject* first, EnumObject* second) {
|
||||
return first->entries != NULL && second->entries != NULL && first->active == second->active;
|
||||
}
|
||||
|
||||
EnumObject* obj::EnumObject::create(tp::InitialierList<const char*> list) {
|
||||
auto enum_object = (EnumObject*)obj::NDO->create("enum");
|
||||
enum_object->init(list);
|
||||
return enum_object;
|
||||
}
|
||||
|
||||
alni allocated_size(EnumObject* self) {
|
||||
alni out = sizeof(uhalni) * 2 + sizeof(tp::alni*);
|
||||
if (self->entries) {
|
||||
out += self->nentries * sizeof(alni) * 2;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions EnumObjectTypeConversions = {
|
||||
.from_int = (object_from_int) EnumObject::from_int,
|
||||
.from_float = (object_from_float) EnumObject::from_float,
|
||||
.from_string = (object_from_string) EnumObject::from_string,
|
||||
.to_string = (object_to_string) EnumObject::to_string,
|
||||
.to_int = (object_to_int) EnumObject::to_int,
|
||||
.to_float = (object_to_float) EnumObject::to_float,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::EnumObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) EnumObject::constructor,
|
||||
.destructor = (object_destructor) EnumObject::destructor,
|
||||
.copy = (object_copy) EnumObject::copy,
|
||||
.size = sizeof(EnumObject),
|
||||
.name = "enum",
|
||||
.convesions = &EnumObjectTypeConversions,
|
||||
.save_size = (object_save_size)save_size,
|
||||
.save = (object_save)save,
|
||||
.load = (object_load)load,
|
||||
.comparison = (object_compare) EnumObject::compare,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "primitives/enumobject.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void EnumObject::constructor(EnumObject* self) {
|
||||
self->active = 0;
|
||||
self->nentries = 0;
|
||||
self->entries = NULL;
|
||||
}
|
||||
|
||||
void obj::EnumObject::destructor(EnumObject* self) {
|
||||
if (self->entries) free(self->entries);
|
||||
}
|
||||
|
||||
void EnumObject::copy(EnumObject* self, const EnumObject* in) {
|
||||
if (self->entries) free(self->entries);
|
||||
self->active = in->active;
|
||||
self->nentries = in->nentries;
|
||||
|
||||
self->entries = (alni*) malloc(self->nentries * ENV_ALNI_SIZE_B);
|
||||
tp::memCopy(self->entries, in->entries, self->nentries * ENV_ALNI_SIZE_B);
|
||||
}
|
||||
|
||||
void obj::EnumObject::init(tp::InitialierList<const char*> list) {
|
||||
|
||||
if (entries) free(entries);
|
||||
|
||||
active = 0;
|
||||
nentries = (uhalni) list.size();
|
||||
entries = (alni*) malloc(nentries * ENV_ALNI_SIZE_B);
|
||||
tp::memSetVal(entries, nentries * ENV_ALNI_SIZE_B, 0);
|
||||
|
||||
alni* entry = entries;
|
||||
for (auto elem : list) {
|
||||
|
||||
alni len = tp::String::Logic::calcLength(elem);
|
||||
if (len > ENV_ALNI_SIZE_B - 1) len = ENV_ALNI_SIZE_B - 1;
|
||||
|
||||
tp::memCopy(entry, elem, len);
|
||||
|
||||
for (alni* chech_entry = entries; chech_entry != entry; chech_entry++) {
|
||||
DEBUG_ASSERT(tp::memCompare(chech_entry, entry, ENV_ALNI_SIZE_B) != 0);
|
||||
}
|
||||
|
||||
entry++;
|
||||
}
|
||||
}
|
||||
|
||||
const char* obj::EnumObject::getActiveName() { return getItemName(active); }
|
||||
|
||||
const char* obj::EnumObject::getItemName(tp::uhalni idx) {
|
||||
DEBUG_ASSERT(entries && idx >= 0 && idx < nentries);
|
||||
return (const char*) (entries + idx);
|
||||
}
|
||||
|
||||
void EnumObject::from_int(EnumObject* self, alni in) {
|
||||
if (self->entries && in >= 0 && in < self->nentries) {
|
||||
self->active = uhalni(in);
|
||||
}
|
||||
}
|
||||
|
||||
void EnumObject::from_float(EnumObject* self, alnf in) {
|
||||
if (self->entries && in >= 0 && in < self->nentries) {
|
||||
self->active = uhalni(in);
|
||||
}
|
||||
}
|
||||
|
||||
void EnumObject::from_string(EnumObject* self, String in) {
|
||||
if (self->entries) {
|
||||
alni* entry = self->entries;
|
||||
for (uhalni i = 0; i < self->nentries; i++) {
|
||||
if (tp::String::Logic::isEqualLogic((const char*) entry, in.read())) {
|
||||
self->active = i;
|
||||
}
|
||||
entry += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String EnumObject::to_string(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return tp::String();
|
||||
}
|
||||
auto val = (const char*) (&self->entries[self->active]);
|
||||
return String(val);
|
||||
}
|
||||
|
||||
alni EnumObject::to_int(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return -1;
|
||||
}
|
||||
return alni(self->active);
|
||||
}
|
||||
|
||||
alnf EnumObject::to_float(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return -1;
|
||||
}
|
||||
return alnf(self->active);
|
||||
}
|
||||
|
||||
static alni save_size(EnumObject* self) {
|
||||
if (!self->entries) {
|
||||
return sizeof(uhalni);
|
||||
}
|
||||
return sizeof(uhalni) + sizeof(uhalni) + sizeof(alni) * self->nentries;
|
||||
}
|
||||
|
||||
static void save(EnumObject* self, ArchiverOut& file_self) {
|
||||
if (!self->entries) {
|
||||
uhalni empty_code = -1;
|
||||
file_self << empty_code;
|
||||
return;
|
||||
}
|
||||
file_self << self->active;
|
||||
file_self << self->nentries;
|
||||
file_self.writeBytes((tp::int1*) self->entries, self->nentries * ENV_ALNI_SIZE_B);
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, EnumObject* self) {
|
||||
file_self >> self->active;
|
||||
if (self->active == -1) {
|
||||
self->nentries = 0;
|
||||
self->entries = NULL;
|
||||
return;
|
||||
}
|
||||
file_self >> self->nentries;
|
||||
self->entries = (alni*) malloc(self->nentries * ENV_ALNI_SIZE_B);
|
||||
file_self.readBytes((tp::int1*) self->entries, self->nentries * ENV_ALNI_SIZE_B);
|
||||
}
|
||||
|
||||
bool obj::EnumObject::compare(EnumObject* first, EnumObject* second) { return first->entries != NULL && second->entries != NULL && first->active == second->active; }
|
||||
|
||||
EnumObject* obj::EnumObject::create(tp::InitialierList<const char*> list) {
|
||||
auto enum_object = (EnumObject*) obj::NDO->create("enum");
|
||||
enum_object->init(list);
|
||||
return enum_object;
|
||||
}
|
||||
|
||||
alni allocated_size(EnumObject* self) {
|
||||
alni out = sizeof(uhalni) * 2 + sizeof(tp::alni*);
|
||||
if (self->entries) {
|
||||
out += self->nentries * sizeof(alni) * 2;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions EnumObjectTypeConversions = {
|
||||
.from_int = (object_from_int) EnumObject::from_int,
|
||||
.from_float = (object_from_float) EnumObject::from_float,
|
||||
.from_string = (object_from_string) EnumObject::from_string,
|
||||
.to_string = (object_to_string) EnumObject::to_string,
|
||||
.to_int = (object_to_int) EnumObject::to_int,
|
||||
.to_float = (object_to_float) EnumObject::to_float,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::EnumObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) EnumObject::constructor,
|
||||
.destructor = (object_destructor) EnumObject::destructor,
|
||||
.copy = (object_copy) EnumObject::copy,
|
||||
.size = sizeof(EnumObject),
|
||||
.name = "enum",
|
||||
.convesions = &EnumObjectTypeConversions,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
.comparison = (object_compare) EnumObject::compare,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
};
|
||||
|
|
@ -1,102 +1,72 @@
|
|||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/floatobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void FloatObject::constructor(FloatObject* self) {
|
||||
self->val = 0;
|
||||
}
|
||||
|
||||
void FloatObject::copy(FloatObject* self, const FloatObject* in) {
|
||||
self->val = in->val;
|
||||
}
|
||||
|
||||
FloatObject* FloatObject::create(alnf in) {
|
||||
NDO_CASTV(FloatObject, NDO->create("float"), out)->val = alnf(in);
|
||||
return out;
|
||||
}
|
||||
|
||||
void FloatObject::from_int(FloatObject* self, alni in) {
|
||||
self->val = alnf(in);
|
||||
}
|
||||
|
||||
void FloatObject::from_float(FloatObject* self, alnf in) {
|
||||
self->val = in;
|
||||
}
|
||||
|
||||
void FloatObject::from_string(FloatObject* self, String in) {
|
||||
self->val = alnf(in);
|
||||
}
|
||||
|
||||
String FloatObject::to_string(FloatObject* self) {
|
||||
return String(self->val);
|
||||
}
|
||||
|
||||
alni FloatObject::to_int(FloatObject* self) {
|
||||
return alni(self->val);
|
||||
}
|
||||
|
||||
alnf FloatObject::to_float(FloatObject* self) {
|
||||
return self->val;
|
||||
}
|
||||
|
||||
static alni save_size(FloatObject* self) {
|
||||
return sizeof(alnf);
|
||||
}
|
||||
|
||||
static void save(FloatObject* self, ArchiverOut& file_self) {
|
||||
file_self << self->val;
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, FloatObject* self) {
|
||||
file_self >> self->val;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions FloatObjectTypeConversions = {
|
||||
.from_int = (object_from_int) FloatObject::from_int,
|
||||
.from_float = (object_from_float) FloatObject::from_float,
|
||||
.from_string = (object_from_string) FloatObject::from_string,
|
||||
.to_string = (object_to_string) FloatObject::to_string,
|
||||
.to_int = (object_to_int) FloatObject::to_int,
|
||||
.to_float = (object_to_float) FloatObject::to_float,
|
||||
};
|
||||
|
||||
static void mul(FloatObject* self, FloatObject* in) {
|
||||
self->val *= in->val;
|
||||
}
|
||||
|
||||
static void sub(FloatObject* self, FloatObject* in) {
|
||||
self->val -= in->val;
|
||||
}
|
||||
|
||||
static void add(FloatObject* self, FloatObject* in) {
|
||||
self->val += in->val;
|
||||
}
|
||||
|
||||
static void divide(FloatObject* self, FloatObject* in) {
|
||||
self->val /= in->val;
|
||||
}
|
||||
|
||||
struct ObjectTypeAriphmetics FloatObject::TypeAriphm = {
|
||||
.add = (object_add)add,
|
||||
.sub = (object_sub)sub,
|
||||
.mul = (object_mul)mul,
|
||||
.div = (object_div)divide,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::FloatObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) FloatObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) FloatObject::copy,
|
||||
.size = sizeof(FloatObject),
|
||||
.name = "float",
|
||||
.convesions = &FloatObjectTypeConversions,
|
||||
.ariphmetics = &FloatObject::TypeAriphm,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
#pragma once
|
||||
|
||||
#include "primitives/floatobject.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void FloatObject::constructor(FloatObject* self) { self->val = 0; }
|
||||
|
||||
void FloatObject::copy(FloatObject* self, const FloatObject* in) { self->val = in->val; }
|
||||
|
||||
FloatObject* FloatObject::create(alnf in) {
|
||||
NDO_CASTV(FloatObject, NDO->create("float"), out)->val = alnf(in);
|
||||
return out;
|
||||
}
|
||||
|
||||
void FloatObject::from_int(FloatObject* self, alni in) { self->val = alnf(in); }
|
||||
|
||||
void FloatObject::from_float(FloatObject* self, alnf in) { self->val = in; }
|
||||
|
||||
void FloatObject::from_string(FloatObject* self, String in) { self->val = alnf(in); }
|
||||
|
||||
String FloatObject::to_string(FloatObject* self) { return String(self->val); }
|
||||
|
||||
alni FloatObject::to_int(FloatObject* self) { return alni(self->val); }
|
||||
|
||||
alnf FloatObject::to_float(FloatObject* self) { return self->val; }
|
||||
|
||||
static alni save_size(FloatObject* self) { return sizeof(alnf); }
|
||||
|
||||
static void save(FloatObject* self, ArchiverOut& file_self) { file_self << self->val; }
|
||||
|
||||
static void load(ArchiverIn& file_self, FloatObject* self) { file_self >> self->val; }
|
||||
|
||||
struct ObjectTypeConversions FloatObjectTypeConversions = {
|
||||
.from_int = (object_from_int) FloatObject::from_int,
|
||||
.from_float = (object_from_float) FloatObject::from_float,
|
||||
.from_string = (object_from_string) FloatObject::from_string,
|
||||
.to_string = (object_to_string) FloatObject::to_string,
|
||||
.to_int = (object_to_int) FloatObject::to_int,
|
||||
.to_float = (object_to_float) FloatObject::to_float,
|
||||
};
|
||||
|
||||
static void mul(FloatObject* self, FloatObject* in) { self->val *= in->val; }
|
||||
|
||||
static void sub(FloatObject* self, FloatObject* in) { self->val -= in->val; }
|
||||
|
||||
static void add(FloatObject* self, FloatObject* in) { self->val += in->val; }
|
||||
|
||||
static void divide(FloatObject* self, FloatObject* in) { self->val /= in->val; }
|
||||
|
||||
struct ObjectTypeAriphmetics FloatObject::TypeAriphm = {
|
||||
.add = (object_add) add,
|
||||
.sub = (object_sub) sub,
|
||||
.mul = (object_mul) mul,
|
||||
.div = (object_div) divide,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::FloatObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) FloatObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) FloatObject::copy,
|
||||
.size = sizeof(FloatObject),
|
||||
.name = "float",
|
||||
.convesions = &FloatObjectTypeConversions,
|
||||
.ariphmetics = &FloatObject::TypeAriphm,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
};
|
||||
|
|
@ -1,82 +1,78 @@
|
|||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/interpreterobject.h"
|
||||
#include "primitives/linkobject.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void InterpreterObject::constructor(InterpreterObject* self) {
|
||||
new (&self->mInterpreter) Interpreter();
|
||||
|
||||
self->createMember("dict", "globals");
|
||||
self->createMember("link", "target method");
|
||||
}
|
||||
|
||||
void InterpreterObject::destructor(InterpreterObject* self) {
|
||||
self->mInterpreter.~Interpreter();
|
||||
}
|
||||
|
||||
void InterpreterObject::load(ArchiverIn& file_self, InterpreterObject* self) {
|
||||
new (&self->mInterpreter) Interpreter();
|
||||
}
|
||||
|
||||
bool InterpreterObject::running() { return !mInterpreter.finished(); }
|
||||
|
||||
void InterpreterObject::exec(obj::ClassObject* self, tp::InitialierList<Interpreter::GlobalDef> globals) {
|
||||
|
||||
if (running()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto link_target = getMember<obj::LinkObject>("target method");
|
||||
if (!link_target) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto target = link_target->getLink();
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(obj::MethodObject, target, method);
|
||||
if (!method || !method->mScript->mBytecode.mInstructions.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
mInterpreter.execAll(method, self, getMember<obj::DictObject>("globals"), globals);
|
||||
}
|
||||
|
||||
void InterpreterObject::debug() {
|
||||
if (running()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto link_target = getMember<obj::LinkObject>("target method");
|
||||
if (!link_target) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto target = link_target->getLink();
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(obj::MethodObject, target, method);
|
||||
if (!method || !method->mScript->mBytecode.mInstructions.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
mInterpreter.exec(method, this, getMember<obj::DictObject>("globals"));
|
||||
}
|
||||
|
||||
struct obj::ObjectType InterpreterObject::TypeData = {
|
||||
.base = &ClassObject::TypeData,
|
||||
.constructor = (object_constructor)InterpreterObject::constructor,
|
||||
.destructor = (object_destructor)InterpreterObject::destructor,
|
||||
.size = sizeof(InterpreterObject),
|
||||
.name = "interpreter",
|
||||
.load = (object_load)InterpreterObject::load,
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/interpreterobject.h"
|
||||
#include "primitives/linkobject.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void InterpreterObject::constructor(InterpreterObject* self) {
|
||||
new (&self->mInterpreter) Interpreter();
|
||||
|
||||
self->createMember("dict", "globals");
|
||||
self->createMember("link", "target method");
|
||||
}
|
||||
|
||||
void InterpreterObject::destructor(InterpreterObject* self) { self->mInterpreter.~Interpreter(); }
|
||||
|
||||
void InterpreterObject::load(ArchiverIn& file_self, InterpreterObject* self) { new (&self->mInterpreter) Interpreter(); }
|
||||
|
||||
bool InterpreterObject::running() { return !mInterpreter.finished(); }
|
||||
|
||||
void InterpreterObject::exec(obj::ClassObject* self, tp::InitialierList<Interpreter::GlobalDef> globals) {
|
||||
|
||||
if (running()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto link_target = getMember<obj::LinkObject>("target method");
|
||||
if (!link_target) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto target = link_target->getLink();
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(obj::MethodObject, target, method);
|
||||
if (!method || !method->mScript->mBytecode.mInstructions.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
mInterpreter.execAll(method, self, getMember<obj::DictObject>("globals"), globals);
|
||||
}
|
||||
|
||||
void InterpreterObject::debug() {
|
||||
if (running()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto link_target = getMember<obj::LinkObject>("target method");
|
||||
if (!link_target) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto target = link_target->getLink();
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(obj::MethodObject, target, method);
|
||||
if (!method || !method->mScript->mBytecode.mInstructions.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
mInterpreter.exec(method, this, getMember<obj::DictObject>("globals"));
|
||||
}
|
||||
|
||||
struct obj::ObjectType InterpreterObject::TypeData = {
|
||||
.base = &ClassObject::TypeData,
|
||||
.constructor = (object_constructor) InterpreterObject::constructor,
|
||||
.destructor = (object_destructor) InterpreterObject::destructor,
|
||||
.size = sizeof(InterpreterObject),
|
||||
.name = "interpreter",
|
||||
.load = (object_load) InterpreterObject::load,
|
||||
};
|
||||
|
|
@ -1,104 +1,74 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/intobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void IntObject::constructor(Object* self) {
|
||||
NDO_CAST(IntObject, self)->val = 0;
|
||||
}
|
||||
|
||||
void IntObject::copy(IntObject* self, const IntObject* in) {
|
||||
self->val = in->val;
|
||||
}
|
||||
|
||||
IntObject* IntObject::create(alni in) {
|
||||
NDO_CASTV(IntObject, NDO->create("int"), out)->val = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
void IntObject::from_int(Object* self, alni in) {
|
||||
NDO_CAST(IntObject, self)->val = in;
|
||||
}
|
||||
|
||||
void IntObject::from_float(Object* self, alnf in) {
|
||||
NDO_CAST(IntObject, self)->val = (alni)in;
|
||||
}
|
||||
|
||||
void IntObject::from_string(Object* self, String in) {
|
||||
NDO_CAST(IntObject, self)->val = alni(in);
|
||||
}
|
||||
|
||||
String IntObject::to_string(Object* self) {
|
||||
return String(NDO_CAST(IntObject, self)->val);
|
||||
}
|
||||
|
||||
alni IntObject::to_int(Object* self) {
|
||||
return alni(NDO_CAST(IntObject, self)->val);
|
||||
}
|
||||
|
||||
alnf IntObject::to_float(Object* self) {
|
||||
return alnf(NDO_CAST(IntObject, self)->val);
|
||||
}
|
||||
|
||||
static alni save_size(IntObject* self) {
|
||||
return sizeof(alni);
|
||||
}
|
||||
|
||||
static void save(IntObject* self, ArchiverOut& file_self) {
|
||||
file_self << self->val;
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, IntObject* self) {
|
||||
file_self >> self->val;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions IntObjectTypeConversions = {
|
||||
.from_int = IntObject::from_int,
|
||||
.from_float = IntObject::from_float,
|
||||
.from_string = IntObject::from_string,
|
||||
.to_string = IntObject::to_string,
|
||||
.to_int = IntObject::to_int,
|
||||
.to_float = IntObject::to_float,
|
||||
};
|
||||
|
||||
void divide(IntObject* self, IntObject* in) {
|
||||
self->val /= in->val;
|
||||
}
|
||||
|
||||
void mul(IntObject* self, IntObject* in) {
|
||||
self->val *= in->val;
|
||||
}
|
||||
|
||||
void sub(IntObject* self, IntObject* in) {
|
||||
self->val -= in->val;
|
||||
}
|
||||
|
||||
void add(IntObject* self, IntObject* in) {
|
||||
self->val += in->val;
|
||||
}
|
||||
|
||||
struct ObjectTypeAriphmetics IntObject::TypeAriphm = {
|
||||
.add = (object_add) add,
|
||||
.sub = (object_sub) sub,
|
||||
.mul = (object_mul) mul,
|
||||
.div = (object_div) divide,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::IntObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = IntObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) IntObject::copy,
|
||||
.size = sizeof(IntObject),
|
||||
.name = "int",
|
||||
.convesions = &IntObjectTypeConversions,
|
||||
.ariphmetics = &IntObject::TypeAriphm,
|
||||
.save_size = (object_save_size)save_size,
|
||||
.save = (object_save)save,
|
||||
.load = (object_load)load,
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/intobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void IntObject::constructor(Object* self) { NDO_CAST(IntObject, self)->val = 0; }
|
||||
|
||||
void IntObject::copy(IntObject* self, const IntObject* in) { self->val = in->val; }
|
||||
|
||||
IntObject* IntObject::create(alni in) {
|
||||
NDO_CASTV(IntObject, NDO->create("int"), out)->val = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
void IntObject::from_int(Object* self, alni in) { NDO_CAST(IntObject, self)->val = in; }
|
||||
|
||||
void IntObject::from_float(Object* self, alnf in) { NDO_CAST(IntObject, self)->val = (alni) in; }
|
||||
|
||||
void IntObject::from_string(Object* self, String in) { NDO_CAST(IntObject, self)->val = alni(in); }
|
||||
|
||||
String IntObject::to_string(Object* self) { return String(NDO_CAST(IntObject, self)->val); }
|
||||
|
||||
alni IntObject::to_int(Object* self) { return alni(NDO_CAST(IntObject, self)->val); }
|
||||
|
||||
alnf IntObject::to_float(Object* self) { return alnf(NDO_CAST(IntObject, self)->val); }
|
||||
|
||||
static alni save_size(IntObject* self) { return sizeof(alni); }
|
||||
|
||||
static void save(IntObject* self, ArchiverOut& file_self) { file_self << self->val; }
|
||||
|
||||
static void load(ArchiverIn& file_self, IntObject* self) { file_self >> self->val; }
|
||||
|
||||
struct ObjectTypeConversions IntObjectTypeConversions = {
|
||||
.from_int = IntObject::from_int,
|
||||
.from_float = IntObject::from_float,
|
||||
.from_string = IntObject::from_string,
|
||||
.to_string = IntObject::to_string,
|
||||
.to_int = IntObject::to_int,
|
||||
.to_float = IntObject::to_float,
|
||||
};
|
||||
|
||||
void divide(IntObject* self, IntObject* in) { self->val /= in->val; }
|
||||
|
||||
void mul(IntObject* self, IntObject* in) { self->val *= in->val; }
|
||||
|
||||
void sub(IntObject* self, IntObject* in) { self->val -= in->val; }
|
||||
|
||||
void add(IntObject* self, IntObject* in) { self->val += in->val; }
|
||||
|
||||
struct ObjectTypeAriphmetics IntObject::TypeAriphm = {
|
||||
.add = (object_add) add,
|
||||
.sub = (object_sub) sub,
|
||||
.mul = (object_mul) mul,
|
||||
.div = (object_div) divide,
|
||||
};
|
||||
|
||||
struct obj::ObjectType obj::IntObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = IntObject::constructor,
|
||||
.destructor = NULL,
|
||||
.copy = (object_copy) IntObject::copy,
|
||||
.size = sizeof(IntObject),
|
||||
.name = "int",
|
||||
.convesions = &IntObjectTypeConversions,
|
||||
.ariphmetics = &IntObject::TypeAriphm,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
};
|
||||
|
|
@ -1,133 +1,109 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/linkobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void LinkObject::constructor(Object* self) {
|
||||
NDO_CAST(LinkObject, self)->link = 0;
|
||||
}
|
||||
|
||||
void LinkObject::destructor(LinkObject* self) {
|
||||
if (self->link) NDO->destroy(self->link);
|
||||
}
|
||||
|
||||
void LinkObject::copy(Object* self, const Object* in) {
|
||||
NDO_CAST(LinkObject, self)->setLink(NDO_CAST(LinkObject, in)->link);
|
||||
}
|
||||
|
||||
LinkObject* LinkObject::create(Object* in) {
|
||||
NDO_CASTV(LinkObject, NDO->create("link"), out)->link = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
alni LinkObject::save_size(LinkObject* self) {
|
||||
return sizeof(alni);
|
||||
}
|
||||
|
||||
void LinkObject::save(LinkObject* self, ArchiverOut& file_self) {
|
||||
if (self->link != NULL) {
|
||||
alni link_object_save_adress = NDO->save(file_self, self->link);
|
||||
file_self << link_object_save_adress;
|
||||
}
|
||||
else {
|
||||
alni null = -1;
|
||||
file_self << null;
|
||||
}
|
||||
}
|
||||
|
||||
void LinkObject::load(ArchiverIn& file_self, LinkObject* self) {
|
||||
|
||||
alni saved_object_adress;
|
||||
file_self >> saved_object_adress;
|
||||
|
||||
if (saved_object_adress == -1) {
|
||||
self->link = NULL;
|
||||
}
|
||||
else {
|
||||
self->link = NDO->load(file_self, saved_object_adress);
|
||||
NDO->refinc(self->link);
|
||||
}
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> LinkObject::childs_retrival(LinkObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
if (self->link) out.append(self->link);
|
||||
return out;
|
||||
}
|
||||
|
||||
alni LinkObject::allocated_size(LinkObject* self) {
|
||||
return sizeof(Object*);
|
||||
}
|
||||
|
||||
alni LinkObject::allocated_size_recursive(LinkObject* self) {
|
||||
alni out = sizeof(Object*);
|
||||
if (self->link) {
|
||||
out += NDO->objsize_ram_recursive_util(self->link, self->link->type);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Object* LinkObject::getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
void LinkObject::setLink(Object* obj) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
if (link) NDO->destroy(link);
|
||||
if (obj) NDO->refinc(obj);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
link = obj;
|
||||
}
|
||||
|
||||
static auto tm_set = TypeMethod{
|
||||
.nameid = "set",
|
||||
.descr = "sets the link",
|
||||
.args = {
|
||||
{ "target", NULL }
|
||||
},
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
auto const self = (LinkObject*)tm->self;
|
||||
auto const target = tm->args[0].obj;
|
||||
self->setLink(target);
|
||||
}
|
||||
};
|
||||
|
||||
static auto tm_get = TypeMethod{
|
||||
.nameid = "get",
|
||||
.descr = "gets the link",
|
||||
.exec = [](const TypeMethod* tm) {
|
||||
auto const self = (LinkObject*)tm->self;
|
||||
auto link = self->getLink();
|
||||
if (link) {
|
||||
tm->ret.obj = link;
|
||||
}
|
||||
},
|
||||
.ret = { "the link", NULL }
|
||||
};
|
||||
|
||||
struct obj::ObjectType LinkObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = LinkObject::constructor,
|
||||
.destructor = (object_destructor)LinkObject::destructor,
|
||||
.copy = LinkObject::copy,
|
||||
.size = sizeof(LinkObject),
|
||||
.name = "link",
|
||||
.convesions = NULL,
|
||||
.save_size = (object_save_size)LinkObject::save_size,
|
||||
.save = (object_save)LinkObject::save,
|
||||
.load = (object_load)LinkObject::load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival)LinkObject::childs_retrival,
|
||||
.allocated_size = (object_allocated_size)LinkObject::allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive)LinkObject::allocated_size_recursive,
|
||||
|
||||
.type_methods = {
|
||||
.methods = {
|
||||
&tm_set,
|
||||
&tm_get,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "primitives/linkobject.h"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void LinkObject::constructor(Object* self) { NDO_CAST(LinkObject, self)->link = 0; }
|
||||
|
||||
void LinkObject::destructor(LinkObject* self) {
|
||||
if (self->link) NDO->destroy(self->link);
|
||||
}
|
||||
|
||||
void LinkObject::copy(Object* self, const Object* in) { NDO_CAST(LinkObject, self)->setLink(NDO_CAST(LinkObject, in)->link); }
|
||||
|
||||
LinkObject* LinkObject::create(Object* in) {
|
||||
NDO_CASTV(LinkObject, NDO->create("link"), out)->link = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
alni LinkObject::save_size(LinkObject* self) { return sizeof(alni); }
|
||||
|
||||
void LinkObject::save(LinkObject* self, ArchiverOut& file_self) {
|
||||
if (self->link != NULL) {
|
||||
alni link_object_save_adress = NDO->save(file_self, self->link);
|
||||
file_self << link_object_save_adress;
|
||||
} else {
|
||||
alni null = -1;
|
||||
file_self << null;
|
||||
}
|
||||
}
|
||||
|
||||
void LinkObject::load(ArchiverIn& file_self, LinkObject* self) {
|
||||
|
||||
alni saved_object_adress;
|
||||
file_self >> saved_object_adress;
|
||||
|
||||
if (saved_object_adress == -1) {
|
||||
self->link = NULL;
|
||||
} else {
|
||||
self->link = NDO->load(file_self, saved_object_adress);
|
||||
NDO->refinc(self->link);
|
||||
}
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> LinkObject::childs_retrival(LinkObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
if (self->link) out.append(self->link);
|
||||
return out;
|
||||
}
|
||||
|
||||
alni LinkObject::allocated_size(LinkObject* self) { return sizeof(Object*); }
|
||||
|
||||
alni LinkObject::allocated_size_recursive(LinkObject* self) {
|
||||
alni out = sizeof(Object*);
|
||||
if (self->link) {
|
||||
out += NDO->objsize_ram_recursive_util(self->link, self->link->type);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Object* LinkObject::getLink() { return link; }
|
||||
|
||||
void LinkObject::setLink(Object* obj) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
if (link) NDO->destroy(link);
|
||||
if (obj) NDO->refinc(obj);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
link = obj;
|
||||
}
|
||||
|
||||
static auto tm_set = TypeMethod{ .nameid = "set", .descr = "sets the link", .args = { { "target", NULL } }, .exec = [](const TypeMethod* tm) {
|
||||
auto const self = (LinkObject*) tm->self;
|
||||
auto const target = tm->args[0].obj;
|
||||
self->setLink(target);
|
||||
} };
|
||||
|
||||
static auto tm_get = TypeMethod{ .nameid = "get",
|
||||
.descr = "gets the link",
|
||||
.exec =
|
||||
[](const TypeMethod* tm) {
|
||||
auto const self = (LinkObject*) tm->self;
|
||||
auto link = self->getLink();
|
||||
if (link) {
|
||||
tm->ret.obj = link;
|
||||
}
|
||||
},
|
||||
.ret = { "the link", NULL } };
|
||||
|
||||
struct obj::ObjectType LinkObject::TypeData = { .base = NULL,
|
||||
.constructor = LinkObject::constructor,
|
||||
.destructor = (object_destructor) LinkObject::destructor,
|
||||
.copy = LinkObject::copy,
|
||||
.size = sizeof(LinkObject),
|
||||
.name = "link",
|
||||
.convesions = NULL,
|
||||
.save_size = (object_save_size) LinkObject::save_size,
|
||||
.save = (object_save) LinkObject::save,
|
||||
.load = (object_load) LinkObject::load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival) LinkObject::childs_retrival,
|
||||
.allocated_size = (object_allocated_size) LinkObject::allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive) LinkObject::allocated_size_recursive,
|
||||
|
||||
.type_methods = { .methods = {
|
||||
&tm_set,
|
||||
&tm_get,
|
||||
} } };
|
||||
|
|
@ -1,137 +1,133 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/listobject.h"
|
||||
#include "primitives/intobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void ListObject::constructor(Object* in) {
|
||||
NDO_CASTV(ListObject, in, self);
|
||||
new (&self->items) List<Object*>();
|
||||
}
|
||||
|
||||
void ListObject::copy(Object* in, const Object* target) {
|
||||
NDO_CASTV(ListObject, in, self);
|
||||
NDO_CASTV(ListObject, target, src);
|
||||
|
||||
destructor(in);
|
||||
|
||||
for (auto item : src->items) {
|
||||
self->pushBack(NDO->instatiate(item.data()));
|
||||
}
|
||||
}
|
||||
|
||||
void ListObject::destructor(Object* in) {
|
||||
NDO_CASTV(ListObject, in, self);
|
||||
for (auto item : self->items) {
|
||||
NDO->destroy(item.data());
|
||||
}
|
||||
self->items.removeAll();
|
||||
}
|
||||
|
||||
alni ListObject::save_size(ListObject* self) {
|
||||
alni len = self->items.length();
|
||||
return (len + 1) * sizeof(alni);
|
||||
}
|
||||
|
||||
void ListObject::save(ListObject* self, ArchiverOut& file_self) {
|
||||
alni len = self->items.length();
|
||||
file_self << len;
|
||||
|
||||
for (auto item : self->items) {
|
||||
alni ndo_object_adress = NDO->save(file_self, item.data());
|
||||
file_self << ndo_object_adress;
|
||||
}
|
||||
}
|
||||
|
||||
void ListObject::load(ArchiverIn& file_self, ListObject* self) {
|
||||
new (&self->items) tp::List<Object*>();
|
||||
|
||||
alni len;
|
||||
file_self >> len;
|
||||
|
||||
for (alni i = 0; i < len; i++) {
|
||||
alni ndo_object_adress;
|
||||
file_self >> ndo_object_adress;
|
||||
self->pushBack(NDO->load(file_self, ndo_object_adress));
|
||||
}
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> ListObject::childs_retrival(ListObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
out.reserve(self->items.length());
|
||||
ualni i = 0;
|
||||
for (auto item : self->items) {
|
||||
out[i] = item.data();
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
alni ListObject::allocated_size(ListObject* self) {
|
||||
// return self->items.sizeAllocatedMem();
|
||||
return {};
|
||||
}
|
||||
|
||||
alni ListObject::allocated_size_recursive(ListObject* self) {
|
||||
ASSERT(false)
|
||||
//alni out = self->items.sizeAllocatedMem();
|
||||
for (auto item : self->items) {
|
||||
//out += NDO->objsize_ram_recursive_util(item.data(), item->type);
|
||||
}
|
||||
// return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ListObject::pushBack(Object* obj) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
obj::NDO->refinc(obj);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.pushBack(obj);
|
||||
}
|
||||
|
||||
void ListObject::pushFront(Object* obj) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
obj::NDO->refinc(obj);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.pushFront(obj);
|
||||
}
|
||||
|
||||
void ListObject::delNode(tp::List<Object*>::Node* node) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
obj::NDO->destroy(node->data);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.deleteNode(node);
|
||||
}
|
||||
|
||||
void ListObject::popBack() {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
auto obj = items.last();
|
||||
if (obj) obj::NDO->destroy(obj->data);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.popBack();
|
||||
}
|
||||
|
||||
const tp::List<Object*>& ListObject::getItems() const {
|
||||
return items;
|
||||
}
|
||||
|
||||
struct obj::ObjectType obj::ListObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = ListObject::constructor,
|
||||
.destructor = ListObject::destructor,
|
||||
.copy = ListObject::copy,
|
||||
.size = sizeof(ListObject),
|
||||
.name = "list",
|
||||
.convesions = NULL,
|
||||
.save_size = (object_save_size)save_size,
|
||||
.save = (object_save)save,
|
||||
.load = (object_load)load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival) childs_retrival,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive) allocated_size_recursive
|
||||
};
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/intobject.h"
|
||||
#include "primitives/listobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void ListObject::constructor(Object* in) {
|
||||
NDO_CASTV(ListObject, in, self);
|
||||
new (&self->items) List<Object*>();
|
||||
}
|
||||
|
||||
void ListObject::copy(Object* in, const Object* target) {
|
||||
NDO_CASTV(ListObject, in, self);
|
||||
NDO_CASTV(ListObject, target, src);
|
||||
|
||||
destructor(in);
|
||||
|
||||
for (auto item : src->items) {
|
||||
self->pushBack(NDO->instatiate(item.data()));
|
||||
}
|
||||
}
|
||||
|
||||
void ListObject::destructor(Object* in) {
|
||||
NDO_CASTV(ListObject, in, self);
|
||||
for (auto item : self->items) {
|
||||
NDO->destroy(item.data());
|
||||
}
|
||||
self->items.removeAll();
|
||||
}
|
||||
|
||||
alni ListObject::save_size(ListObject* self) {
|
||||
alni len = self->items.length();
|
||||
return (len + 1) * sizeof(alni);
|
||||
}
|
||||
|
||||
void ListObject::save(ListObject* self, ArchiverOut& file_self) {
|
||||
alni len = self->items.length();
|
||||
file_self << len;
|
||||
|
||||
for (auto item : self->items) {
|
||||
alni ndo_object_adress = NDO->save(file_self, item.data());
|
||||
file_self << ndo_object_adress;
|
||||
}
|
||||
}
|
||||
|
||||
void ListObject::load(ArchiverIn& file_self, ListObject* self) {
|
||||
new (&self->items) tp::List<Object*>();
|
||||
|
||||
alni len;
|
||||
file_self >> len;
|
||||
|
||||
for (alni i = 0; i < len; i++) {
|
||||
alni ndo_object_adress;
|
||||
file_self >> ndo_object_adress;
|
||||
self->pushBack(NDO->load(file_self, ndo_object_adress));
|
||||
}
|
||||
}
|
||||
|
||||
tp::Buffer<Object*> ListObject::childs_retrival(ListObject* self) {
|
||||
tp::Buffer<Object*> out;
|
||||
out.reserve(self->items.length());
|
||||
ualni i = 0;
|
||||
for (auto item : self->items) {
|
||||
out[i] = item.data();
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
alni ListObject::allocated_size(ListObject* self) {
|
||||
// return self->items.sizeAllocatedMem();
|
||||
return {};
|
||||
}
|
||||
|
||||
alni ListObject::allocated_size_recursive(ListObject* self) {
|
||||
ASSERT(false)
|
||||
// alni out = self->items.sizeAllocatedMem();
|
||||
for (auto item : self->items) {
|
||||
// out += NDO->objsize_ram_recursive_util(item.data(), item->type);
|
||||
}
|
||||
// return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ListObject::pushBack(Object* obj) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
obj::NDO->refinc(obj);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.pushBack(obj);
|
||||
}
|
||||
|
||||
void ListObject::pushFront(Object* obj) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
obj::NDO->refinc(obj);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.pushFront(obj);
|
||||
}
|
||||
|
||||
void ListObject::delNode(tp::List<Object*>::Node* node) {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
obj::NDO->destroy(node->data);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.deleteNode(node);
|
||||
}
|
||||
|
||||
void ListObject::popBack() {
|
||||
#ifdef OBJECT_REF_COUNT
|
||||
auto obj = items.last();
|
||||
if (obj) obj::NDO->destroy(obj->data);
|
||||
#endif // OBJECT_REF_COUNT
|
||||
items.popBack();
|
||||
}
|
||||
|
||||
const tp::List<Object*>& ListObject::getItems() const { return items; }
|
||||
|
||||
struct obj::ObjectType obj::ListObject::TypeData = { .base = NULL,
|
||||
.constructor = ListObject::constructor,
|
||||
.destructor = ListObject::destructor,
|
||||
.copy = ListObject::copy,
|
||||
.size = sizeof(ListObject),
|
||||
.name = "list",
|
||||
.convesions = NULL,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
.childs_retrival = (object_debug_all_childs_retrival) childs_retrival,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
.allocated_size_recursive = (object_allocated_size_recursive) allocated_size_recursive };
|
||||
|
|
@ -1,76 +1,65 @@
|
|||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/methodobject.h"
|
||||
#include "core/scriptsection.h"
|
||||
#include "compiler/function.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
struct ObjectType MethodObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor)MethodObject::constructor,
|
||||
.destructor = (object_destructor)MethodObject::destructor,
|
||||
.copy = (object_copy)MethodObject::copy,
|
||||
.size = sizeof(MethodObject),
|
||||
.name = "method",
|
||||
.convesions = NULL,
|
||||
.save_size = (object_save_size)MethodObject::save_size,
|
||||
.save = (object_save)MethodObject::save,
|
||||
.load = (object_load)MethodObject::load,
|
||||
};
|
||||
|
||||
void MethodObject::constructor(MethodObject* self) {
|
||||
self->mScript = obj::ScriptSection::globalHandle()->createScript();
|
||||
}
|
||||
|
||||
void MethodObject::copy(MethodObject* self, MethodObject* in) {
|
||||
obj::ScriptSection::globalHandle()->changeScript(&self->mScript, &in->mScript);
|
||||
}
|
||||
|
||||
void MethodObject::destructor(MethodObject* self) {
|
||||
obj::ScriptSection::globalHandle()->abandonScript(self->mScript);
|
||||
}
|
||||
|
||||
tp::alni MethodObject::save_size(MethodObject* self) {
|
||||
// script_table_file_address & script string object address
|
||||
return sizeof(tp::alni);
|
||||
}
|
||||
|
||||
void MethodObject::save(MethodObject* self, ArchiverOut& file_self) {
|
||||
auto script_section = obj::ScriptSection::globalHandle();
|
||||
// script_table_file_address
|
||||
tp::alni script_table_file_address = script_section->get_script_file_adress(self->mScript);
|
||||
file_self << script_table_file_address;
|
||||
}
|
||||
|
||||
void MethodObject::load(ArchiverIn& file_self, obj::MethodObject* self) {
|
||||
auto script_section = obj::ScriptSection::globalHandle();
|
||||
// script_table_file_address
|
||||
tp::alni script_table_file_address;
|
||||
file_self >> script_table_file_address;
|
||||
self->mScript = script_section->get_scritp_from_file_adress(script_table_file_address);
|
||||
}
|
||||
|
||||
|
||||
void MethodObject::Initialize() {
|
||||
obj::ScriptSection::initialize();
|
||||
NDO->define(&MethodObject::TypeData);
|
||||
obj::NDO->type_groups.addType(&MethodObject::TypeData, { "Primitives" });
|
||||
}
|
||||
|
||||
void MethodObject::UnInitialize() {
|
||||
obj::ScriptSection::uninitialize();
|
||||
}
|
||||
|
||||
void MethodObject::compile() {
|
||||
BCgen::Compile(this);
|
||||
}
|
||||
|
||||
MethodObject* MethodObject::create(tp::String script) {
|
||||
auto out = (MethodObject*)NDO->create(MethodObject::TypeData.name);
|
||||
out->mScript->mReadable->val = script;
|
||||
out->compile();
|
||||
return out;
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "compiler/function.h"
|
||||
#include "core/scriptsection.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
using namespace obj;
|
||||
|
||||
struct ObjectType MethodObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = (object_constructor) MethodObject::constructor,
|
||||
.destructor = (object_destructor) MethodObject::destructor,
|
||||
.copy = (object_copy) MethodObject::copy,
|
||||
.size = sizeof(MethodObject),
|
||||
.name = "method",
|
||||
.convesions = NULL,
|
||||
.save_size = (object_save_size) MethodObject::save_size,
|
||||
.save = (object_save) MethodObject::save,
|
||||
.load = (object_load) MethodObject::load,
|
||||
};
|
||||
|
||||
void MethodObject::constructor(MethodObject* self) { self->mScript = obj::ScriptSection::globalHandle()->createScript(); }
|
||||
|
||||
void MethodObject::copy(MethodObject* self, MethodObject* in) { obj::ScriptSection::globalHandle()->changeScript(&self->mScript, &in->mScript); }
|
||||
|
||||
void MethodObject::destructor(MethodObject* self) { obj::ScriptSection::globalHandle()->abandonScript(self->mScript); }
|
||||
|
||||
tp::alni MethodObject::save_size(MethodObject* self) {
|
||||
// script_table_file_address & script string object address
|
||||
return sizeof(tp::alni);
|
||||
}
|
||||
|
||||
void MethodObject::save(MethodObject* self, ArchiverOut& file_self) {
|
||||
auto script_section = obj::ScriptSection::globalHandle();
|
||||
// script_table_file_address
|
||||
tp::alni script_table_file_address = script_section->get_script_file_adress(self->mScript);
|
||||
file_self << script_table_file_address;
|
||||
}
|
||||
|
||||
void MethodObject::load(ArchiverIn& file_self, obj::MethodObject* self) {
|
||||
auto script_section = obj::ScriptSection::globalHandle();
|
||||
// script_table_file_address
|
||||
tp::alni script_table_file_address;
|
||||
file_self >> script_table_file_address;
|
||||
self->mScript = script_section->get_scritp_from_file_adress(script_table_file_address);
|
||||
}
|
||||
|
||||
void MethodObject::Initialize() {
|
||||
obj::ScriptSection::initialize();
|
||||
NDO->define(&MethodObject::TypeData);
|
||||
obj::NDO->type_groups.addType(&MethodObject::TypeData, { "Primitives" });
|
||||
}
|
||||
|
||||
void MethodObject::UnInitialize() { obj::ScriptSection::uninitialize(); }
|
||||
|
||||
void MethodObject::compile() { BCgen::Compile(this); }
|
||||
|
||||
MethodObject* MethodObject::create(tp::String script) {
|
||||
auto out = (MethodObject*) NDO->create(MethodObject::TypeData.name);
|
||||
out->mScript->mReadable->val = script;
|
||||
out->compile();
|
||||
return out;
|
||||
}
|
||||
|
|
@ -1,60 +1,50 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
obj::NullObject* obj::NdoNull_globalInstance = NULL;
|
||||
bool uninit_flag = false;
|
||||
|
||||
void NullObject::uninit() {
|
||||
uninit_flag = true;
|
||||
NDO->destroy(NdoNull_globalInstance);
|
||||
}
|
||||
|
||||
void NullObject::destructor(Object* self) {
|
||||
DEBUG_ASSERT(uninit_flag && "Only one the instance of NullObject exists and thus it can't be destroyed");
|
||||
}
|
||||
|
||||
|
||||
String to_string(NullObject* self) {
|
||||
return "NULL";
|
||||
}
|
||||
|
||||
alni to_int(NullObject* self) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
alnf to_float(NullObject* self) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
obj::TypeMethods* tm_construct() {
|
||||
auto out = new obj::TypeMethods();
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions NullObjectTypeConversions = {
|
||||
.from_int = NULL,
|
||||
.from_float = NULL,
|
||||
.from_string = NULL,
|
||||
.to_string = (object_to_string) to_string,
|
||||
.to_int = (object_to_int) to_int,
|
||||
.to_float = (object_to_float) to_float,
|
||||
};
|
||||
|
||||
|
||||
struct ObjectType NullObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = NULL,
|
||||
.destructor = NullObject::destructor,
|
||||
.copy = NULL,
|
||||
.size = sizeof(NullObject),
|
||||
.name = "null",
|
||||
.convesions = &NullObjectTypeConversions,
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
obj::NullObject* obj::NdoNull_globalInstance = NULL;
|
||||
bool uninit_flag = false;
|
||||
|
||||
void NullObject::uninit() {
|
||||
uninit_flag = true;
|
||||
NDO->destroy(NdoNull_globalInstance);
|
||||
}
|
||||
|
||||
void NullObject::destructor(Object* self) { DEBUG_ASSERT(uninit_flag && "Only one the instance of NullObject exists and thus it can't be destroyed"); }
|
||||
|
||||
String to_string(NullObject* self) { return "NULL"; }
|
||||
|
||||
alni to_int(NullObject* self) { return 0; }
|
||||
|
||||
alnf to_float(NullObject* self) { return 0; }
|
||||
|
||||
obj::TypeMethods* tm_construct() {
|
||||
auto out = new obj::TypeMethods();
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions NullObjectTypeConversions = {
|
||||
.from_int = NULL,
|
||||
.from_float = NULL,
|
||||
.from_string = NULL,
|
||||
.to_string = (object_to_string) to_string,
|
||||
.to_int = (object_to_int) to_int,
|
||||
.to_float = (object_to_float) to_float,
|
||||
};
|
||||
|
||||
struct ObjectType NullObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = NULL,
|
||||
.destructor = NullObject::destructor,
|
||||
.copy = NULL,
|
||||
.size = sizeof(NullObject),
|
||||
.name = "null",
|
||||
.convesions = &NullObjectTypeConversions,
|
||||
};
|
||||
|
|
@ -1,96 +1,78 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/stringobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void StringObject::constructor(Object* self) {
|
||||
new (&NDO_CAST(StringObject, self)->val) String();
|
||||
}
|
||||
|
||||
void StringObject::destructor(StringObject* self) {
|
||||
self->val.~String();
|
||||
}
|
||||
|
||||
void StringObject::copy(Object* self, const Object* in) {
|
||||
NDO_CAST(StringObject, self)->val = NDO_CAST(StringObject, in)->val;
|
||||
}
|
||||
|
||||
StringObject* StringObject::create(String in) {
|
||||
NDO_CASTV(StringObject, NDO->create("str"), out)->val = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
void StringObject::from_int(StringObject* self, alni in) {
|
||||
// self->val = in;
|
||||
}
|
||||
|
||||
void StringObject::from_float(StringObject* self, alnf in) {
|
||||
// self->val = in;
|
||||
}
|
||||
|
||||
void StringObject::from_string(StringObject* self, String in) {
|
||||
// self->val = in;
|
||||
}
|
||||
|
||||
String StringObject::to_string(StringObject* self) {
|
||||
return self->val;
|
||||
}
|
||||
|
||||
alni StringObject::to_int(StringObject* self) {
|
||||
return alni(self->val);
|
||||
}
|
||||
|
||||
alnf StringObject::to_float(StringObject* self) {
|
||||
return alnf(self->val);
|
||||
}
|
||||
|
||||
static alni save_size(StringObject* self) {
|
||||
return tp::SaveSizeCounter::calc(self->val);
|
||||
}
|
||||
|
||||
static void save(StringObject* self, ArchiverOut& file_self) {
|
||||
file_self << self->val;
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, StringObject* self) {
|
||||
new (&self->val) tp::String();
|
||||
file_self >> self->val;
|
||||
}
|
||||
|
||||
alni allocated_size(StringObject* self) {
|
||||
// return self->val.sizeAllocatedMem();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool compare_strings(StringObject* left, StringObject* right) {
|
||||
return left->val == right->val;
|
||||
}
|
||||
|
||||
struct ObjectTypeConversions StringObjectTypeConversions = {
|
||||
.from_int = (object_from_int)StringObject::from_int,
|
||||
.from_float = (object_from_float)StringObject::from_float,
|
||||
.from_string = (object_from_string)StringObject::from_string,
|
||||
.to_string = (object_to_string)StringObject::to_string,
|
||||
.to_int = (object_to_int)StringObject::to_int,
|
||||
.to_float = (object_to_float)StringObject::to_float,
|
||||
};
|
||||
|
||||
struct obj::ObjectType StringObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = StringObject::constructor,
|
||||
.destructor = (object_destructor)StringObject::destructor,
|
||||
.copy = StringObject::copy,
|
||||
.size = sizeof(StringObject),
|
||||
.name = "str",
|
||||
.convesions = &StringObjectTypeConversions,
|
||||
.save_size = (object_save_size)save_size,
|
||||
.save = (object_save)save,
|
||||
.load = (object_load)load,
|
||||
.comparison = (object_compare) compare_strings,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
#include "primitives/stringobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
void StringObject::constructor(Object* self) { new (&NDO_CAST(StringObject, self)->val) String(); }
|
||||
|
||||
void StringObject::destructor(StringObject* self) { self->val.~String(); }
|
||||
|
||||
void StringObject::copy(Object* self, const Object* in) { NDO_CAST(StringObject, self)->val = NDO_CAST(StringObject, in)->val; }
|
||||
|
||||
StringObject* StringObject::create(String in) {
|
||||
NDO_CASTV(StringObject, NDO->create("str"), out)->val = in;
|
||||
return out;
|
||||
}
|
||||
|
||||
void StringObject::from_int(StringObject* self, alni in) {
|
||||
// self->val = in;
|
||||
}
|
||||
|
||||
void StringObject::from_float(StringObject* self, alnf in) {
|
||||
// self->val = in;
|
||||
}
|
||||
|
||||
void StringObject::from_string(StringObject* self, String in) {
|
||||
// self->val = in;
|
||||
}
|
||||
|
||||
String StringObject::to_string(StringObject* self) { return self->val; }
|
||||
|
||||
alni StringObject::to_int(StringObject* self) { return alni(self->val); }
|
||||
|
||||
alnf StringObject::to_float(StringObject* self) { return alnf(self->val); }
|
||||
|
||||
static alni save_size(StringObject* self) { return tp::SaveSizeCounter::calc(self->val); }
|
||||
|
||||
static void save(StringObject* self, ArchiverOut& file_self) { file_self << self->val; }
|
||||
|
||||
static void load(ArchiverIn& file_self, StringObject* self) {
|
||||
new (&self->val) tp::String();
|
||||
file_self >> self->val;
|
||||
}
|
||||
|
||||
alni allocated_size(StringObject* self) {
|
||||
// return self->val.sizeAllocatedMem();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool compare_strings(StringObject* left, StringObject* right) { return left->val == right->val; }
|
||||
|
||||
struct ObjectTypeConversions StringObjectTypeConversions = {
|
||||
.from_int = (object_from_int) StringObject::from_int,
|
||||
.from_float = (object_from_float) StringObject::from_float,
|
||||
.from_string = (object_from_string) StringObject::from_string,
|
||||
.to_string = (object_to_string) StringObject::to_string,
|
||||
.to_int = (object_to_int) StringObject::to_int,
|
||||
.to_float = (object_to_float) StringObject::to_float,
|
||||
};
|
||||
|
||||
struct obj::ObjectType StringObject::TypeData = {
|
||||
.base = NULL,
|
||||
.constructor = StringObject::constructor,
|
||||
.destructor = (object_destructor) StringObject::destructor,
|
||||
.copy = StringObject::copy,
|
||||
.size = sizeof(StringObject),
|
||||
.name = "str",
|
||||
.convesions = &StringObjectTypeConversions,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
.comparison = (object_compare) compare_strings,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
};
|
||||
|
|
@ -1,80 +1,72 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/typeobject.h"
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
TypeObject* TypeObject::create(const ObjectType* type) {
|
||||
NDO_CASTV(TypeObject, NDO->create("typeobject"), out);
|
||||
out->mTypeRef = type;
|
||||
return out;
|
||||
}
|
||||
|
||||
static alni save_size(TypeObject* self) {
|
||||
tp::String const nameid(self->mTypeRef->name);
|
||||
return nameid.size() + sizeof(nameid.size());
|
||||
}
|
||||
|
||||
static void save(TypeObject* self, ArchiverOut& file_self) {
|
||||
tp::String const nameid(self->mTypeRef->name);
|
||||
file_self << nameid;
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, TypeObject* self) {
|
||||
tp::String nameid;
|
||||
file_self >> nameid;
|
||||
|
||||
auto idx = NDO->types.presents(nameid);
|
||||
|
||||
if (idx) {
|
||||
self->mTypeRef = NDO->types.getSlotVal(idx);
|
||||
}
|
||||
else {
|
||||
self->mTypeRef = &NullObject::TypeData;
|
||||
}
|
||||
}
|
||||
|
||||
static alni allocated_size(TypeObject* self) {
|
||||
return sizeof(alni);
|
||||
}
|
||||
|
||||
static void from_string(TypeObject* self, tp::String in) {
|
||||
auto idx = NDO->types.presents(in);
|
||||
|
||||
if (idx) {
|
||||
self->mTypeRef = NDO->types.getSlotVal(idx);
|
||||
}
|
||||
else {
|
||||
self->mTypeRef = &NullObject::TypeData;
|
||||
}
|
||||
}
|
||||
|
||||
static String to_string(TypeObject* self) {
|
||||
return self->mTypeRef->name;
|
||||
}
|
||||
|
||||
bool comparator(TypeObject* left, TypeObject* right) {
|
||||
return left->mTypeRef == right->mTypeRef;
|
||||
}
|
||||
|
||||
static struct ObjectTypeConversions conversions = {
|
||||
.from_string = (object_from_string) from_string,
|
||||
.to_string = (object_to_string) to_string,
|
||||
};
|
||||
|
||||
struct obj::ObjectType TypeObject::TypeData = {
|
||||
.base = NULL,
|
||||
//.constructor = (object_constructor) TypeObject::constructor,
|
||||
.size = sizeof(TypeObject),
|
||||
.name = "typeobject",
|
||||
.convesions = &conversions,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
.comparison = (object_compare) comparator,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "primitives/typeobject.h"
|
||||
#include "NewPlacement.hpp"
|
||||
#include "primitives/nullobject.h"
|
||||
|
||||
using namespace obj;
|
||||
using namespace tp;
|
||||
|
||||
TypeObject* TypeObject::create(const ObjectType* type) {
|
||||
NDO_CASTV(TypeObject, NDO->create("typeobject"), out);
|
||||
out->mTypeRef = type;
|
||||
return out;
|
||||
}
|
||||
|
||||
static alni save_size(TypeObject* self) {
|
||||
tp::String const nameid(self->mTypeRef->name);
|
||||
return nameid.size() + sizeof(nameid.size());
|
||||
}
|
||||
|
||||
static void save(TypeObject* self, ArchiverOut& file_self) {
|
||||
tp::String const nameid(self->mTypeRef->name);
|
||||
file_self << nameid;
|
||||
}
|
||||
|
||||
static void load(ArchiverIn& file_self, TypeObject* self) {
|
||||
tp::String nameid;
|
||||
file_self >> nameid;
|
||||
|
||||
auto idx = NDO->types.presents(nameid);
|
||||
|
||||
if (idx) {
|
||||
self->mTypeRef = NDO->types.getSlotVal(idx);
|
||||
} else {
|
||||
self->mTypeRef = &NullObject::TypeData;
|
||||
}
|
||||
}
|
||||
|
||||
static alni allocated_size(TypeObject* self) { return sizeof(alni); }
|
||||
|
||||
static void from_string(TypeObject* self, tp::String in) {
|
||||
auto idx = NDO->types.presents(in);
|
||||
|
||||
if (idx) {
|
||||
self->mTypeRef = NDO->types.getSlotVal(idx);
|
||||
} else {
|
||||
self->mTypeRef = &NullObject::TypeData;
|
||||
}
|
||||
}
|
||||
|
||||
static String to_string(TypeObject* self) { return self->mTypeRef->name; }
|
||||
|
||||
bool comparator(TypeObject* left, TypeObject* right) { return left->mTypeRef == right->mTypeRef; }
|
||||
|
||||
static struct ObjectTypeConversions conversions = {
|
||||
.from_string = (object_from_string) from_string,
|
||||
.to_string = (object_to_string) to_string,
|
||||
};
|
||||
|
||||
struct obj::ObjectType TypeObject::TypeData = {
|
||||
.base = NULL,
|
||||
//.constructor = (object_constructor) TypeObject::constructor,
|
||||
.size = sizeof(TypeObject),
|
||||
.name = "typeobject",
|
||||
.convesions = &conversions,
|
||||
.save_size = (object_save_size) save_size,
|
||||
.save = (object_save) save,
|
||||
.load = (object_load) load,
|
||||
.comparison = (object_compare) comparator,
|
||||
.allocated_size = (object_allocated_size) allocated_size,
|
||||
};
|
||||
|
|
@ -7,7 +7,6 @@
|
|||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
|
||||
void testCore();
|
||||
void testPrimitives();
|
||||
void testInterpreter();
|
||||
|
|
|
|||
|
|
@ -26,11 +26,8 @@ TEST_DEF_STATIC(BasicAPI) {
|
|||
TEST(NDO_CAST(IntObject, savedInt));
|
||||
TEST(integer->val == NDO_CAST(IntObject, savedInt)->val);
|
||||
|
||||
|
||||
NDO->destroy(integer);
|
||||
NDO->destroy(savedInt);
|
||||
}
|
||||
|
||||
TEST_DEF(Core) {
|
||||
testBasicAPI();
|
||||
}
|
||||
TEST_DEF(Core) { testBasicAPI(); }
|
||||
|
|
@ -3,12 +3,12 @@
|
|||
|
||||
#include "Testing.hpp"
|
||||
|
||||
#include "compiler/function.h"
|
||||
#include "core/object.h"
|
||||
#include "primitives/methodobject.h"
|
||||
#include "interpreter/interpreter.h"
|
||||
#include "primitives/interpreterobject.h"
|
||||
#include "primitives/linkobject.h"
|
||||
#include "compiler/function.h"
|
||||
#include "interpreter/interpreter.h"
|
||||
#include "primitives/methodobject.h"
|
||||
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
|
@ -57,7 +57,6 @@ TEST_DEF_STATIC(Complex) {
|
|||
|
||||
interpreter->exec();
|
||||
|
||||
|
||||
NDO->destroy(interpreter);
|
||||
}
|
||||
|
||||
|
|
@ -84,5 +83,5 @@ TEST_DEF_STATIC(Simple) {
|
|||
|
||||
TEST_DEF(Interpreter) {
|
||||
testSimple();
|
||||
//testComplex();
|
||||
// testComplex();
|
||||
}
|
||||
|
|
@ -30,6 +30,4 @@ TEST_DEF_STATIC(Dict) {
|
|||
NDO->destroy(dictLoaded);
|
||||
}
|
||||
|
||||
TEST_DEF(Primitives) {
|
||||
testDict();
|
||||
}
|
||||
TEST_DEF(Primitives) { testDict(); }
|
||||
Loading…
Add table
Add a link
Reference in a new issue