Objects Initial

This commit is contained in:
IlushaShurupov 2023-07-24 21:55:19 +03:00 committed by Ilya Shurupov
parent 0f639ba3b1
commit 94057d2a66
76 changed files with 7895 additions and 17 deletions

View file

@ -0,0 +1,42 @@
#pragma once
#include "interpreter/bytecode.h"
#include "core/object.h"
namespace obj {
namespace BCgen {
struct ConstObject {
obj::Object* mObj = NULL;
tp::alni mConstIdx = 0;
ConstObject();
ConstObject(obj::Object* mObj);
};
struct ConstObjectsPool {
tp::Map<tp::String, ConstObject*> mMethods;
tp::Map<tp::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(tp::String val);
ConstObject* get(tp::alnf val);
ConstObject* get(bool val);
ConstObject* addMethod(tp::String method_id, obj::Object* method);
ConstObject* registerObject(obj::Object* obj);
void save(tp::Buffer<ConstData>& out);
~ConstObjectsPool();
};
};
};

View file

@ -0,0 +1,126 @@
#pragma once
#include "Strings.hpp"
#include "Buffer.hpp"
#include "interpreter/opcodes.h"
namespace obj {
namespace BCgen {
struct ExpressionChild;
struct ExpressionCall;
struct Expression {
enum class Type {
NONE,
NEW,
LOCAL,
CONST,
CHILD,
CALL,
ARIPHM,
FUNC,
BOOLEAN,
SELF,
} mType = Type::NONE;
bool mValueUsed = false;
Expression();
Expression(Type type);
ExpressionChild* ExprChild(tp::String id);
ExpressionCall* ExprCall(tp::init_list<Expression*> args);
};
struct ExpressionNew : public Expression {
tp::String mNewType;
ExpressionNew(tp::String type);
};
struct ExpressionLocal : public Expression {
tp::String mLocalId;
ExpressionLocal(tp::String id);
};
struct ExpressionFunc : public Expression {
tp::String mFuncId;
ExpressionFunc(tp::String id);
};
struct ExpressionChild : public Expression {
Expression* mParent = NULL;
tp::String mLocalId;
bool mMethod = false;
ExpressionChild(Expression* mParent, tp::String id);
};
struct ExpressionCall : public Expression {
Expression* mParent = NULL;
tp::Buffer<Expression*> mArgs;
ExpressionCall(Expression* mParent, tp::init_list<Expression*> args);
};
struct ExpressionAriphm : public Expression {
Expression* mLeft = NULL;
Expression* mRight = NULL;
OpCode mOpType;
ExpressionAriphm(Expression* left, Expression* right, OpCode type);
};
struct ExpressionBoolean : public Expression {
Expression* mLeft = NULL;
Expression* mRight = NULL;
enum class BoolType : tp::uint1 {
AND = 24U,
OR,
EQUAL,
NOT_EQUAL,
MORE,
LESS,
EQUAL_OR_MORE,
EQUAL_OR_LESS,
NOT,
} mBoolType;
ExpressionBoolean(Expression* left, Expression* right, BoolType type);
ExpressionBoolean(Expression* invert);
};
struct ExpressionConst : public Expression {
enum ConstType { STR, INT, BOOL, FLT } mConstType;
tp::String str;
tp::alni integer = 0;
tp::alnf floating = 0;
bool boolean = 0;
ExpressionConst(tp::String val);
ExpressionConst(const char* val);
ExpressionConst(tp::alni val);
ExpressionConst(tp::int4 val);
ExpressionConst(tp::flt4 val);
ExpressionConst(tp::alnf val);
ExpressionConst(bool val);
};
struct ExpressionSelf : public Expression {
ExpressionSelf();
};
ExpressionLocal* ExprLocal(tp::String id);
ExpressionSelf* ExprSelf();
ExpressionFunc* ExprFunc(tp::String id);
ExpressionNew* ExprNew(tp::String id);
ExpressionAriphm* ExprAriphm(Expression* left, Expression* right, OpCode type);
ExpressionBoolean* ExprBool(Expression* left, Expression* right, OpCode type);
ExpressionBoolean* ExprBoolNot(Expression* invert);
template <typename ConstType>
ExpressionConst* ExprConst(ConstType val) {
return new ExpressionConst(val);
}
};
};

View file

@ -0,0 +1,47 @@
#pragma once
#include "Strings.hpp"
#include "List.hpp"
#include "Map.hpp"
#include "instruction.h"
#include "statement.h"
#include "constants.h"
namespace obj {
struct MethodObject;
namespace BCgen {
struct FunctionDefinition {
FunctionDefinition* mPrnt = NULL;
// signature
tp::String mFunctionId;
tp::List<ConstObject*> mArgsOrder;
ConstObjectsPool mConstants;
tp::Map<tp::String, ConstObject*> mLocals;
tp::List<Instruction> mInstructions;
FunctionDefinition(tp::String function_id, tp::Buffer<tp::String> args, FunctionDefinition* prnt);
FunctionDefinition() {}
void generateByteCode(ByteCode& out);
tp::List<Instruction>::Node* inst(Instruction inst);
void EvalExpr(Expression* expr);
void EvalStatement(Statement* expr);
ConstObject* defineLocal(tp::String id);
};
void init();
void deinit();
void Genereate(ByteCode& out, StatementScope* body);
bool Compile(MethodObject* obj);
};
};

