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

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

View file

@ -5,11 +5,8 @@
#include "Map.hpp"
namespace obj {
namespace tp::obj {
struct MethodObject;
};
namespace obj {
struct ByteCode;
@ -17,16 +14,16 @@ namespace obj {
struct CallFrame {
enum { CALL_DEPTH = 1024 };
obj::Object* mSelf = nullptr;
obj::MethodObject* mMethod = nullptr;
tp::ualni mIp = 0;
Object* mSelf = nullptr;
MethodObject* mMethod = nullptr;
ualni mIp = 0;
};
void enter(const CallFrame& frame);
void leave();
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 "CallStack.hpp"
namespace obj {
namespace tp::obj {
struct Interpreter {
OperandStack mOperandsStack;
ScopeStack mScopeStack;
CallStack mCallStack;
obj::Object* mLastParent = nullptr;
Object* mLastParent = nullptr;
bool mIsTypeMethod = false;
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 stepBytecodeIn();
@ -24,6 +24,6 @@ namespace obj {
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 "core/Object.hpp"
namespace obj {
namespace tp::obj {
// can be other aligned value as well
typedef obj::Object* Operand;
typedef Object* Operand;
class OperandStack {
enum : tp::alni {
enum : alni {
MAX_STACK_SIZE = 512
};
public:
Operand* mBuff;
tp::ualni mIdx;
ualni mIdx;
OperandStack();
void push(Operand operand);
@ -35,5 +35,4 @@ namespace obj {
~OperandStack();
};
};
}

View file

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

View file

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