Commit 37fabd11 authored by zygzagZ's avatar zygzagZ

refactor evalExpr

parent eb4aaa96
......@@ -75,7 +75,7 @@ void Prog::swap(Prog & other)
Prog::~Prog()
{
delete(listtopdef_);
if (listtopdef_) delete(listtopdef_);
}
......@@ -119,7 +119,7 @@ void FnDef::swap(FnDef & other)
FnDef::~FnDef()
{
delete(fundef_);
if (fundef_) delete(fundef_);
}
......@@ -163,7 +163,7 @@ void ClDef::swap(ClDef & other)
ClDef::~ClDef()
{
delete(classdef_);
if (classdef_) delete(classdef_);
}
......@@ -216,10 +216,10 @@ void FuncDef::swap(FuncDef & other)
FuncDef::~FuncDef()
{
delete(type_);
delete(pident_);
delete(listarg_);
delete(block_);
if (type_) delete(type_);
if (pident_) delete(pident_);
if (listarg_) delete(listarg_);
if (block_) delete(block_);
}
......@@ -266,8 +266,8 @@ void Ar::swap(Ar & other)
Ar::~Ar()
{
delete(type_);
delete(pident_);
if (type_) delete(type_);
if (pident_) delete(pident_);
}
......@@ -314,8 +314,8 @@ void ClassDefN::swap(ClassDefN & other)
ClassDefN::~ClassDefN()
{
delete(pident_);
delete(classblock_);
if (pident_) delete(pident_);
if (classblock_) delete(classblock_);
}
......@@ -365,9 +365,9 @@ void ClassDefE::swap(ClassDefE & other)
ClassDefE::~ClassDefE()
{
delete(pident_1);
delete(pident_2);
delete(classblock_);
if (pident_1) delete(pident_1);
if (pident_2) delete(pident_2);
if (classblock_) delete(classblock_);
}
......@@ -411,7 +411,7 @@ void ClassBl::swap(ClassBl & other)
ClassBl::~ClassBl()
{
delete(listclassblockdef_);
if (listclassblockdef_) delete(listclassblockdef_);
}
......@@ -455,7 +455,7 @@ void ClassMthd::swap(ClassMthd & other)
ClassMthd::~ClassMthd()
{
delete(fundef_);
if (fundef_) delete(fundef_);
}
......@@ -502,8 +502,8 @@ void ClassFld::swap(ClassFld & other)
ClassFld::~ClassFld()
{
delete(type_);
delete(listitem_);
if (type_) delete(type_);
if (listitem_) delete(listitem_);
}
......@@ -547,7 +547,7 @@ void Blk::swap(Blk & other)
Blk::~Blk()
{
delete(liststmt_);
if (liststmt_) delete(liststmt_);
}
......@@ -631,7 +631,7 @@ void BStmt::swap(BStmt & other)
BStmt::~BStmt()
{
delete(block_);
if (block_) delete(block_);
}
......@@ -678,8 +678,8 @@ void Decl::swap(Decl & other)
Decl::~Decl()
{
delete(type_);
delete(listitem_);
if (type_) delete(type_);
if (listitem_) delete(listitem_);
}
......@@ -726,8 +726,8 @@ void Ass::swap(Ass & other)
Ass::~Ass()
{
delete(expr_1);
delete(expr_2);
if (expr_1) delete(expr_1);
if (expr_2) delete(expr_2);
}
......@@ -771,7 +771,7 @@ void Incr::swap(Incr & other)
Incr::~Incr()
{
delete(expr_);
if (expr_) delete(expr_);
}
......@@ -815,7 +815,7 @@ void Decr::swap(Decr & other)
Decr::~Decr()
{
delete(expr_);
if (expr_) delete(expr_);
}
......@@ -859,7 +859,7 @@ void Ret::swap(Ret & other)
Ret::~Ret()
{
delete(expr_);
if (expr_) delete(expr_);
}
......@@ -946,8 +946,8 @@ void Cond::swap(Cond & other)
Cond::~Cond()
{
delete(expr_);
delete(stmt_);
if (expr_) delete(expr_);
if (stmt_) delete(stmt_);
}
......@@ -997,9 +997,9 @@ void CondElse::swap(CondElse & other)
CondElse::~CondElse()
{
delete(expr_);
delete(stmt_1);
delete(stmt_2);
if (expr_) delete(expr_);
if (stmt_1) delete(stmt_1);
if (stmt_2) delete(stmt_2);
}
......@@ -1046,8 +1046,8 @@ void While::swap(While & other)
While::~While()
{
delete(expr_);
delete(stmt_);
if (expr_) delete(expr_);
if (stmt_) delete(stmt_);
}
......@@ -1091,7 +1091,7 @@ void SExp::swap(SExp & other)
SExp::~SExp()
{
delete(expr_);
if (expr_) delete(expr_);
}
......@@ -1144,10 +1144,10 @@ void ForEach::swap(ForEach & other)
ForEach::~ForEach()
{
delete(type_);
delete(pident_);
delete(expr_);
delete(stmt_);
if (type_) delete(type_);
if (pident_) delete(pident_);
if (expr_) delete(expr_);
if (stmt_) delete(stmt_);
}
......@@ -1194,8 +1194,8 @@ void Item::swap(Item & other)
Item::~Item()
{
delete(pident_);
delete(expr_);
if (pident_) delete(pident_);
if (expr_) delete(expr_);
}
......@@ -1402,7 +1402,7 @@ void Array::swap(Array & other)
Array::~Array()
{
delete(type_);
if (type_) delete(type_);
}
......@@ -1444,7 +1444,7 @@ void ClassT::swap(ClassT & other)
ClassT::~ClassT()
{
delete(pident_);
if (pident_) delete(pident_);
}
......@@ -1491,8 +1491,8 @@ void Fun::swap(Fun & other)
Fun::~Fun()
{
delete(type_);
delete(listtype_);
if (type_) delete(type_);
if (listtype_) delete(listtype_);
}
......@@ -1536,7 +1536,7 @@ void EVar::swap(EVar & other)
EVar::~EVar()
{
delete(pident_);
if (pident_) delete(pident_);
}
......@@ -1583,8 +1583,8 @@ void EIndexAcc::swap(EIndexAcc & other)
EIndexAcc::~EIndexAcc()
{
delete(expr_1);
delete(expr_2);
if (expr_1) delete(expr_1);
if (expr_2) delete(expr_2);
}
......@@ -1631,8 +1631,8 @@ void EClsMmbr::swap(EClsMmbr & other)
EClsMmbr::~EClsMmbr()
{
delete(expr_);
delete(pident_);
if (expr_) delete(expr_);
if (pident_) delete(pident_);
}
......@@ -1679,8 +1679,8 @@ void EApp::swap(EApp & other)
EApp::~EApp()
{
delete(expr_);
delete(listexpr_);
if (expr_) delete(expr_);
if (listexpr_) delete(listexpr_);
}
......@@ -1893,8 +1893,8 @@ void ENewArray::swap(ENewArray & other)
ENewArray::~ENewArray()
{
delete(type_);
delete(expr_);
if (type_) delete(type_);
if (expr_) delete(expr_);
}
......@@ -1938,7 +1938,7 @@ void ENewClass::swap(ENewClass & other)
ENewClass::~ENewClass()
{
delete(pident_);
if (pident_) delete(pident_);
}
......@@ -1982,7 +1982,7 @@ void NullCast::swap(NullCast & other)
NullCast::~NullCast()
{
delete(expr_);
if (expr_) delete(expr_);
}
......@@ -2026,7 +2026,7 @@ void Neg::swap(Neg & other)
Neg::~Neg()
{
delete(expr_);
if (expr_) delete(expr_);
}
......@@ -2070,7 +2070,7 @@ void Not::swap(Not & other)
Not::~Not()
{
delete(expr_);
if (expr_) delete(expr_);
}
......@@ -2120,9 +2120,9 @@ void EMul::swap(EMul & other)
EMul::~EMul()
{
delete(expr_1);
delete(mulop_);
delete(expr_2);
if (expr_1) delete(expr_1);
if (mulop_) delete(mulop_);
if (expr_2) delete(expr_2);
}
......@@ -2172,9 +2172,9 @@ void EAdd::swap(EAdd & other)
EAdd::~EAdd()
{
delete(expr_1);
delete(addop_);
delete(expr_2);
if (expr_1) delete(expr_1);
if (addop_) delete(addop_);
if (expr_2) delete(expr_2);
}
......@@ -2224,9 +2224,9 @@ void ERel::swap(ERel & other)
ERel::~ERel()
{
delete(expr_1);
delete(relop_);
delete(expr_2);
if (expr_1) delete(expr_1);
if (relop_) delete(relop_);
if (expr_2) delete(expr_2);
}
......@@ -2273,8 +2273,8 @@ void EAnd::swap(EAnd & other)
EAnd::~EAnd()
{
delete(expr_1);
delete(expr_2);
if (expr_1) delete(expr_1);
if (expr_2) delete(expr_2);
}
......@@ -2321,8 +2321,8 @@ void EOr::swap(EOr & other)
EOr::~EOr()
{
delete(expr_1);
delete(expr_2);
if (expr_1) delete(expr_1);
if (expr_2) delete(expr_2);
}
......@@ -2879,6 +2879,7 @@ ListExpr *ListExpr::clone() const
std::string Fun::printName() const{
if (!type_) return "function";
std::string ret = type_->printName() + "(";
bool fst = true;
for (auto i : *listtype_) {
......
......@@ -665,7 +665,7 @@ public:
virtual void accept(Visitor *v);
virtual Str *clone() const;
void swap(Str &);
std::string printName() const { return "str"; }
std::string printName() const { return "string"; }
bool isEqual(Type const *other) const {
if (dynamic_cast<const Str*>(other))
return true;
......@@ -739,11 +739,12 @@ public:
ClassT(const ClassT &);
ClassT &operator=(const ClassT &);
ClassT(PIdent *p1);
ClassT() : pident_(nullptr) {};
~ClassT();
virtual void accept(Visitor *v);
virtual ClassT *clone() const;
void swap(ClassT &);
std::string printName() const { return "class " + pident_->string_; }
std::string printName() const { return pident_ ? ("class " + pident_->string_) : "class"; }
bool isEqual(Type const *other) const;
};
......@@ -754,6 +755,7 @@ public:
ListType *listtype_;
Fun(const Fun &);
Fun() : type_(nullptr), listtype_(nullptr) {};
Fun &operator=(const Fun &);
Fun(Type *p1, ListType *p2);
~Fun();
......
......@@ -3,8 +3,6 @@
FunctionInfo::FunctionInfo(FuncDef *expr, ClassInfoPtr 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());
......@@ -29,6 +27,8 @@ ClassInfo::ClassInfo(PIdent *ident, BindingPtr parent /*= nullptr*/)
: VarInfo(ident),
Binding(parent)
{
type = make_shared<ClassT>(ident->clone());
type->binding = parent;
};
......
......@@ -12,8 +12,11 @@ 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(string name, TypePtr type) : name(name), type(type), lineLocation(-1) {}
VarInfo(Ar* arg) : VarInfo(arg->pident_, arg->type_) {}
virtual ~VarInfo() {}
virtual string kind() const { return "variable"; }
string describe() const { return kind() + " " + name + " at line " + to_string(lineLocation); }
PIdent *ident;
string name;
......@@ -33,7 +36,10 @@ public:
weak_ptr<ClassInfo> klass;
weak_ptr<Binding> binding;
FunctionInfo(FuncDef *expr, ClassInfoPtr klass = nullptr);
FunctionInfo(string name, TypePtr type) : VarInfo(name, type) {};
// FunctionInfo(PIdent *ident, ClassInfoPtr klass = nullptr) : VarInfo(ident), block(NULL), klass(klass) {};
virtual string kind() const { return "function"; }
};
......@@ -56,7 +62,7 @@ public:
ClassInfo(PIdent *ident, BindingPtr parent = nullptr);
// ClassInfoPtr getParent() const { return dynamic_pointer_cast<ClassInfo>(parent); };
virtual string kind() const { return "class"; }
};
......
......@@ -70,7 +70,7 @@ int main(int argc, char ** argv)
TypeCheck checker;
checker.check(parse_tree);
} catch (ParseError const &e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << e.what() << std::endl;
return 1;
}
std::cout << "OK!" << endl;
......
......@@ -6,28 +6,17 @@ using namespace std;
ParseError::ParseError(string reason, int line) : runtime_error("ParseError"), line(line)
{
stringstream ss;
ss << "ParseError";
if (line != -1) {
ss << " at line " << line;
}
ss << ": " << reason;
msg = ss.str();
msg = reason + "\nAt line " + to_string(line);
}
ParseError::ParseError(string reason, Visitable *expr) : runtime_error("ParseError")
{
stringstream ss;
ss << "ParseError";
if (expr) {
line = expr->lineno;
ss << " at line " << line;
}
ss << ": " << reason;
if (expr) {
ss << " Expression: " << PrintAbsyn().print(expr);
}
msg = ss.str();
msg = reason + "\nIn expression at line " + to_string(line) + ": " + PrintAbsyn().print(expr);
}
ParseError::ParseError(string reason, VarInfo &var) : runtime_error("ParseError") {
msg = reason + "\nIn " + var.describe();
}
RedefinedError::RedefinedError(PIdent *ident, VarInfoPtr orig) {
......@@ -46,7 +35,7 @@ UndefinedError::UndefinedError(PIdent *ident) {
InvalidTypeError::InvalidTypeError(Type &expected, vector<shared_ptr<Type>> received, Visitable *expr) {
stringstream ss;
ss << "Invalid expression type at line " << expr->lineno << ". Expected \"" << expected.printName() << "\", instead received ";
ss << "Invalid expression type: Expected " << expected.printName() << ", instead received ";
bool fst = true;
for (auto i : received) {
if (fst) fst = false;
......@@ -55,17 +44,15 @@ InvalidTypeError::InvalidTypeError(Type &expected, vector<shared_ptr<Type>> rece
}
ss << ".";
if (expr) {
ss << " In: ";
PrintAbsyn p;
ss << p.print(expr);
line = expr->lineno;
ss << "\nIn expression at line " << line << ": " << PrintAbsyn().print(expr);
}
msg = ss.str();
line = expr->lineno;
}
InvalidTypeError::InvalidTypeError(Type &expected, vector<Type*> received, Visitable *expr) {
stringstream ss;
ss << "Invalid expression type at line " << expr->lineno << ". Expected \"" << expected.printName() << "\", instead received ";
ss << "Invalid expression type: Expected " << expected.printName() << ", instead received ";
bool fst = true;
for (auto i : received) {
if (fst) fst = false;
......@@ -74,9 +61,8 @@ InvalidTypeError::InvalidTypeError(Type &expected, vector<Type*> received, Visit
}
ss << ".";
if (expr) {
ss << " In: ";
PrintAbsyn p;
ss << p.print(expr);
line = expr->lineno;
ss << "\nIn expression at line " << line << ": " << PrintAbsyn().print(expr);
}
msg = ss.str();
line = expr->lineno;
......
......@@ -18,6 +18,8 @@ protected:
public:
ParseError(string reason, int line = -1);
ParseError(string reason, Visitable *expr);
ParseError(string reason, VarInfo &var);
ParseError(const ParseError &e, Visitable *expr) : ParseError(e.what(), expr) {};
virtual const char * what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW { return msg.data(); }
};
......
This diff is collapsed.
......@@ -4,6 +4,7 @@
#include "Absyn.h"
#include "Info.h"
#include "ParseError.h"
#include <memory>
......@@ -17,11 +18,44 @@ class TypeCheck : public Visitor
shared_ptr<Scope> scope;
shared_ptr<Type> lastType, returnType;
set<int> posv;
void checkFunction(FunctionInfoPtr f);
void checkReturnStatement(shared_ptr<Type> type, Stmt *stmt);
void checkFunctionRet();
void addIOFunctions(Type &type);
void setupEnv();
template<typename T>
shared_ptr<T> evalExpr(Visitable *expr) {
if (!expr) throw runtime_error("No expr to eval");
lastType = nullptr;
try {
expr->accept(this);
} catch (const ParseError &err) {
throw ParseError(err, expr);
}
shared_ptr<T> ret = dynamic_pointer_cast<T>(lastType);
if (!ret) {
T expect;
throw InvalidTypeError(expect, {lastType}, expr);
}
lastType = nullptr;
return ret;
};
shared_ptr<Type> evalExpr(Visitable *expr) {
if (!expr) throw runtime_error("No expr to eval");
try {
expr->accept(this);
} catch (const ParseError &err) {
throw ParseError(err, expr);
}
if (!lastType) throw ParseError("No expression type", expr);
auto ret = lastType;
lastType = nullptr;
return ret;
};
public:
BindingPtr getParentBinding() const { return scope->currentClass ? static_pointer_cast<Binding>(scope->currentClass) : static_pointer_cast<Binding>(scope); }
TypeCheck();
......
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