This commit is contained in:
IlyaShurupov 2024-03-06 10:47:37 +03:00 committed by Ilya Shurupov
parent b647271081
commit 87fb15ed13
9 changed files with 351 additions and 368 deletions

View file

@ -6,126 +6,120 @@
#include "interpreter/opcodes.h"
namespace obj {
namespace BCgen {
namespace obj::BCgen {
struct ExpressionChild;
struct ExpressionCall;
struct Expression {
enum class Type {
NONE,
NEW,
LOCAL,
CONST_EXPR,
CHILD,
CALL,
ARITHMETICS,
FUNC,
BOOLEAN,
SELF,
LIST,
} mType = Type::NONE;
struct Expression {
enum class Type {
NONE,
NEW,
LOCAL,
CONST_EXPR,
CHILD,
CALL,
ARIPHM,
FUNC,
BOOLEAN,
SELF,
LIST,
} mType = Type::NONE;
bool mValueUsed = false;
bool mValueUsed = false;
explicit Expression(Type type);
virtual ~Expression() = default;
Expression();
Expression(Type type);
ExpressionChild* ExprChild(tp::String id);
ExpressionCall* ExprCall(class ExpressionList* args);
};
struct ExpressionList : public Expression {
tp::Buffer<Expression*> mItems;
ExpressionList();
};
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;
ExpressionList* mArgs;
ExpressionCall(Expression* mParent, ExpressionList* 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);
}
struct ExpressionChild* ExprChild(const tp::String& id);
struct ExpressionCall* ExprCall(class ExpressionList* args);
};
};
struct ExpressionList : public Expression {
tp::Buffer<Expression*> mItems;
ExpressionList();
~ExpressionList() override;
};
struct ExpressionNew : public Expression {
tp::String mNewType;
explicit ExpressionNew(const tp::String& type);
~ExpressionNew() override;
};
struct ExpressionLocal : public Expression {
tp::String mLocalId;
explicit ExpressionLocal(const tp::String& id);
~ExpressionLocal() override;
};
struct ExpressionFunc : public Expression {
tp::String mFuncId;
explicit ExpressionFunc(const tp::String& id);
~ExpressionFunc() override;
};
struct ExpressionChild : public Expression {
Expression* mParent = nullptr;
tp::String mLocalId;
bool mMethod = false;
ExpressionChild(Expression* mParent, const tp::String& id);
~ExpressionChild() override;
};
struct ExpressionCall : public Expression {
Expression* mParent = nullptr;
ExpressionList* mArgs = nullptr;
ExpressionCall(Expression* mParent, ExpressionList* args);
~ExpressionCall() override;
};
struct ExpressionArithmetics : public Expression {
Expression* mLeft = nullptr;
Expression* mRight = nullptr;
OpCode mOpType = OpCode::NONE;
ExpressionArithmetics(Expression* left, Expression* right, OpCode type);
~ExpressionArithmetics() override;
};
struct ExpressionBoolean : public Expression {
Expression* mLeft = nullptr;
Expression* mRight = nullptr;
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);
explicit ExpressionBoolean(Expression* invert);
~ExpressionBoolean() override;
};
struct ExpressionConst : public Expression {
enum ConstType { STR, INT, BOOL, FLT } mConstType;
tp::String str;
tp::alni integer = 0;
tp::alnf floating = 0;
bool boolean = false;
explicit ExpressionConst(const tp::String& val);
explicit ExpressionConst(const char* val);
explicit ExpressionConst(tp::alni val);
explicit ExpressionConst(tp::int4 val);
explicit ExpressionConst(tp::flt4 val);
explicit ExpressionConst(tp::alnf val);
explicit ExpressionConst(bool val);
~ExpressionConst() override;
};
struct ExpressionSelf : public Expression {
ExpressionSelf();
~ExpressionSelf() override = default;
};
}

View file

@ -3,115 +3,109 @@
#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;
namespace obj::BCgen {
struct Statement {
bool mValueUsed = false;
enum class Type {
NONE,
SCOPE,
DEF_FUNC,
DEF_LOCAL,
RET,
PRINT,
COPY,
IF,
WHILE,
IGNORE,
CALL,
CLASS_DEF,
} mType = Type::NONE;
Statement() {}
Statement(Type type);
};
bool mValueUsed = false;
struct StatementScope : public Statement {
tp::Buffer<Statement*> mStatements;
bool mPushToScopeStack = false;
StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack);
};
struct StatementFuncDef : public Statement {
tp::Buffer<tp::String> mArgs;
tp::String mFunctionId;
StatementScope* mStatements;
StatementFuncDef(tp::String function_id);
};
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::InitialierList<tp::String> args, tp::InitialierList<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::InitialierList<Statement*> statements, bool aPushToScopeStack);
StatementWhile* StmWhile(Expression* condition, StatementScope* scope);
StatementIgnore* StmIgnore(Expression* expr);
StatementClassDef* StmClassDef(tp::String id, StatementScope* scope);
explicit Statement(Type type);
virtual ~Statement() = default;
};
};
struct StatementScope : public Statement {
tp::Buffer<Statement*> mStatements;
bool mPushToScopeStack = false;
StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack);
~StatementScope() override;
};
struct StatementFuncDef : public Statement {
tp::Buffer<tp::String> mArgs;
tp::String mFunctionId;
StatementScope* mStatements = nullptr;
explicit StatementFuncDef(const tp::String& function_id);
~StatementFuncDef() override;
};
struct StatementLocalDef : public Statement {
tp::String mLocalId;
Expression* mNewExpr = nullptr;
ExpressionConst* mConstExpr = nullptr;
bool mIsConstExpr = false;
StatementLocalDef(const tp::String& id, Expression* value);
~StatementLocalDef() override;
};
struct StatementCopy : public Statement {
Expression* mLeft = nullptr;
Expression* mRight = nullptr;
StatementCopy(Expression* left, Expression* right);
~StatementCopy() override;
};
struct StatementReturn : public Statement {
Expression* mRet = nullptr;
explicit StatementReturn(Expression* ret);
StatementReturn();
~StatementReturn() override;
};
struct StatementPrint : public Statement {
Expression* mTarget = nullptr;
explicit StatementPrint(Expression* mTarget);
~StatementPrint() override;
};
struct StatementIgnore : public Statement {
Expression* mExpr = nullptr;
explicit StatementIgnore(Expression* expr);
~StatementIgnore() override;
};
struct StatementIf : public Statement {
Expression* mCondition = nullptr;
StatementScope* mOnTrue = nullptr;
StatementScope* mOnFalse = nullptr;
StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false);
~StatementIf() override;
};
struct StatementWhile : public Statement {
Expression* mCondition = nullptr;
StatementScope* mScope = nullptr;
StatementWhile(Expression* condition, StatementScope* scope);
~StatementWhile() override;
};
struct StatementClassDef : public Statement {
tp::String mClassId;
StatementScope* mScope = nullptr;
StatementClassDef(const tp::String& class_id, StatementScope* scope);
~StatementClassDef() override;
};
}