clean up object module

This commit is contained in:
IlyaShurupov 2024-03-23 12:32:57 +03:00
parent 4c66704830
commit 5d9e270aa9
68 changed files with 1223 additions and 1229 deletions

View file

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

View file

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

View file

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

View file

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

View file

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