This commit is contained in:
IlyaShurupov 2024-03-06 10:47:37 +03:00 committed by Ilusha
parent e628e7c1e2
commit 828096de17
9 changed files with 351 additions and 368 deletions

View file

@ -3,32 +3,13 @@
using namespace obj; using namespace obj;
using namespace BCgen; using namespace BCgen;
Expression::Expression() {}
Expression::Expression(Type type) : Expression::Expression(Type type) :
mType(type) {} mType(type) {}
ExpressionChild* Expression::ExprChild(tp::String id) { return new ExpressionChild(this, id); } ExpressionChild* Expression::ExprChild(const tp::String& id) { return new ExpressionChild(this, id); }
ExpressionCall* Expression::ExprCall(ExpressionList* args) { return new ExpressionCall(this, args); } ExpressionCall* Expression::ExprCall(ExpressionList* args) { return new ExpressionCall(this, args); }
ExpressionLocal* obj::BCgen::ExprLocal(tp::String id) { return new ExpressionLocal(id); }
ExpressionSelf* obj::BCgen::ExprSelf() { return new ExpressionSelf(); }
ExpressionNew* obj::BCgen::ExprNew(tp::String type) { return new ExpressionNew(type); }
ExpressionAriphm* obj::BCgen::ExprAriphm(Expression* left, Expression* right, OpCode type) {
return new ExpressionAriphm(left, right, type);
}
ExpressionFunc* obj::BCgen::ExprFunc(tp::String id) { return new ExpressionFunc(id); }
ExpressionBoolean* obj::BCgen::ExprBool(Expression* left, Expression* right, obj::OpCode type) {
return new ExpressionBoolean(left, right, ExpressionBoolean::BoolType(type));
}
ExpressionBoolean* obj::BCgen::ExprBoolNot(Expression* invert) { return new ExpressionBoolean(invert); }
ExpressionBoolean::ExpressionBoolean(Expression* left, Expression* right, BoolType type) : ExpressionBoolean::ExpressionBoolean(Expression* left, Expression* right, BoolType type) :
Expression(Type::BOOLEAN), Expression(Type::BOOLEAN),
mLeft(left), mLeft(left),
@ -40,60 +21,103 @@ ExpressionBoolean::ExpressionBoolean(Expression* invert) :
mLeft(invert), mLeft(invert),
mBoolType(BoolType::NOT) {} mBoolType(BoolType::NOT) {}
ExpressionBoolean::~ExpressionBoolean() {
delete mLeft;
delete mRight;
}
ExpressionCall::ExpressionCall(Expression* mParent, ExpressionList* args) : ExpressionCall::ExpressionCall(Expression* mParent, ExpressionList* args) :
Expression(Type::CALL), Expression(Type::CALL),
mParent(mParent) { mParent(mParent) {
mArgs = args; mArgs = args;
} }
ExpressionCall::~ExpressionCall() {
delete mParent;
delete mArgs;
}
ExpressionList::ExpressionList() : ExpressionList::ExpressionList() :
Expression(Type::LIST) {} Expression(Type::LIST) {}
ExpressionNew::ExpressionNew(tp::String type) : ExpressionList::~ExpressionList() {
for (auto item : mItems) {
delete item.data();
}
}
ExpressionNew::ExpressionNew(const tp::String& type) :
Expression(Type::NEW), Expression(Type::NEW),
mNewType(type) {} mNewType(type) {}
ExpressionLocal::ExpressionLocal(tp::String id) :
ExpressionNew::~ExpressionNew() = default;
ExpressionLocal::ExpressionLocal(const tp::String& id) :
Expression(Type::LOCAL), Expression(Type::LOCAL),
mLocalId(id) {} mLocalId(id) {}
ExpressionLocal::~ExpressionLocal() = default;
ExpressionSelf::ExpressionSelf() : ExpressionSelf::ExpressionSelf() :
Expression(Type::SELF) {} Expression(Type::SELF) {}
ExpressionChild::ExpressionChild(Expression* mParent, tp::String id) :
ExpressionChild::ExpressionChild(Expression* mParent, const tp::String& id) :
Expression(Type::CHILD), Expression(Type::CHILD),
mParent(mParent), mParent(mParent),
mLocalId(id) {} mLocalId(id) {}
ExpressionAriphm::ExpressionAriphm(Expression* left, Expression* right, OpCode type) :
Expression(Type::ARIPHM), ExpressionChild::~ExpressionChild() { delete mParent; }
ExpressionArithmetics::ExpressionArithmetics(Expression* left, Expression* right, OpCode type) :
Expression(Type::ARITHMETICS),
mLeft(left), mLeft(left),
mRight(right), mRight(right),
mOpType(type) {} mOpType(type) {}
ExpressionFunc::ExpressionFunc(tp::String id) :
ExpressionArithmetics::~ExpressionArithmetics() {
delete mLeft;
delete mRight;
}
ExpressionFunc::ExpressionFunc(const tp::String& id) :
Expression(Type::FUNC), Expression(Type::FUNC),
mFuncId(id) {} mFuncId(id) {}
ExpressionConst::ExpressionConst(tp::String val) :
ExpressionFunc::~ExpressionFunc() = default;
ExpressionConst::ExpressionConst(const tp::String& val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(STR), mConstType(STR),
str(val) {} str(val) {}
ExpressionConst::ExpressionConst(const char* val) : ExpressionConst::ExpressionConst(const char* val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(STR), mConstType(STR),
str(val) {} str(val) {}
ExpressionConst::ExpressionConst(tp::int4 val) : ExpressionConst::ExpressionConst(tp::int4 val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(INT), mConstType(INT),
integer(val) {} integer(val) {}
ExpressionConst::ExpressionConst(tp::flt4 val) : ExpressionConst::ExpressionConst(tp::flt4 val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(FLT), mConstType(FLT),
floating(val) {} floating(val) {}
ExpressionConst::ExpressionConst(tp::alni val) : ExpressionConst::ExpressionConst(tp::alni val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(INT), mConstType(INT),
integer(val) {} integer(val) {}
ExpressionConst::ExpressionConst(tp::alnf val) : ExpressionConst::ExpressionConst(tp::alnf val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(FLT), mConstType(FLT),
floating(val) {} floating(val) {}
ExpressionConst::ExpressionConst(bool val) : ExpressionConst::ExpressionConst(bool val) :
Expression(Type::CONST_EXPR), Expression(Type::CONST_EXPR),
mConstType(BOOL), mConstType(BOOL),
boolean(val) {} boolean(val) {}
ExpressionConst::~ExpressionConst() = default;

View file

@ -212,18 +212,17 @@ void FunctionDefinition::EvalStatement(Statement* stm) {
type = "str"; type = "str";
break; break;
} }
default:
{
ASSERT(0 && "Cantbe");
}
} }
auto new_expr = new ExpressionNew(type); auto new_expr = new ExpressionNew(type);
auto defLocalExpr = new StatementLocalDef(stm_local_def->mLocalId, new_expr);
EvalStatement(StmDefLocal(stm_local_def->mLocalId, new_expr)); EvalStatement(defLocalExpr);
EvalExpr(stm_local_def->mConstExpr); EvalExpr(stm_local_def->mConstExpr);
inst(Instruction(OpCode::LOAD_LOCAL, mConstants.get(stm_local_def->mLocalId))); inst(Instruction(OpCode::LOAD_LOCAL, mConstants.get(stm_local_def->mLocalId)));
inst(Instruction(OpCode::OBJ_COPY)); inst(Instruction(OpCode::OBJ_COPY));
delete defLocalExpr;
} }
break; break;
} }
@ -256,8 +255,6 @@ void FunctionDefinition::EvalStatement(Statement* stm) {
default: ASSERT(0) default: ASSERT(0)
} }
delete stm;
} }
void FunctionDefinition::EvalExpr(Expression* expr) { void FunctionDefinition::EvalExpr(Expression* expr) {
@ -283,9 +280,9 @@ void FunctionDefinition::EvalExpr(Expression* expr) {
inst(Instruction(OpCode::LOAD_LOCAL, mConstants.get(func->mFuncId))); inst(Instruction(OpCode::LOAD_LOCAL, mConstants.get(func->mFuncId)));
break; break;
} }
case Expression::Type::ARIPHM: case Expression::Type::ARITHMETICS:
{ {
auto ariphm = (ExpressionAriphm*) expr; auto ariphm = (ExpressionArithmetics*) expr;
EvalExpr(ariphm->mRight); EvalExpr(ariphm->mRight);
EvalExpr(ariphm->mLeft); EvalExpr(ariphm->mLeft);
inst(Instruction(ariphm->mOpType)); inst(Instruction(ariphm->mOpType));
@ -359,7 +356,6 @@ void FunctionDefinition::EvalExpr(Expression* expr) {
default: ASSERT(0) default: ASSERT(0)
} }
delete expr;
} }
tp::alni instSize(const Instruction& inst) { tp::alni instSize(const Instruction& inst) {
@ -553,32 +549,16 @@ tp::List<Instruction>::Node* FunctionDefinition::inst(Instruction inst) {
return mInstructions.last(); return mInstructions.last();
} }
static Parser* sParger = NULL; void obj::BCgen::init() {}
void obj::BCgen::init() { void obj::BCgen::deinit() {}
ASSERT(!sParger);
if (!sParger) {
sParger = new Parser();
}
}
void obj::BCgen::deinit() {
ASSERT(sParger);
if (sParger) {
delete sParger;
sParger = nullptr;
}
}
bool obj::BCgen::Compile(obj::MethodObject* method) { bool obj::BCgen::Compile(obj::MethodObject* method) {
ASSERT(method); Parser sParger;
ASSERT(sParger);
if (!sParger) return false;
auto script = method->mScript->mReadable->val; auto script = method->mScript->mReadable->val;
auto res = sParger->parse(script); auto res = sParger.parse(script);
if (res.err) { if (res.err) {
// TODO : print parse error // TODO : print parse error

View file

@ -3,112 +3,105 @@
using namespace obj; using namespace obj;
using namespace BCgen; using namespace BCgen;
StatementFuncDef::StatementFuncDef(tp::String function_id) { StatementFuncDef::StatementFuncDef(const tp::String& function_id) :
mType = Type::DEF_FUNC; Statement(Type::DEF_FUNC) {
mFunctionId = function_id; mFunctionId = function_id;
} }
StatementLocalDef::StatementLocalDef(tp::String id, Expression* value) : StatementFuncDef::~StatementFuncDef() { delete mStatements; }
StatementLocalDef::StatementLocalDef(const tp::String& id, Expression* value) :
mLocalId(id), mLocalId(id),
mNewExpr(value) { Statement(Type::DEF_LOCAL) {
mType = Type::DEF_LOCAL;
if (value->mType == Expression::Type::CONST_EXPR) {
mIsConstExpr = true;
mConstExpr = (ExpressionConst*) value;
} else {
mIsConstExpr = false;
mNewExpr = value;
}
} }
StatementLocalDef::StatementLocalDef(tp::String id, ExpressionConst* value) : StatementLocalDef::~StatementLocalDef() {
mLocalId(id), delete mNewExpr;
mConstExpr(value), delete mConstExpr;
mIsConstExpr(true) {
mType = Type::DEF_LOCAL;
} }
StatementCopy::StatementCopy(Expression* left, Expression* right) : StatementCopy::StatementCopy(Expression* left, Expression* right) :
mLeft(left), mLeft(left),
mRight(right) { mRight(right),
mType = Type::COPY; Statement(Type::COPY) {}
StatementCopy::~StatementCopy() {
delete mLeft;
delete mRight;
} }
StatementReturn::StatementReturn() { mType = Type::RET; } StatementReturn::StatementReturn() :
Statement(Type::RET) {}
StatementReturn::StatementReturn(Expression* ret) : StatementReturn::StatementReturn(Expression* ret) :
mRet(ret) { mRet(ret),
mType = Type::RET; Statement(Type::RET) {}
}
StatementReturn::~StatementReturn() { delete mRet; }
StatementPrint::StatementPrint(Expression* target) : StatementPrint::StatementPrint(Expression* target) :
mTarget(target) { mTarget(target),
mType = Type::PRINT; Statement(Type::PRINT) {}
}
StatementScope::StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack) { StatementPrint::~StatementPrint() { delete mTarget; }
mType = Type::SCOPE;
StatementScope::StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack) :
Statement(Type::SCOPE) {
mStatements = statements; mStatements = statements;
mPushToScopeStack = aPushToScopeStack; mPushToScopeStack = aPushToScopeStack;
} }
StatementIf::StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) { StatementScope::~StatementScope() {
mType = Type::IF; for (auto iter : mStatements) {
delete iter.data();
}
}
StatementIf::StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) :
Statement(Type::IF) {
mOnFalse = on_false; mOnFalse = on_false;
mOnTrue = on_true; mOnTrue = on_true;
mCondition = condition; mCondition = condition;
} }
StatementWhile::StatementWhile(Expression* condition, StatementScope* scope) { StatementIf::~StatementIf() {
mType = Type::WHILE; delete mCondition;
delete mOnTrue;
delete mOnFalse;
}
StatementWhile::StatementWhile(Expression* condition, StatementScope* scope) :
Statement(Type::WHILE) {
mScope = scope; mScope = scope;
mCondition = condition; mCondition = condition;
} }
StatementIgnore::StatementIgnore(Expression* expr) { StatementWhile::~StatementWhile() {
mType = Type::IGNORE; delete mCondition;
delete mScope;
}
StatementIgnore::StatementIgnore(Expression* expr) :
Statement(Type::IGNORE) {
mExpr = expr; mExpr = expr;
} }
StatementClassDef::StatementClassDef(tp::String class_id, StatementScope* scope) { StatementIgnore::~StatementIgnore() { delete mExpr; }
StatementClassDef::StatementClassDef(const tp::String& class_id, StatementScope* scope) :
Statement(Type::CLASS_DEF) {
mClassId = class_id; mClassId = class_id;
mScope = scope; mScope = scope;
mType = Type::CLASS_DEF;
}
// helpers
StatementFuncDef*
obj::BCgen::StmDefFunc(tp::String id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> stms) {
auto out = new StatementFuncDef(id);
auto scope = new StatementScope(stms, true);
out->mStatements = scope;
out->mArgs = args;
return out;
} }
StatementLocalDef* obj::BCgen::StmDefLocal(tp::String id, Expression* value) { StatementClassDef::~StatementClassDef() { delete mScope; }
if (value->mType == Expression::Type::CONST_EXPR) {
return new StatementLocalDef(id, (ExpressionConst*) value);
}
return new StatementLocalDef(id, value); Statement::Statement(Statement::Type type) { mType = type; }
}
StatementCopy* obj::BCgen::StmCopy(Expression* left, Expression* right) { return new StatementCopy(left, right); }
StatementPrint* obj::BCgen::StmPrint(Expression* target) { return new StatementPrint(target); }
StatementReturn* obj::BCgen::StmReturn() { return new StatementReturn(); }
StatementReturn* obj::BCgen::StmReturn(Expression* obj) { return new StatementReturn(obj); }
StatementIf* obj::BCgen::StmIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) {
return new StatementIf(condition, on_true, on_false);
}
StatementScope* obj::BCgen::StmScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack = false) {
return new StatementScope(statements, aPushToScopeStack);
}
StatementWhile* obj::BCgen::StmWhile(Expression* condition, StatementScope* scope) {
return new StatementWhile(condition, scope);
}
StatementIgnore* obj::BCgen::StmIgnore(Expression* expr) { return new StatementIgnore(expr); }
StatementClassDef* obj::BCgen::StmClassDef(tp::String id, StatementScope* scope) {
return new StatementClassDef(id, scope);
}