View file

@ -0,0 +1,52 @@
#pragma once
#include "interpreter/opcodes.h"
#include "core/object.h"
#include "List.hpp"
namespace obj {
namespace BCgen {
struct ConstObject;
struct Instruction {
OpCode mOp = OpCode::NONE;
enum class ArgType {
NO_ARG,
PARAM,
CONST,
} mArgType = ArgType::NO_ARG;
enum class InstType {
NONE,
JUMP,
JUMP_IF,
JUMP_IF_NOT,
EXEC,
PURE_CONST,
} mInstType = InstType::NONE;
tp::alni mParam = 0;
tp::alni mParamBytes = 1;
ConstObject* mConstData = NULL;
ConstObject* mConstData2 = NULL;
tp::alni mInstIdx = 0;
tp::List<Instruction>::Node* mInstTarget = NULL;
Instruction();
Instruction(ConstObject* constData);
Instruction(OpCode op);
Instruction(OpCode op, ConstObject* constData);
Instruction(OpCode op, ConstObject* constData, ConstObject* constData2);
Instruction(OpCode op, tp::alni param, tp::alni nBytes);
Instruction(tp::List<Instruction>::Node* inst, InstType jump_type);
};
};
};

View file

@ -0,0 +1,116 @@
#pragma once
#include "expression.h"
namespace obj {
namespace BCgen {
struct Statement {
enum class Type {
NONE,
SCOPE,
DEF_FUNC,
DEF_LOCAL,
RET,
PRINT,
COPY,
IF,
WHILE,
IGNORE,
CALL,
CLASS_DEF,
} mType = Type::NONE;
bool mValueUsed = false;
Statement() {}
Statement(Type type);
};
struct StatementScope : public Statement {
tp::Buffer<Statement*> mStatements;
bool mPushToScopeStack = false;
StatementScope(tp::init_list<Statement*> statements, bool aPushToScopeStack);
};
struct StatementFuncDef : public Statement {
tp::Buffer<tp::String> mArgs;
tp::String mFunctionId;
tp::Buffer<Statement*> mStatements;
StatementFuncDef(tp::String function_id, tp::init_list<tp::String> args, tp::init_list<Statement*> statements);
};
struct StatementLocalDef : public Statement {
tp::String mLocalId;
Expression* mNewExpr = NULL;
ExpressionConst* mConstExpr = NULL;
bool mIsConstExpr = false;
StatementLocalDef(tp::String id, Expression* value);
StatementLocalDef(tp::String id, ExpressionConst* value);
};
struct StatementCopy : public Statement {
Expression* mLeft = NULL;
Expression* mRight = NULL;
StatementCopy(Expression* left, Expression* right);
};
struct StatementReturn : public Statement {
Expression* mRet = NULL;
StatementReturn(Expression* ret);
StatementReturn();
};
struct StatementPrint : public Statement {
Expression* mTarget = NULL;
StatementPrint(Expression* mTarget);
};
struct StatementIgnore : public Statement {
Expression* mExpr = NULL;
StatementIgnore(Expression* expr);
};
struct StatementIf : public Statement {
Expression* mCondition = NULL;
StatementScope* mOnTrue = NULL;
StatementScope* mOnFalse = NULL;
StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false);
};
struct StatementWhile : public Statement {
Expression* mCondition = NULL;
StatementScope* mScope = NULL;
StatementWhile(Expression* condition, StatementScope* scope);
};
struct StatementClassDef : public Statement {
tp::String mClassId;
StatementScope* mScope = NULL;
StatementClassDef(tp::String class_id, StatementScope* scope);
};
// Helpers
StatementFuncDef* StmDefFunc(tp::String id, tp::init_list<tp::String> args, tp::init_list<Statement*> stms);
StatementLocalDef* StmDefLocal(tp::String id, Expression* value);
StatementCopy* StmCopy(Expression* left, Expression* right);
StatementPrint* StmPrint(Expression* target);
StatementReturn* StmReturn(Expression* obj);
StatementReturn* StmReturn();
StatementIf* StmIf(Expression* condition, StatementScope* on_true, StatementScope* on_false);
StatementScope* StmScope(tp::init_list<Statement*> statements, bool aPushToScopeStack);
StatementWhile* StmWhile(Expression* condition, StatementScope* scope);
StatementIgnore* StmIgnore(Expression* expr);
StatementClassDef* StmClassDef(tp::String id, StatementScope* scope);
};
};

View file

@ -0,0 +1,226 @@
#pragma once
#include "Common.hpp"
#include "Map.hpp"
#include "List.hpp"
#include "Buffer.hpp"
#include "Strings.hpp"
#include "LocalConnection.hpp"
#include "core/typegroups.h"
#include "core/typemethods.h"
//#include "interpreter/interpreter.h"
/* Steps to create custom Object:
define name of object
define base type
define struct members
implement construct, destruct and copy methods */
#define OBJECT_REF_COUNT
#ifdef _DEBUG
#define NDO_CAST(cast_type, ptr) ((cast_type*)ndo_cast(ptr, &cast_type::TypeData))
#define NDO_CAST_ASSERT(cast_type, ptr) {assert(ndo_cast(ptr, &cast_type::TypeData))}
#else
#define NDO_CAST_ASSERT(cast_type, ptr) {assert(ndo_cast(ptr, &cast_type::TypeData))}
#define NDO_CAST(cast_type, ptr) ((cast_type*)ndo_cast(ptr, &cast_type::TypeData))
#endif
#define NDO_CASTV(cast_type, ptr, var_name) cast_type* var_name = NDO_CAST(cast_type, ptr); var_name
#define NDO_MEMH_FROM_NDO(ndo_ptr) (((ObjectMemHead*)ndo_ptr) - 1)
#define NDO_FROM_MEMH(ndo_ptr) ((Object*)(ndo_ptr + 1))
namespace obj {
class Archiver : public tp::LocalConnection {
public:
enum Type { SAVE, LOAD };
Archiver() = default;
Archiver(const char*, Type) {};
bool opened;
tp::ualni adress{};
// not yet writen address
// always offsets on write without specific address
tp::ualni avl_adress{};
void write_bytes(const tp::int1* in, tp::alni size, tp::alni adress = -1) {}
template <typename Type>
void write(Type* in, tp::alni adress = -1) {
write_bytes((tp::int1*) in, sizeof(Type), adress);
}
void read_bytes(tp::int1* in, tp::alni size, tp::alni adress = -1) {}
template <typename Type>
void read(Type* in, tp::alni adress = -1) {
read_bytes((tp::int1*) in, sizeof(Type), adress);
}
};
extern tp::ModuleManifest gModuleObjects;
extern struct objects_api* NDO;
struct ObjectMemHead {
ObjectMemHead* up;
ObjectMemHead* down;
tp::alni flags;
#ifdef OBJECT_REF_COUNT
tp::alni refc;
#endif
};
struct Object {
const struct ObjectType* type;
};
typedef void (*object_from_int)(Object* self, tp::alni in);
typedef void (*object_from_float)(Object* self, tp::alnf in);
typedef void (*object_from_string)(Object* self, tp::String in);
typedef tp::String(*object_to_string)(Object* self);
typedef tp::alni(*object_to_int)(Object* self);
typedef tp::alnf(*object_to_float)(Object* self);
struct ObjectTypeConversions {
object_from_int from_int;
object_from_float from_float;
object_from_string from_string;
object_to_string to_string;
object_to_int to_int;
object_to_float to_float;
};
typedef void (*object_add)(Object* self, Object* operand);
typedef void (*object_sub)(Object* self, Object* operand);
typedef void (*object_mul)(Object* self, Object* operand);
typedef void (*object_div)(Object* self, Object* operand);
struct ObjectTypeAriphmetics {
object_add add;
object_sub sub;
object_mul mul;
object_div div;
};
typedef void (*object_constructor)(Object* self);
typedef void (*object_destructor)(Object* self);
typedef void (*object_copy)(Object* self, const Object* target);
typedef tp::alni(*object_save_size)(Object* self);
typedef void (*object_save)(Object*, Archiver&);
typedef void (*object_load)(Archiver&, Object*);
typedef bool (*object_compare)(Object*, Object*);
typedef tp::Buffer<Object*> (*object_debug_all_childs_retrival)(Object*);
typedef tp::alni (*object_allocated_size)(Object*); // default value = type->size - sizeof(ObjectType*)
typedef tp::alni (*object_allocated_size_recursive)(Object*); // default value = object_allocated_size
struct ObjectType {
const ObjectType* base = NULL;
object_constructor constructor = NULL;
object_destructor destructor = NULL;
object_copy copy = NULL;
tp::alni size = NULL;
const char* name;
const ObjectTypeConversions* convesions = NULL;
const ObjectTypeAriphmetics* ariphmetics = NULL;
object_save_size save_size = NULL;
object_save save = NULL;
object_load load = NULL;
object_compare comparison = NULL;
void* vtable = NULL;
const char* description = NULL;
object_debug_all_childs_retrival childs_retrival = NULL;
object_allocated_size allocated_size = NULL;
object_allocated_size_recursive allocated_size_recursive = NULL;
TypeMethods type_methods;
};
#define SAVE_LOAD_MAX_CALLBACK_SLOTS 100
typedef void (pre_save_callback)(void* self, Archiver&);
typedef void (pre_load_callback)(void* self, Archiver&);
typedef void (post_save_callback)(void* self, Archiver&);
typedef void (post_load_callback)(void* self, Archiver&);
typedef tp::alni (slcb_size_callback)(void* self, Archiver&);
struct save_load_callbacks {
void* self;
pre_save_callback* pre_save;
slcb_size_callback* size;
pre_load_callback* pre_load;
post_save_callback* post_save;
post_load_callback* post_load;
};
struct objects_api {
tp::Map<tp::String, const ObjectType*> types;
obj::TypeGroups type_groups;
Interpreter* interp = NULL;
objects_api();
~objects_api();
void define(ObjectType* type);
Object* create(const tp::String& name);
Object* copy(Object* self, const Object* in);
bool compare(Object* first, Object* second);
Object* instatiate(Object*);
void set(Object* self, tp::alni val);
void set(Object* self, tp::alnf val);
void set(Object* self, tp::String val);
tp::alni toInt(Object* self);
tp::alnf toFloat(Object* self);
bool toBool(Object* self);
tp::String toString(Object* self);
void clear_object_flags();
void destroy(Object* in);
#ifdef OBJECT_REF_COUNT
void refinc(Object* in);
tp::halni getrefc(Object* in);
private:
void setrefc(Object* in, tp::halni refc);
public:
#endif
save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS];
tp::alni sl_callbacks_load_idx = 0;
void add_sl_callbacks(save_load_callbacks*);
tp::alni objsize_file(Object* self);
tp::alni objsize_file_recursive(Object* self);
tp::alni objsize_ram(Object* self);
tp::alni objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object = true);
tp::alni objsize_ram_recursive(Object* self);
bool save(Object*, tp::String path, bool compressed = true);
Object* load(tp::String path);
tp::alni save(Archiver&, Object*);
Object* load(Archiver&, tp::alni file_adress);
};
Object* ndo_cast(const Object* in, const ObjectType* to_type);
template <typename Type>
Type* create() {
return (Type*)obj::NDO->create(Type::TypeData.name);
}
};

