Commit b4061d29 authored by zygzagZ's avatar zygzagZ

refactor v2

parent 37fabd11
...@@ -1427,6 +1427,7 @@ ClassT::ClassT(PIdent *p1) ...@@ -1427,6 +1427,7 @@ ClassT::ClassT(PIdent *p1)
ClassT::ClassT(const ClassT & other) ClassT::ClassT(const ClassT & other)
{ {
pident_ = other.pident_->clone(); pident_ = other.pident_->clone();
binding = other.binding;
} }
ClassT &ClassT::operator=(const ClassT & other) ClassT &ClassT::operator=(const ClassT & other)
......
...@@ -719,11 +719,12 @@ public: ...@@ -719,11 +719,12 @@ public:
Array(const Array &); Array(const Array &);
Array &operator=(const Array &); Array &operator=(const Array &);
Array(Type *p1); Array(Type *p1);
Array() : type_(nullptr) {};
~Array(); ~Array();
virtual void accept(Visitor *v); virtual void accept(Visitor *v);
virtual Array *clone() const; virtual Array *clone() const;
void swap(Array &); void swap(Array &);
std::string printName() const { return type_->printName() + "[]"; } std::string printName() const { return type_ ? type_->printName() + "[]" : "array"; }
bool isEqual(Type const *other) const { bool isEqual(Type const *other) const {
if (const Array* casted = dynamic_cast<const Array*>(other)) if (const Array* casted = dynamic_cast<const Array*>(other))
return *type_ == *casted->type_; return *type_ == *casted->type_;
......
...@@ -36,7 +36,7 @@ public: ...@@ -36,7 +36,7 @@ public:
weak_ptr<ClassInfo> klass; weak_ptr<ClassInfo> klass;
weak_ptr<Binding> binding; weak_ptr<Binding> binding;
FunctionInfo(FuncDef *expr, ClassInfoPtr klass = nullptr); FunctionInfo(FuncDef *expr, ClassInfoPtr klass = nullptr);
FunctionInfo(string name, TypePtr type) : VarInfo(name, type) {}; FunctionInfo(string name, TypePtr type) : VarInfo(name, type), block(nullptr) {};
// FunctionInfo(PIdent *ident, ClassInfoPtr klass = nullptr) : VarInfo(ident), block(NULL), klass(klass) {}; // FunctionInfo(PIdent *ident, ClassInfoPtr klass = nullptr) : VarInfo(ident), block(NULL), klass(klass) {};
virtual string kind() const { return "function"; } virtual string kind() const { return "function"; }
......
...@@ -208,18 +208,14 @@ void TypeCheck::visitCondElse(CondElse *cond_else) ...@@ -208,18 +208,14 @@ void TypeCheck::visitCondElse(CondElse *cond_else)
void TypeCheck::visitWhile(While *while_) void TypeCheck::visitWhile(While *while_)
{ {
while_->expr_->accept(this); evalExpr<Bool>(while_->expr_);
Bool expect;
if (*lastType != expect) {
throw InvalidTypeError(expect, {lastType}, while_->expr_);
}
while_->stmt_->accept(this); while_->stmt_->accept(this);
returnType = nullptr; returnType = nullptr;
} }
void TypeCheck::visitSExp(SExp *s_exp) void TypeCheck::visitSExp(SExp *s_exp)
{ {
s_exp->expr_->accept(this); evalExpr(s_exp->expr_);
} }
void TypeCheck::visitForEach(ForEach *for_each) void TypeCheck::visitForEach(ForEach *for_each)
...@@ -228,8 +224,8 @@ void TypeCheck::visitForEach(ForEach *for_each) ...@@ -228,8 +224,8 @@ void TypeCheck::visitForEach(ForEach *for_each)
for_each->type_->accept(this); for_each->type_->accept(this);
Type &iterType = *lastType; Type &iterType = *lastType;
Array expect(for_each->type_); Array expect(for_each->type_);
for_each->expr_->accept(this); auto arrType = evalExpr<Array>(for_each->expr_);
if (*lastType != expect) { if (*arrType != expect) {
throw InvalidTypeError(expect, {lastType}, for_each->expr_); throw InvalidTypeError(expect, {lastType}, for_each->expr_);
} }
...@@ -283,16 +279,9 @@ void TypeCheck::visitEVar(EVar *e_var) ...@@ -283,16 +279,9 @@ void TypeCheck::visitEVar(EVar *e_var)
void TypeCheck::visitEIndexAcc(EIndexAcc *e_index_acc) void TypeCheck::visitEIndexAcc(EIndexAcc *e_index_acc)
{ {
e_index_acc->expr_1->accept(this); auto type = evalExpr<Array>(e_index_acc->expr_1);
shared_ptr<Array> type = dynamic_pointer_cast<Array>(lastType); evalExpr<Int>(e_index_acc->expr_2);
if (!type) {
throw ParseError("Table expected", e_index_acc->expr_1);
}
e_index_acc->expr_2->accept(this);
Int expect;
if (*lastType != expect) {
throw InvalidTypeError(expect, {lastType}, e_index_acc->expr_2);
}
shared_ptr<Type> last(type->type_->clone()); shared_ptr<Type> last(type->type_->clone());
last->binding = scope->currentBinding; last->binding = scope->currentBinding;
lastType = last; lastType = last;
...@@ -300,11 +289,11 @@ void TypeCheck::visitEIndexAcc(EIndexAcc *e_index_acc) ...@@ -300,11 +289,11 @@ void TypeCheck::visitEIndexAcc(EIndexAcc *e_index_acc)
void TypeCheck::visitEClsMmbr(EClsMmbr *e_cls_mmbr) void TypeCheck::visitEClsMmbr(EClsMmbr *e_cls_mmbr)
{ {
e_cls_mmbr->expr_->accept(this); auto exprType = evalExpr(e_cls_mmbr->expr_);
shared_ptr<ClassT> type = dynamic_pointer_cast<ClassT>(lastType); shared_ptr<ClassT> type = dynamic_pointer_cast<ClassT>(exprType);
if (!type) { if (!type) {
// it is an array and we ask for length // it is an array and we ask for length
if (dynamic_pointer_cast<Array>(lastType)) { if (dynamic_pointer_cast<Array>(exprType)) {
if (e_cls_mmbr->pident_->string_ == "length") { if (e_cls_mmbr->pident_->string_ == "length") {
lastType = make_shared<Int>(); lastType = make_shared<Int>();
return; return;
...@@ -330,11 +319,10 @@ void TypeCheck::visitEApp(EApp *e_app) ...@@ -330,11 +319,10 @@ void TypeCheck::visitEApp(EApp *e_app)
for (unsigned int i = 0; i < type->listtype_->size(); i++) { for (unsigned int i = 0; i < type->listtype_->size(); i++) {
try { try {
Expr *expr = e_app->listexpr_->at(i); Expr *expr = e_app->listexpr_->at(i);
expr->accept(this); auto exprType = evalExpr(expr);
if (!lastType) throw ParseError("No expr return type", expr);
auto *expect = type->listtype_->at(i); auto *expect = type->listtype_->at(i);
if (*expect != *lastType) { if (*expect != *exprType) {
throw InvalidTypeError(*expect, {lastType}, expr); throw InvalidTypeError(*expect, {exprType}, expr);
} }
} catch (const ParseError &e) { } catch (const ParseError &e) {
throw ParseError(string(e.what()) + "\nIn argument " + to_string(i+1) + " of function call:", e_app); throw ParseError(string(e.what()) + "\nIn argument " + to_string(i+1) + " of function call:", e_app);
...@@ -368,18 +356,13 @@ void TypeCheck::visitEString(EString *e) ...@@ -368,18 +356,13 @@ void TypeCheck::visitEString(EString *e)
void TypeCheck::visitENewArray(ENewArray *e) void TypeCheck::visitENewArray(ENewArray *e)
{ {
e->expr_->accept(this); evalExpr<Int>(e->expr_);
auto a = lastType;
Int expect;
if (*a != expect) {
throw InvalidTypeError(expect, {a}, e->expr_);
}
e->type_->accept(this); e->type_->accept(this);
if (dynamic_cast<Void*>(e->type_)) { if (dynamic_cast<Void*>(e->type_)) {
throw ParseError("Tablica typu void!", e); throw ParseError("Tablica typu void!", e);
} }
auto ret = make_shared<Array>(e->type_); // make type auto ret = make_shared<Array>(e->type_->clone()); // make type
ret->binding = scope->currentBinding; ret->type_->binding = scope->currentBinding;
lastType = ret; lastType = ret;
} }
...@@ -455,26 +438,16 @@ void TypeCheck::visitERel(ERel *e) ...@@ -455,26 +438,16 @@ void TypeCheck::visitERel(ERel *e)
void TypeCheck::visitEAnd(EAnd *e) void TypeCheck::visitEAnd(EAnd *e)
{ {
e->expr_1->accept(this); auto a = evalExpr<Bool>(e->expr_1);
auto a = lastType; auto b = evalExpr<Bool>(e->expr_2);
e->expr_2->accept(this); lastType = b;
auto b = lastType;
Bool expect;
if (!a || !b || *a != expect || *a != *b) {
throw InvalidTypeError(expect, {a, b}, e);
}
} }
void TypeCheck::visitEOr(EOr *e) void TypeCheck::visitEOr(EOr *e)
{ {
e->expr_1->accept(this); auto a = evalExpr<Bool>(e->expr_1);
auto a = lastType; auto b = evalExpr<Bool>(e->expr_2);
e->expr_2->accept(this); lastType = b;
auto b = lastType;
Bool expect;
if (!a || !b || *a != expect || *a != *b) {
throw InvalidTypeError(expect, {a, b}, e);
}
} }
void TypeCheck::visitListTopDef(ListTopDef *list_top_def) void TypeCheck::visitListTopDef(ListTopDef *list_top_def)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment