Commit 65b130e8 authored by zygzagZ's avatar zygzagZ

Poprawki alokacji i utrzymywania zmiennych

parent f608a142
......@@ -529,7 +529,8 @@ Blk::Blk(ListStmt *p1)
Blk::Blk(const Blk & other)
{
liststmt_ = other.liststmt_->clone();
binding = other.binding;
function = other.function;
}
Blk &Blk::operator=(const Blk & other)
......@@ -542,7 +543,8 @@ Blk &Blk::operator=(const Blk & other)
void Blk::swap(Blk & other)
{
std::swap(liststmt_, other.liststmt_);
std::swap(binding, other.binding);
std::swap(function, other.function);
}
Blk::~Blk()
......@@ -1217,10 +1219,9 @@ Item *Item::clone() const
/******************** 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)
void Int::swap(Int & other)
{
Type::swap(other);
}
Int::~Int()
......@@ -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)
void Str::swap(Str & other)
{
Type::swap(other);
}
Str::~Str()
......@@ -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)
void Bool::swap(Bool & other)
{
Type::swap(other);
}
Bool::~Bool()
......@@ -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)
void Void::swap(Void & other)
{
Type::swap(other);
}
Void::~Void()
......@@ -1381,7 +1382,7 @@ Array::Array(Type *p1)
}
Array::Array(const Array & other)
Array::Array(const Array & other) : Type(other)
{
type_ = other.type_->clone();
......@@ -1396,6 +1397,7 @@ Array &Array::operator=(const Array & other)
void Array::swap(Array & other)
{
Type::swap(other);
std::swap(type_, other.type_);
}
......@@ -1424,10 +1426,9 @@ ClassT::ClassT(PIdent *p1)
pident_ = p1;
}
ClassT::ClassT(const ClassT & other)
ClassT::ClassT(const ClassT & other) : Type(other)
{
pident_ = other.pident_->clone();
binding = other.binding;
}
ClassT &ClassT::operator=(const ClassT & other)
......@@ -1439,8 +1440,8 @@ ClassT &ClassT::operator=(const ClassT & other)
void ClassT::swap(ClassT & other)
{
Type::swap(other);
std::swap(pident_, other.pident_);
std::swap(binding, other.binding);
}
ClassT::~ClassT()
......@@ -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();
listtype_ = other.listtype_->clone();
......@@ -1485,9 +1486,9 @@ Fun &Fun::operator=(const Fun & other)
void Fun::swap(Fun & other)
{
Type::swap(other);
std::swap(type_, other.type_);
std::swap(listtype_, other.listtype_);
}
Fun::~Fun()
......
......@@ -223,8 +223,9 @@ public:
class Type : public Visitable
{
public:
// Type() {};
// Type(const &Type other) : binding(other.binding) {};
Type() {};
Type(const Type &other) : binding(other.binding) {};
virtual void swap(Type &other) { std::swap(binding, other.binding); }
std::weak_ptr<Binding> binding;
virtual Type *clone() const = 0;
virtual std::string printName() const = 0;
......
......@@ -7,7 +7,10 @@ FunctionInfo::FunctionInfo(FuncDef *expr, ClassInfoPtr klass)
type = make_shared<Fun>(expr->type_, funArgs);
arguments.reserve(expr->listarg_->size());
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_);
}
}
......
......@@ -11,7 +11,7 @@ using namespace std;
class VarInfo {
public:
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(Ar* arg) : VarInfo(arg->pident_, arg->type_) {}
virtual ~VarInfo() {}
......
......@@ -11,18 +11,18 @@ void TypeCheck::visitClassDef(ClassDef *t) {
if (t->getParent()) {
parent = scope->classes[t->getParent()];
if (!parent) {
cout << "undef classdef !parent" << endl;
throw UndefinedError(t->getParent());
}
} else {
parent = scope;
}
if (state == findClasses) {
auto c = make_shared<ClassInfo>(t->getName(), parent);
static_pointer_cast<ClassT>(c->type)->pident_->var = c;
t->getName()->var = c;
scope->classes << c;
classDefs.emplace_back(c, t);
}
}
void TypeCheck::visitPIdent(PIdent *p_ident)
......@@ -33,7 +33,7 @@ void TypeCheck::visitPIdent(PIdent *p_ident)
lastType = var->type;
if (auto ptr = dynamic_pointer_cast<ClassT>(lastType)) {
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)
void TypeCheck::visitFuncDef(FuncDef *def)
{
FunctionInfoPtr f = make_shared<FunctionInfo>(def, scope->currentClass);
if (state <= findClasses) {
return;
}
// findFunctions
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ę
// FunctionInfo tworzy argumenty
def->listarg_->accept(this);
auto &target = getParentBinding()->functions;
target << f;
......@@ -560,6 +566,14 @@ void TypeCheck::setupEnv() {
{ Int x; addIOFunctions(x); }
{ Str 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)
......@@ -568,14 +582,21 @@ void TypeCheck::check(Visitable *v)
throw std::runtime_error("already initialized");
}
setupEnv();
state = State::checkType;
state = State::findClasses;
scope->currentBinding = scope;
// najpierw zebranie nazw klas
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) {
scope->currentBinding = scope->currentClass = p.first;
p.second->getBlock()->accept(this);
......
......@@ -12,8 +12,9 @@ class TypeCheck : public Visitor
{
enum State {
initialized,
buildInfo,
checkType
findClasses,
findFunctions,
visit
} state;
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