View file

@ -0,0 +1,36 @@
#pragma once
#include "primitives/methodobject.h"
namespace obj {
// global singleton API for method object to manage the script
struct ScriptSection {
tp::List<Script*> mScripts;
Script* createScript();
void changeScript(Script** current_script, Script** new_script);
void abandonScript(Script* script);
tp::alni get_script_file_adress(Script* in);
Script* get_scritp_from_file_adress(tp::alni file_adress);
~ScriptSection();
static void initialize();
static void uninitialize();
static ScriptSection* globalHandle();
private:
static obj::save_load_callbacks slcb_script_section;
void delete_script(Script* script);
void reference_script(Script* script);
static void save_script_table_to_file(ScriptSection* self, Archiver& file);
static void load_script_table_from_file(ScriptSection* self, Archiver& file);
static tp::alni save_script_table_to_file_size(ScriptSection* self, Archiver& file);
};
};

View file

@ -0,0 +1,40 @@
#pragma once
#include "Map.hpp"
#include "Strings.hpp"
namespace obj {
struct ObjectType;
struct objects_api;
class TypeGroups {
public:
typedef tp::Map<tp::String, obj::TypeGroups*> Dict;
TypeGroups();
friend struct obj::objects_api;
TypeGroups(bool is_group);
void addType(ObjectType* type, tp::init_list<const char*> path, tp::alni cur_arg = 0);
void setType(ObjectType* type);
bool isGroup();
Dict* getChilds();
const obj::ObjectType* getType();
~TypeGroups();
private:
bool is_group;
union {
Dict childs;
const obj::ObjectType* type;
};
};
};

View file

@ -0,0 +1,63 @@
#pragma once
#include "Buffer.hpp"
#include "Strings.hpp"
namespace obj {
struct Interpreter;
struct Object;
struct ObjectType;
struct TypeMethod {
enum { MAX_ARGS = 8 };
struct Arg {
const char* descr = NULL;
mutable Object* obj = NULL;
};
const char* nameid = NULL;
const char* descr = NULL;
mutable Object* self = NULL;
Arg args[MAX_ARGS];
tp::int1 mNargs = NULL;
void (*exec)(const TypeMethod* tm) = NULL;
mutable Arg ret;
void operator()(Interpreter* interp) const;
void init();
};
extern TypeMethod gDefaultTypeMethods[3];
struct TypeMethods {
enum : tp::int2 { MAX_TYPE_METHODS = 128 };
TypeMethod* methods[MAX_TYPE_METHODS];
tp::halni mNMethods = NULL;
struct LookupKey {
tp::int2 key = -1;
tp::int2 type_depth = -1;
operator bool() { return key != -1; }
};
static LookupKey presents(const ObjectType*, tp::String);
static const TypeMethod* getMethod(const ObjectType*, LookupKey);
void init();
tp::halni nMethods() const;
private:
tp::int2 presents(tp::String) const;
const TypeMethod* getMethod(tp::int2) const;
};
};

View file

@ -0,0 +1,28 @@
#pragma once
#include "opcodes.h"
#include "primitives/intobject.h"
#include "primitives/stringobject.h"
#include "Buffer.hpp"
#include "Strings.hpp"
namespace obj {
typedef Object* ConstData;
struct ByteCode {
tp::Buffer<ConstData> mConstants;
tp::Buffer<OpCode> mInstructions;
tp::ualni mInstructionIdx = 0;
tp::ualni mArgumentsLoaded = 0;
~ByteCode() {
for (auto const_obj : mConstants) {
NDO->destroy(const_obj.data());
}
}
};
};

View file

@ -0,0 +1,32 @@
#pragma once
#include "core/object.h"
#include "primitives/classobject.h"
#include "Map.hpp"
namespace obj {
struct MethodObject;
};
namespace obj {
struct ByteCode;
struct CallStack {
struct CallFrame {
enum { CALL_DEPTH = 1024 };
obj::Object* mSelf = NULL;
obj::MethodObject* mMethod = NULL;
tp::ualni mIp = 0;
};
void enter(const CallFrame& frame);
void leave();
ByteCode* getBytecode();
tp::halni len() const;
tp::ConstSizeBuffer<CallFrame, CallFrame::CALL_DEPTH> mStack;
};
};