View file

@ -13,7 +13,7 @@ static UserData scope_empty(const UserData*, const Node*, size_t) { return new S
static UserData stm_list_append(const UserData* start, const Node*, size_t) { static UserData stm_list_append(const UserData* start, const Node*, size_t) {
auto scope = (StatementScope*) start[0]; auto scope = (StatementScope*) start[0];
auto stm = (Statement*) start[2]; auto stm = (Statement*) start[1];
scope->mStatements.append(stm); scope->mStatements.append(stm);
return scope; return scope;
} }
@ -153,19 +153,19 @@ static UserData expr_bool_lesser_eq(const UserData* start, const Node*, size_t)
} }
static UserData expr_add(const UserData* start, const Node*, size_t) { static UserData expr_add(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_ADD); return new ExpressionArithmetics((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_ADD);
} }
static UserData expr_subtract(const UserData* start, const Node*, size_t) { static UserData expr_subtract(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_SUB); return new ExpressionArithmetics((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_SUB);
} }
static UserData expr_multiply(const UserData* start, const Node*, size_t) { static UserData expr_multiply(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_MUL); return new ExpressionArithmetics((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_MUL);
} }
static UserData expr_divide(const UserData* start, const Node*, size_t) { static UserData expr_divide(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_DIV); return new ExpressionArithmetics((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_DIV);
} }
static UserData expr_compound(const UserData* start, const Node*, size_t) { return start[1]; } static UserData expr_compound(const UserData* start, const Node*, size_t) { return start[1]; }

