Commit 067f6adb authored by zygzagZ's avatar zygzagZ

Dziedziczenie, przypisywanie dziedziczących, fix na currentBinding jak

wychodzi z klasy
parent 7734538e
......@@ -2891,9 +2891,9 @@ if (!type_) return "function";
return ret + ")";
}
bool Fun::isEqual(Type const *other) const {
bool Fun::isEqual(Type const *other, bool sub) const {
if (const Fun* casted = dynamic_cast<const Fun*>(other)) {
if (*type_ != *casted->type_ || listtype_->size() != casted->listtype_->size()) {
if (!type_->isEqual(casted->type_, sub) || listtype_->size() != casted->listtype_->size()) {
return false;
}
// TODO: czy sprawdzać argumenty?
......@@ -2901,15 +2901,16 @@ bool Fun::isEqual(Type const *other) const {
return false;
}
bool ClassT::isEqual(Type const *other) const {
bool ClassT::isEqual(Type const *other, bool sub) const {
if (const ClassT* casted = dynamic_cast<const ClassT*>(other)) {
auto v1 = pident_->var.lock(), v2 = casted->pident_->var.lock();
return v1 && v1 == v2;
// BindingPtr ba = binding.lock(), bb = casted->binding.lock();
// if (!ba || !bb) {
// throw std::runtime_error("Binding nie znaleziony na typie klasy!");
// }
// return ba->classes[pident_] == bb->classes[pident_];
if (!v1 || !v2) return false;
if (v1 == v2) return true;
if (!sub) return false;
auto klass = static_pointer_cast<ClassInfo>(v2);
if (auto parent = klass->getParent()) {
return isEqual(&*parent->type, sub);
}
}
return false;
}
......@@ -228,9 +228,9 @@ public:
std::weak_ptr<Binding> binding;
virtual Type *clone() const = 0;
virtual std::string printName() const = 0;
virtual bool isEqual(Type const *f) const = 0;
bool operator==(Type const & f) { return isEqual(&f); }
bool operator!=(Type const & f) { return !isEqual(&f); }
virtual bool isEqual(Type const *f, bool sub = false) const = 0;
bool operator==(Type const & f) const { return isEqual(&f); }
bool operator!=(Type const & f) const { return !isEqual(&f); }
};
class Expr : public Visitable
......@@ -648,7 +648,7 @@ public:
virtual Int *clone() const;
void swap(Int &);
std::string printName() const { return "int"; }
bool isEqual(Type const *other) const {
bool isEqual(Type const *other, bool sub = false) const {
if (dynamic_cast<const Int*>(other))
return true;
return false;
......@@ -666,7 +666,7 @@ public:
virtual Str *clone() const;
void swap(Str &);
std::string printName() const { return "string"; }
bool isEqual(Type const *other) const {
bool isEqual(Type const *other, bool sub = false) const {
if (dynamic_cast<const Str*>(other))
return true;
return false;
......@@ -685,7 +685,7 @@ public:
virtual Bool *clone() const;
void swap(Bool &);
std::string printName() const { return "bool"; }
bool isEqual(Type const *other) const {
bool isEqual(Type const *other, bool sub = false) const {
if (dynamic_cast<const Bool*>(other))
return true;
return false;
......@@ -704,7 +704,7 @@ public:
virtual Void *clone() const;
void swap(Void &);
std::string printName() const { return "void"; }
bool isEqual(Type const *other) const {
bool isEqual(Type const *other, bool sub = false) const {
if (dynamic_cast<const Void*>(other))
return true;
return false;
......@@ -725,9 +725,9 @@ public:
virtual Array *clone() const;
void swap(Array &);
std::string printName() const { return type_ ? type_->printName() + "[]" : "array"; }
bool isEqual(Type const *other) const {
bool isEqual(Type const *other, bool sub = false) const {
if (const Array* casted = dynamic_cast<const Array*>(other))
return *type_ == *casted->type_;
return type_->isEqual(casted->type_, sub);
return false;
}
};
......@@ -746,7 +746,7 @@ public:
virtual ClassT *clone() const;
void swap(ClassT &);
std::string printName() const { return pident_ ? ("class " + pident_->string_) : "class"; }
bool isEqual(Type const *other) const;
bool isEqual(Type const *other, bool sub = false) const;
};
class Fun : public Type
......@@ -764,7 +764,7 @@ public:
virtual Fun *clone() const;
void swap(Fun &);
std::string printName() const;
bool isEqual(Type const *other) const;
bool isEqual(Type const *other, bool sub = false) const;
};
class EVar : public Expr
......
......@@ -64,6 +64,7 @@ public:
ClassInfo(PIdent *ident, BindingPtr parent = nullptr);
virtual string kind() const { return "class"; }
ClassInfoPtr getParent() const { return dynamic_pointer_cast<ClassInfo>(parent.lock()); };
};
......
......@@ -83,7 +83,6 @@ int main(int argc, char ** argv)
auto ss = std::stringstream{source};
int l = 1;
int ll = max(max(to_string(e.line - CONTEXT_LINES).length(), to_string(e.line + CONTEXT_LINES).length()), 3lu);
cout << "ll: " << ll << endl;
for (std::string line; std::getline(ss, line, '\n');l++) {
auto diff = abs(e.line - l);
if (diff <= CONTEXT_LINES) {
......
......@@ -23,7 +23,8 @@ void TypeCheck::visitClassDef(ClassDef *t) {
scope->currentBinding = scope->currentClass = c;
t->getBlock()->accept(this);
scope->currentBinding = scope->currentClass = nullptr;
scope->currentClass = nullptr;
scope->currentBinding = scope;
}
void TypeCheck::visitPIdent(PIdent *p_ident)
......@@ -140,7 +141,7 @@ void TypeCheck::visitDecl(Decl *decl)
// el->accept(this); // nie trzeba bo el to tylko ident = expr
if (el->expr_) {
auto exprType = evalExpr(el->expr_);
if (*type != *exprType) {
if (!type->isEqual(&*exprType, true)) {
throw InvalidTypeError(*type, {exprType}, el->expr_);
}
}
......@@ -155,7 +156,7 @@ void TypeCheck::visitAss(Ass *ass)
{
auto a = evalExpr(ass->expr_1);
auto b = evalExpr(ass->expr_2);
if (*a != *b) {
if (!a->isEqual(&*b, true)) {
throw InvalidTypeError(*a, {b}, ass->expr_1);
}
}
......@@ -310,13 +311,7 @@ void TypeCheck::visitEClsMmbr(EClsMmbr *e_cls_mmbr)
if (klass != type->pident_->var.lock()) {
throw ParseError("ej lol inny var");
}
VarInfoPtr var = klass->variables.local(e_cls_mmbr->pident_->string_);
while (!var && klass) {
klass = dynamic_pointer_cast<ClassInfo>(klass->getParent());
if (klass) {
var = klass->variables.local(e_cls_mmbr->pident_->string_);
}
}
VarInfoPtr var = klass->variables[e_cls_mmbr->pident_];
if (!var || var == scope->variables.local(e_cls_mmbr->pident_->string_)) {
cout << "undef clsmmbr !var" << endl;
throw UndefinedError(e_cls_mmbr->pident_);
......@@ -444,7 +439,7 @@ void TypeCheck::visitERel(ERel *e)
{
auto a = evalExpr(e->expr_1);
auto b = evalExpr(e->expr_2);
if (dynamic_cast<EQU*>(e->relop_)) {
if (dynamic_cast<EQU*>(e->relop_) || dynamic_cast<NE*>(e->relop_)) {
if (*a != *b) {
throw InvalidTypeError(*a, {b}, e);
}
......@@ -566,6 +561,9 @@ void TypeCheck::check(Visitable *v)
}
setupEnv();
state = State::checkType;
scope->currentBinding = scope;
v->accept(this);
for (auto c : scope->classes) {
......@@ -609,6 +607,9 @@ void TypeCheck::checkFunction(FunctionInfoPtr f)
BindingPtr binding = make_shared<Binding>(getParentBinding());
f->binding = binding;
if (scope->currentClass) {
binding->variables << make_shared<VarInfo>("self", scope->currentClass->type);
}
for (auto arg : f->arguments) {
binding->variables << arg;
......
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