View file

@ -0,0 +1,29 @@
#pragma once
#include "operandsstack.h"
#include "scopestack.h"
#include "callstack.h"
namespace obj {
struct Interpreter {
OperandStack mOperandsStack;
ScopeStack mScopeStack;
CallStack mCallStack;
obj::Object* mLastParent = NULL;
bool mIsTypeMethod = false;
const TypeMethod* mTypeMethod = NULL;
typedef struct { obj::Object* obj; tp::String id; } GlobalDef;
void exec(obj::MethodObject* method, obj::ClassObject* self = NULL, obj::DictObject* globals = NULL, tp::init_list<GlobalDef> globals2 = {});
void stepBytecode();
void stepBytecodeIn();
void stepBytecodeOut();
bool finished();
void execAll(obj::MethodObject* method, obj::ClassObject* self = NULL, obj::DictObject* globals = NULL, tp::init_list<GlobalDef> globals2 = {});
};
};

View file

@ -0,0 +1,158 @@
#pragma once
#include "Common.hpp"
// No Nested Scopes
// Opcodes:
// Opcode input can be:
// 0) No Input
// 1) operands from OperandsStack (Operand)
// 2) Index of bytecode->ConstData from bytecode->Instruction (ConstData)
// 3) Raw Bytes from bytecode->Instruction (Param)
namespace obj {
extern struct OpcodeInfos gOpcodeInfos;
enum class OpCode : tp::uint1 {
NONE = 0x00,
HALT,
TERMINATE,
DEF_LOCAL,
// Operand : String : local_id
// Operand : Any : object to be local
LOAD_CONST,
LOAD_LOCAL,
// ConstData : idx of const object
IGNORE,
SCOPE_IN,
SCOPE_OUT,
CALL,
RETURN_OBJ,
// Operand : Base : returned object
RETURN,
CHILD,
// Operand : String : child_id
// Operand : Base : parent
// Param : 1 bytes : is method?
PUSH_ARGS,
SAVE_ARGS,
// Param : nubber of args
OBJ_CREATE_LOCAL,
// Operand : String : type
// Operand : String : id
OBJ_CREATE,
// Operand : String : type
OBJ_COPY,
// Operand : Base: target
// Operand : Base: blueprint
OBJ_SAVE,
OBJ_LOAD,
// Operand : String : path
// Operand : Base : target
OBJ_ADD,
OBJ_SUB,
OBJ_MUL,
OBJ_DIV,
AND,
OR,
EQUAL,
NOT_EQUAL,
MORE,
LESS,
EQUAL_OR_MORE,
EQUAL_OR_LESS,
// Operand : Base: left
// Operand : Base: right
NOT,
// Operand : Base: inv
JUMP,
JUMP_R,
// Param : 2 bytes : offset
JUMP_IF,
JUMP_IF_R,
JUMP_IF_NOT,
JUMP_IF_NOT_R,
// Operand : Base: condition
// Param : 2 bytes : offset
PRINT,
// Operand : Base: target
CLASS_CONSTRUCT,
SELF,
// ...
END_OPCODES
};
struct OpcodeInfos {
struct OperandsInfo {
struct Operand {
const char* obj_type = nullptr;
const char* desc = nullptr;
};
enum { MAX_OPERANDS = 5 };
Operand buff[MAX_OPERANDS];
tp::halni len = 0;
OperandsInfo();
OperandsInfo(tp::init_list<Operand> list);
};
struct ParamsInfo {
struct Param {
const char* desc = nullptr;
tp::halni bytes = 1;
};
enum { MAX_PARAMS = 5 };
Param buff[MAX_PARAMS];
tp::halni len = 0;
ParamsInfo();
ParamsInfo(tp::init_list<Param> list);
};
struct OpInfo {
const char* name = nullptr;
const char* desc = nullptr;
OperandsInfo operands;
ParamsInfo params;
tp::uint1 opsize();
};
OpcodeInfos();
OpInfo fetch(OpCode code);
private:
OpInfo buff[(tp::alni)OpCode::END_OPCODES];
void add(OpCode code, const OpInfo& info);
};
}

View file

@ -0,0 +1,39 @@
#pragma once
#include "bytecode.h"
#include "core/object.h"
namespace obj {
// can be other aligned value as well
typedef obj::Object* Operand;
class OperandStack {
enum : tp::alni {
MAX_STACK_SIZE = 512
};
public:
Operand* mBuff;
tp::ualni mIdx;
OperandStack();
void push(Operand operand);
void pop();
Operand getOperand();
template <typename ObjectType>
ObjectType* getOperand() {
auto operand = getOperand();
auto ret = NDO_CAST(ObjectType, operand);
ASSERT(ret && "Operand Has Invalid Object Type");
return ret;
}
~OperandStack();
};
};

View file

