Commit e9518825 authored by zygzagZ's avatar zygzagZ

InfoList to set

parent 2a6c6696
t.cpp
*.o *.o
*.l *.l
*.y *.y
......
...@@ -111,6 +111,11 @@ class ListType; ...@@ -111,6 +111,11 @@ class ListType;
class ListExpr; class ListExpr;
class PIdent; class PIdent;
class Binding;
class VarInfo;
class FunctionInfo;
class ClassInfo;
/******************** Visitor Interfaces ********************/ /******************** Visitor Interfaces ********************/
class Visitor class Visitor
...@@ -283,6 +288,8 @@ public: ...@@ -283,6 +288,8 @@ public:
class Block : public Visitable class Block : public Visitable
{ {
public: public:
std::weak_ptr<Binding> binding;
std::weak_ptr<FunctionInfo> function;
virtual Block *clone() const = 0; virtual Block *clone() const = 0;
}; };
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include "Absyn.h" #include "Absyn.h"
#include <memory> #include <memory>
#include <unordered_map> #include <set>
using namespace std; using namespace std;
class VarInfo; class VarInfo;
class FunctionInfo; class FunctionInfo;
...@@ -68,6 +68,7 @@ public: ...@@ -68,6 +68,7 @@ public:
InfoList<ClassInfo> classes; InfoList<ClassInfo> classes;
InfoList<FunctionInfo> functions; InfoList<FunctionInfo> functions;
ClassInfoPtr currentClass; ClassInfoPtr currentClass;
FunctionInfoPtr currentFunction;
Scope(); Scope();
}; };
......
#include "Info.h" #include "Info.h"
template<typename T>
struct Compare
{
using is_transparent = void;
bool operator() (const T& a, const T& b) const {
return a->name < b->name;
}
bool operator() (const T& a, const string &b) const {
return a->name < b;
}
bool operator() (const string &a, const T& b) const {
return a < b->name;
}
};
template<typename T, template<typename T,
typename = typename std::enable_if<std::is_base_of<VarInfo, T>::value>::type, typename = typename std::enable_if<std::is_base_of<VarInfo, T>::value>::type,
typename TPtr = shared_ptr<T>> typename TPtr = shared_ptr<T>>
class InfoList : public unordered_map<string, TPtr> { class InfoList : public set<TPtr, Compare<TPtr>> {
public: public:
InfoList<T> *parent; InfoList<T> *parent;
InfoList(InfoList<T> *parent = nullptr) : parent(parent) {}; InfoList(InfoList<T> *parent = nullptr) : parent(parent) {};
...@@ -11,15 +30,14 @@ public: ...@@ -11,15 +30,14 @@ public:
TPtr ret = local(name); TPtr ret = local(name);
return ret ? ret : parent ? (*parent)[name] : nullptr; return ret ? ret : parent ? (*parent)[name] : nullptr;
} }
TPtr local(string name) const { TPtr local(string name) const {
auto p = (*this).find(name); const auto p = this->find(name);
if (p != this->end()) { return p != this->end() ? *p : nullptr;
return p->second;
}
return nullptr;
} }
void add(TPtr obj) { void add(TPtr obj) {
this->emplace(obj->name, obj); this->emplace(obj);
} }
InfoList<T>& operator<<(TPtr obj) { InfoList<T>& operator<<(TPtr obj) {
......
...@@ -644,4 +644,19 @@ void TypeCheck::check(Visitable *v) ...@@ -644,4 +644,19 @@ void TypeCheck::check(Visitable *v)
} }
state = State::checkType; state = State::checkType;
v->accept(this); v->accept(this);
for (auto c : scope.classes) {
for (auto f : c->functions) {
}
}
}
void TypeCheck::checkFunction(FunctionInfoPtr f)
{
scope.currentFunction = f;
f->block->accept(this);
scope.currentFunction = nullptr;
} }
...@@ -17,6 +17,8 @@ class TypeCheck : public Visitor ...@@ -17,6 +17,8 @@ class TypeCheck : public Visitor
Scope scope; Scope scope;
shared_ptr<Type> lastType; shared_ptr<Type> lastType;
void checkFunction(FunctionInfoPtr f);
public: public:
TypeCheck(); TypeCheck();
void check(Visitable *v); void check(Visitable *v);
......
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