Commit e9518825 authored by zygzagZ's avatar zygzagZ

InfoList to set

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