@ -0,0 +1,46 @@
#pragma once
#include "core/object.h"
#include "Map.hpp"
namespace obj {
struct Scope {
typedef tp::Map<tp::String, obj::Object*> ObjDict;
typedef tp::List<obj::Object*> ObjList;
ObjDict mLocals;
ObjList mTemps;
bool mChildReachable = true;
~Scope();
};
class ScopeStack {
enum : tp::alni {
MAX_STACK_SIZE = 1024 * 4
};
public:
Scope* mBuff;
tp::ualni mIdx;
tp::ualni mIterIdx;
ScopeStack();
void enterScope(bool aChildReachable);
void leaveScope();
void addLocal(obj::Object* local, tp::String id);
void addTemp(obj::Object* tmp);
void popTemp();
void addTempReturn(obj::Object* ret);
obj::Object* getLocal(tp::String& str);
Scope* getCurrentScope();
~ScopeStack();
private:
obj::Object* getLocalUtil(Scope& scope, tp::String* id);
};
};

View file

@ -0,0 +1,12 @@
#pragma once
#include "interpreter/bytecode.h"
namespace obj {
struct Script {
obj::StringObject* mReadable;
ByteCode mBytecode;
void compile();
};
};

View file

@ -0,0 +1,195 @@
#pragma once
#include "NewPlacement.hpp"
#include "compiler/statement.h"
#include "compiler/expression.h"
#include "Tokenizer.hpp"
namespace obj {
class Parser {
struct Token {
tp::String token;
tp::alni integer;
tp::alnf floating;
bool boolean;
tp::String str;
enum TokType : tp::alni {
__START__ = 0,
ID,
CONST_INT,
CONST_FLOAT,
CONST_STRING,
SCOPE_IN,
SCOPE_OUT,
ASSIGN,
DEF_FUNC,
PRINT,
IF,
ELSE,
WHILE,
BRACKET_IN,
BRACKET_OUT,
COMMA,
NEW,
CHILD,
RETURN,
EQUAL,
NOT_EQUAL,
MORE,
LESS,
QE_OR_MORE,
QE_OR_LESS,
BOOL_NOT,
BOOL_AND,
BOOL_OR,
ADD,
MUL,
SUB,
DIV,
QUOTES,
CONST_TRUE,
CONST_FALSE,
STM_END,
SPACE,
VAR,
CLASS_DEF,
SELF,
COMMENT_BLOCK,
__END__,
NO_TOKEN,
FAILED,
SOURCE_END_TOKEN,
} type;
operator TokType() {
return type;
}
bool isAriphm();
bool isBoolean();
};
typedef tp::SimpleTokenizer<char, Token::TokType, Token::NO_TOKEN, Token::FAILED, Token::SOURCE_END_TOKEN> Tokenizer;
Tokenizer mTok;
enum class ExprType {
BOOLEAN_NOT = 0,
BOOLEAN,
Ariphm,
NEW,
LOCAL,
CONST,
Compound
};
enum class StmType {
DefFunc = 0,
DefLocal,
Return,
Print,
If,
While,
Copy,
Call,
CLASS_DEF
};
public:
Parser();
struct Error {
tp::String mDescr = "No Description";
tp::alni mAdvanecedIdx = NULL;
Error() {}
Error(tp::String descr, tp::alni idx) {
mDescr = descr;
mAdvanecedIdx = idx;
}
tp::Pair<tp::alni, tp::alni> get_err_location(const char* stream) {
tp::alni line = 1;
tp::alni colomn = 1;
//assert(mAdvanecedIdx);
for (auto i : tp::Range(mAdvanecedIdx)) {
if (stream[i] == '\n') {
colomn = 0;
line++;
}
colomn++;
}
return { line, colomn };
}
};
struct Resault {
Error* err = NULL;
obj::BCgen::StatementScope* scope = NULL;
} mRes;
Resault parse(const tp::String& stream);
private:
Token tokRead();
Token tokLookup();
void tokSkip();
bool tokInputLeft();
Tokenizer::Cursor tokGetCursor();
void tokSetCursor(Tokenizer::Cursor);
BCgen::Expression* parseExprCompound();
BCgen::Expression* parseExprNEW();
BCgen::Expression* parseExprLOCAL();
BCgen::Expression* parseExprCONST();
BCgen::Expression* parseExprCHILD(BCgen::Expression* prnt);
BCgen::Expression* parseExprCALL(BCgen::Expression* prnt);
BCgen::Expression* parseExprAriphm();
BCgen::Expression* parseExprBOOLEAN();
BCgen::Expression* parseExprBOOLEAN_NOT();
BCgen::Expression* parseExprFUNC();
BCgen::Expression* parseExprChain(BCgen::Expression* prnt);
BCgen::Expression* parseExpr(tp::init_list<ExprType> expressions = {
ExprType::BOOLEAN_NOT,
ExprType::BOOLEAN,
ExprType::Ariphm,
ExprType::NEW,
ExprType::LOCAL,
ExprType::CONST,
});
BCgen::Statement* parseStmCall();
BCgen::Statement* parseStmDefFunc();
BCgen::Statement* parseStmDefLocal();
BCgen::Statement* parseStmReturn();
BCgen::Statement* parseStmPrint();
BCgen::Statement* parseStmIf();
BCgen::Statement* parseStmWhile();
BCgen::Statement* parseStmCopy();
BCgen::Statement* parseStmClassDef();
BCgen::Statement* parseStm(tp::init_list<StmType> stm_types = {
StmType::DefFunc,
StmType::DefLocal,
StmType::Return,
StmType::Print,
StmType::If,
StmType::While,
StmType::Copy,
StmType::Call,
StmType::CLASS_DEF
});
BCgen::StatementScope* parseScope(bool aPushToScopeStack);
};
};

