Commit 37fabd11 authored by zygzagZ's avatar zygzagZ

refactor evalExpr

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