Commit 65b130e8 authored by zygzagZ's avatar zygzagZ

Poprawki alokacji i utrzymywania zmiennych

parent f608a142
...@@ -529,7 +529,8 @@ Blk::Blk(ListStmt *p1) ...@@ -529,7 +529,8 @@ Blk::Blk(ListStmt *p1)
Blk::Blk(const Blk & other) Blk::Blk(const Blk & other)
{ {
liststmt_ = other.liststmt_->clone(); liststmt_ = other.liststmt_->clone();
binding = other.binding;
function = other.function;
} }
Blk &Blk::operator=(const Blk & other) Blk &Blk::operator=(const Blk & other)
...@@ -542,7 +543,8 @@ Blk &Blk::operator=(const Blk & other) ...@@ -542,7 +543,8 @@ Blk &Blk::operator=(const Blk & other)
void Blk::swap(Blk & other) void Blk::swap(Blk & other)
{ {
std::swap(liststmt_, other.liststmt_); std::swap(liststmt_, other.liststmt_);
std::swap(binding, other.binding);
std::swap(function, other.function);
} }
Blk::~Blk() Blk::~Blk()
...@@ -1217,10 +1219,9 @@ Item *Item::clone() const ...@@ -1217,10 +1219,9 @@ Item *Item::clone() const
/******************** Int ********************/ /******************** Int ********************/
Int::Int() Int::Int()
{ {
} }
Int::Int(const Int & other) Int::Int(const Int & other) : Type(other)
{ {
} }
...@@ -1234,7 +1235,7 @@ Int &Int::operator=(const Int & other) ...@@ -1234,7 +1235,7 @@ Int &Int::operator=(const Int & other)
void Int::swap(Int & other) void Int::swap(Int & other)
{ {
Type::swap(other);
} }
Int::~Int() Int::~Int()
...@@ -1260,7 +1261,7 @@ Str::Str() ...@@ -1260,7 +1261,7 @@ Str::Str()
} }
Str::Str(const Str & other) Str::Str(const Str & other) : Type(other)
{ {
} }
...@@ -1274,7 +1275,7 @@ Str &Str::operator=(const Str & other) ...@@ -1274,7 +1275,7 @@ Str &Str::operator=(const Str & other)
void Str::swap(Str & other) void Str::swap(Str & other)
{ {
Type::swap(other);
} }
Str::~Str() Str::~Str()
...@@ -1300,7 +1301,7 @@ Bool::Bool() ...@@ -1300,7 +1301,7 @@ Bool::Bool()
} }
Bool::Bool(const Bool & other) Bool::Bool(const Bool & other) : Type(other)
{ {
} }
...@@ -1314,7 +1315,7 @@ Bool &Bool::operator=(const Bool & other) ...@@ -1314,7 +1315,7 @@ Bool &Bool::operator=(const Bool & other)
void Bool::swap(Bool & other) void Bool::swap(Bool & other)
{ {
Type::swap(other);
} }
Bool::~Bool() Bool::~Bool()
...@@ -1340,7 +1341,7 @@ Void::Void() ...@@ -1340,7 +1341,7 @@ Void::Void()
} }
Void::Void(const Void & other) Void::Void(const Void & other) : Type(other)
{ {
} }
...@@ -1354,7 +1355,7 @@ Void &Void::operator=(const Void & other) ...@@ -1354,7 +1355,7 @@ Void &Void::operator=(const Void & other)
void Void::swap(Void & other) void Void::swap(Void & other)
{ {
Type::swap(other);
} }
Void::~Void() Void::~Void()
...@@ -1381,7 +1382,7 @@ Array::Array(Type *p1) ...@@ -1381,7 +1382,7 @@ Array::Array(Type *p1)
} }
Array::Array(const Array & other) Array::Array(const Array & other) : Type(other)
{ {
type_ = other.type_->clone(); type_ = other.type_->clone();
...@@ -1396,6 +1397,7 @@ Array &Array::operator=(const Array & other) ...@@ -1396,6 +1397,7 @@ Array &Array::operator=(const Array & other)
void Array::swap(Array & other) void Array::swap(Array & other)
{ {
Type::swap(other);
std::swap(type_, other.type_); std::swap(type_, other.type_);
} }
...@@ -1424,10 +1426,9 @@ ClassT::ClassT(PIdent *p1) ...@@ -1424,10 +1426,9 @@ ClassT::ClassT(PIdent *p1)
pident_ = p1; pident_ = p1;
} }
ClassT::ClassT(const ClassT & other) ClassT::ClassT(const ClassT & other) : Type(other)
{ {
pident_ = other.pident_->clone(); pident_ = other.pident_->clone();
binding = other.binding;
} }
ClassT &ClassT::operator=(const ClassT & other) ClassT &ClassT::operator=(const ClassT & other)
...@@ -1439,8 +1440,8 @@ ClassT &ClassT::operator=(const ClassT & other) ...@@ -1439,8 +1440,8 @@ ClassT &ClassT::operator=(const ClassT & other)
void ClassT::swap(ClassT & other) void ClassT::swap(ClassT & other)
{ {
Type::swap(other);
std::swap(pident_, other.pident_); std::swap(pident_, other.pident_);
std::swap(binding, other.binding);
} }
ClassT::~ClassT() ClassT::~ClassT()
...@@ -1469,7 +1470,7 @@ Fun::Fun(Type *p1, ListType *p2) ...@@ -1469,7 +1470,7 @@ Fun::Fun(Type *p1, ListType *p2)
} }
Fun::Fun(const Fun & other) Fun::Fun(const Fun & other) : Type(other)
{ {
type_ = other.type_->clone(); type_ = other.type_->clone();
listtype_ = other.listtype_->clone(); listtype_ = other.listtype_->clone();
...@@ -1485,9 +1486,9 @@ Fun &Fun::operator=(const Fun & other) ...@@ -1485,9 +1486,9 @@ Fun &Fun::operator=(const Fun & other)
void Fun::swap(Fun & other) void Fun::swap(Fun & other)
{ {
Type::swap(other);
std::swap(type_, other.type_); std::swap(type_, other.type_);
std::swap(listtype_, other.listtype_); std::swap(listtype_, other.listtype_);
} }
Fun::~Fun() Fun::~Fun()
......
...@@ -223,8 +223,9 @@ public: ...@@ -223,8 +223,9 @@ public:
class Type : public Visitable class Type : public Visitable
{ {
public: public:
// Type() {}; Type() {};
// Type(const &Type other) : binding(other.binding) {}; Type(const Type &other) : binding(other.binding) {};
virtual void swap(Type &other) { std::swap(binding, other.binding); }
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;
......
...@@ -7,7 +7,10 @@ FunctionInfo::FunctionInfo(FuncDef *expr, ClassInfoPtr klass) ...@@ -7,7 +7,10 @@ FunctionInfo::FunctionInfo(FuncDef *expr, ClassInfoPtr klass)
type = make_shared<Fun>(expr->type_, funArgs); type = make_shared<Fun>(expr->type_, funArgs);
arguments.reserve(expr->listarg_->size()); arguments.reserve(expr->listarg_->size());
for (auto arg : *expr->listarg_) { for (auto arg : *expr->listarg_) {
arguments.emplace_back(make_shared<VarInfo>((Ar*)arg)); VarInfoPtr var = make_shared<VarInfo>((Ar*)arg);
var->ident->var = var;
arg->pident_->var = var;
arguments.emplace_back(var);
funArgs->emplace_back(arg->type_); funArgs->emplace_back(arg->type_);
} }
} }
......
...@@ -11,7 +11,7 @@ using namespace std; ...@@ -11,7 +11,7 @@ using namespace std;
class VarInfo { class VarInfo {
public: public:
VarInfo(PIdent* ident, TypePtr type) : ident(ident), name(ident->string_), type(type), lineLocation(ident->lineno) {} VarInfo(PIdent* ident, TypePtr type) : ident(ident), name(ident->string_), type(type), lineLocation(ident->lineno) {}
VarInfo(PIdent* ident, Type* type = nullptr) : ident(ident), name(ident->string_), type(type), lineLocation(ident->lineno) {} VarInfo(PIdent* ident, Type* type = nullptr) : ident(ident), name(ident->string_), type(type ? type->clone() : nullptr), lineLocation(ident->lineno) {}
VarInfo(string name, TypePtr type) : name(name), type(type), lineLocation(-1) {} VarInfo(string name, TypePtr type) : name(name), type(type), lineLocation(-1) {}
VarInfo(Ar* arg) : VarInfo(arg->pident_, arg->type_) {} VarInfo(Ar* arg) : VarInfo(arg->pident_, arg->type_) {}
virtual ~VarInfo() {} virtual ~VarInfo() {}
......
...@@ -11,18 +11,18 @@ void TypeCheck::visitClassDef(ClassDef *t) { ...@@ -11,18 +11,18 @@ void TypeCheck::visitClassDef(ClassDef *t) {
if (t->getParent()) { if (t->getParent()) {
parent = scope->classes[t->getParent()]; parent = scope->classes[t->getParent()];
if (!parent) { if (!parent) {
cout << "undef classdef !parent" << endl;
throw UndefinedError(t->getParent()); throw UndefinedError(t->getParent());
} }
} else { } else {
parent = scope; parent = scope;
} }
auto c = make_shared<ClassInfo>(t->getName(), parent); if (state == findClasses) {
static_pointer_cast<ClassT>(c->type)->pident_->var = c; auto c = make_shared<ClassInfo>(t->getName(), parent);
t->getName()->var = c; static_pointer_cast<ClassT>(c->type)->pident_->var = c;
scope->classes << c; t->getName()->var = c;
scope->classes << c;
classDefs.emplace_back(c, t); classDefs.emplace_back(c, t);
}
} }
void TypeCheck::visitPIdent(PIdent *p_ident) void TypeCheck::visitPIdent(PIdent *p_ident)
...@@ -33,7 +33,7 @@ void TypeCheck::visitPIdent(PIdent *p_ident) ...@@ -33,7 +33,7 @@ void TypeCheck::visitPIdent(PIdent *p_ident)
lastType = var->type; lastType = var->type;
if (auto ptr = dynamic_pointer_cast<ClassT>(lastType)) { if (auto ptr = dynamic_pointer_cast<ClassT>(lastType)) {
if (!ptr->pident_ || !ptr->pident_->var.lock()) if (!ptr->pident_ || !ptr->pident_->var.lock())
throw ParseError("chuj"); throw runtime_error("either no pident or no var assigned found");
} }
} }
...@@ -54,12 +54,18 @@ void TypeCheck::visitClDef(ClDef *cl_def) ...@@ -54,12 +54,18 @@ void TypeCheck::visitClDef(ClDef *cl_def)
void TypeCheck::visitFuncDef(FuncDef *def) void TypeCheck::visitFuncDef(FuncDef *def)
{ {
FunctionInfoPtr f = make_shared<FunctionInfo>(def, scope->currentClass); if (state <= findClasses) {
return;
}
// findFunctions
def->type_->accept(this); def->type_->accept(this);
def->listarg_->accept(this);
FunctionInfoPtr f = make_shared<FunctionInfo>(def, scope->currentClass);
// pident nie trzeba bo definiujemy a nie odwołujemy się // pident nie trzeba bo definiujemy a nie odwołujemy się
// FunctionInfo tworzy argumenty // FunctionInfo tworzy argumenty
def->listarg_->accept(this);
auto &target = getParentBinding()->functions; auto &target = getParentBinding()->functions;
target << f; target << f;
...@@ -103,7 +109,7 @@ void TypeCheck::visitClassFld(ClassFld *decl) ...@@ -103,7 +109,7 @@ void TypeCheck::visitClassFld(ClassFld *decl)
throw ParseError("Initialization in class variable definition", decl); throw ParseError("Initialization in class variable definition", decl);
} }
VarInfoPtr var = make_shared<VarInfo>(el->pident_, type); VarInfoPtr var = make_shared<VarInfo>(el->pident_, type);
el->pident_->var = var; el->pident_->var = var;
scope->currentClass->variables << var; scope->currentClass->variables << var;
} }
...@@ -560,6 +566,14 @@ void TypeCheck::setupEnv() { ...@@ -560,6 +566,14 @@ void TypeCheck::setupEnv() {
{ Int x; addIOFunctions(x); } { Int x; addIOFunctions(x); }
{ Str x; addIOFunctions(x); } { Str x; addIOFunctions(x); }
// { Bool x; addIOFunctions(x); } // { Bool x; addIOFunctions(x); }
// void error()
{
TypePtr fun = make_shared<Fun>(Void().clone(), new ListType());
FunctionInfoPtr error = make_shared<FunctionInfo>("error", fun);
scope->functions << error;
}
} }
void TypeCheck::check(Visitable *v) void TypeCheck::check(Visitable *v)
...@@ -568,14 +582,21 @@ void TypeCheck::check(Visitable *v) ...@@ -568,14 +582,21 @@ void TypeCheck::check(Visitable *v)
throw std::runtime_error("already initialized"); throw std::runtime_error("already initialized");
} }
setupEnv(); setupEnv();
state = State::checkType; state = State::findClasses;
scope->currentBinding = scope; scope->currentBinding = scope;
// najpierw zebranie nazw klas // najpierw zebranie nazw klas
v->accept(this); v->accept(this);
// teraz bloki klas jak juz znamy wszystkie istniejace klasy state = State::findFunctions;
scope->currentBinding = scope;
// zebranie funkcji zewnętrznych
v->accept(this);
state = State::visit;
// teraz bloki klas jak juz znamy wszystkie istniejace klasy i funkcje
for (auto p : classDefs) { for (auto p : classDefs) {
scope->currentBinding = scope->currentClass = p.first; scope->currentBinding = scope->currentClass = p.first;
p.second->getBlock()->accept(this); p.second->getBlock()->accept(this);
......
...@@ -12,8 +12,9 @@ class TypeCheck : public Visitor ...@@ -12,8 +12,9 @@ class TypeCheck : public Visitor
{ {
enum State { enum State {
initialized, initialized,
buildInfo, findClasses,
checkType findFunctions,
visit
} state; } state;
shared_ptr<Scope> scope; shared_ptr<Scope> scope;
......
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