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);
};
};