Commit c7ae29b0 authored by zygzagZ's avatar zygzagZ

POC

parent 5f84c1c3
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Absyn.h" #include "Absyn.h"
#include "Info.h"
/******************** PIdent ********************/ /******************** PIdent ********************/
PIdent::PIdent(String p1, Integer p2) PIdent::PIdent(String p1, Integer p2)
...@@ -1421,13 +1422,11 @@ Array *Array::clone() const ...@@ -1421,13 +1422,11 @@ Array *Array::clone() const
ClassT::ClassT(PIdent *p1) ClassT::ClassT(PIdent *p1)
{ {
pident_ = p1; pident_ = p1;
} }
ClassT::ClassT(const ClassT & other) ClassT::ClassT(const ClassT & other)
{ {
pident_ = other.pident_->clone(); pident_ = other.pident_->clone();
} }
ClassT &ClassT::operator=(const ClassT & other) ClassT &ClassT::operator=(const ClassT & other)
...@@ -1440,7 +1439,7 @@ ClassT &ClassT::operator=(const ClassT & other) ...@@ -1440,7 +1439,7 @@ ClassT &ClassT::operator=(const ClassT & other)
void ClassT::swap(ClassT & other) void ClassT::swap(ClassT & other)
{ {
std::swap(pident_, other.pident_); std::swap(pident_, other.pident_);
std::swap(binding, other.binding);
} }
ClassT::~ClassT() ClassT::~ClassT()
...@@ -2895,6 +2894,18 @@ bool Fun::isEqual(Type const *other) const { ...@@ -2895,6 +2894,18 @@ bool Fun::isEqual(Type const *other) const {
if (*type_ != *casted->type_ || listtype_->size() != casted->listtype_->size()) { if (*type_ != *casted->type_ || listtype_->size() != casted->listtype_->size()) {
return false; return false;
} }
// TODO: czy sprawdzać argumenty?
}
return false;
}
bool ClassT::isEqual(Type const *other) const {
if (const ClassT* casted = dynamic_cast<const ClassT*>(other)) {
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_];
} }
return false; return false;
} }
...@@ -223,6 +223,8 @@ public: ...@@ -223,6 +223,8 @@ public:
class Type : public Visitable class Type : public Visitable
{ {
public: public:
// Type() {};
// Type(const &Type other) : 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;
...@@ -741,11 +743,8 @@ public: ...@@ -741,11 +743,8 @@ public:
virtual void accept(Visitor *v); virtual void accept(Visitor *v);
virtual ClassT *clone() const; virtual ClassT *clone() const;
void swap(ClassT &); void swap(ClassT &);
std::string printName() const { return "class"; } std::string printName() const { return "class " + pident_->string_; }
bool isEqual(Type const *other) const { bool isEqual(Type const *other) const;
// TODO: implement ClassT comparison
return false;
}
}; };
class Fun : public Type class Fun : public Type
......
#include "Info.h" #include "Info.h"
FunctionInfo::FunctionInfo(FuncDef *expr, ClassInfoPtr klass) FunctionInfo::FunctionInfo(FuncDef *expr, ClassInfoPtr klass)
: VarInfo(expr->pident_, expr->type_), block(expr->block_), klass(klass) : VarInfo(expr->pident_, nullptr), block(expr->block_), klass(klass)
{ {
// TODO: tutaj leakujemy listtype
ListType *funArgs = new ListType();
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)); arguments.emplace_back(make_shared<VarInfo>((Ar*)arg));
funArgs->emplace_back(arg->type_);
} }
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "Info.h" #include "Info.h"
#include "ParseError.h" #include "ParseError.h"
#include "TypeDefs.h" #include "TypeDefs.h"
#include <iostream>
template<typename T> template<typename T>
struct Compare struct Compare
......
...@@ -27,6 +27,7 @@ ParseError::ParseError(string reason, Visitable *expr) : runtime_error("ParseErr ...@@ -27,6 +27,7 @@ ParseError::ParseError(string reason, Visitable *expr) : runtime_error("ParseErr
if (expr) { if (expr) {
ss << " Expression: " << PrintAbsyn().print(expr); ss << " Expression: " << PrintAbsyn().print(expr);
} }
msg = ss.str();
} }
RedefinedError::RedefinedError(PIdent *ident, VarInfoPtr orig) { RedefinedError::RedefinedError(PIdent *ident, VarInfoPtr orig) {
......
This diff is collapsed.
...@@ -21,7 +21,9 @@ class TypeCheck : public Visitor ...@@ -21,7 +21,9 @@ class TypeCheck : public Visitor
void checkFunction(FunctionInfoPtr f); void checkFunction(FunctionInfoPtr f);
void checkReturnStatement(shared_ptr<Type> type, Stmt *stmt); void checkReturnStatement(shared_ptr<Type> type, Stmt *stmt);
void checkFunctionRet(); void checkFunctionRet();
void setupEnv();
public: public:
BindingPtr getParentBinding() const { return scope->currentClass ? static_pointer_cast<Binding>(scope->currentClass) : static_pointer_cast<Binding>(scope); }
TypeCheck(); TypeCheck();
void check(Visitable *v); void check(Visitable *v);
protected: protected:
......
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