View file

@ -0,0 +1,24 @@
#pragma once
#include "core/object.h"
namespace obj {
struct BoolObject : Object {
tp::alni val;
static ObjectType TypeData;
static void constructor(BoolObject* self);
static void copy(BoolObject* self, const BoolObject* in);
static BoolObject* create(bool in);
static void from_int(BoolObject* self, tp::alni in);
static void from_float(BoolObject* self, tp::alnf in);
static void from_string(BoolObject* self, tp::String in);
static tp::String to_string(BoolObject* self);
static tp::alni to_int(BoolObject* self);
static tp::alnf to_float(BoolObject* self);
};
};

View file

@ -0,0 +1,49 @@
#pragma once
#include "primitives/dictobject.h"
namespace obj {
struct ClassObject : Object {
static ObjectType TypeData;
static void copy(ClassObject* self, const ClassObject* in);
static void destructor(ClassObject* self);
static void constructor(ClassObject* self);
static tp::alni save_size(ClassObject* self);
static void save(ClassObject* self, Archiver& file_self);
static void load(Archiver& file_self, ClassObject* self);
DictObject* members;
void addMember(Object* obj, tp::String id);
void createMember(tp::String type, tp::String id);
template<typename Type>
Type* createMember(tp::String id) {
auto out = NDO->create(Type::TypeData.name);
addMember(out, id);
#ifdef OBJECT_REF_COUNT
NDO->destroy(out);
#endif // OBJECT_REF_COUNT
return (Type*) out;
}
template<typename Type>
Type* getMember(const tp::String& id) {
auto idx = members->presents(id);
if (idx) {
return ((Type*)obj::ndo_cast(members->getSlotVal(idx), &Type::TypeData));
}
return NULL;
}
template<typename Type>
Type* getMemberAssert(const tp::String& id) {
auto out = getMember<Type>(id);
assert(out && "invalid member access");
return out;
}
};
};

View file

@ -0,0 +1,24 @@
#pragma once
#include "core/object.h"
#include "Color.hpp"
namespace obj {
struct ColorObject : Object {
tp::RGBA mCol;
static ObjectType TypeData;
static ObjectTypeAriphmetics TypeAriphm;
static void constructor(Object* self);
static void copy(ColorObject* self, const ColorObject* in);
static void from_int(ColorObject* self, tp::alni in);
static void from_float(ColorObject* self, tp::alnf in);
static tp::String to_string(ColorObject* self);
static ColorObject* create(tp::RGBA in);
};
};

View file

@ -0,0 +1,31 @@
#pragma once
#include "core/object.h"
namespace obj {
struct DictObject : Object {
static ObjectType TypeData;
static void copy(Object* self, const Object* in);
static void destructor(Object* self);
static void constructor(Object* self);
static tp::alni save_size(DictObject* self);
static void save(DictObject* self, Archiver& file_self);
static void load(Archiver& file_self, DictObject* self);
static tp::Buffer<Object*> childs_retrival(DictObject* self);
static tp::alni allocated_size(DictObject* self);
static tp::alni allocated_size_recursive(DictObject* self);
void put(tp::String, Object*);
void remove(tp::String);
Object* get(tp::String);
tp::Map<tp::String, Object*>::Idx presents(tp::String);
Object* getSlotVal(tp::alni);
const tp::Map<tp::String, Object*>& getItems() const;
private:
tp::Map<tp::String, Object*> items;
};
};

View file

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

View file

@ -0,0 +1,26 @@
#pragma once
#include "core/object.h"
namespace obj {
struct FloatObject : Object {
tp::alnf val;
static ObjectType TypeData;
static ObjectTypeAriphmetics TypeAriphm;
static void constructor(FloatObject* self);
static void copy(FloatObject* self, const FloatObject* in);
static FloatObject* create(tp::alnf in);
static void from_int(FloatObject* self, tp::alni in);
static void from_float(FloatObject* self, tp::alnf in);
static void from_string(FloatObject* self, tp::String in);
static tp::String to_string(FloatObject* self);
static tp::alni to_int(FloatObject* self);
static tp::alnf to_float(FloatObject* self);
};
};

View file

@ -0,0 +1,19 @@
#pragma once
#include "interpreter/interpreter.h"
#include "primitives/classobject.h"
namespace obj {
struct InterpreterObject : ClassObject {
static ObjectType TypeData;
Interpreter mInterpreter;
static void destructor(InterpreterObject* self);
static void constructor(InterpreterObject* self);
static void load(Archiver& file_self, InterpreterObject* self);
void exec(obj::ClassObject* self = NULL, tp::init_list<Interpreter::GlobalDef> globals = {});
void debug();
bool running();
};
};

View file