View file

@ -6,11 +6,7 @@
#include "interpreter/opcodes.h" #include "interpreter/opcodes.h"
namespace obj { namespace obj::BCgen {
namespace BCgen {
struct ExpressionChild;
struct ExpressionCall;
struct Expression { struct Expression {
enum class Type { enum class Type {
@ -20,7 +16,7 @@ namespace obj {
CONST_EXPR, CONST_EXPR,
CHILD, CHILD,
CALL, CALL,
ARIPHM, ARITHMETICS,
FUNC, FUNC,
BOOLEAN, BOOLEAN,
SELF, SELF,
@ -29,56 +25,63 @@ namespace obj {
bool mValueUsed = false; bool mValueUsed = false;
Expression(); explicit Expression(Type type);
Expression(Type type); virtual ~Expression() = default;
ExpressionChild* ExprChild(tp::String id); struct ExpressionChild* ExprChild(const tp::String& id);
ExpressionCall* ExprCall(class ExpressionList* args); struct ExpressionCall* ExprCall(class ExpressionList* args);
}; };
struct ExpressionList : public Expression { struct ExpressionList : public Expression {
tp::Buffer<Expression*> mItems; tp::Buffer<Expression*> mItems;
ExpressionList(); ExpressionList();
~ExpressionList() override;
}; };
struct ExpressionNew : public Expression { struct ExpressionNew : public Expression {
tp::String mNewType; tp::String mNewType;
ExpressionNew(tp::String type); explicit ExpressionNew(const tp::String& type);
~ExpressionNew() override;
}; };
struct ExpressionLocal : public Expression { struct ExpressionLocal : public Expression {
tp::String mLocalId; tp::String mLocalId;
ExpressionLocal(tp::String id); explicit ExpressionLocal(const tp::String& id);
~ExpressionLocal() override;
}; };
struct ExpressionFunc : public Expression { struct ExpressionFunc : public Expression {
tp::String mFuncId; tp::String mFuncId;
ExpressionFunc(tp::String id); explicit ExpressionFunc(const tp::String& id);
~ExpressionFunc() override;
}; };
struct ExpressionChild : public Expression { struct ExpressionChild : public Expression {
Expression* mParent = NULL; Expression* mParent = nullptr;
tp::String mLocalId; tp::String mLocalId;
bool mMethod = false; bool mMethod = false;
ExpressionChild(Expression* mParent, tp::String id); ExpressionChild(Expression* mParent, const tp::String& id);
~ExpressionChild() override;
}; };
struct ExpressionCall : public Expression { struct ExpressionCall : public Expression {
Expression* mParent = NULL; Expression* mParent = nullptr;
ExpressionList* mArgs; ExpressionList* mArgs = nullptr;
ExpressionCall(Expression* mParent, ExpressionList* args); ExpressionCall(Expression* mParent, ExpressionList* args);
~ExpressionCall() override;
}; };
struct ExpressionAriphm : public Expression { struct ExpressionArithmetics : public Expression {
Expression* mLeft = NULL; Expression* mLeft = nullptr;
Expression* mRight = NULL; Expression* mRight = nullptr;
OpCode mOpType; OpCode mOpType = OpCode::NONE;
ExpressionAriphm(Expression* left, Expression* right, OpCode type); ExpressionArithmetics(Expression* left, Expression* right, OpCode type);
~ExpressionArithmetics() override;
}; };
struct ExpressionBoolean : public Expression { struct ExpressionBoolean : public Expression {
Expression* mLeft = NULL; Expression* mLeft = nullptr;
Expression* mRight = NULL; Expression* mRight = nullptr;
enum class BoolType : tp::uint1 { enum class BoolType : tp::uint1 {
AND = 24U, AND = 24U,
@ -93,7 +96,8 @@ namespace obj {
} mBoolType; } mBoolType;
ExpressionBoolean(Expression* left, Expression* right, BoolType type); ExpressionBoolean(Expression* left, Expression* right, BoolType type);
ExpressionBoolean(Expression* invert); explicit ExpressionBoolean(Expression* invert);
~ExpressionBoolean() override;
}; };
struct ExpressionConst : public Expression { struct ExpressionConst : public Expression {
@ -101,31 +105,21 @@ namespace obj {
tp::String str; tp::String str;
tp::alni integer = 0; tp::alni integer = 0;
tp::alnf floating = 0; tp::alnf floating = 0;
bool boolean = 0; bool boolean = false;
ExpressionConst(tp::String val); explicit ExpressionConst(const tp::String& val);
ExpressionConst(const char* val); explicit ExpressionConst(const char* val);
ExpressionConst(tp::alni val); explicit ExpressionConst(tp::alni val);
ExpressionConst(tp::int4 val); explicit ExpressionConst(tp::int4 val);
ExpressionConst(tp::flt4 val); explicit ExpressionConst(tp::flt4 val);
ExpressionConst(tp::alnf val); explicit ExpressionConst(tp::alnf val);
ExpressionConst(bool val); explicit ExpressionConst(bool val);
~ExpressionConst() override;
}; };
struct ExpressionSelf : public Expression { struct ExpressionSelf : public Expression {
ExpressionSelf(); ExpressionSelf();
~ExpressionSelf() override = default;
}; };
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

@ -3,9 +3,9 @@
#include "expression.h" #include "expression.h"
namespace obj { namespace obj::BCgen {
namespace BCgen {
struct Statement { struct Statement {
enum class Type { enum class Type {
NONE, NONE,
SCOPE, SCOPE,
@ -23,8 +23,8 @@ namespace obj {
bool mValueUsed = false; bool mValueUsed = false;
Statement() {} explicit Statement(Type type);
Statement(Type type); virtual ~Statement() = default;
}; };
struct StatementScope : public Statement { struct StatementScope : public Statement {
@ -32,86 +32,80 @@ namespace obj {
bool mPushToScopeStack = false; bool mPushToScopeStack = false;
StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack); StatementScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack);
~StatementScope() override;
}; };
struct StatementFuncDef : public Statement { struct StatementFuncDef : public Statement {
tp::Buffer<tp::String> mArgs; tp::Buffer<tp::String> mArgs;
tp::String mFunctionId; tp::String mFunctionId;
StatementScope* mStatements; StatementScope* mStatements = nullptr;
StatementFuncDef(tp::String function_id); explicit StatementFuncDef(const tp::String& function_id);
~StatementFuncDef() override;
}; };
struct StatementLocalDef : public Statement { struct StatementLocalDef : public Statement {
tp::String mLocalId; tp::String mLocalId;
Expression* mNewExpr = NULL; Expression* mNewExpr = nullptr;
ExpressionConst* mConstExpr = NULL; ExpressionConst* mConstExpr = nullptr;
bool mIsConstExpr = false; bool mIsConstExpr = false;
StatementLocalDef(tp::String id, Expression* value); StatementLocalDef(const tp::String& id, Expression* value);
StatementLocalDef(tp::String id, ExpressionConst* value); ~StatementLocalDef() override;
}; };
struct StatementCopy : public Statement { struct StatementCopy : public Statement {
Expression* mLeft = NULL; Expression* mLeft = nullptr;
Expression* mRight = NULL; Expression* mRight = nullptr;
StatementCopy(Expression* left, Expression* right); StatementCopy(Expression* left, Expression* right);
~StatementCopy() override;
}; };
struct StatementReturn : public Statement { struct StatementReturn : public Statement {
Expression* mRet = NULL; Expression* mRet = nullptr;
StatementReturn(Expression* ret); explicit StatementReturn(Expression* ret);
StatementReturn(); StatementReturn();
~StatementReturn() override;
}; };
struct StatementPrint : public Statement { struct StatementPrint : public Statement {
Expression* mTarget = NULL; Expression* mTarget = nullptr;
StatementPrint(Expression* mTarget); explicit StatementPrint(Expression* mTarget);
~StatementPrint() override;
}; };
struct StatementIgnore : public Statement { struct StatementIgnore : public Statement {
Expression* mExpr = NULL; Expression* mExpr = nullptr;
StatementIgnore(Expression* expr); explicit StatementIgnore(Expression* expr);
~StatementIgnore() override;
}; };
struct StatementIf : public Statement { struct StatementIf : public Statement {
Expression* mCondition = NULL; Expression* mCondition = nullptr;
StatementScope* mOnTrue = NULL; StatementScope* mOnTrue = nullptr;
StatementScope* mOnFalse = NULL; StatementScope* mOnFalse = nullptr;
StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false); StatementIf(Expression* condition, StatementScope* on_true, StatementScope* on_false);
~StatementIf() override;
}; };
struct StatementWhile : public Statement { struct StatementWhile : public Statement {
Expression* mCondition = NULL; Expression* mCondition = nullptr;
StatementScope* mScope = NULL; StatementScope* mScope = nullptr;
StatementWhile(Expression* condition, StatementScope* scope); StatementWhile(Expression* condition, StatementScope* scope);
~StatementWhile() override;
}; };
struct StatementClassDef : public Statement { struct StatementClassDef : public Statement {
tp::String mClassId; tp::String mClassId;
StatementScope* mScope = NULL; StatementScope* mScope = nullptr;
StatementClassDef(tp::String class_id, StatementScope* scope); StatementClassDef(const tp::String& class_id, StatementScope* scope);
}; ~StatementClassDef() override;
// 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);
};
}; };
}

View file

@ -41,10 +41,8 @@ if (i == 10) {
)"; )";
auto script = R"( auto script = R"(
{
var i = 10; var i = 10;
print (i + 10) * 5; print (i + 10) * 5;
}
)"; )";
TEST_DEF_STATIC(Complex) { TEST_DEF_STATIC(Complex) {
@ -86,6 +84,5 @@ TEST_DEF_STATIC(Simple) {
TEST_DEF(Interpreter) { TEST_DEF(Interpreter) {
testSimple(); testSimple();
exit(0);
// testComplex(); // testComplex();
} }

View file

@ -9,8 +9,9 @@ using namespace obj;
TEST_DEF_STATIC(Basic) { TEST_DEF_STATIC(Basic) {
Parser parser; Parser parser;
String stream = "{ var i = true; print (i + 1) * 10; }"; String stream = "var i = true; print (i + 1) * 10;";
auto res = parser.parse(stream); auto res = parser.parse(stream);
delete res.scope;
} }
TEST_DEF(Parser) { testBasic(); } TEST_DEF(Parser) { testBasic(); }

View file

@ -16,11 +16,6 @@ int main() {
tp::ModuleManifest* deps[] = { &gModuleObjects, nullptr }; tp::ModuleManifest* deps[] = { &gModuleObjects, nullptr };
tp::ModuleManifest module("ObjectsTests", nullptr, nullptr, deps); tp::ModuleManifest module("ObjectsTests", nullptr, nullptr, deps);
if (module.initialize()) {
testInterpreter();
module.deinitialize();
}
if (module.initialize()) { if (module.initialize()) {
testParser(); testParser();
module.deinitialize(); module.deinitialize();
@ -36,5 +31,10 @@ int main() {
module.deinitialize(); module.deinitialize();
} }
if (module.initialize()) {
testInterpreter();
module.deinitialize();
}
return 0; return 0;
} }