clean up object module

This commit is contained in:
IlyaShurupov 2024-03-23 12:32:57 +03:00 committed by Ilya Shurupov
parent e78885d452
commit f8c82b7e29
68 changed files with 1223 additions and 1229 deletions

View file

@ -8,6 +8,9 @@
#include "imgui.h" #include "imgui.h"
#include "imgui_internal.h" #include "imgui_internal.h"
using namespace tp;
using namespace obj;
namespace ImGui { namespace ImGui {
bool SubMenuBegin(const char* desc, int level); bool SubMenuBegin(const char* desc, int level);
@ -104,7 +107,7 @@ void obj::ObjectsGUI::setClipboard(obj::Object* obj) {
mClipboard = obj; mClipboard = obj;
if (mClipboard) { if (mClipboard) {
obj::NDO->refinc(obj); obj::NDO->increaseReferenceCount(obj);
} }
} }
@ -145,7 +148,7 @@ obj::ObjectsGUI::ObjectsGUI() { assert(obj::NDO && "Objects library is not initi
void obj::ObjectsGUI::cd(obj::Object* child, const std::string& name) { void obj::ObjectsGUI::cd(obj::Object* child, const std::string& name) {
mActive = child; mActive = child;
mViewStack.pushBack({ mActive, name }); mViewStack.pushBack({ mActive, name });
obj::NDO->refinc(child); obj::NDO->increaseReferenceCount(child);
} }
void obj::ObjectsGUI::cdup() { void obj::ObjectsGUI::cdup() {
@ -587,7 +590,7 @@ void obj::ObjectsGUI::dictViewEdit(obj::DictObject* dict, const std::string& key
if (bool(idx)) { if (bool(idx)) {
// Notify("Object with such name Already Exists"); // Notify("Object with such name Already Exists");
} else { } else {
obj::NDO->refinc(obj); obj::NDO->increaseReferenceCount(obj);
dict->remove(key); dict->remove(key);
auto id = std::string(mNameEdit); auto id = std::string(mNameEdit);
dict->put(id, obj); dict->put(id, obj);
@ -915,7 +918,7 @@ void obj::ObjectsGUI::explorer() {
} }
if (ImGui::Selectable("Instantiate ")) { if (ImGui::Selectable("Instantiate ")) {
setClipboard(obj::NDO->instatiate(curretn_object)); setClipboard(obj::NDO->instantiate(curretn_object));
// Notify("Object copied to clipboard"); // Notify("Object copied to clipboard");
} }
@ -1005,7 +1008,7 @@ void obj::ObjectsGUI::properties(const obj::ObjectType* type, bool top_of_tree_v
assert(mActive); assert(mActive);
if (mActive->type != type) return; if (mActive->type != type) return;
ImGui::Text(" RefCount : %i", obj::NDO->getrefc(mActive)); ImGui::Text(" RefCount : %i", obj::NDO->getReferenceCount(mActive));
ImGui::Text(" Type : %s", type->name); ImGui::Text(" Type : %s", type->name);

View file

@ -8,7 +8,7 @@
#include <string> #include <string>
namespace obj { namespace tp::obj {
class ObjectsGUI { class ObjectsGUI {
@ -24,7 +24,7 @@ namespace obj {
operator bool() { return obj; } operator bool() { return obj; }
}; };
tp::List<ViewStackNode> mViewStack; List<ViewStackNode> mViewStack;
obj::Object* mRoot = NULL; obj::Object* mRoot = NULL;
obj::Object* mActive = NULL; obj::Object* mActive = NULL;
obj::Object* mClipboard = NULL; obj::Object* mClipboard = NULL;

View file

@ -2,12 +2,11 @@
#include "primitives/MethodObject.hpp" #include "primitives/MethodObject.hpp"
#include "primitives/PrimitiveObjects.hpp" #include "primitives/PrimitiveObjects.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace BCgen; using namespace obj;
ConstObject::ConstObject() {} ConstObject::ConstObject() = default;
ConstObject::ConstObject(obj::Object* mObj) : ConstObject::ConstObject(Object* mObj) :
mObj(mObj) {} mObj(mObj) {}
ConstObjectsPool::~ConstObjectsPool() { ConstObjectsPool::~ConstObjectsPool() {
@ -42,15 +41,15 @@ ConstObjectsPool::~ConstObjectsPool() {
} }
} }
ConstObject* ConstObjectsPool::registerObject(obj::Object* obj) { ConstObject* ConstObjectsPool::registerObject(Object* obj) {
ConstObject* out = new ConstObject(obj); auto out = new ConstObject(obj);
mTotalObjects++; mTotalObjects++;
return out; return out;
} }
ConstObject* ConstObjectsPool::get(tp::alni val) { ConstObject* ConstObjectsPool::get(alni val) {
auto idx = mIntegers.presents(val); auto idx = mIntegers.presents(val);
ConstObject* const_obj = nullptr; ConstObject* const_obj;
if (idx) { if (idx) {
const_obj = mIntegers.getSlotVal(idx); const_obj = mIntegers.getSlotVal(idx);
} else { } else {
@ -62,7 +61,7 @@ ConstObject* ConstObjectsPool::get(tp::alni val) {
ConstObject* ConstObjectsPool::get(const std::string& val) { ConstObject* ConstObjectsPool::get(const std::string& val) {
auto idx = mStrings.presents(val); auto idx = mStrings.presents(val);
ConstObject* const_obj = nullptr; ConstObject* const_obj;
if (idx) { if (idx) {
const_obj = mStrings.getSlotVal(idx); const_obj = mStrings.getSlotVal(idx);
} else { } else {
@ -72,9 +71,9 @@ ConstObject* ConstObjectsPool::get(const std::string& val) {
return const_obj; return const_obj;
} }
ConstObject* ConstObjectsPool::get(tp::alnf val) { ConstObject* ConstObjectsPool::get(alnf val) {
auto idx = mFloats.presents(val); auto idx = mFloats.presents(val);
ConstObject* const_obj = nullptr; ConstObject* const_obj;
if (idx) { if (idx) {
const_obj = mFloats.getSlotVal(idx); const_obj = mFloats.getSlotVal(idx);
} else { } else {
@ -100,17 +99,17 @@ ConstObject* ConstObjectsPool::get(bool val) {
} }
} }
ConstObject* ConstObjectsPool::addMethod(const std::string& method_id, obj::Object* method) { ConstObject* ConstObjectsPool::addMethod(const std::string& method_id, Object* method) {
ASSERT(NDO_CAST(MethodObject, method) && "Object is not a method object"); ASSERT(NDO_CAST(MethodObject, method) && "Object is not a method object")
ASSERT(!mMethods.presents(method_id) && "Method Redefinition"); ASSERT(!mMethods.presents(method_id) && "Method Redefinition")
auto out = registerObject(method); auto out = registerObject(method);
mMethods.put(method_id, out); mMethods.put(method_id, out);
return out; return out;
} }
void ConstObjectsPool::save(tp::Buffer<ConstData>& out) { void ConstObjectsPool::save(Buffer<ConstData>& out) {
out.reserve(mTotalObjects); out.reserve(mTotalObjects);
tp::alni data_idx = 0; alni data_idx = 0;
for (auto obj : mMethods) { for (auto obj : mMethods) {
out[data_idx] = obj->val->mObj; out[data_idx] = obj->val->mObj;
obj->val->mConstIdx = data_idx; obj->val->mConstIdx = data_idx;

View file

@ -1,7 +1,9 @@
#include "compiler/Expressions.hpp" #include "compiler/Expressions.hpp"
#include <utility>
using namespace tp;
using namespace obj; using namespace obj;
using namespace BCgen;
Expression::Expression(Type type) : Expression::Expression(Type type) :
mType(type) {} mType(type) {}
@ -46,25 +48,25 @@ ExpressionList::~ExpressionList() {
} }
} }
ExpressionNew::ExpressionNew(const std::string& type) : ExpressionNew::ExpressionNew(std::string type) :
Expression(Type::NEW), Expression(Type::NEW),
mNewType(type) {} mNewType(std::move(type)) {}
ExpressionNew::~ExpressionNew() = default; ExpressionNew::~ExpressionNew() = default;
ExpressionLocal::ExpressionLocal(const std::string& id) : ExpressionLocal::ExpressionLocal(std::string id) :
Expression(Type::LOCAL), Expression(Type::LOCAL),
mLocalId(id) {} mLocalId(std::move(id)) {}
ExpressionLocal::~ExpressionLocal() = default; ExpressionLocal::~ExpressionLocal() = default;
ExpressionSelf::ExpressionSelf() : ExpressionSelf::ExpressionSelf() :
Expression(Type::SELF) {} Expression(Type::SELF) {}
ExpressionChild::ExpressionChild(Expression* mParent, const std::string& id) : ExpressionChild::ExpressionChild(Expression* mParent, std::string id) :
Expression(Type::CHILD), Expression(Type::CHILD),
mParent(mParent), mParent(mParent),
mLocalId(id) {} mLocalId(std::move(id)) {}
ExpressionChild::~ExpressionChild() { delete mParent; } ExpressionChild::~ExpressionChild() { delete mParent; }
@ -79,38 +81,38 @@ ExpressionArithmetics::~ExpressionArithmetics() {
delete mRight; delete mRight;
} }
ExpressionFunc::ExpressionFunc(const std::string& id) : ExpressionFunc::ExpressionFunc(std::string id) :
Expression(Type::FUNC), Expression(Type::FUNC),
mFuncId(id) {} mFuncId(std::move(id)) {}
ExpressionFunc::~ExpressionFunc() = default; ExpressionFunc::~ExpressionFunc() = default;
ExpressionConst::ExpressionConst(const std::string& val) : ExpressionConst::ExpressionConst(std::string val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(STR), mConstType(STR),
str(val) {} str(std::move(val)) {}
ExpressionConst::ExpressionConst(const char* val) : ExpressionConst::ExpressionConst(const char* val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(STR), mConstType(STR),
str(val) {} str(val) {}
ExpressionConst::ExpressionConst(tp::int4 val) : ExpressionConst::ExpressionConst(int4 val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(INT), mConstType(INT),
integer(val) {} integer(val) {}
ExpressionConst::ExpressionConst(tp::flt4 val) : ExpressionConst::ExpressionConst(flt4 val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(FLT), mConstType(FLT),
floating(val) {} floating(val) {}
ExpressionConst::ExpressionConst(tp::alni val) : ExpressionConst::ExpressionConst(alni val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(INT), mConstType(INT),
integer(val) {} integer(val) {}
ExpressionConst::ExpressionConst(tp::alnf val) : ExpressionConst::ExpressionConst(alnf val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(FLT), mConstType(FLT),
floating(val) {} floating(val) {}

View file

@ -6,12 +6,10 @@
#include "parser/Parser.hpp" #include "parser/Parser.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj; using namespace obj;
using namespace BCgen;
void obj::BCgen::Genereate(ByteCode& out, StatementScope* body) { void obj::Generate(ByteCode& out, StatementScope* body) {
auto root = FunctionDefinition(); auto root = FunctionDefinition();
root.inst(Instruction(OpCode::SCOPE_IN)); root.inst(Instruction(OpCode::SCOPE_IN));
@ -26,18 +24,18 @@ void obj::BCgen::Genereate(ByteCode& out, StatementScope* body) {
} }
ConstObject* FunctionDefinition::defineLocal(const std::string& id) { ConstObject* FunctionDefinition::defineLocal(const std::string& id) {
auto idx = mLocals.presents(id); // auto idx = mLocals.presents(id);
// RelAssert(!idx && "Local Redefinition"); // RelAssert(!idx && "Local Redefinition");
auto const_str_id = mConstants.get(id); auto const_str_id = mConstants.get(id);
mLocals.put(id, const_str_id); mLocals.put(id, const_str_id);
return const_str_id; return const_str_id;
} }
FunctionDefinition::FunctionDefinition(const std::string& function_id, tp::Buffer<std::string> args, FunctionDefinition* prnt) { FunctionDefinition::FunctionDefinition(const std::string& function_id, const Buffer<std::string>& args, FunctionDefinition*) {
mFunctionId = function_id; mFunctionId = function_id;
inst(Instruction(OpCode::SAVE_ARGS, args.size(), 1)); inst(Instruction(OpCode::SAVE_ARGS, (alni) args.size(), 1));
for (auto id : args) { for (auto id : args) {
ASSERT(!mLocals.presents(id.data()) && "Argument Redefinition"); ASSERT(!mLocals.presents(id.data()) && "Argument Redefinition")
auto const_data = mConstants.get(id.data()); auto const_data = mConstants.get(id.data());
mArgsOrder.pushBack(const_data); mArgsOrder.pushBack(const_data);
mLocals.put(id.data(), const_data); mLocals.put(id.data(), const_data);
@ -124,7 +122,7 @@ void FunctionDefinition::EvalStatement(Statement* stm) {
// define method as local // define method as local
auto idx = mLocals.presents(func.mFunctionId); auto idx = mLocals.presents(func.mFunctionId);
ASSERT(!idx && "Local Redefinition with function name"); ASSERT(!idx && "Local Redefinition with function name")
mLocals.put(func.mFunctionId, mConstants.get(func.mFunctionId)); mLocals.put(func.mFunctionId, mConstants.get(func.mFunctionId));
// create and register const func object // create and register const func object
@ -152,7 +150,7 @@ void FunctionDefinition::EvalStatement(Statement* stm) {
// define method as local // define method as local
auto idx = mLocals.presents(func.mFunctionId); auto idx = mLocals.presents(func.mFunctionId);
ASSERT(!idx && "Local Redefinition with function name"); ASSERT(!idx && "Local Redefinition with function name")
mLocals.put(func.mFunctionId, mConstants.get(func.mFunctionId)); mLocals.put(func.mFunctionId, mConstants.get(func.mFunctionId));
// create and register const func object // create and register const func object
@ -164,7 +162,7 @@ void FunctionDefinition::EvalStatement(Statement* stm) {
// check for return statements // check for return statements
ASSERT( ASSERT(
child_stm.data()->mType != Statement::Type::RET && "return statements are not allowed in class definition" child_stm.data()->mType != Statement::Type::RET && "return statements are not allowed in class definition"
); )
func.EvalStatement(child_stm.data()); func.EvalStatement(child_stm.data());
} }
// create one last instruction - constructing class from function execution state // create one last instruction - constructing class from function execution state
@ -280,10 +278,10 @@ void FunctionDefinition::EvalExpr(Expression* expr) {
} }
case Expression::Type::ARITHMETICS: case Expression::Type::ARITHMETICS:
{ {
auto ariphm = (ExpressionArithmetics*) expr; auto arithmeticExpression = (ExpressionArithmetics*) expr;
EvalExpr(ariphm->mRight); EvalExpr(arithmeticExpression->mRight);
EvalExpr(ariphm->mLeft); EvalExpr(arithmeticExpression->mLeft);
inst(Instruction(ariphm->mOpType)); inst(Instruction(arithmeticExpression->mOpType));
break; break;
} }
case Expression::Type::NEW: case Expression::Type::NEW:
@ -302,29 +300,29 @@ void FunctionDefinition::EvalExpr(Expression* expr) {
} }
case Expression::Type::CONST_EXPR: case Expression::Type::CONST_EXPR:
{ {
auto constobj = (ExpressionConst*) expr; auto constObject = (ExpressionConst*) expr;
switch (constobj->mConstType) { switch (constObject->mConstType) {
case ExpressionConst::STR: case ExpressionConst::STR:
{ {
inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constobj->str))); inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constObject->str)));
break; break;
} }
case ExpressionConst::INT: case ExpressionConst::INT:
{ {
inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constobj->integer))); inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constObject->integer)));
break; break;
} }
case ExpressionConst::FLT: case ExpressionConst::FLT:
{ {
inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constobj->floating))); inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constObject->floating)));
break; break;
} }
case ExpressionConst::BOOL: case ExpressionConst::BOOL:
{ {
inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constobj->boolean))); inst(Instruction(OpCode::LOAD_CONST, mConstants.get(constObject->boolean)));
break; break;
} }
}; }
break; break;
} }
case Expression::Type::CHILD: case Expression::Type::CHILD:
@ -343,7 +341,7 @@ void FunctionDefinition::EvalExpr(Expression* expr) {
case Expression::Type::CALL: case Expression::Type::CALL:
{ {
auto call = (ExpressionCall*) expr; auto call = (ExpressionCall*) expr;
inst(Instruction(OpCode::PUSH_ARGS, call->mArgs->mItems.size(), 1)); inst(Instruction(OpCode::PUSH_ARGS, (alni) call->mArgs->mItems.size(), 1));
for (auto arg : call->mArgs->mItems) { for (auto arg : call->mArgs->mItems) {
EvalExpr(arg.data()); EvalExpr(arg.data());
} }
@ -356,13 +354,13 @@ void FunctionDefinition::EvalExpr(Expression* expr) {
} }
} }
tp::alni instSize(const Instruction& inst) { alni instSize(const Instruction& inst) {
switch (inst.mInstType) { switch (inst.mInstType) {
case Instruction::InstType::JUMP: case Instruction::InstType::JUMP:
case Instruction::InstType::JUMP_IF_NOT: case Instruction::InstType::JUMP_IF_NOT:
case Instruction::InstType::JUMP_IF: case Instruction::InstType::JUMP_IF:
{ {
// 2 bytes for offset 1 byte for instrtuction // 2 bytes for offset 1 byte for instruction
return 3; return 3;
} }
case Instruction::InstType::PURE_CONST: case Instruction::InstType::PURE_CONST:
@ -371,7 +369,7 @@ tp::alni instSize(const Instruction& inst) {
} }
case Instruction::InstType::EXEC: case Instruction::InstType::EXEC:
{ {
tp::alni out = 1; alni out = 1;
switch (inst.mArgType) { switch (inst.mArgType) {
case Instruction::ArgType::PARAM: case Instruction::ArgType::PARAM:
{ {
@ -392,7 +390,7 @@ tp::alni instSize(const Instruction& inst) {
} }
default: default:
{ {
ASSERT(0); ASSERT(0)
} }
} }
} }
@ -402,30 +400,30 @@ tp::alni instSize(const Instruction& inst) {
} }
default: default:
{ {
ASSERT(0); ASSERT(0)
} }
} }
ASSERT(0); ASSERT(0)
return 0; return 0;
} }
void writeConst(ByteCode& out, tp::alni& idx, tp::uint2 data) { void writeConst(ByteCode& out, alni& idx, uint2 data) {
for (auto byte : tp::Range(sizeof(tp::uint2))) { for (auto byte : Range(sizeof(uint2))) {
out.mInstructions[idx] = OpCode((tp::int1)(data >> byte * 8)); out.mInstructions[idx] = OpCode((int1)(data >> byte * 8));
idx++; idx++;
} }
} }
void writeParam(ByteCode& out, tp::alni& idx, tp::int1* data, tp::alni size) { void writeParam(ByteCode& out, alni& idx, const int1* data, alni size) {
for (auto byte : tp::Range(size)) { for (auto byte : Range(size)) {
out.mInstructions[idx] = OpCode(data[byte]); out.mInstructions[idx] = OpCode(data[byte]);
idx++; idx++;
} }
} }
tp::alni calcOffset(tp::List<Instruction>::Node* jump_inst, Instruction* to) { alni calcOffset(List<Instruction>::Node* jump_inst, Instruction* to) {
tp::alni offset = 0; alni offset = 0;
bool reversed = jump_inst->data.mInstIdx > to->mInstIdx; bool reversed = jump_inst->data.mInstIdx > to->mInstIdx;
auto iter_node = reversed ? jump_inst : jump_inst->next; auto iter_node = reversed ? jump_inst : jump_inst->next;
while (&iter_node->data != to) { while (&iter_node->data != to) {
@ -445,13 +443,13 @@ void FunctionDefinition::generateByteCode(ByteCode& out) {
mConstants.save(out.mConstants); mConstants.save(out.mConstants);
tp::alni inst_len = 0; alni inst_len = 0;
for (auto inst_iter : mInstructions) { for (auto inst_iter : mInstructions) {
inst_len += instSize(inst_iter.data()); inst_len += instSize(inst_iter.data());
} }
out.mInstructions.reserve(inst_len); out.mInstructions.reserve(inst_len);
tp::alni idx = 0; alni idx = 0;
for (auto inst_iter : mInstructions) { for (auto inst_iter : mInstructions) {
auto inst = inst_iter.data(); auto inst = inst_iter.data();
@ -460,7 +458,7 @@ void FunctionDefinition::generateByteCode(ByteCode& out) {
case Instruction::InstType::JUMP_IF_NOT: case Instruction::InstType::JUMP_IF_NOT:
case Instruction::InstType::JUMP: case Instruction::InstType::JUMP:
{ {
tp::alni offset = calcOffset(inst_iter.node(), inst.mInstTarget); alni offset = calcOffset(inst_iter.node(), inst.mInstTarget);
if (offset == 0) { if (offset == 0) {
out.mInstructions[idx] = OpCode::NONE; out.mInstructions[idx] = OpCode::NONE;
@ -490,14 +488,14 @@ void FunctionDefinition::generateByteCode(ByteCode& out) {
idx++; idx++;
tp::alni offset_mod = abs(offset); alni offset_mod = abs(offset);
tp::uint2 offset_param = (tp::uint2) offset_mod; auto offset_param = (uint2) offset_mod;
writeParam(out, idx, (tp::int1*) &offset_param, 2); writeParam(out, idx, (int1*) &offset_param, 2);
break; break;
} }
case Instruction::InstType::PURE_CONST: case Instruction::InstType::PURE_CONST:
{ {
writeConst(out, idx, (tp::uint2) inst.mConstData->mConstIdx); writeConst(out, idx, (uint2) inst.mConstData->mConstIdx);
break; break;
} }
case Instruction::InstType::EXEC: case Instruction::InstType::EXEC:
@ -507,14 +505,14 @@ void FunctionDefinition::generateByteCode(ByteCode& out) {
switch (inst.mArgType) { switch (inst.mArgType) {
case Instruction::ArgType::PARAM: case Instruction::ArgType::PARAM:
{ {
writeParam(out, idx, (tp::int1*) &inst.mParam, inst.mParamBytes); writeParam(out, idx, (int1*) &inst.mParam, inst.mParamBytes);
break; break;
} }
case Instruction::ArgType::CONST_ARG: case Instruction::ArgType::CONST_ARG:
{ {
writeConst(out, idx, (tp::uint2) inst.mConstData->mConstIdx); writeConst(out, idx, (uint2) inst.mConstData->mConstIdx);
if (inst.mConstData2) { if (inst.mConstData2) {
writeConst(out, idx, (tp::uint2) inst.mConstData2->mConstIdx); writeConst(out, idx, (uint2) inst.mConstData2->mConstIdx);
} }
break; break;
} }
@ -524,14 +522,14 @@ void FunctionDefinition::generateByteCode(ByteCode& out) {
} }
default: default:
{ {
ASSERT(0); ASSERT(0)
} }
} }
break; break;
} }
default: default:
{ {
ASSERT(0); ASSERT(0)
} }
case Instruction::InstType::NONE: case Instruction::InstType::NONE:
{ {
@ -540,18 +538,18 @@ void FunctionDefinition::generateByteCode(ByteCode& out) {
} }
} }
tp::List<Instruction>::Node* FunctionDefinition::inst(Instruction inst) { List<Instruction>::Node* FunctionDefinition::inst(Instruction inst) {
mInstructions.pushBack(inst); mInstructions.pushBack(inst);
auto out = &mInstructions.last()->data; auto out = &mInstructions.last()->data;
out->mInstIdx = mInstructions.length() - 1; out->mInstIdx = (alni) mInstructions.length() - 1;
return mInstructions.last(); return mInstructions.last();
} }
void obj::BCgen::init() {} void obj::initialize() {}
void obj::BCgen::deinit() {} void obj::finalize() {}
bool obj::BCgen::Compile(obj::MethodObject* method) { bool obj::Compile(obj::MethodObject* method) {
Parser parser; Parser parser;
@ -563,7 +561,7 @@ bool obj::BCgen::Compile(obj::MethodObject* method) {
return false; return false;
} }
BCgen::Genereate(method->mScript->mBytecode, res.scope); Generate(method->mScript->mBytecode, res.scope);
delete res.scope; delete res.scope;

View file

@ -1,9 +1,8 @@
#include "compiler/Instructions.hpp" #include "compiler/Instructions.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace BCgen; using namespace obj;
Instruction::Instruction(OpCode op) : Instruction::Instruction(OpCode op) :
mOp(op), mOp(op),

View file

@ -1,7 +1,8 @@
#include "compiler/Statements.hpp" #include "compiler/Statements.hpp"
#include <utility>
using namespace tp;
using namespace obj; using namespace obj;
using namespace BCgen;
StatementFuncDef::StatementFuncDef(const std::string& function_id) : StatementFuncDef::StatementFuncDef(const std::string& function_id) :
Statement(Type::DEF_FUNC) { Statement(Type::DEF_FUNC) {
@ -10,8 +11,8 @@ StatementFuncDef::StatementFuncDef(const std::string& function_id) :
StatementFuncDef::~StatementFuncDef() { delete mStatements; } StatementFuncDef::~StatementFuncDef() { delete mStatements; }
StatementLocalDef::StatementLocalDef(const std::string& id, Expression* value) : StatementLocalDef::StatementLocalDef(std::string id, Expression* value) :
mLocalId(id), mLocalId(std::move(id)),
Statement(Type::DEF_LOCAL) { Statement(Type::DEF_LOCAL) {
if (value->mType == Expression::Type::CONST_EXPR) { if (value->mType == Expression::Type::CONST_EXPR) {
@ -53,7 +54,7 @@ StatementPrint::StatementPrint(Expression* target) :
StatementPrint::~StatementPrint() { delete mTarget; } StatementPrint::~StatementPrint() { delete mTarget; }
StatementScope::StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack) : StatementScope::StatementScope(InitialierList<Statement*> statements, bool aPushToScopeStack) :
Statement(Type::SCOPE) { Statement(Type::SCOPE) {
mStatements = statements; mStatements = statements;
mPushToScopeStack = aPushToScopeStack; mPushToScopeStack = aPushToScopeStack;

View file

@ -1,241 +1,239 @@
#include "core/Object.hpp" #include "core/Object.hpp"
#include "primitives/ClassObject.hpp" #include "primitives/ClassObject.hpp"
#include "primitives/MethodObject.hpp" #include "primitives/MethodObject.hpp"
#include "primitives/NullObject.hpp"
#include "compiler/Functions.hpp"
#include "interpreter/Interpreter.hpp" #include "interpreter/Interpreter.hpp"
namespace obj { using namespace tp;
using namespace obj;
void save_string(ArchiverOut& file, const std::string& string) { void obj::save_string(ArchiverOut& file, const std::string& string) {
file << string.size(); file << string.size();
file.writeBytes(string.c_str(), string.size()); file.writeBytes(string.c_str(), string.size());
} }
tp::ualni save_string_size(const std::string& string) { ualni obj::save_string_size(const std::string& string) {
return string.size() + sizeof(string.size()); return string.size() + sizeof(string.size());
} }
void load_string(ArchiverIn& file, std::string& out) { void obj::load_string(ArchiverIn& file, std::string& out) {
typeof(out.size()) size; typeof(out.size()) size;
file >> size; file >> size;
auto buff = new char[size + 1]; auto buff = new char[size + 1];
file.readBytes(buff, size); file.readBytes(buff, size);
buff[size] = 0; buff[size] = 0;
out = buff; out = buff;
delete[] buff; delete[] buff;
} }
Object* ObjectMemAllocate(const ObjectType* type); Object* ObjectMemAllocate(const ObjectType* type);
void ObjectMemDeallocate(Object* in); void ObjectMemDeallocate(Object* in);
objects_api* NDO = nullptr; objects_api* obj::NDO = nullptr;
void hierarchy_copy(Object* self, const Object* in, const ObjectType* type); void hierarchy_copy(Object* self, const Object* in, const ObjectType* type);
void hierarchy_construct(Object* self, const ObjectType* type); void hierarchy_construct(Object* self, const ObjectType* type);
objects_api::objects_api() { objects_api::objects_api() {
tp::memSetVal(sl_callbacks, SAVE_LOAD_MAX_CALLBACK_SLOTS * sizeof(save_load_callbacks*), 0); memSetVal(sl_callbacks, SAVE_LOAD_MAX_CALLBACK_SLOTS * sizeof(save_load_callbacks*), 0);
interp = new Interpreter(); interp = new Interpreter();
} }
objects_api::~objects_api() { delete interp; } objects_api::~objects_api() { delete interp; }
void objects_api::define(ObjectType* type) { void objects_api::define(ObjectType* type) {
MODULE_SANITY_CHECK(gModuleObjects); MODULE_SANITY_CHECK(gModuleObjects)
DEBUG_ASSERT(NDO && "using uninitialize objects api"); DEBUG_ASSERT(NDO && "using uninitialized objects api")
DEBUG_ASSERT(!types.presents(type->name) && "Type Redefinition"); DEBUG_ASSERT(!types.presents(type->name) && "Type Redefinition")
types.put(type->name, type); types.put(type->name, type);
type->type_methods.init(); type->type_methods.init();
} }
Object* objects_api::create(const std::string& name) { Object* objects_api::create(const std::string& name) {
MODULE_SANITY_CHECK(gModuleObjects); MODULE_SANITY_CHECK(gModuleObjects)
const ObjectType* type = types.get(name); const ObjectType* type = types.get(name);
Object* obj_instance = ObjectMemAllocate(type); Object* obj_instance = ObjectMemAllocate(type);
if (!obj_instance) { if (!obj_instance) {
return nullptr;
}
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 nullptr;
}
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, const std::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;
}
std::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;
}
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
if (mh->refc > 1) {
mh->refc--;
return;
}
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);
}
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;
}
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 nullptr; return nullptr;
} }
};
hierarchy_construct(obj_instance, obj_instance->type);
setReferenceCount(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 nullptr;
}
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 memCompare(first, second, first->type->size) == 0;
}
return false;
}
Object* objects_api::instantiate(Object* in) {
obj::Object* obj = NDO->create(in->type->name);
NDO->copy(obj, in);
return obj;
}
void objects_api::set(Object* self, 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, 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, const std::string& val) {
if (self->type->convesions && self->type->convesions->from_string) {
self->type->convesions->from_string(self, val);
return;
}
}
alni objects_api::toInt(Object* self) {
DEBUG_ASSERT(self->type->convesions && self->type->convesions->to_int)
return self->type->convesions->to_int(self);
}
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;
}
std::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) const {
if (!in) {
return;
}
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
if (mh->refc > 1) {
mh->refc--;
return;
}
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);
}
halni objects_api::getReferenceCount(Object* in) {
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
return (halni) mh->refc;
}
void objects_api::increaseReferenceCount(Object* in) {
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
mh->refc++;
}
void objects_api::setReferenceCount(Object* in, halni count) {
ObjectMemHead* mh = NDO_MEMH_FROM_NDO(in);
mh->refc = count;
}
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* obj::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 nullptr;
}

View file

@ -6,434 +6,434 @@
#include <malloc.h> #include <malloc.h>
namespace obj { using namespace tp;
using namespace obj;
ObjectMemHead* bottom = nullptr; ObjectMemHead* bottom = nullptr;
tp::ualni count = 0; ualni count = 0;
struct ObjectsFileHeader { struct ObjectsFileHeader {
char name[10] = { 0 }; char name[10] = { 0 };
char version[10] = { 0 }; char version[10] = { 0 };
ObjectsFileHeader(bool default_val = true) { ObjectsFileHeader(bool default_val = true) {
if (default_val) { if (default_val) {
tp::memCopy(&name, "objects", 8); memCopy(&name, "objects", 8);
tp::memCopy(&version, "0", 2); memCopy(&version, "0", 2);
}
}
};
Object* ObjectMemAllocate(const ObjectType* type) {
ObjectMemHead* memh = (ObjectMemHead*) malloc(type->size + sizeof(ObjectMemHead));
if (!memh) {
return nullptr;
}
memh->down = nullptr;
memh->flags = 0;
memh->refc = (tp::alni) 1;
if (bottom) {
bottom->down = memh;
}
memh->up = bottom;
bottom = memh;
count++;
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;
}
free(memh);
count--;
}
void logTypeData(const ObjectType* type) {
printf("type - %s\n", type->name);
if (type->base) {
printf("Based on ");
logTypeData(type->base);
} }
} }
};
tp::ualni getObjCount() { return count; } Object* ObjectMemAllocate(const ObjectType* type) {
ObjectMemHead* memh = (ObjectMemHead*) malloc(type->size + sizeof(ObjectMemHead));
void assertNoLeaks() { if (!memh) {
if (bottom) { return nullptr;
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 { memh->down = nullptr;
Object* load_head_adress = 0; memh->flags = 0;
tp::halni refc = 0;
};
tp::int1* loaded_file = nullptr; memh->refc = (alni) 1;
void objects_api::clear_object_flags() { if (bottom) {
// clear all object flags bottom->down = memh;
for (ObjectMemHead* iter = bottom; iter; iter = iter->up) {
iter->flags = -1;
}
} }
tp::alni& getObjectFlags(Object* in) { return NDO_MEMH_FROM_NDO(in)->flags; } memh->up = bottom;
bottom = memh;
tp::alni objsize_ram_util(Object* self, const ObjectType* type) { count++;
tp::alni out = 0;
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;
}
free(memh);
count--;
}
void obj::logTypeData(const ObjectType* type) {
printf("type - %s\n", type->name);
if (type->base) {
printf("Based on ");
logTypeData(type->base);
}
}
ualni obj::getObjCount() { return count; }
void obj::assertNoLeaks() {
if (bottom) {
printf("ERROR : not all objects are destroyed\n");
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;
halni refc = 0;
};
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;
}
}
alni& getObjectFlags(Object* in) { return NDO_MEMH_FROM_NDO(in)->flags; }
alni objsize_ram_util(Object* self, const ObjectType* type) {
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;
}
alni objects_api::objsize_ram(Object* self) { return objsize_ram_util(self, self->type); }
alni objects_api::objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object) {
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) { if (type->allocated_size) {
out += type->allocated_size(self); out += type->allocated_size(self);
} else { } else {
out += type->size - sizeof(ObjectType*); 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); } if (type->base) {
out += objsize_ram_recursive_util(self, type->base, false);
} else {
out += sizeof(ObjectType*);
}
tp::alni objects_api::objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object) { return out;
tp::alni out = 0; }
if (different_object) { alni objects_api::objsize_ram_recursive(Object* self) {
if (getObjectFlags(self) == 1) { clear_object_flags();
return 0; return objsize_ram_recursive_util(self, self->type);
} }
getObjectFlags(self) = 1; alni objsize_file_util(Object* self, const ObjectType* type) {
} alni out = 0;
if (type->allocated_size_recursive) { if (type->save_size) {
out += type->allocated_size_recursive(self); out += type->save_size(self);
} else { }
if (type->allocated_size) {
out += type->allocated_size(self); if (type->base) {
} else { out += objsize_file_util(self, type->base);
out += type->size - sizeof(ObjectType*); }
return out;
}
alni objects_api::objsize_file(Object* self) { return objsize_file_util(self, self->type); }
alni objsize_file_recursive_util(Object* self, const ObjectType* type) {
alni out = 0;
getObjectFlags(self) = 1;
if (type->save_size) {
out += type->save_size(self);
}
if (type->childs_retrival) {
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_ram_recursive_util(self, type->base, false);
} else {
out += sizeof(ObjectType*);
}
return out;
} }
tp::alni objects_api::objsize_ram_recursive(Object* self) { if (type->base) {
clear_object_flags(); out += objsize_file_recursive_util(self, type->base);
return objsize_ram_recursive_util(self, self->type);
} }
tp::alni objsize_file_util(Object* self, const ObjectType* type) { return out;
tp::alni out = 0; }
if (type->save_size) { alni objects_api::objsize_file_recursive(Object* self) {
out += type->save_size(self); clear_object_flags();
} return objsize_file_recursive_util(self, self->type);
}
if (type->base) { void object_recursive_save(ArchiverOut& ndf, Object* self, const ObjectType* type) {
out += objsize_file_util(self, type->base); if (type->base) {
} object_recursive_save(ndf, self, type->base);
return out;
} }
tp::alni objects_api::objsize_file(Object* self) { return objsize_file_util(self, self->type); } // automatically offsets for parent type to write
if (type->save) {
type->save(self, ndf);
}
}
tp::alni objsize_file_recursive_util(Object* self, const ObjectType* type) { alni objects_api::save(ArchiverOut& ndf, Object* in) {
tp::alni out = 0; // if already saved return file_adress
if (NDO_MEMH_FROM_NDO(in)->flags != -1) {
getObjectFlags(self) = 1; return NDO_MEMH_FROM_NDO(in)->flags;
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) { // save write adress for parent save function call
clear_object_flags(); alni tmp_adress = ndf.getAddress();
return objsize_file_recursive_util(self, self->type);
// save requested object to first available adress
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, getReferenceCount(in) };
ndf << ofh;
save_string(ndf, in->type->name);
// allocate for object file header
ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + save_string_size(in->type->name));
// calc max size needed for saving all hierarchy of types
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);
} }
void object_recursive_save(ArchiverOut& ndf, Object* self, const ObjectType* type) { // automatically offsets for parent type to read
if (type->base) { if (type->load) {
object_recursive_save(ndf, self, type->base); type->load(ndf, out);
} }
}
// automatically offsets for parent type to write Object* objects_api::load(ArchiverIn& ndf, alni file_adress) {
if (type->save) { // check if already saved
type->save(self, ndf); if (((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress) {
return ((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress;
}
// save read address
alni parent_file_adress = ndf.getAddress();
// set read address
ndf.setAddress(file_adress);
ObjectFileHead ofh;
ndf >> ofh;
std::string type_name;
load_string(ndf, type_name);
const ObjectType* object_type = NDO->types.get(type_name);
Object* out = ObjectMemAllocate(object_type);
if (!out) {
return nullptr;
}
setReferenceCount(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, const std::string& path, bool compressed) {
ArchiverOut ndf(path.c_str());
if (!ndf.isOpened()) {
return false;
}
clear_object_flags();
// save version info
ObjectsFileHeader header;
ndf << header;
ndf.setFreeAddress(ndf.getAddress());
// pre allocate
for (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));
} }
} }
tp::alni objects_api::save(ArchiverOut& ndf, Object* in) { // pre-save
// if already saved return file_adress for (alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
if (NDO_MEMH_FROM_NDO(in)->flags != -1) { if (sl_callbacks[i] && sl_callbacks[i]->pre_save) {
return NDO_MEMH_FROM_NDO(in)->flags; sl_callbacks[i]->pre_save(sl_callbacks[i]->self, ndf);
}
// 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;
save_string(ndf, in->type->name);
// allocate for object file header
ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + save_string_size(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) { save(ndf, in);
// check if already saved
if (((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress) { // post-save
return ((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress; for (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);
} }
// save read address
tp::alni parent_file_adress = ndf.getAddress();
// set read address
ndf.setAddress(file_adress);
ObjectFileHead ofh;
ndf >> ofh;
std::string type_name;
load_string(ndf, type_name);
const ObjectType* object_type = NDO->types.get(type_name);
Object* out = ObjectMemAllocate(object_type);
if (!out) {
return nullptr;
}
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, const std::string& path, bool compressed) { // TODO : add compression
ArchiverOut ndf(path.c_str()); /*
if (compressed) {
auto temp = path + ".bz2";
compressF2F(path.read(), temp.cstr());
if (!ndf.isOpened()) { File::remove(path.read());
return false; File::rename(temp.cstr(), path.read());
} }
*/
clear_object_flags(); return true;
}
// save version info Object* objects_api::load(const std::string& path) {
ObjectsFileHeader header; /*
ndf << header; auto temp_file_name = path + ".unz";
bool unz_res = decompressF2F(path.cstr(), temp_file_name.cstr());
ndf.setFreeAddress(ndf.getAddress()); if (!unz_res) {
return nullptr;
// 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(const std::string& path) { File ndf(temp_file_name.cstr(), osfile_openflags::LOAD);
/* */
auto temp_file_name = path + ".unz";
bool unz_res = tp::decompressF2F(path.cstr(), temp_file_name.cstr());
if (!unz_res) { ArchiverIn ndf(path.c_str());
return nullptr;
}
File ndf(temp_file_name.cstr(), tp::osfile_openflags::LOAD); if (!ndf.isOpened()) {
*/ return nullptr;
ArchiverIn ndf(path.c_str());
if (!ndf.isOpened()) {
return nullptr;
}
// check for compatibility
ObjectsFileHeader current_header;
ObjectsFileHeader loaded_header(false);
ndf >> loaded_header;
if (!tp::memEqual(&current_header, &loaded_header, sizeof(ObjectsFileHeader))) {
return nullptr;
}
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 == nullptr) {
File::remove(temp_file_name.cstr());
}
*/
return out;
} }
void objects_api::add_sl_callbacks(save_load_callbacks* in) { // check for compatibility
sl_callbacks[sl_callbacks_load_idx] = in; ObjectsFileHeader current_header;
sl_callbacks_load_idx++; ObjectsFileHeader loaded_header(false);
ndf >> loaded_header;
if (!memEqual(&current_header, &loaded_header, sizeof(ObjectsFileHeader))) {
return nullptr;
} }
};
ndf.setAddress(0);
const auto fileSize = ndf.getSize();
loaded_file = (int1*) malloc(fileSize);
memSetVal(loaded_file, fileSize, 0);
ndf.readBytes(loaded_file, fileSize);
ndf.setAddress(sizeof(ObjectsFileHeader));
// preload
for (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 (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 == nullptr) {
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++;
}

View file

@ -16,8 +16,8 @@
#include "primitives/StringObject.hpp" #include "primitives/StringObject.hpp"
#include "primitives/TypeObject.hpp" #include "primitives/TypeObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
static void defineTypes() { static void defineTypes() {
NDO->define(&DictObject::TypeData); NDO->define(&DictObject::TypeData);
@ -50,9 +50,9 @@ static void defineGroups() {
NDO->type_groups.addType(&InterpreterObject::TypeData, { "scripting" }); NDO->type_groups.addType(&InterpreterObject::TypeData, { "scripting" });
} }
static bool init(const tp::ModuleManifest*) { static bool init(const ModuleManifest*) {
if (!NDO) NDO = new objects_api(); if (!NDO) NDO = new objects_api();
obj::BCgen::init(); obj::initialize();
MethodObject::Initialize(); MethodObject::Initialize();
@ -62,12 +62,12 @@ static bool init(const tp::ModuleManifest*) {
return true; return true;
} }
static void deinit(const tp::ModuleManifest*) { static void deinit(const ModuleManifest*) {
NullObject::uninit(); NullObject::uninit();
MethodObject::UnInitialize(); MethodObject::UnInitialize();
obj::BCgen::deinit(); obj::finalize();
assertNoLeaks(); assertNoLeaks();
@ -75,8 +75,8 @@ static void deinit(const tp::ModuleManifest*) {
NDO = nullptr; NDO = nullptr;
} }
static tp::ModuleManifest* sModuleDependencies[] = { static ModuleManifest* sModuleDependencies[] = {
nullptr nullptr
}; };
tp::ModuleManifest obj::gModuleObjects = tp::ModuleManifest("Objects", init, deinit, sModuleDependencies); ModuleManifest obj::gModuleObjects = ModuleManifest("Objects", init, deinit, sModuleDependencies);

View file

@ -1,18 +1,19 @@
#include "core/ScriptSection.hpp" #include "core/ScriptSection.hpp"
using namespace tp;
using namespace obj; using namespace obj;
ScriptSection* gScriptSection = nullptr; ScriptSection* gScriptSection = nullptr;
struct script_data_head { struct script_data_head {
tp::alni refc = 0; alni refc = 0;
tp::alni store_adress = 0; alni store_adress = 0;
}; };
void set_script_head_store_adress(Script* in, tp::alni address) { ((script_data_head*) in - 1)->store_adress = address; } void set_script_head_store_adress(Script* in, 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; } alni get_script_head_store_adress(Script* in) { return ((script_data_head*) in - 1)->store_adress; }
Script* ScriptSection::createScript() { Script* ScriptSection::createScript() {
auto sdhead = (script_data_head*) malloc(sizeof(script_data_head) + sizeof(Script)); auto sdhead = (script_data_head*) malloc(sizeof(script_data_head) + sizeof(Script));
@ -29,7 +30,7 @@ Script* ScriptSection::createScript() {
new (out) Script(); new (out) Script();
out->mReadable = obj::StringObject::create(""); out->mReadable = obj::StringObject::create("");
obj::NDO->refinc(out->mReadable); obj::NDO->increaseReferenceCount(out->mReadable);
return out; return out;
} }
@ -60,29 +61,29 @@ void ScriptSection::changeScript(Script** current_script, Script** new_script) {
*current_script = *new_script; *current_script = *new_script;
} }
tp::alni ScriptSection::get_script_file_adress(Script* in) { return get_script_head_store_adress(in); } 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) { alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file) {
tp::alni size = 0; alni size = 0;
size += 5; // header size += 5; // header
size += sizeof(tp::alni); // scripts length size += sizeof(alni); // scripts length
for (auto iter : self->mScripts) { for (auto iter : self->mScripts) {
set_script_head_store_adress(iter.data(), file.getAddress() + size); set_script_head_store_adress(iter.data(), file.getAddress() + size);
size += sizeof(tp::alni); // scripts string obj ref size += sizeof(alni); // scripts string obj ref
size += sizeof(tp::alni); // constants length size += sizeof(alni); // constants length
for (auto const_obj : iter->mBytecode.mConstants) { for (auto const_obj : iter->mBytecode.mConstants) {
size += sizeof(tp::alni); // constant object addres size += sizeof(alni); // constant object addres
} }
// instructions // instructions
size += tp::SaveSizeCounter::calc(iter->mBytecode.mInstructions); size += SaveSizeCounter::calc(iter->mBytecode.mInstructions);
} }
size += sizeof(tp::alni); // objects mem offset size += sizeof(alni); // objects mem offset
size += 5; // header size += 5; // header
return size; return size;
} }
@ -92,7 +93,7 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut&
file.writeBytes("/scr/", 5); file.writeBytes("/scr/", 5);
// scripts length // scripts length
tp::alni scripts_count = self->mScripts.length(); alni scripts_count = self->mScripts.length();
file << scripts_count; file << scripts_count;
for (auto iter : self->mScripts) { for (auto iter : self->mScripts) {
@ -102,7 +103,7 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut&
file << obj_addres; file << obj_addres;
// constants length // constants length
tp::alni consts_count = iter->mBytecode.mConstants.size(); alni consts_count = iter->mBytecode.mConstants.size();
file << consts_count; file << consts_count;
for (auto const_obj : iter->mBytecode.mConstants) { for (auto const_obj : iter->mBytecode.mConstants) {
@ -118,40 +119,40 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut&
// header // header
file.writeBytes("/scr/", 5); file.writeBytes("/scr/", 5);
tp::alni objects_mem_offset = file.getFreeAddress(); alni objects_mem_offset = file.getFreeAddress();
file << objects_mem_offset; file << objects_mem_offset;
} }
void load_constants(ScriptSection* self, ArchiverIn& file, tp::alni start_addr) { void load_constants(ScriptSection* self, ArchiverIn& file, alni start_addr) {
auto addres = file.getAddress(); auto addres = file.getAddress();
file.setAddress(start_addr); file.setAddress(start_addr);
file.setAddress(start_addr + 5); // header file.setAddress(start_addr + 5); // header
tp::alni scripts_count; alni scripts_count;
file >> scripts_count; file >> scripts_count;
for (tp::alni i = 0; i < scripts_count; i++) { for (alni i = 0; i < scripts_count; i++) {
auto script = self->get_scritp_from_file_adress(file.getAddress()); auto script = self->get_scritp_from_file_adress(file.getAddress());
// script text // script text
tp::alni str_addr; alni str_addr;
file >> str_addr; file >> str_addr;
NDO->destroy(script->mReadable); // we already have string object in the script when creating script 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)); script->mReadable = NDO_CAST(obj::StringObject, obj::NDO->load(file, str_addr));
file.setAddress(file.getAddress() + sizeof(tp::alni)); // constants length file.setAddress(file.getAddress() + sizeof(alni)); // constants length
for (auto const_obj : script->mBytecode.mConstants) { for (auto const_obj : script->mBytecode.mConstants) {
tp::alni consts_addr; alni consts_addr;
file >> consts_addr; file >> consts_addr;
const_obj.data() = obj::NDO->load(file, consts_addr); const_obj.data() = obj::NDO->load(file, consts_addr);
} }
// instructions // instructions
file.setAddress(file.getAddress() + tp::SaveSizeCounter::calc(script->mBytecode.mInstructions)); file.setAddress(file.getAddress() + SaveSizeCounter::calc(script->mBytecode.mInstructions));
} }
file.setAddress(addres); file.setAddress(addres);
@ -168,26 +169,26 @@ void ScriptSection::load_script_table_from_file(ScriptSection* self, ArchiverIn&
file.setAddress(file.getAddress() + 5); file.setAddress(file.getAddress() + 5);
// scripts length // scripts length
tp::alni scripts_count; alni scripts_count;
file >> scripts_count; file >> scripts_count;
for (tp::alni i = 0; i < scripts_count; i++) { for (alni i = 0; i < scripts_count; i++) {
auto new_script = self->createScript(); auto new_script = self->createScript();
set_script_head_store_adress(new_script, file.getAddress()); set_script_head_store_adress(new_script, file.getAddress());
// scripts text // scripts text
file.setAddress(file.getAddress() + sizeof(tp::alni)); file.setAddress(file.getAddress() + sizeof(alni));
// constants length // constants length
tp::alni consts_count; alni consts_count;
file >> consts_count; file >> consts_count;
new_script->mBytecode.mConstants.reserve(consts_count); new_script->mBytecode.mConstants.reserve(consts_count);
for (tp::alni j = 0; j < consts_count; j++) { for (alni j = 0; j < consts_count; j++) {
// constant object addres // constant object addres
tp::alni consts_addr; alni consts_addr;
file >> consts_addr; file >> consts_addr;
// skiping // skiping
@ -203,13 +204,13 @@ void ScriptSection::load_script_table_from_file(ScriptSection* self, ArchiverIn&
// header // header
file.setAddress(file.getAddress() + 5); file.setAddress(file.getAddress() + 5);
tp::alni objects_mem_offset; alni objects_mem_offset;
file >> objects_mem_offset; file >> objects_mem_offset;
file.setAddress(objects_mem_offset); file.setAddress(objects_mem_offset);
} }
Script* ScriptSection::get_scritp_from_file_adress(tp::alni file_adress) { Script* ScriptSection::get_scritp_from_file_adress(alni file_adress) {
for (auto iter : mScripts) { for (auto iter : mScripts) {
if (get_script_head_store_adress(iter.data()) == file_adress) { if (get_script_head_store_adress(iter.data()) == file_adress) {
return iter.data(); return iter.data();

View file

@ -3,6 +3,7 @@
#include "core/Object.hpp" #include "core/Object.hpp"
using namespace tp;
using namespace obj; using namespace obj;
obj::TypeGroups::TypeGroups() : obj::TypeGroups::TypeGroups() :
@ -31,10 +32,10 @@ void obj::TypeGroups::setType(ObjectType* type) {
this->type = type; this->type = type;
} }
void obj::TypeGroups::addType(ObjectType* type, tp::InitialierList<const char*> path, tp::alni cur_dir_idx) { void obj::TypeGroups::addType(ObjectType* type, InitialierList<const char*> path, alni cur_dir_idx) {
DEBUG_ASSERT(is_group); DEBUG_ASSERT(is_group);
tp::alni dir_len = (tp::alni) path.size(); alni dir_len = (alni) path.size();
if (dir_len == cur_dir_idx) { if (dir_len == cur_dir_idx) {
TypeGroups* type_ref = new TypeGroups(false); TypeGroups* type_ref = new TypeGroups(false);
@ -44,7 +45,7 @@ void obj::TypeGroups::addType(ObjectType* type, tp::InitialierList<const char*>
} }
TypeGroups* group = nullptr; TypeGroups* group = nullptr;
tp::alni index = 0; alni index = 0;
for (auto dir : path) { for (auto dir : path) {
if (index != cur_dir_idx) { if (index != cur_dir_idx) {

View file

@ -7,6 +7,7 @@
#include "interpreter/Interpreter.hpp" #include "interpreter/Interpreter.hpp"
using namespace tp;
using namespace obj; using namespace obj;
TypeMethod obj::gDefaultTypeMethods[] = { TypeMethod obj::gDefaultTypeMethods[] = {
@ -40,17 +41,17 @@ TypeMethod obj::gDefaultTypeMethods[] = {
}, },
}; };
tp::int2 TypeMethods::presents(const std::string& id) const { int2 TypeMethods::presents(const std::string& id) const {
for (tp::int2 idx = 0; idx < mNMethods; idx++) { for (int2 idx = 0; idx < mNMethods; idx++) {
if (id == methods[idx]->nameid) { if (id == methods[idx]->nameid) {
return idx; return idx;
} }
} }
tp::int2 idx = 0; int2 idx = 0;
for (auto& tm : gDefaultTypeMethods) { for (auto& tm : gDefaultTypeMethods) {
if (id == tm.nameid) { if (id == tm.nameid) {
return { tp::int2(idx + MAX_TYPE_METHODS) }; return { int2(idx + MAX_TYPE_METHODS) };
} }
idx++; idx++;
} }
@ -60,8 +61,8 @@ tp::int2 TypeMethods::presents(const std::string& id) const {
TypeMethods::LookupKey TypeMethods::presents(const ObjectType* type, const std::string& id) { TypeMethods::LookupKey TypeMethods::presents(const ObjectType* type, const std::string& id) {
tp::int2 depth = 0; int2 depth = 0;
tp::int2 idx = 0; int2 idx = 0;
for (auto iter_type = type; iter_type; iter_type = iter_type->base) { for (auto iter_type = type; iter_type; iter_type = iter_type->base) {
idx = iter_type->type_methods.presents(id); idx = iter_type->type_methods.presents(id);
@ -74,7 +75,7 @@ TypeMethods::LookupKey TypeMethods::presents(const ObjectType* type, const std::
return { idx, depth }; return { idx, depth };
} }
const TypeMethod* TypeMethods::getMethod(tp::int2 key) const { const TypeMethod* TypeMethods::getMethod(int2 key) const {
if (key < MAX_TYPE_METHODS) { if (key < MAX_TYPE_METHODS) {
return methods[key]; return methods[key];
} }
@ -94,14 +95,14 @@ void TypeMethods::init() {
mNMethods++; mNMethods++;
} }
for (tp::int1 i = 0; i < mNMethods; i++) { for (int1 i = 0; i < mNMethods; i++) {
methods[i]->init(); methods[i]->init();
} }
// initialize and use finate state automata to lookup methods // initialize and use finate state automata to lookup methods
} }
tp::halni TypeMethods::nMethods() const { return mNMethods; } halni TypeMethods::nMethods() const { return mNMethods; }
void TypeMethod::operator()(Interpreter* interp) const { void TypeMethod::operator()(Interpreter* interp) const {
for (auto i = 1; i <= mNargs; i++) { for (auto i = 1; i <= mNargs; i++) {

View file

@ -4,6 +4,7 @@
#include "interpreter/ByteCode.hpp" #include "interpreter/ByteCode.hpp"
#include "primitives/MethodObject.hpp" #include "primitives/MethodObject.hpp"
using namespace tp;
using namespace obj; using namespace obj;
void CallStack::enter(const CallStack::CallFrame& frame) { void CallStack::enter(const CallStack::CallFrame& frame) {
@ -14,7 +15,7 @@ void CallStack::enter(const CallStack::CallFrame& frame) {
frame.mMethod->mScript->mBytecode.mArgumentsLoaded = 0; frame.mMethod->mScript->mBytecode.mArgumentsLoaded = 0;
frame.mMethod->mScript->mBytecode.mInstructionIdx = 0; frame.mMethod->mScript->mBytecode.mInstructionIdx = 0;
obj::NDO->refinc(frame.mMethod); obj::NDO->increaseReferenceCount(frame.mMethod);
mStack.append(frame); mStack.append(frame);
} }
@ -31,4 +32,4 @@ void CallStack::leave() {
ByteCode* CallStack::getBytecode() { return &mStack.last().mMethod->mScript->mBytecode; } ByteCode* CallStack::getBytecode() { return &mStack.last().mMethod->mScript->mBytecode; }
tp::halni CallStack::len() const { return mStack.size(); } halni CallStack::len() const { return mStack.size(); }

View file

@ -5,20 +5,21 @@
#include "Timing.hpp" #include "Timing.hpp"
using namespace tp;
using namespace obj; using namespace obj;
inline tp::uint1 read_byte(ByteCode* bytecode) { inline uint1 read_byte(ByteCode* bytecode) {
auto out = (tp::uint1) bytecode->mInstructions[bytecode->mInstructionIdx]; auto out = (uint1) bytecode->mInstructions[bytecode->mInstructionIdx];
bytecode->mInstructionIdx++; bytecode->mInstructionIdx++;
return out; return out;
} }
// exeption for opcodes // exeption for opcodes
// reads 2 bytes from instructions in order to load constant onto operands stack // reads 2 bytes from instructions in order to load constant onto operands stack
tp::uint2 loadConstDataIdx(ByteCode* bytecode) { uint2 loadConstDataIdx(ByteCode* bytecode) {
tp::uint2 out = 0; uint2 out = 0;
out |= ((tp::uint2) read_byte(bytecode)); out |= ((uint2) read_byte(bytecode));
out |= ((tp::uint2) read_byte(bytecode) << 8); out |= ((uint2) read_byte(bytecode) << 8);
return out; return out;
} }
@ -26,9 +27,9 @@ tp::uint2 loadConstDataIdx(ByteCode* bytecode) {
// reads 2 bytes from instructions in order to load constant onto operands stack // reads 2 bytes from instructions in order to load constant onto operands stack
void skip_param(ByteCode* bytecode) { bytecode->mInstructionIdx += 2; } void skip_param(ByteCode* bytecode) { bytecode->mInstructionIdx += 2; }
tp::uint2 param(ByteCode* bytecode) { return loadConstDataIdx(bytecode); } uint2 param(ByteCode* bytecode) { return loadConstDataIdx(bytecode); }
void Interpreter::exec(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, tp::InitialierList<GlobalDef> globals2) { void Interpreter::exec(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, InitialierList<GlobalDef> globals2) {
if (!method->mScript->mBytecode.mInstructions.size()) { if (!method->mScript->mBytecode.mInstructions.size()) {
return; return;
} }
@ -52,7 +53,7 @@ void Interpreter::exec(obj::MethodObject* method, obj::ClassObject* self, obj::D
bool Interpreter::finished() { return !mCallStack.len(); } bool Interpreter::finished() { return !mCallStack.len(); }
void Interpreter::stepBytecode() { void Interpreter::stepBytecode() {
tp::halni call_depth = mCallStack.len(); halni call_depth = mCallStack.len();
do { do {
stepBytecodeIn(); stepBytecodeIn();
if (finished()) { if (finished()) {
@ -66,7 +67,7 @@ void Interpreter::stepBytecodeOut() {
return; return;
} }
tp::halni call_depth = mCallStack.len(); halni call_depth = mCallStack.len();
if (!call_depth) { if (!call_depth) {
return; return;
} }
@ -87,7 +88,7 @@ void Interpreter::stepBytecodeIn() {
auto bytecode = mCallStack.getBytecode(); auto bytecode = mCallStack.getBytecode();
if (bytecode->mInstructionIdx >= (tp::ualni) bytecode->mInstructions.size()) { if (bytecode->mInstructionIdx >= (ualni) bytecode->mInstructions.size()) {
// just return // just return
if (mScopeStack.mIdx) { if (mScopeStack.mIdx) {
mScopeStack.leaveScope(); mScopeStack.leaveScope();
@ -114,14 +115,14 @@ void Interpreter::stepBytecodeIn() {
case OpCode::HALT: case OpCode::HALT:
{ {
while (true) { while (true) {
tp::sleep(3); sleep(3);
} }
break; break;
} }
case OpCode::TERMINATE: case OpCode::TERMINATE:
{ {
// tp::terminate(0); // terminate(0);
} }
case OpCode::IGNORE: case OpCode::IGNORE:
@ -242,7 +243,7 @@ void Interpreter::stepBytecodeIn() {
auto id = mOperandsStack.getOperand<StringObject>(); auto id = mOperandsStack.getOperand<StringObject>();
auto obj = mOperandsStack.getOperand(); auto obj = mOperandsStack.getOperand();
mScopeStack.addLocal(obj, id->val); mScopeStack.addLocal(obj, id->val);
NDO->refinc(obj); NDO->increaseReferenceCount(obj);
// mScopeStack.popTemp(); // mScopeStack.popTemp();
break; break;
} }
@ -270,7 +271,7 @@ void Interpreter::stepBytecodeIn() {
// class is a function - execute it as a constructor // class is a function - execute it as a constructor
// PUSH_ARGS protocol // PUSH_ARGS protocol
tp::uint2 len = 0; uint2 len = 0;
mOperandsStack.push((Object*) len); mOperandsStack.push((Object*) len);
mOperandsStack.push(nullptr); mOperandsStack.push(nullptr);
@ -318,7 +319,7 @@ void Interpreter::stepBytecodeIn() {
// +4) object2 // +4) object2
// ... // ...
tp::uint2 len = read_byte(bytecode); uint2 len = read_byte(bytecode);
mOperandsStack.push((Object*) len); mOperandsStack.push((Object*) len);
mOperandsStack.push(nullptr); mOperandsStack.push(nullptr);
break; break;
@ -326,11 +327,11 @@ void Interpreter::stepBytecodeIn() {
case OpCode::SAVE_ARGS: case OpCode::SAVE_ARGS:
{ {
tp::uint2 args_len = read_byte(bytecode); uint2 args_len = read_byte(bytecode);
auto argument = mOperandsStack.getOperand(); auto argument = mOperandsStack.getOperand();
while (argument) { while (argument) {
NDO->refinc(argument); NDO->increaseReferenceCount(argument);
auto argument_id = bytecode->mConstants[loadConstDataIdx(bytecode)]; auto argument_id = bytecode->mConstants[loadConstDataIdx(bytecode)];
NDO_CASTV(StringObject, argument_id, id); NDO_CASTV(StringObject, argument_id, id);
@ -340,7 +341,7 @@ void Interpreter::stepBytecodeIn() {
argument = mOperandsStack.getOperand(); argument = mOperandsStack.getOperand();
} }
tp::uint2 saved_len = tp::uint2(tp::ualni(mOperandsStack.getOperand())); uint2 saved_len = uint2(ualni(mOperandsStack.getOperand()));
ASSERT(args_len == saved_len && "invalid number of arguments passefd"); ASSERT(args_len == saved_len && "invalid number of arguments passefd");
break; break;
@ -388,7 +389,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type"); ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left); auto res = NDO->instantiate(left);
res->type->ariphmetics->add(res, right); res->type->ariphmetics->add(res, right);
mScopeStack.addTemp(res); mScopeStack.addTemp(res);
@ -403,7 +404,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type"); ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left); auto res = NDO->instantiate(left);
res->type->ariphmetics->sub(res, right); res->type->ariphmetics->sub(res, right);
mScopeStack.addTemp(res); mScopeStack.addTemp(res);
@ -418,7 +419,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type"); ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left); auto res = NDO->instantiate(left);
res->type->ariphmetics->mul(res, right); res->type->ariphmetics->mul(res, right);
mScopeStack.addTemp(res); mScopeStack.addTemp(res);
@ -433,7 +434,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type"); ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left); auto res = NDO->instantiate(left);
res->type->ariphmetics->div(res, right); res->type->ariphmetics->div(res, right);
mScopeStack.addTemp(res); mScopeStack.addTemp(res);
@ -593,7 +594,7 @@ void Interpreter::stepBytecodeIn() {
} }
} }
void Interpreter::execAll(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, tp::InitialierList<GlobalDef> globals2) { void Interpreter::execAll(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, InitialierList<GlobalDef> globals2) {
if (!method->mScript->mBytecode.mInstructions.size()) { if (!method->mScript->mBytecode.mInstructions.size()) {
return; return;
} }

View file

@ -1,6 +1,7 @@
#include "interpreter/OperandsStack.hpp" #include "interpreter/OperandsStack.hpp"
using namespace tp;
using namespace obj; using namespace obj;
OperandStack::OperandStack() { OperandStack::OperandStack() {

View file

@ -3,18 +3,17 @@
#include "interpreter/OperatoinCodes.hpp" #include "interpreter/OperatoinCodes.hpp"
namespace obj { using namespace tp;
OpcodeInfos gOpcodeInfos;
};
using namespace obj; using namespace obj;
OpcodeInfos tp::obj::gOpcodeInfos;
#define CONST_IDX_BYTES 2 #define CONST_IDX_BYTES 2
#define OP(opcode, name, desc, ops, params) add(opcode, { #name, #desc, ops, params }); #define OP(opcode, name, desc, ops, params) add(opcode, { #name, #desc, ops, params });
OpcodeInfos::OperandsInfo::OperandsInfo() {} OpcodeInfos::OperandsInfo::OperandsInfo() {}
OpcodeInfos::OperandsInfo::OperandsInfo(tp::InitialierList<Operand> list) { OpcodeInfos::OperandsInfo::OperandsInfo(InitialierList<Operand> list) {
DEBUG_ASSERT(MAX_OPERANDS >= list.size()); DEBUG_ASSERT(MAX_OPERANDS >= list.size());
for (auto item : list) { for (auto item : list) {
buff[len] = item; buff[len] = item;
@ -23,7 +22,7 @@ OpcodeInfos::OperandsInfo::OperandsInfo(tp::InitialierList<Operand> list) {
} }
OpcodeInfos::ParamsInfo::ParamsInfo() {} OpcodeInfos::ParamsInfo::ParamsInfo() {}
OpcodeInfos::ParamsInfo::ParamsInfo(tp::InitialierList<Param> list) { OpcodeInfos::ParamsInfo::ParamsInfo(InitialierList<Param> list) {
DEBUG_ASSERT(MAX_PARAMS >= list.size()); DEBUG_ASSERT(MAX_PARAMS >= list.size());
for (auto item : list) { for (auto item : list) {
buff[len] = item; buff[len] = item;
@ -31,8 +30,8 @@ OpcodeInfos::ParamsInfo::ParamsInfo(tp::InitialierList<Param> list) {
} }
} }
tp::uint1 OpcodeInfos::OpInfo::opsize() { uint1 OpcodeInfos::OpInfo::opsize() {
tp::uint1 out = 1; uint1 out = 1;
for (auto i = 0; i < params.len; i++) { for (auto i = 0; i < params.len; i++) {
out += params.buff[i].bytes; out += params.buff[i].bytes;
} }
@ -364,8 +363,8 @@ OpcodeInfos::OpcodeInfos() {
} }
OpcodeInfos::OpInfo OpcodeInfos::fetch(OpCode code) { OpcodeInfos::OpInfo OpcodeInfos::fetch(OpCode code) {
DEBUG_ASSERT((tp::alni) code >= 0 && (tp::alni) code < (tp::alni) OpCode::END_OPCODES); DEBUG_ASSERT((alni) code >= 0 && (alni) code < (alni) OpCode::END_OPCODES);
return buff[(tp::alni) code]; return buff[(alni) code];
} }
void OpcodeInfos::add(OpCode code, const OpInfo& info) { buff[(tp::alni) code] = info; } void OpcodeInfos::add(OpCode code, const OpInfo& info) { buff[(alni) code] = info; }

View file

@ -1,6 +1,7 @@
#include "interpreter/ScopeStack.hpp" #include "interpreter/ScopeStack.hpp"
using namespace tp;
using namespace obj; using namespace obj;
Scope::~Scope() { Scope::~Scope() {
@ -46,7 +47,7 @@ void ScopeStack::leaveScope() {
} }
void ScopeStack::addTemp(obj::Object* tmp) { void ScopeStack::addTemp(obj::Object* tmp) {
obj::NDO->refinc(tmp); obj::NDO->increaseReferenceCount(tmp);
mBuff[mIdx - 1].mTemps.pushBack(tmp); mBuff[mIdx - 1].mTemps.pushBack(tmp);
} }
@ -57,7 +58,7 @@ void ScopeStack::popTemp() {
void ScopeStack::addTempReturn(obj::Object* ret) { void ScopeStack::addTempReturn(obj::Object* ret) {
if (mIdx >= 2) { if (mIdx >= 2) {
obj::NDO->refinc(ret); obj::NDO->increaseReferenceCount(ret);
mBuff[mIdx - 2].mTemps.pushBack(ret); mBuff[mIdx - 2].mTemps.pushBack(ret);
} }
} }
@ -69,7 +70,7 @@ void ScopeStack::addLocal(obj::Object* local, const std::string& id) {
if (idx) { if (idx) {
obj::NDO->destroy(locals.getSlotVal(idx)); obj::NDO->destroy(locals.getSlotVal(idx));
} }
obj::NDO->refinc(local); obj::NDO->increaseReferenceCount(local);
locals.put(id, local); locals.put(id, local);
} }

View file

@ -1,7 +1,8 @@
#include "Private.hpp" #include "Private.hpp"
using namespace obj::BCgen; using namespace tp;
using namespace obj;
typedef lalr::ParserNode<SymbolType> Node; typedef lalr::ParserNode<SymbolType> Node;
@ -189,7 +190,7 @@ static UserData tmp(const UserData* start, const Node*, size_t) {
#define BIND(name) parser.set_action_handler(#name, &(name)) #define BIND(name) parser.set_action_handler(#name, &(name))
void bind(LalrParser& parser) { void obj::bind(LalrParser& parser) {
BIND(scope); BIND(scope);
BIND(stm_list_append); BIND(stm_list_append);

View file

@ -3,6 +3,7 @@
#include "Private.hpp" #include "Private.hpp"
using namespace tp;
using namespace obj; using namespace obj;
lalr::ErrorPolicy::~ErrorPolicy() = default; lalr::ErrorPolicy::~ErrorPolicy() = default;
@ -37,10 +38,10 @@ Parser::Result Parser::parse(const std::string& stream) {
parser.parse(streamStd.begin(), streamStd.end()); parser.parse(streamStd.begin(), streamStd.end());
BCgen::StatementScope* out = nullptr; StatementScope* out = nullptr;
if (parser.accepted() && parser.full()) { if (parser.accepted() && parser.full()) {
out = (BCgen::StatementScope*) parser.user_data().statement; out = (StatementScope*) parser.user_data().statement;
} }
return { out, !(parser.accepted() && parser.full()) }; return { out, !(parser.accepted() && parser.full()) };

View file

@ -9,42 +9,44 @@
#include "Tree.hpp" #include "Tree.hpp"
struct UserNode {
obj::BCgen::Expression* expression = nullptr;
obj::BCgen::Statement* statement = nullptr;
tp::Buffer<std::string>* arguments = nullptr;
UserNode(obj::BCgen::Statement* stm) :
statement(stm) {}
UserNode(obj::BCgen::Expression* expr) :
expression(expr) {}
UserNode(tp::Buffer<std::string>* argList) :
arguments(argList) {}
UserNode() = default;
obj::BCgen::Statement* releaseStm() {
auto out = statement;
statement = nullptr;
return out;
}
void destroy() {
delete statement;
delete arguments;
delete expression;
}
};
typedef UserNode UserData;
typedef char SymbolType;
typedef std::basic_string<SymbolType>::iterator IteratorType;
extern const lalr::ParserStateMachine* oscript_parser_state_machine; extern const lalr::ParserStateMachine* oscript_parser_state_machine;
typedef lalr::Parser<IteratorType, UserData> LalrParser; namespace tp::obj {
struct UserNode {
obj::Expression* expression = nullptr;
obj::Statement* statement = nullptr;
tp::Buffer<std::string>* arguments = nullptr;
void bind(LalrParser& parser); UserNode(obj::Statement* stm) :
statement(stm) {}
UserNode(obj::Expression* expr) :
expression(expr) {}
UserNode(tp::Buffer<std::string>* argList) :
arguments(argList) {}
UserNode() = default;
obj::Statement* releaseStm() {
auto out = statement;
statement = nullptr;
return out;
}
void destroy() {
delete statement;
delete arguments;
delete expression;
}
};
typedef UserNode UserData;
typedef char SymbolType;
typedef std::basic_string<SymbolType>::iterator IteratorType;
typedef lalr::Parser<IteratorType, UserData> LalrParser;
void bind(LalrParser& parser);
}

View file

@ -2,8 +2,8 @@
#include "primitives/BoolObject.hpp" #include "primitives/BoolObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void BoolObject::constructor(BoolObject* self) { self->val = false; } void BoolObject::constructor(BoolObject* self) { self->val = false; }

View file

@ -3,8 +3,8 @@
#include "primitives/DictObject.hpp" #include "primitives/DictObject.hpp"
#include "primitives/NullObject.hpp" #include "primitives/NullObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void ClassObject::constructor(ClassObject* self) { void ClassObject::constructor(ClassObject* self) {
self->members = NDO_CAST(DictObject, NDO->create("dict")); self->members = NDO_CAST(DictObject, NDO->create("dict"));
@ -38,7 +38,7 @@ void ClassObject::load(ArchiverIn& file_self, ClassObject* self) {
alni ndo_object_adress; alni ndo_object_adress;
file_self >> ndo_object_adress; file_self >> ndo_object_adress;
self->members = NDO_CAST(DictObject, NDO->load(file_self, ndo_object_adress)); self->members = NDO_CAST(DictObject, NDO->load(file_self, ndo_object_adress));
NDO->refinc(self->members); NDO->increaseReferenceCount(self->members);
} }
tp::Buffer<Object*> childs_retrival(ClassObject* self) { tp::Buffer<Object*> childs_retrival(ClassObject* self) {

View file

@ -1,8 +1,8 @@
#include "primitives/ColorObject.hpp" #include "primitives/ColorObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void ColorObject::constructor(Object* self) { NDO_CAST(ColorObject, self)->mCol = tp::RGBA(1.f); } void ColorObject::constructor(Object* self) { NDO_CAST(ColorObject, self)->mCol = tp::RGBA(1.f); }

View file

@ -2,8 +2,8 @@
#include "primitives/DictObject.hpp" #include "primitives/DictObject.hpp"
#include "primitives/StringObject.hpp" #include "primitives/StringObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void DictObject::constructor(Object* self) { void DictObject::constructor(Object* self) {
NDO_CASTV(DictObject, self, dict); NDO_CASTV(DictObject, self, dict);
@ -19,7 +19,7 @@ void DictObject::copy(Object* in, const Object* target) {
constructor(self); constructor(self);
for (auto item : src->items) { for (auto item : src->items) {
auto instance = NDO->instatiate(item->val); auto instance = NDO->instantiate(item->val);
self->items.put(item->key, instance); self->items.put(item->key, instance);
} }
} }
@ -120,7 +120,7 @@ alni DictObject::allocated_size_recursive(DictObject* self) {
void DictObject::put(const std::string& str, Object* obj) { void DictObject::put(const std::string& str, Object* obj) {
DEBUG_ASSERT(obj); DEBUG_ASSERT(obj);
NDO->refinc(obj); NDO->increaseReferenceCount(obj);
items.put(str, obj); items.put(str, obj);
} }

View file

@ -4,8 +4,8 @@
#include <malloc.h> #include <malloc.h>
#include <cstring> #include <cstring>
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void EnumObject::constructor(EnumObject* self) { void EnumObject::constructor(EnumObject* self) {
self->active = 0; self->active = 0;

View file

@ -1,7 +1,7 @@
#include "primitives/FloatObject.hpp" #include "primitives/FloatObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void FloatObject::constructor(FloatObject* self) { self->val = 0; } void FloatObject::constructor(FloatObject* self) { self->val = 0; }

View file

@ -1,8 +1,8 @@
#include "primitives/IntObject.hpp" #include "primitives/IntObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void IntObject::constructor(Object* self) { NDO_CAST(IntObject, self)->val = 0; } void IntObject::constructor(Object* self) { NDO_CAST(IntObject, self)->val = 0; }

View file

@ -3,8 +3,8 @@
#include "primitives/LinkObject.hpp" #include "primitives/LinkObject.hpp"
#include "primitives/MethodObject.hpp" #include "primitives/MethodObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void InterpreterObject::constructor(InterpreterObject* self) { void InterpreterObject::constructor(InterpreterObject* self) {
new (&self->mInterpreter) Interpreter(); new (&self->mInterpreter) Interpreter();

View file

@ -1,8 +1,8 @@
#include "primitives/LinkObject.hpp" #include "primitives/LinkObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void LinkObject::constructor(Object* self) { NDO_CAST(LinkObject, self)->link = 0; } void LinkObject::constructor(Object* self) { NDO_CAST(LinkObject, self)->link = 0; }
@ -38,7 +38,7 @@ void LinkObject::load(ArchiverIn& file_self, LinkObject* self) {
self->link = nullptr; self->link = nullptr;
} else { } else {
self->link = NDO->load(file_self, saved_object_adress); self->link = NDO->load(file_self, saved_object_adress);
NDO->refinc(self->link); NDO->increaseReferenceCount(self->link);
} }
} }
@ -62,7 +62,7 @@ Object* LinkObject::getLink() { return link; }
void LinkObject::setLink(Object* obj) { void LinkObject::setLink(Object* obj) {
if (link) NDO->destroy(link); if (link) NDO->destroy(link);
if (obj) NDO->refinc(obj); if (obj) NDO->increaseReferenceCount(obj);
link = obj; link = obj;
} }

View file

@ -2,8 +2,8 @@
#include "primitives/IntObject.hpp" #include "primitives/IntObject.hpp"
#include "primitives/ListObject.hpp" #include "primitives/ListObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void ListObject::constructor(Object* in) { void ListObject::constructor(Object* in) {
NDO_CASTV(ListObject, in, self); NDO_CASTV(ListObject, in, self);
@ -17,7 +17,7 @@ void ListObject::copy(Object* in, const Object* target) {
destructor(in); destructor(in);
for (auto item : src->items) { for (auto item : src->items) {
self->pushBack(NDO->instatiate(item.data())); self->pushBack(NDO->instantiate(item.data()));
} }
} }
@ -84,12 +84,12 @@ alni ListObject::allocated_size_recursive(ListObject* self) {
} }
void ListObject::pushBack(Object* obj) { void ListObject::pushBack(Object* obj) {
obj::NDO->refinc(obj); obj::NDO->increaseReferenceCount(obj);
items.pushBack(obj); items.pushBack(obj);
} }
void ListObject::pushFront(Object* obj) { void ListObject::pushFront(Object* obj) {
obj::NDO->refinc(obj); obj::NDO->increaseReferenceCount(obj);
items.pushFront(obj); items.pushFront(obj);
} }

View file

@ -3,6 +3,7 @@
#include "core/ScriptSection.hpp" #include "core/ScriptSection.hpp"
#include "primitives/MethodObject.hpp" #include "primitives/MethodObject.hpp"
using namespace tp;
using namespace obj; using namespace obj;
struct ObjectType MethodObject::TypeData = { struct ObjectType MethodObject::TypeData = {
@ -52,7 +53,7 @@ void MethodObject::Initialize() {
void MethodObject::UnInitialize() { obj::ScriptSection::uninitialize(); } void MethodObject::UnInitialize() { obj::ScriptSection::uninitialize(); }
void MethodObject::compile() { BCgen::Compile(this); } void MethodObject::compile() { Compile(this); }
MethodObject* MethodObject::create(const std::string& script) { MethodObject* MethodObject::create(const std::string& script) {
auto out = (MethodObject*) NDO->create(MethodObject::TypeData.name); auto out = (MethodObject*) NDO->create(MethodObject::TypeData.name);

View file

@ -2,8 +2,8 @@
#include "primitives/NullObject.hpp" #include "primitives/NullObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
obj::NullObject* obj::NdoNull_globalInstance = nullptr; obj::NullObject* obj::NdoNull_globalInstance = nullptr;
bool uninit_flag = false; bool uninit_flag = false;

View file

@ -2,8 +2,8 @@
#include "primitives/StringObject.hpp" #include "primitives/StringObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
void StringObject::constructor(Object* self) { new (&NDO_CAST(StringObject, self)->val) std::string(); } void StringObject::constructor(Object* self) { new (&NDO_CAST(StringObject, self)->val) std::string(); }

View file

@ -2,8 +2,8 @@
#include "primitives/TypeObject.hpp" #include "primitives/TypeObject.hpp"
#include "primitives/NullObject.hpp" #include "primitives/NullObject.hpp"
using namespace obj;
using namespace tp; using namespace tp;
using namespace obj;
TypeObject* TypeObject::create(const ObjectType* type) { TypeObject* TypeObject::create(const ObjectType* type) {
NDO_CASTV(TypeObject, NDO->create("typeobject"), out); NDO_CASTV(TypeObject, NDO->create("typeobject"), out);

View file

@ -1,45 +1,42 @@
#pragma once #pragma once
#include "interpreter/ByteCode.hpp"
#include "core/Object.hpp" #include "core/Object.hpp"
#include "interpreter/ByteCode.hpp"
namespace obj { namespace tp::obj {
namespace BCgen {
class ConstObject { class ConstObject {
public: public:
obj::Object* mObj = nullptr; Object* mObj = nullptr;
tp::alni mConstIdx = 0; alni mConstIdx = 0;
ConstObject(); ConstObject();
ConstObject(obj::Object* mObj); ConstObject(Object* mObj);
};
class ConstObjectsPool {
public:
tp::Map<std::string, ConstObject*> mMethods;
tp::Map<std::string, ConstObject*> mStrings;
tp::Map<tp::alni, ConstObject*> mIntegers;
tp::Map<tp::alnf, ConstObject*> mFloats;
ConstObject mBoolTrue;
ConstObject mBoolFalse;
bool mDelete = true;
tp::alni mTotalObjects = 0;
ConstObject* get(tp::alni val);
ConstObject* get(const std::string& val);
ConstObject* get(tp::alnf val);
ConstObject* get(bool val);
ConstObject* addMethod(const std::string& method_id, obj::Object* method);
ConstObject* registerObject(obj::Object* obj);
void save(tp::Buffer<ConstData>& out);
ConstObjectsPool() = default;
~ConstObjectsPool();
};
}; };
};
class ConstObjectsPool {
public:
Map<std::string, ConstObject*> mMethods;
Map<std::string, ConstObject*> mStrings;
Map<alni, ConstObject*> mIntegers;
Map<alnf, ConstObject*> mFloats;
ConstObject mBoolTrue;
ConstObject mBoolFalse;
bool mDelete = true;
alni mTotalObjects = 0;
ConstObject* get(alni val);
ConstObject* get(const std::string& val);
ConstObject* get(alnf val);
ConstObject* get(bool val);
ConstObject* addMethod(const std::string& method_id, Object* method);
ConstObject* registerObject(Object* obj);
void save(Buffer<ConstData>& out);
ConstObjectsPool() = default;
~ConstObjectsPool();
};
}

View file

@ -1,12 +1,10 @@
#pragma once #pragma once
#include <string>
#include "Buffer.hpp" #include "Buffer.hpp"
#include "interpreter/OperatoinCodes.hpp" #include "interpreter/OperatoinCodes.hpp"
#include <string>
namespace obj::BCgen { namespace tp::obj {
struct Expression { struct Expression {
enum class Type { enum class Type {
@ -33,26 +31,26 @@ namespace obj::BCgen {
}; };
struct ExpressionList : public Expression { struct ExpressionList : public Expression {
tp::Buffer<Expression*> mItems; Buffer<Expression*> mItems;
ExpressionList(); ExpressionList();
~ExpressionList() override; ~ExpressionList() override;
}; };
struct ExpressionNew : public Expression { struct ExpressionNew : public Expression {
std::string mNewType; std::string mNewType;
explicit ExpressionNew(const std::string& type); explicit ExpressionNew(std::string type);
~ExpressionNew() override; ~ExpressionNew() override;
}; };
struct ExpressionLocal : public Expression { struct ExpressionLocal : public Expression {
std::string mLocalId; std::string mLocalId;
explicit ExpressionLocal(const std::string& id); explicit ExpressionLocal(std::string id);
~ExpressionLocal() override; ~ExpressionLocal() override;
}; };
struct ExpressionFunc : public Expression { struct ExpressionFunc : public Expression {
std::string mFuncId; std::string mFuncId;
explicit ExpressionFunc(const std::string& id); explicit ExpressionFunc(std::string id);
~ExpressionFunc() override; ~ExpressionFunc() override;
}; };
@ -60,7 +58,7 @@ namespace obj::BCgen {
Expression* mParent = nullptr; Expression* mParent = nullptr;
std::string mLocalId; std::string mLocalId;
bool mMethod = false; bool mMethod = false;
ExpressionChild(Expression* mParent, const std::string& id); ExpressionChild(Expression* mParent, std::string id);
~ExpressionChild() override; ~ExpressionChild() override;
}; };
@ -83,7 +81,7 @@ namespace obj::BCgen {
Expression* mLeft = nullptr; Expression* mLeft = nullptr;
Expression* mRight = nullptr; Expression* mRight = nullptr;
enum class BoolType : tp::uint1 { enum class BoolType : uint1 {
AND = 24U, AND = 24U,
OR, OR,
EQUAL, EQUAL,
@ -103,16 +101,16 @@ namespace obj::BCgen {
struct ExpressionConst : public Expression { struct ExpressionConst : public Expression {
enum ConstType { STR, INT, BOOL, FLT } mConstType; enum ConstType { STR, INT, BOOL, FLT } mConstType;
std::string str; std::string str;
tp::alni integer = 0; alni integer = 0;
tp::alnf floating = 0; alnf floating = 0;
bool boolean = false; bool boolean = false;
explicit ExpressionConst(const std::string& val); explicit ExpressionConst(std::string val);
explicit ExpressionConst(const char* val); explicit ExpressionConst(const char* val);
explicit ExpressionConst(tp::alni val); explicit ExpressionConst(alni val);
explicit ExpressionConst(tp::int4 val); explicit ExpressionConst(int4 val);
explicit ExpressionConst(tp::flt4 val); explicit ExpressionConst(flt4 val);
explicit ExpressionConst(tp::alnf val); explicit ExpressionConst(alnf val);
explicit ExpressionConst(bool val); explicit ExpressionConst(bool val);
~ExpressionConst() override; ~ExpressionConst() override;

View file

@ -1,7 +1,5 @@
#pragma once #pragma once
#include <string>
#include "List.hpp" #include "List.hpp"
#include "Map.hpp" #include "Map.hpp"
@ -9,39 +7,38 @@
#include "Statements.hpp" #include "Statements.hpp"
#include "Constants.hpp" #include "Constants.hpp"
namespace obj { #include <string>
namespace tp::obj {
struct MethodObject; struct MethodObject;
namespace BCgen { struct FunctionDefinition {
struct FunctionDefinition { FunctionDefinition* mPrnt = nullptr;
FunctionDefinition* mPrnt = nullptr;
// signature // signature
std::string mFunctionId; std::string mFunctionId;
tp::List<ConstObject*> mArgsOrder; List<ConstObject*> mArgsOrder;
ConstObjectsPool mConstants; ConstObjectsPool mConstants;
tp::Map<std::string, ConstObject*> mLocals; Map<std::string, ConstObject*> mLocals;
tp::List<Instruction> mInstructions; List<Instruction> mInstructions;
FunctionDefinition(const std::string& function_id, tp::Buffer<std::string> args, FunctionDefinition* prnt); FunctionDefinition(const std::string& function_id, const Buffer<std::string>& args, FunctionDefinition* prnt);
FunctionDefinition() {} FunctionDefinition() {}
void generateByteCode(ByteCode& out); void generateByteCode(ByteCode& out);
tp::List<Instruction>::Node* inst(Instruction inst); List<Instruction>::Node* inst(Instruction inst);
void EvalExpr(Expression* expr);
void EvalStatement(Statement* expr);
ConstObject* defineLocal(const std::string& id); void EvalExpr(Expression* expr);
}; void EvalStatement(Statement* expr);
void init(); ConstObject* defineLocal(const std::string& id);
void deinit();
void Genereate(ByteCode& out, StatementScope* body);
bool Compile(MethodObject* obj);
}; };
};
void initialize();
void finalize();
void Generate(ByteCode& out, StatementScope* body);
bool Compile(MethodObject* obj);
}

View file

@ -2,52 +2,47 @@
#pragma once #pragma once
#include "interpreter/OperatoinCodes.hpp" #include "interpreter/OperatoinCodes.hpp"
#include "core/Object.hpp" #include "core/Object.hpp"
#include "List.hpp" #include "List.hpp"
namespace obj { namespace tp::obj {
namespace BCgen { class ConstObject;
class ConstObject; class Instruction {
public:
class Instruction { OpCode mOp = OpCode::NONE;
public:
OpCode mOp = OpCode::NONE; enum class ArgType : ualni {
NO_ARG,
PARAM,
CONST_ARG,
} mArgType = ArgType::NO_ARG;
enum class ArgType : tp::ualni { enum class InstType : ualni {
NO_ARG, NONE,
PARAM, JUMP,
CONST_ARG, JUMP_IF,
} mArgType = ArgType::NO_ARG; JUMP_IF_NOT,
EXEC,
PURE_CONST,
} mInstType = InstType::NONE;
enum class InstType : tp::ualni { alni mParam = 0;
NONE, alni mParamBytes = 1;
JUMP,
JUMP_IF,
JUMP_IF_NOT,
EXEC,
PURE_CONST,
} mInstType = InstType::NONE;
tp::alni mParam = 0; ConstObject* mConstData = nullptr;
tp::alni mParamBytes = 1; ConstObject* mConstData2 = nullptr;
ConstObject* mConstData = nullptr; alni mInstIdx = 0;
ConstObject* mConstData2 = nullptr; Instruction* mInstTarget = nullptr;
tp::alni mInstIdx = 0; Instruction();
Instruction* mInstTarget = nullptr; Instruction(ConstObject* constData);
Instruction(OpCode op);
Instruction(); Instruction(OpCode op, ConstObject* constData);
Instruction(ConstObject* constData); Instruction(OpCode op, ConstObject* constData, ConstObject* constData2);
Instruction(OpCode op); Instruction(OpCode op, alni param, alni nBytes);
Instruction(OpCode op, ConstObject* constData); Instruction(Instruction* inst, InstType jump_type);
Instruction(OpCode op, ConstObject* constData, ConstObject* constData2);
Instruction(OpCode op, tp::alni param, tp::alni nBytes);
Instruction(Instruction* inst, InstType jump_type);
};
}; };
}; }

View file

@ -3,7 +3,7 @@
#include "Expressions.hpp" #include "Expressions.hpp"
namespace obj::BCgen { namespace tp::obj {
struct Statement { struct Statement {
enum class Type { enum class Type {
@ -28,15 +28,15 @@ namespace obj::BCgen {
}; };
struct StatementScope : public Statement { struct StatementScope : public Statement {
tp::Buffer<Statement*> mStatements; Buffer<Statement*> mStatements;
bool mPushToScopeStack = false; bool mPushToScopeStack = false;
StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack); StatementScope(InitialierList<Statement*> statements, bool aPushToScopeStack);
~StatementScope() override; ~StatementScope() override;
}; };
struct StatementFuncDef : public Statement { struct StatementFuncDef : public Statement {
tp::Buffer<std::string> mArgs; Buffer<std::string> mArgs;
std::string mFunctionId; std::string mFunctionId;
StatementScope* mStatements = nullptr; StatementScope* mStatements = nullptr;
@ -50,7 +50,7 @@ namespace obj::BCgen {
ExpressionConst* mConstExpr = nullptr; ExpressionConst* mConstExpr = nullptr;
bool mIsConstExpr = false; bool mIsConstExpr = false;
StatementLocalDef(const std::string& id, Expression* value); StatementLocalDef(std::string id, Expression* value);
~StatementLocalDef() override; ~StatementLocalDef() override;
}; };

View file

@ -10,12 +10,12 @@
#include "core/TypeGroups.hpp" #include "core/TypeGroups.hpp"
#include "core/TypeMethods.hpp" #include "core/TypeMethods.hpp"
#include "Archiver.hpp" #include "ObjectArchiver.hpp"
#include "SizeCounter.hpp" #include "SizeCounter.hpp"
#include <string> #include <string>
//#include "interpreter/interpreter.h" #include "core/ObjectArchiver.hpp"
/* Steps to create custom Object: /* Steps to create custom Object:
define name of object define name of object
@ -37,80 +37,29 @@ implement construct, destruct and copy methods */
#define NDO_MEMH_FROM_NDO(ndo_ptr) (((ObjectMemHead*)ndo_ptr) - 1) #define NDO_MEMH_FROM_NDO(ndo_ptr) (((ObjectMemHead*)ndo_ptr) - 1)
#define NDO_FROM_MEMH(ndo_ptr) ((Object*)(ndo_ptr + 1)) #define NDO_FROM_MEMH(ndo_ptr) ((Object*)(ndo_ptr + 1))
namespace obj { namespace tp::obj {
extern ModuleManifest gModuleObjects;
template<bool tRead>
class Archiver : public tp::ArchiverTemplate<tRead> {
tp::LocalConnection mConnection;
tp::ualni mAddress = 0;
tp::ualni mFirstNotWritten = 0;
public:
Archiver() = default;
explicit Archiver(const char* location) {
mConnection.connect(tp::LocalConnection::Location(location), tp::LocalConnection::Type(tRead));
};
void writeBytes(const tp::int1* val, tp::ualni size) override {
mConnection.setPointer(mAddress);
mConnection.writeBytes(val, size);
incrementAddresses(size);
}
void readBytes(tp::int1* val, tp::ualni size) override {
mConnection.setPointer(mAddress);
mConnection.readBytes(val, size);
incrementAddresses(size);
}
tp::ualni getAddress() { return mAddress; }
void setAddress(tp::ualni addr) { mAddress = addr; }
tp::ualni getFreeAddress() { return mFirstNotWritten; }
void setFreeAddress(tp::ualni addr) { mFirstNotWritten = addr; }
bool isOpened() { return mConnection.getConnectionStatus().isOpened(); }
tp::ualni getSize() { return mConnection.size(); }
private:
void incrementAddresses(tp::ualni size) {
// if (mAddress + size > mFirstNotWritten) mFirstNotWritten = mAddress + size;
mAddress += size;
}
};
using ArchiverIn = Archiver<true>;
using ArchiverOut = Archiver<false>;
void save_string(ArchiverOut& file, const std::string& string);
tp::ualni save_string_size(const std::string& string);
void load_string(ArchiverIn& file, std::string& out);
extern tp::ModuleManifest gModuleObjects;
extern struct objects_api* NDO; extern struct objects_api* NDO;
struct ObjectMemHead { struct ObjectMemHead {
ObjectMemHead* up; ObjectMemHead* up;
ObjectMemHead* down; ObjectMemHead* down;
tp::alni flags; alni flags;
tp::alni refc; alni refc;
}; };
struct Object { struct Object {
const struct ObjectType* type; const struct ObjectType* type;
}; };
typedef void (*object_from_int)(Object* self, tp::alni in); typedef void (*object_from_int)(Object* self, alni in);
typedef void (*object_from_float)(Object* self, tp::alnf in); typedef void (*object_from_float)(Object* self, alnf in);
typedef void (*object_from_string)(Object* self, const std::string& in); typedef void (*object_from_string)(Object* self, const std::string& in);
typedef std::string(*object_to_string)(Object* self); typedef std::string(*object_to_string)(Object* self);
typedef tp::alni(*object_to_int)(Object* self); typedef alni(*object_to_int)(Object* self);
typedef tp::alnf(*object_to_float)(Object* self); typedef alnf(*object_to_float)(Object* self);
struct ObjectTypeConversions { struct ObjectTypeConversions {
object_from_int from_int; object_from_int from_int;
@ -137,22 +86,22 @@ namespace obj {
typedef void (*object_destructor)(Object* self); typedef void (*object_destructor)(Object* self);
typedef void (*object_copy)(Object* self, const Object* target); typedef void (*object_copy)(Object* self, const Object* target);
typedef tp::alni(*object_save_size)(Object* self); typedef alni(*object_save_size)(Object* self);
typedef void (*object_save)(Object*, ArchiverOut&); typedef void (*object_save)(Object*, ArchiverOut&);
typedef void (*object_load)(ArchiverIn&, Object*); typedef void (*object_load)(ArchiverIn&, Object*);
typedef bool (*object_compare)(Object*, Object*); typedef bool (*object_compare)(Object*, Object*);
typedef tp::Buffer<Object*> (*object_debug_all_childs_retrival)(Object*); typedef Buffer<Object*> (*object_debug_all_childs_retrival)(Object*);
typedef tp::alni (*object_allocated_size)(Object*); // default value = type->size - sizeof(ObjectType*) typedef alni (*object_allocated_size)(Object*); // default value = type->size - sizeof(ObjectType*)
typedef tp::alni (*object_allocated_size_recursive)(Object*); // default value = object_allocated_size typedef alni (*object_allocated_size_recursive)(Object*); // default value = object_allocated_size
struct ObjectType { struct ObjectType {
const ObjectType* base = nullptr; const ObjectType* base = nullptr;
object_constructor constructor = nullptr; object_constructor constructor = nullptr;
object_destructor destructor = nullptr; object_destructor destructor = nullptr;
object_copy copy = nullptr; object_copy copy = nullptr;
tp::alni size = 0; alni size = 0;
const char* name; const char* name;
const ObjectTypeConversions* convesions = nullptr; const ObjectTypeConversions* convesions = nullptr;
const ObjectTypeAriphmetics* ariphmetics = nullptr; const ObjectTypeAriphmetics* ariphmetics = nullptr;
@ -174,7 +123,7 @@ namespace obj {
typedef void (pre_load_callback)(void* self, ArchiverIn&); typedef void (pre_load_callback)(void* self, ArchiverIn&);
typedef void (post_save_callback)(void* self, ArchiverOut&); typedef void (post_save_callback)(void* self, ArchiverOut&);
typedef void (post_load_callback)(void* self, ArchiverIn&); typedef void (post_load_callback)(void* self, ArchiverIn&);
typedef tp::alni (slcb_size_callback)(void* self, ArchiverOut&); typedef alni (slcb_size_callback)(void* self, ArchiverOut&);
struct save_load_callbacks { struct save_load_callbacks {
void* self; void* self;
@ -188,8 +137,8 @@ namespace obj {
struct objects_api { struct objects_api {
tp::Map<std::string, const ObjectType*> types; Map<std::string, const ObjectType*> types;
obj::TypeGroups type_groups; TypeGroups type_groups;
Interpreter* interp = nullptr; Interpreter* interp = nullptr;
objects_api(); objects_api();
@ -197,54 +146,54 @@ namespace obj {
void define(ObjectType* type); void define(ObjectType* type);
Object* create(const std::string& name); Object* create(const std::string& name);
Object* copy(Object* self, const Object* in); static Object* copy(Object* self, const Object* in);
bool compare(Object* first, Object* second); static bool compare(Object* first, Object* second);
Object* instatiate(Object*); static Object* instantiate(Object*);
void set(Object* self, tp::alni val); static void set(Object* self, alni val);
void set(Object* self, tp::alnf val); static void set(Object* self, alnf val);
void set(Object* self, const std::string& val); static void set(Object* self, const std::string& val);
tp::alni toInt(Object* self); static alni toInt(Object* self);
tp::alnf toFloat(Object* self); static alnf toFloat(Object* self);
bool toBool(Object* self); static bool toBool(Object* self);
std::string toString(Object* self); static std::string toString(Object* self);
void clear_object_flags(); void clear_object_flags();
void destroy(Object* in); void destroy(Object* in) const;
void refinc(Object* in); static void increaseReferenceCount(Object* in);
tp::halni getrefc(Object* in); static halni getReferenceCount(Object* in);
private: private:
void setrefc(Object* in, tp::halni refc); static void setReferenceCount(Object* in, halni count);
public: public:
save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS]; save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS]{};
tp::alni sl_callbacks_load_idx = 0; alni sl_callbacks_load_idx = 0;
void add_sl_callbacks(save_load_callbacks*); void add_sl_callbacks(save_load_callbacks*);
tp::alni objsize_file(Object* self); alni objsize_file(Object* self);
tp::alni objsize_file_recursive(Object* self); alni objsize_file_recursive(Object* self);
tp::alni objsize_ram(Object* self); alni objsize_ram(Object* self);
tp::alni objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object = true); alni objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object = true);
tp::alni objsize_ram_recursive(Object* self); alni objsize_ram_recursive(Object* self);
bool save(Object*, const std::string& path, bool compressed = true); bool save(Object*, const std::string& path, bool compressed = true);
Object* load(const std::string& path); Object* load(const std::string& path);
tp::alni save(ArchiverOut&, Object*); alni save(ArchiverOut&, Object*);
Object* load(ArchiverIn&, tp::alni file_adress); Object* load(ArchiverIn&, alni file_adress);
}; };
void logTypeData(const ObjectType* type); void logTypeData(const ObjectType* type);
void assertNoLeaks(); void assertNoLeaks();
tp::ualni getObjCount(); ualni getObjCount();
Object* ndo_cast(const Object* in, const ObjectType* to_type); Object* ndo_cast(const Object* in, const ObjectType* to_type);
template <typename Type> template <typename Type>
Type* create() { Type* create() {
return (Type*)obj::NDO->create(Type::TypeData.name); return (Type*)NDO->create(Type::TypeData.name);
} }
}; }

View file

@ -0,0 +1,56 @@
#pragma once
#include "ObjectArchiver.hpp"
#include "LocalConnection.hpp"
namespace tp::obj {
template<bool tRead>
class Archiver : public ArchiverTemplate<tRead> {
LocalConnection mConnection;
ualni mAddress = 0;
ualni mFirstNotWritten = 0;
public:
Archiver() = default;
explicit Archiver(const char* location) {
mConnection.connect(LocalConnection::Location(location), LocalConnection::Type(tRead));
};
void writeBytes(const int1* val, ualni size) override {
mConnection.setPointer(mAddress);
mConnection.writeBytes(val, size);
incrementAddresses(size);
}
void readBytes(int1* val, ualni size) override {
mConnection.setPointer(mAddress);
mConnection.readBytes(val, size);
incrementAddresses(size);
}
ualni getAddress() { return mAddress; }
void setAddress(ualni addr) { mAddress = addr; }
ualni getFreeAddress() { return mFirstNotWritten; }
void setFreeAddress(ualni addr) { mFirstNotWritten = addr; }
bool isOpened() { return mConnection.getConnectionStatus().isOpened(); }
ualni getSize() { return mConnection.size(); }
private:
void incrementAddresses(ualni size) {
// if (mAddress + size > mFirstNotWritten) mFirstNotWritten = mAddress + size;
mAddress += size;
}
};
using ArchiverIn = Archiver<true>;
using ArchiverOut = Archiver<false>;
void save_string(ArchiverOut& file, const std::string& string);
ualni save_string_size(const std::string& string);
void load_string(ArchiverIn& file, std::string& out);
}

View file

@ -2,19 +2,19 @@
#include "primitives/MethodObject.hpp" #include "primitives/MethodObject.hpp"
namespace obj { namespace tp::obj {
// global singleton API for method object to manage the script // global singleton API for method object to manage the script
struct ScriptSection { struct ScriptSection {
tp::List<Script*> mScripts; List<Script*> mScripts;
Script* createScript(); Script* createScript();
void changeScript(Script** current_script, Script** new_script); void changeScript(Script** current_script, Script** new_script);
void abandonScript(Script* script); void abandonScript(Script* script);
tp::alni get_script_file_adress(Script* in); alni get_script_file_adress(Script* in);
Script* get_scritp_from_file_adress(tp::alni file_adress); Script* get_scritp_from_file_adress(alni file_adress);
~ScriptSection(); ~ScriptSection();
@ -31,6 +31,6 @@ namespace obj {
static void save_script_table_to_file(ScriptSection* self, ArchiverOut& file); static void save_script_table_to_file(ScriptSection* self, ArchiverOut& file);
static void load_script_table_from_file(ScriptSection* self, ArchiverIn& file); static void load_script_table_from_file(ScriptSection* self, ArchiverIn& file);
static tp::alni save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file); static alni save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file);
}; };
}; }

View file

@ -4,7 +4,7 @@
#include "Map.hpp" #include "Map.hpp"
#include <string> #include <string>
namespace obj { namespace tp::obj {
struct ObjectType; struct ObjectType;
struct objects_api; struct objects_api;
@ -13,15 +13,15 @@ namespace obj {
public: public:
typedef tp::Map<std::string, obj::TypeGroups*> Dict; typedef Map<std::string, TypeGroups*> Dict;
TypeGroups(); TypeGroups();
friend struct obj::objects_api; friend struct objects_api;
explicit TypeGroups(bool is_group); explicit TypeGroups(bool is_group);
void addType(ObjectType* type, tp::InitialierList<const char*> path, tp::alni cur_arg = 0); void addType(ObjectType* type, InitialierList<const char*> path, alni cur_arg = 0);
void setType(ObjectType* type); void setType(ObjectType* type);
bool isGroup(); bool isGroup();
Dict* getChilds(); Dict* getChilds();
@ -36,5 +36,4 @@ namespace obj {
const obj::ObjectType* type; const obj::ObjectType* type;
}; };
}; };
}
};

View file

@ -4,7 +4,7 @@
#include "Buffer.hpp" #include "Buffer.hpp"
#include <string> #include <string>
namespace obj { namespace tp::obj {
struct Interpreter; struct Interpreter;
struct Object; struct Object;
@ -23,7 +23,7 @@ namespace obj {
mutable Object* self = nullptr; mutable Object* self = nullptr;
Arg args[MAX_ARGS]; Arg args[MAX_ARGS];
tp::int1 mNargs = 0; int1 mNargs = 0;
void (*exec)(const TypeMethod* tm) = nullptr; void (*exec)(const TypeMethod* tm) = nullptr;
@ -37,14 +37,14 @@ namespace obj {
extern TypeMethod gDefaultTypeMethods[3]; extern TypeMethod gDefaultTypeMethods[3];
struct TypeMethods { struct TypeMethods {
enum : tp::int2 { MAX_TYPE_METHODS = 128 }; enum : int2 { MAX_TYPE_METHODS = 128 };
TypeMethod* methods[MAX_TYPE_METHODS]; TypeMethod* methods[MAX_TYPE_METHODS];
tp::halni mNMethods = 0; halni mNMethods = 0;
struct LookupKey { struct LookupKey {
tp::int2 key = -1; int2 key = -1;
tp::int2 type_depth = -1; int2 type_depth = -1;
operator bool() { return key != -1; } operator bool() { return key != -1; }
}; };
@ -53,11 +53,10 @@ namespace obj {
void init(); void init();
tp::halni nMethods() const; halni nMethods() const;
private: private:
tp::int2 presents(const std::string&) const; int2 presents(const std::string&) const;
const TypeMethod* getMethod(tp::int2) const; const TypeMethod* getMethod(int2) const;
}; };
}
};

View file

@ -9,15 +9,15 @@
#include "Buffer.hpp" #include "Buffer.hpp"
#include <string> #include <string>
namespace obj { namespace tp::obj {
typedef Object* ConstData; typedef Object* ConstData;
struct ByteCode { struct ByteCode {
tp::Buffer<ConstData> mConstants; Buffer<ConstData> mConstants;
tp::Buffer<OpCode> mInstructions; Buffer<OpCode> mInstructions;
tp::ualni mInstructionIdx = 0; ualni mInstructionIdx = 0;
tp::ualni mArgumentsLoaded = 0; ualni mArgumentsLoaded = 0;
~ByteCode() { ~ByteCode() {
for (auto const_obj : mConstants) { for (auto const_obj : mConstants) {
@ -25,4 +25,4 @@ namespace obj {
} }
} }
}; };
}; }

View file

@ -5,11 +5,8 @@
#include "Map.hpp" #include "Map.hpp"
namespace obj { namespace tp::obj {
struct MethodObject; struct MethodObject;
};
namespace obj {
struct ByteCode; struct ByteCode;
@ -17,16 +14,16 @@ namespace obj {
struct CallFrame { struct CallFrame {
enum { CALL_DEPTH = 1024 }; enum { CALL_DEPTH = 1024 };
obj::Object* mSelf = nullptr; Object* mSelf = nullptr;
obj::MethodObject* mMethod = nullptr; MethodObject* mMethod = nullptr;
tp::ualni mIp = 0; ualni mIp = 0;
}; };
void enter(const CallFrame& frame); void enter(const CallFrame& frame);
void leave(); void leave();
ByteCode* getBytecode(); ByteCode* getBytecode();
[[nodiscard]] tp::halni len() const; [[nodiscard]] halni len() const;
tp::ConstSizeBuffer<CallFrame, CallFrame::CALL_DEPTH> mStack; ConstSizeBuffer<CallFrame, CallFrame::CALL_DEPTH> mStack;
}; };
}; }

View file

@ -4,19 +4,19 @@
#include "ScopeStack.hpp" #include "ScopeStack.hpp"
#include "CallStack.hpp" #include "CallStack.hpp"
namespace obj { namespace tp::obj {
struct Interpreter { struct Interpreter {
OperandStack mOperandsStack; OperandStack mOperandsStack;
ScopeStack mScopeStack; ScopeStack mScopeStack;
CallStack mCallStack; CallStack mCallStack;
obj::Object* mLastParent = nullptr; Object* mLastParent = nullptr;
bool mIsTypeMethod = false; bool mIsTypeMethod = false;
const TypeMethod* mTypeMethod = nullptr; const TypeMethod* mTypeMethod = nullptr;
typedef struct { obj::Object* obj; std::string id; } GlobalDef; typedef struct { Object* obj; std::string id; } GlobalDef;
void exec(obj::MethodObject* method, obj::ClassObject* self = nullptr, obj::DictObject* globals = nullptr, tp::InitialierList<GlobalDef> globals2 = {}); void exec(MethodObject* method, ClassObject* self = nullptr, DictObject* globals = nullptr, InitialierList<GlobalDef> globals2 = {});
void stepBytecode(); void stepBytecode();
void stepBytecodeIn(); void stepBytecodeIn();
@ -24,6 +24,6 @@ namespace obj {
bool finished(); bool finished();
void execAll(obj::MethodObject* method, obj::ClassObject* self = nullptr, obj::DictObject* globals = nullptr, tp::InitialierList<GlobalDef> globals2 = {}); void execAll(MethodObject* method, ClassObject* self = nullptr, DictObject* globals = nullptr, InitialierList<GlobalDef> globals2 = {});
}; };
}; }

View file

@ -3,21 +3,21 @@
#include "ByteCode.hpp" #include "ByteCode.hpp"
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
// can be other aligned value as well // can be other aligned value as well
typedef obj::Object* Operand; typedef Object* Operand;
class OperandStack { class OperandStack {
enum : tp::alni { enum : alni {
MAX_STACK_SIZE = 512 MAX_STACK_SIZE = 512
}; };
public: public:
Operand* mBuff; Operand* mBuff;
tp::ualni mIdx; ualni mIdx;
OperandStack(); OperandStack();
void push(Operand operand); void push(Operand operand);
@ -35,5 +35,4 @@ namespace obj {
~OperandStack(); ~OperandStack();
}; };
}
};

View file

@ -12,11 +12,11 @@
// 2) Index of bytecode->ConstData from bytecode->Instruction (ConstData) // 2) Index of bytecode->ConstData from bytecode->Instruction (ConstData)
// 3) Raw Bytes from bytecode->Instruction (Param) // 3) Raw Bytes from bytecode->Instruction (Param)
namespace obj { namespace tp::obj {
extern struct OpcodeInfos gOpcodeInfos; extern struct OpcodeInfos gOpcodeInfos;
enum class OpCode : tp::uint1 { enum class OpCode : uint1 {
NONE = 0x00, NONE = 0x00,
HALT, HALT,
@ -118,24 +118,24 @@ namespace obj {
enum { MAX_OPERANDS = 5 }; enum { MAX_OPERANDS = 5 };
Operand buff[MAX_OPERANDS]; Operand buff[MAX_OPERANDS];
tp::halni len = 0; halni len = 0;
OperandsInfo(); OperandsInfo();
OperandsInfo(tp::InitialierList<Operand> list); OperandsInfo(InitialierList<Operand> list);
}; };
struct ParamsInfo { struct ParamsInfo {
struct Param { struct Param {
const char* desc = nullptr; const char* desc = nullptr;
tp::halni bytes = 1; halni bytes = 1;
}; };
enum { MAX_PARAMS = 5 }; enum { MAX_PARAMS = 5 };
Param buff[MAX_PARAMS]; Param buff[MAX_PARAMS];
tp::halni len = 0; halni len = 0;
ParamsInfo(); ParamsInfo();
ParamsInfo(tp::InitialierList<Param> list); ParamsInfo(InitialierList<Param> list);
}; };
struct OpInfo { struct OpInfo {
@ -144,14 +144,14 @@ namespace obj {
OperandsInfo operands; OperandsInfo operands;
ParamsInfo params; ParamsInfo params;
tp::uint1 opsize(); uint1 opsize();
}; };
OpcodeInfos(); OpcodeInfos();
OpInfo fetch(OpCode code); OpInfo fetch(OpCode code);
private: private:
OpInfo buff[(tp::alni)OpCode::END_OPCODES]; OpInfo buff[(alni)OpCode::END_OPCODES];
void add(OpCode code, const OpInfo& info); void add(OpCode code, const OpInfo& info);
}; };

View file

@ -3,11 +3,11 @@
#include "core/Object.hpp" #include "core/Object.hpp"
#include "Map.hpp" #include "Map.hpp"
namespace obj { namespace tp::obj {
struct Scope { struct Scope {
typedef tp::Map<std::string, obj::Object*> ObjDict; typedef Map<std::string, Object*> ObjDict;
typedef tp::List<obj::Object*> ObjList; typedef List<Object*> ObjList;
ObjDict mLocals; ObjDict mLocals;
ObjList mTemps; ObjList mTemps;
@ -18,29 +18,28 @@ namespace obj {
class ScopeStack { class ScopeStack {
enum : tp::alni { enum : alni {
MAX_STACK_SIZE = 1024 * 4 MAX_STACK_SIZE = 1024 * 4
}; };
public: public:
Scope* mBuff; Scope* mBuff;
tp::ualni mIdx; ualni mIdx;
tp::ualni mIterIdx; ualni mIterIdx;
ScopeStack(); ScopeStack();
void enterScope(bool aChildReachable); void enterScope(bool aChildReachable);
void leaveScope(); void leaveScope();
void addLocal(obj::Object* local, const std::string& id); void addLocal(Object* local, const std::string& id);
void addTemp(obj::Object* tmp); void addTemp(Object* tmp);
void popTemp(); void popTemp();
void addTempReturn(obj::Object* ret); void addTempReturn(Object* ret);
obj::Object* getLocal(const std::string& str); Object* getLocal(const std::string& str);
Scope* getCurrentScope(); Scope* getCurrentScope();
~ScopeStack(); ~ScopeStack();
private: private:
obj::Object* getLocalUtil(Scope& scope, const std::string& id); Object* getLocalUtil(Scope& scope, const std::string& id);
}; };
}
};

View file

@ -2,11 +2,11 @@
#include "interpreter/ByteCode.hpp" #include "interpreter/ByteCode.hpp"
namespace obj { namespace tp::obj {
struct Script { struct Script {
obj::StringObject* mReadable; StringObject* mReadable;
ByteCode mBytecode; ByteCode mBytecode;
void compile(); void compile();
}; };
}; }

View file

@ -3,20 +3,20 @@
#include "compiler/Statements.hpp" #include "compiler/Statements.hpp"
#include "compiler/Expressions.hpp" #include "compiler/Expressions.hpp"
namespace obj { namespace tp::obj {
class Parser { class Parser {
public: public:
Parser() = default; Parser() = default;
struct Result { struct Result {
obj::BCgen::StatementScope* scope = nullptr; StatementScope* scope = nullptr;
bool isError = false; bool isError = false;
std::string description; std::string description;
tp::halni line = 0; halni line = 0;
tp::halni column = 0; halni column = 0;
}; };
Result parse(const std::string& stream); Result parse(const std::string& stream);
}; };
}; }

View file

@ -3,10 +3,10 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct BoolObject : Object { struct BoolObject : Object {
tp::alni val; alni val;
static ObjectType TypeData; static ObjectType TypeData;
static void constructor(BoolObject* self); static void constructor(BoolObject* self);
@ -14,11 +14,11 @@ namespace obj {
static BoolObject* create(bool in); static BoolObject* create(bool in);
static void from_int(BoolObject* self, tp::alni in); static void from_int(BoolObject* self, alni in);
static void from_float(BoolObject* self, tp::alnf in); static void from_float(BoolObject* self, alnf in);
static void from_string(BoolObject* self, const std::string& in); static void from_string(BoolObject* self, const std::string& in);
static std::string to_string(BoolObject* self); static std::string to_string(BoolObject* self);
static tp::alni to_int(BoolObject* self); static alni to_int(BoolObject* self);
static tp::alnf to_float(BoolObject* self); static alnf to_float(BoolObject* self);
}; };
}; }

View file

@ -2,7 +2,7 @@
#include "primitives/DictObject.hpp" #include "primitives/DictObject.hpp"
namespace obj { namespace tp::obj {
struct ClassObject : Object { struct ClassObject : Object {
@ -10,7 +10,7 @@ namespace obj {
static void copy(ClassObject* self, const ClassObject* in); static void copy(ClassObject* self, const ClassObject* in);
static void destructor(ClassObject* self); static void destructor(ClassObject* self);
static void constructor(ClassObject* self); static void constructor(ClassObject* self);
static tp::alni save_size(ClassObject* self); static alni save_size(ClassObject* self);
static void save(ClassObject* self, ArchiverOut& file_self); static void save(ClassObject* self, ArchiverOut& file_self);
static void load(ArchiverIn& file_self, ClassObject* self); static void load(ArchiverIn& file_self, ClassObject* self);
@ -31,7 +31,7 @@ namespace obj {
Type* getMember(const std::string& id) { Type* getMember(const std::string& id) {
auto idx = members->presents(id); auto idx = members->presents(id);
if (bool(idx)) { if (bool(idx)) {
return ((Type*)obj::ndo_cast(members->getSlotVal(idx), &Type::TypeData)); return ((Type*)ndo_cast(members->getSlotVal(idx), &Type::TypeData));
} }
return NULL; return NULL;
} }
@ -43,4 +43,4 @@ namespace obj {
return out; return out;
} }
}; };
}; }

View file

@ -4,10 +4,10 @@
#include "core/Object.hpp" #include "core/Object.hpp"
#include "Color.hpp" #include "Color.hpp"
namespace obj { namespace tp::obj {
struct ColorObject : Object { struct ColorObject : Object {
tp::RGBA mCol; RGBA mCol;
static ObjectType TypeData; static ObjectType TypeData;
static ObjectTypeAriphmetics TypeAriphm; static ObjectTypeAriphmetics TypeAriphm;
@ -15,10 +15,10 @@ namespace obj {
static void constructor(Object* self); static void constructor(Object* self);
static void copy(ColorObject* self, const ColorObject* in); static void copy(ColorObject* self, const ColorObject* in);
static void from_int(ColorObject* self, tp::alni in); static void from_int(ColorObject* self, alni in);
static void from_float(ColorObject* self, tp::alnf in); static void from_float(ColorObject* self, alnf in);
static std::string to_string(ColorObject* self); static std::string to_string(ColorObject* self);
static ColorObject* create(tp::RGBA in); static ColorObject* create(RGBA in);
}; };
}; }

View file

@ -2,7 +2,7 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct DictObject : Object { struct DictObject : Object {
static ObjectType TypeData; static ObjectType TypeData;
@ -10,22 +10,22 @@ namespace obj {
static void destructor(Object* self); static void destructor(Object* self);
static void constructor(Object* self); static void constructor(Object* self);
static tp::alni save_size(DictObject* self); static alni save_size(DictObject* self);
static void save(DictObject* self, ArchiverOut& file_self); static void save(DictObject* self, ArchiverOut& file_self);
static void load(ArchiverIn& file_self, DictObject* self); static void load(ArchiverIn& file_self, DictObject* self);
static tp::Buffer<Object*> childs_retrival(DictObject* self); static Buffer<Object*> childs_retrival(DictObject* self);
static tp::alni allocated_size(DictObject* self); static alni allocated_size(DictObject* self);
static tp::alni allocated_size_recursive(DictObject* self); static alni allocated_size_recursive(DictObject* self);
void put(const std::string&, Object*); void put(const std::string&, Object*);
void remove(const std::string&); void remove(const std::string&);
Object* get(const std::string&); Object* get(const std::string&);
tp::Map<std::string, Object*>::Idx presents(const std::string&); Map<std::string, Object*>::Idx presents(const std::string&);
Object* getSlotVal(tp::Map<std::string, Object*>::Idx); Object* getSlotVal(Map<std::string, Object*>::Idx);
[[nodiscard]] const tp::Map<std::string, Object*>& getItems() const; [[nodiscard]] const Map<std::string, Object*>& getItems() const;
private: private:
tp::Map<std::string, Object*> items; Map<std::string, Object*> items;
}; };
}; }

View file

@ -3,32 +3,32 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct EnumObject : Object { struct EnumObject : Object {
// one entry is 2 * sizeof(alni) in size // one entry is 2 * sizeof(alni) in size
tp::uhalni active; uhalni active;
tp::uhalni nentries; uhalni nentries;
tp::alni* entries; alni* entries;
static ObjectType TypeData; static ObjectType TypeData;
static void constructor(EnumObject* self); static void constructor(EnumObject* self);
static void destructor(EnumObject* self); static void destructor(EnumObject* self);
static void copy(EnumObject* self, const EnumObject* in); static void copy(EnumObject* self, const EnumObject* in);
void init(tp::InitialierList<const char*> list); void init(InitialierList<const char*> list);
const char* getActiveName(); const char* getActiveName();
const char* getItemName(tp::uhalni idx); const char* getItemName(uhalni idx);
static void from_int(EnumObject* self, tp::alni in); static void from_int(EnumObject* self, alni in);
static void from_float(EnumObject* self, tp::alnf in); static void from_float(EnumObject* self, alnf in);
static void from_string(EnumObject* self, const std::string& in); static void from_string(EnumObject* self, const std::string& in);
static std::string to_string(EnumObject* self); static std::string to_string(EnumObject* self);
static tp::alni to_int(EnumObject* self); static alni to_int(EnumObject* self);
static tp::alnf to_float(EnumObject* self); static alnf to_float(EnumObject* self);
static bool compare(EnumObject* first, EnumObject* second); static bool compare(EnumObject* first, EnumObject* second);
static EnumObject* create(tp::InitialierList<const char*> list); static EnumObject* create(InitialierList<const char*> list);
}; };
}; }

View file

@ -3,10 +3,10 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct FloatObject : Object { struct FloatObject : Object {
tp::alnf val; alnf val;
static ObjectType TypeData; static ObjectType TypeData;
static ObjectTypeAriphmetics TypeAriphm; static ObjectTypeAriphmetics TypeAriphm;
@ -14,13 +14,13 @@ namespace obj {
static void constructor(FloatObject* self); static void constructor(FloatObject* self);
static void copy(FloatObject* self, const FloatObject* in); static void copy(FloatObject* self, const FloatObject* in);
static FloatObject* create(tp::alnf in); static FloatObject* create(alnf in);
static void from_int(FloatObject* self, tp::alni in); static void from_int(FloatObject* self, alni in);
static void from_float(FloatObject* self, tp::alnf in); static void from_float(FloatObject* self, alnf in);
static void from_string(FloatObject* self, const std::string& in); static void from_string(FloatObject* self, const std::string& in);
static std::string to_string(FloatObject* self); static std::string to_string(FloatObject* self);
static tp::alni to_int(FloatObject* self); static alni to_int(FloatObject* self);
static tp::alnf to_float(FloatObject* self); static alnf to_float(FloatObject* self);
}; };
}; }

View file

@ -3,23 +3,23 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct IntObject : Object { struct IntObject : Object {
tp::alni val; alni val;
static ObjectType TypeData; static ObjectType TypeData;
static ObjectTypeAriphmetics TypeAriphm; static ObjectTypeAriphmetics TypeAriphm;
static void constructor(Object* self); static void constructor(Object* self);
static void copy(IntObject* self, const IntObject* in); static void copy(IntObject* self, const IntObject* in);
static IntObject* create(tp::alni in); static IntObject* create(alni in);
static void from_int(Object* self, tp::alni in); static void from_int(Object* self, alni in);
static void from_float(Object* self, tp::alnf in); static void from_float(Object* self, alnf in);
static void from_string(Object* self, const std::string& in); static void from_string(Object* self, const std::string& in);
static std::string to_string(Object* self); static std::string to_string(Object* self);
static tp::alni to_int(Object* self); static alni to_int(Object* self);
static tp::alnf to_float(Object* self); static alnf to_float(Object* self);
}; };
}; }

View file

@ -3,7 +3,7 @@
#include "interpreter/Interpreter.hpp" #include "interpreter/Interpreter.hpp"
#include "primitives/ClassObject.hpp" #include "primitives/ClassObject.hpp"
namespace obj { namespace tp::obj {
struct InterpreterObject : ClassObject { struct InterpreterObject : ClassObject {
static ObjectType TypeData; static ObjectType TypeData;
Interpreter mInterpreter; Interpreter mInterpreter;
@ -12,8 +12,8 @@ namespace obj {
static void constructor(InterpreterObject* self); static void constructor(InterpreterObject* self);
static void load(ArchiverIn& file_self, InterpreterObject* self); static void load(ArchiverIn& file_self, InterpreterObject* self);
void exec(obj::ClassObject* self = nullptr, tp::InitialierList<Interpreter::GlobalDef> globals = {}); void exec(obj::ClassObject* self = nullptr, InitialierList<Interpreter::GlobalDef> globals = {});
void debug(); void debug();
bool running(); bool running();
}; };
}; }

View file

@ -3,7 +3,7 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct LinkObject : Object { struct LinkObject : Object {
static ObjectType TypeData; static ObjectType TypeData;
@ -12,12 +12,12 @@ namespace obj {
static void copy(Object* self, const Object* in); static void copy(Object* self, const Object* in);
static LinkObject* create(Object* in); static LinkObject* create(Object* in);
static tp::alni save_size(LinkObject* self); static alni save_size(LinkObject* self);
static void save(LinkObject* self, ArchiverOut& file_self); static void save(LinkObject* self, ArchiverOut& file_self);
static void load(ArchiverIn& file_self, LinkObject* self); static void load(ArchiverIn& file_self, LinkObject* self);
static tp::alni allocated_size(LinkObject* self); static alni allocated_size(LinkObject* self);
static tp::alni allocated_size_recursive(LinkObject* self); static alni allocated_size_recursive(LinkObject* self);
static tp::Buffer<Object*> childs_retrival(LinkObject* self); static Buffer<Object*> childs_retrival(LinkObject* self);
Object* getLink(); Object* getLink();
void setLink(Object* obj); void setLink(Object* obj);
@ -25,5 +25,4 @@ namespace obj {
private: private:
Object* link; Object* link;
}; };
}
};

View file

@ -3,7 +3,7 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
enum ListMethods { enum ListMethods {
LISTOBJECT_PUSH_BACK, LISTOBJECT_PUSH_BACK,
@ -16,21 +16,21 @@ namespace obj {
static void copy(Object* self, const Object* in); static void copy(Object* self, const Object* in);
static void destructor(Object* self); static void destructor(Object* self);
static tp::alni allocated_size_recursive(ListObject* self); static alni allocated_size_recursive(ListObject* self);
static tp::alni allocated_size(ListObject* self); static alni allocated_size(ListObject* self);
static tp::Buffer<Object*> childs_retrival(ListObject* self); static Buffer<Object*> childs_retrival(ListObject* self);
static void load(ArchiverIn& file_self, ListObject* self); static void load(ArchiverIn& file_self, ListObject* self);
static void save(ListObject* self, ArchiverOut& file_self); static void save(ListObject* self, ArchiverOut& file_self);
static tp::alni save_size(ListObject* self); static alni save_size(ListObject* self);
[[nodiscard]] const tp::List<Object*>& getItems() const; [[nodiscard]] const List<Object*>& getItems() const;
void pushBack(Object* obj); void pushBack(Object* obj);
void pushFront(Object* obj); void pushFront(Object* obj);
void popBack(); void popBack();
void delNode(tp::List<Object*>::Node*); void delNode(List<Object*>::Node*);
private: private:
tp::List<Object*> items; List<Object*> items;
}; };
}; }

View file

@ -3,7 +3,7 @@
#include "primitives/StringObject.hpp" #include "primitives/StringObject.hpp"
#include "interpreter/Script.hpp" #include "interpreter/Script.hpp"
namespace obj { namespace tp::obj {
struct MethodObject : Object { struct MethodObject : Object {
@ -14,7 +14,7 @@ namespace obj {
static void constructor(MethodObject* self); static void constructor(MethodObject* self);
static void copy(MethodObject* self, MethodObject* in); static void copy(MethodObject* self, MethodObject* in);
static void destructor(MethodObject* self); static void destructor(MethodObject* self);
static tp::alni save_size(MethodObject* self); static alni save_size(MethodObject* self);
static void save(MethodObject* self, ArchiverOut& file_self); static void save(MethodObject* self, ArchiverOut& file_self);
static void load(ArchiverIn& file_self, obj::MethodObject* self); static void load(ArchiverIn& file_self, obj::MethodObject* self);
@ -25,4 +25,4 @@ namespace obj {
static MethodObject* create(const std::string& script); static MethodObject* create(const std::string& script);
}; };
}; }

View file

@ -3,7 +3,7 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
extern struct NullObject* NdoNull_globalInstance; extern struct NullObject* NdoNull_globalInstance;
@ -15,18 +15,18 @@ namespace obj {
static Object* null_return() { static Object* null_return() {
if (!NdoNull_globalInstance) { if (!NdoNull_globalInstance) {
NdoNull_globalInstance = (NullObject*) NDO->create("null"); NdoNull_globalInstance = (NullObject*) NDO->create("null");
obj::NDO->refinc(NdoNull_globalInstance); NDO->increaseReferenceCount(NdoNull_globalInstance);
} }
return (Object*) NdoNull_globalInstance; return (Object*) NdoNull_globalInstance;
} }
static Object* null_return_ref() { static Object* null_return_ref() {
obj::NDO->refinc(null_return()); NDO->increaseReferenceCount(null_return());
return NdoNull_globalInstance; return NdoNull_globalInstance;
} }
}; };
#define NDO_NULL obj::NullObject::null_return() #define NDO_NULL NullObject::null_return()
#define NDO_NULL_REF obj::NullObject::null_return_ref() #define NDO_NULL_REF NullObject::null_return_ref()
}; }

View file

@ -3,7 +3,7 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct StringObject : Object { struct StringObject : Object {
std::string val; std::string val;
@ -14,11 +14,11 @@ namespace obj {
static void copy(Object* self, const Object* in); static void copy(Object* self, const Object* in);
static StringObject* create(const std::string& in); static StringObject* create(const std::string& in);
static void from_int(StringObject* self, tp::alni in); static void from_int(StringObject* self, alni in);
static void from_float(StringObject* self, tp::alnf in); static void from_float(StringObject* self, alnf in);
static void from_string(StringObject* self, const std::string& in); static void from_string(StringObject* self, const std::string& in);
static std::string to_string(StringObject* self); static std::string to_string(StringObject* self);
static tp::alni to_int(StringObject* self); static alni to_int(StringObject* self);
static tp::alnf to_float(StringObject* self); static alnf to_float(StringObject* self);
}; };
}; }

View file

@ -3,11 +3,10 @@
#include "core/Object.hpp" #include "core/Object.hpp"
namespace obj { namespace tp::obj {
struct TypeObject : Object { struct TypeObject : Object {
static ObjectType TypeData; static ObjectType TypeData;
const ObjectType* mTypeRef; const ObjectType* mTypeRef;
static TypeObject* create(const ObjectType* type); static TypeObject* create(const ObjectType* type);
}; };
}; }

View file

@ -3,7 +3,7 @@
#include "primitives/PrimitiveObjects.hpp" #include "primitives/PrimitiveObjects.hpp"
tp::ModuleManifest* objDeps[] = { &obj::gModuleObjects, nullptr }; tp::ModuleManifest* objDeps[] = { &tp::obj::gModuleObjects, nullptr };
tp::ModuleManifest objTestModule("ObjectsTests", nullptr, nullptr, objDeps); tp::ModuleManifest objTestModule("ObjectsTests", nullptr, nullptr, objDeps);
#include <UnitTest++/UnitTest++.h> #include <UnitTest++/UnitTest++.h>