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