@ -0,0 +1,25 @@
#pragma once
#include "core/object.h"
namespace obj {
struct IntObject : Object {
tp::alni val;
static ObjectType TypeData;
static ObjectTypeAriphmetics TypeAriphm;
static void constructor(Object* self);
static void copy(IntObject* self, const IntObject* in);
static IntObject* create(tp::alni in);
static void from_int(Object* self, tp::alni in);
static void from_float(Object* self, tp::alnf in);
static void from_string(Object* self, tp::String in);
static tp::String to_string(Object* self);
static tp::alni to_int(Object* self);
static tp::alnf to_float(Object* self);
};
};

View file

@ -0,0 +1,29 @@
#pragma once
#include "core/object.h"
namespace obj {
struct LinkObject : Object {
static ObjectType TypeData;
static void constructor(Object* self);
static void destructor(LinkObject* self);
static void copy(Object* self, const Object* in);
static LinkObject* create(Object* in);
static tp::alni save_size(LinkObject* self);
static void save(LinkObject* self, Archiver& file_self);
static void load(Archiver& file_self, LinkObject* self);
static tp::alni allocated_size(LinkObject* self);
static tp::alni allocated_size_recursive(LinkObject* self);
static tp::Buffer<Object*> childs_retrival(LinkObject* self);
Object* getLink();
void setLink(Object* obj);
private:
Object* link;
};
};

View file

@ -0,0 +1,36 @@
#pragma once
#include "core/object.h"
namespace obj {
enum ListMethods {
LISTOBJECT_PUSH_BACK,
LISTOBJECT_GET_LENGTH,
};
struct ListObject : Object {
static ObjectType TypeData;
static void constructor(Object* self);
static void copy(Object* self, const Object* in);
static void destructor(Object* self);
static tp::alni allocated_size_recursive(ListObject* self);
static tp::alni allocated_size(ListObject* self);
static tp::Buffer<Object*> childs_retrival(ListObject* self);
static void load(Archiver& file_self, ListObject* self);
static void save(ListObject* self, Archiver& file_self);
static tp::alni save_size(ListObject* self);
const tp::List<Object*>& getItems() const;
void pushBack(Object* obj);
void pushFront(Object* obj);
void popBack();
void delNode(tp::List<Object*>::Node*);
private:
tp::List<Object*> items;
};
};

View file

@ -0,0 +1,28 @@
#pragma once
#include "primitives/stringobject.h"
#include "interpreter/script.h"
namespace obj {
struct MethodObject : Object {
static ObjectType TypeData;
Script* mScript;
static void constructor(MethodObject* self);
static void copy(MethodObject* self, MethodObject* in);
static void destructor(MethodObject* self);
static tp::alni save_size(MethodObject* self);
static void save(MethodObject* self,Archiver& file_self);
static void load(Archiver& file_self, obj::MethodObject* self);
static void Initialize();
static void UnInitialize();
void compile();
static MethodObject* create(tp::String script);
};
};

View file

@ -0,0 +1,32 @@
#pragma once
#include "core/object.h"
namespace obj {
extern struct NullObject* NdoNull_globalInstance;
struct NullObject : Object {
static void destructor(Object* self);
static ObjectType TypeData;
static void uninit();
static Object* null_return() {
if (!NdoNull_globalInstance) {
NdoNull_globalInstance = (NullObject*) NDO->create("null");
obj::NDO->refinc(NdoNull_globalInstance);
}
return (Object*) NdoNull_globalInstance;
}
static Object* null_return_ref() {
obj::NDO->refinc(null_return());
return NdoNull_globalInstance;
}
};
#define NDO_NULL obj::NullObject::null_return()
#define NDO_NULL_REF obj::NullObject::null_return_ref()
};

View file

@ -0,0 +1,22 @@
#pragma once
#include "primitives/dictobject.h"
#include "primitives/intobject.h"
#include "primitives/linkobject.h"
#include "primitives/listobject.h"
#include "primitives/nullobject.h"
#include "primitives/stringobject.h"
#include "primitives/boolobject.h"
#include "primitives/floatobject.h"
#include "primitives/enumobject.h"
#include "primitives/classobject.h"
#include "primitives/colorobject.h"
#include "primitives/methodobject.h"
#include "primitives/interpreterobject.h"
#include "primitives/typeobject.h"
namespace obj {
void primitives_define_types();
void primitives_uninitialize();
};

View file

@ -0,0 +1,24 @@
#pragma once
#include "core/object.h"
namespace obj {
struct StringObject : Object {
tp::String val;
static ObjectType TypeData;
static void constructor(Object* self);
static void destructor(StringObject* self);
static void copy(Object* self, const Object* in);
static StringObject* create(tp::String in);
static void from_int(StringObject* self, tp::alni in);
static void from_float(StringObject* self, tp::alnf in);
static void from_string(StringObject* self, tp::String in);
static tp::String to_string(StringObject* self);
static tp::alni to_int(StringObject* self);
static tp::alnf to_float(StringObject* self);
};
};

View file

@ -0,0 +1,13 @@
#pragma once
#include "core/object.h"
namespace obj {
struct TypeObject : Object {
static ObjectType TypeData;
const ObjectType* mTypeRef;
static TypeObject* create(const ObjectType* type);
};
};