Commit 37c6faa0 authored by zygzagZ's avatar zygzagZ

Separate typedefs

parent e9518825
......@@ -1313,94 +1313,50 @@ ForEach *ForEach::clone() const
/******************** NoInit ********************/
NoInit::NoInit(PIdent *p1)
{
pident_ = p1;
}
NoInit::NoInit(const NoInit & other)
{
pident_ = other.pident_->clone();
}
NoInit &NoInit::operator=(const NoInit & other)
{
NoInit tmp(other);
swap(tmp);
return *this;
}
void NoInit::swap(NoInit & other)
{
std::swap(pident_, other.pident_);
}
NoInit::~NoInit()
{
delete(pident_);
}
void NoInit::accept(Visitor *v)
{
v->visitNoInit(this);
}
NoInit *NoInit::clone() const
{
return new NoInit(*this);
}
/******************** Init ********************/
Init::Init(PIdent *p1, Expr *p2)
Item::Item(PIdent *p1, Expr *p2)
{
pident_ = p1;
expr_ = p2;
}
Init::Init(const Init & other)
Item::Item(const Item & other)
{
pident_ = other.pident_->clone();
expr_ = other.expr_->clone();
}
Init &Init::operator=(const Init & other)
Item &Item::operator=(const Item & other)
{
Init tmp(other);
Item tmp(other);
swap(tmp);
return *this;
}
void Init::swap(Init & other)
void Item::swap(Item & other)
{
std::swap(pident_, other.pident_);
std::swap(expr_, other.expr_);
}
Init::~Init()
Item::~Item()
{
delete(pident_);
delete(expr_);
}
void Init::accept(Visitor *v)
void Item::accept(Visitor *v)
{
v->visitInit(this);
v->visitItem(this);
}
Init *Init::clone() const
Item *Item::clone() const
{
return new Init(*this);
return new Item(*this);
}
......
......@@ -20,101 +20,7 @@ typedef std::string Ident;
/******************** Forward Declarations ********************/
class Program;
class TopDef;
class FunDef;
class Arg;
class ClassDef;
class ClassBlock;
class ClassBlockDef;
class Block;
class Stmt;
class Item;
class Type;
class Expr;
class AddOp;
class MulOp;
class RelOp;
class Prog;
class FnDef;
class ClDef;
class FuncDef;
class Ar;
class ClassDefN;
class ClassDefE;
class ClassBl;
class ClassMthd;
class ClassFld;
class Blk;
class Empty;
class BStmt;
class Decl;
class NoInit;
class Init;
class Ass;
class TableAss;
class TableIncr;
class TableDecr;
class Incr;
class Decr;
class Ret;
class VRet;
class Cond;
class CondElse;
class While;
class SExp;
class ForEach;
class Int;
class Str;
class Bool;
class Void;
class Array;
class ClassT;
class Fun;
class EVar;
class ELitInt;
class ELitTrue;
class ELitFalse;
class EApp;
class EString;
class ENewArray;
class ENewClass;
class EClsMmbr;
class EClsMthd;
class Null;
class EIndexAcc;
class ECast;
class Neg;
class Not;
class EMul;
class EAdd;
class ERel;
class EAnd;
class EOr;
class Plus;
class Minus;
class Times;
class Div;
class Mod;
class LTH;
class LE;
class GTH;
class GE;
class EQU;
class NE;
class ListTopDef;
class ListArg;
class ListClassBlockDef;
class ListStmt;
class ListItem;
class ListType;
class ListExpr;
class PIdent;
class Binding;
class VarInfo;
class FunctionInfo;
class ClassInfo;
#include "TypeDefs.h"
/******************** Visitor Interfaces ********************/
......@@ -304,8 +210,16 @@ public:
class Item : public Visitable
{
public:
virtual Item *clone() const = 0;
PIdent *pident_;
Expr *expr_;
Item(const Item &);
Item &operator=(const Item &);
Item(PIdent *p1, Expr *p2 = nullptr);
~Item();
virtual void accept(Visitor *v);
virtual Item *clone() const;
void swap(Item &);
};
class Type : public Visitable
......@@ -363,6 +277,8 @@ public:
virtual void accept(Visitor *v);
virtual PIdent *clone() const;
void swap(PIdent &);
operator std::string() const { return string_; }
};
class Prog : public Program
......@@ -770,34 +686,6 @@ public:
void swap(ForEach &);
};
class NoInit : public Item
{
public:
PIdent *pident_;
NoInit(const NoInit &);
NoInit &operator=(const NoInit &);
NoInit(PIdent *p1);
~NoInit();
virtual void accept(Visitor *v);
virtual NoInit *clone() const;
void swap(NoInit &);
};
class Init : public Item
{
public:
PIdent *pident_;
Expr *expr_;
Init(const Init &);
Init &operator=(const Init &);
Init(PIdent *p1, Expr *p2);
~Init();
virtual void accept(Visitor *v);
virtual Init *clone() const;
void swap(Init &);
};
class Int : public Type
{
......
......@@ -10,16 +10,6 @@ FunctionInfo::FunctionInfo(FuncDef *expr, ClassInfoPtr klass)
}
ClassInfo::ClassInfo(PIdent *ident, ClassInfoPtr parent /*= nullptr*/)
: VarInfo(ident),
parent(parent),
functions(parent ? parent->functions : nullptr),
variables(parent ? parent->variables : nullptr)
{
};
Binding::Binding(shared_ptr<Binding> parent)
: variables(parent->variables),
parent(parent)
......@@ -28,6 +18,15 @@ Binding::Binding(shared_ptr<Binding> parent)
}
ClassInfo::ClassInfo(PIdent *ident, ClassInfoPtr parent /*= nullptr*/)
: VarInfo(ident),
Binding(parent),
functions(parent ? parent->functions : nullptr)
{
};
Scope::Scope()
: currentClass(nullptr)
{
......
......@@ -5,24 +5,15 @@
#include <memory>
#include <set>
using namespace std;
class VarInfo;
class FunctionInfo;
class ClassInfo;
class Binding;
class Type;
using VarInfoPtr = shared_ptr<VarInfo>;
using FunctionInfoPtr = shared_ptr<FunctionInfo>;
using ClassInfoPtr = shared_ptr<ClassInfo>;
using BindingPtr = shared_ptr<Binding>;
#include "InfoList.h"
class VarInfo {
public:
VarInfo(string n, int l = -1, Type* type = NULL) : name(n), type(type), lineLocation(l) {}
VarInfo(PIdent* ident, Type* type = NULL) : VarInfo(ident->string_, ident->integer_, type) {}
VarInfo(PIdent* ident, Type* type = NULL) : ident(ident), name(ident->string_), type(type), lineLocation(ident->integer_) {}
VarInfo(Ar* arg) : VarInfo(arg->pident_, arg->type_) {}
PIdent *ident;
string name;
Type *type;
int lineLocation;
......@@ -38,28 +29,30 @@ public:
Block * block;
vector<VarInfoPtr> arguments;
weak_ptr<ClassInfo> klass;
weak_ptr<Binding> binding;
FunctionInfo(FuncDef *expr, ClassInfoPtr klass = nullptr);
FunctionInfo(PIdent *ident, ClassInfoPtr klass = nullptr) : VarInfo(ident), block(NULL), klass(klass) {};
};
class ClassInfo : public VarInfo
{
ClassInfoPtr parent;
class Binding {
public:
InfoList<FunctionInfo> functions;
InfoList<VarInfo> variables;
BindingPtr parent;
Binding(BindingPtr parent);
ClassInfo(PIdent *ident, ClassInfoPtr parent = nullptr);
ClassInfoPtr getParent() const { return parent; };
BindingPtr getParent() const { return parent; };
};
class ClassInfo : public VarInfo, public Binding
{
public:
InfoList<FunctionInfo> functions;
class Binding {
InfoList<VarInfo> variables;
BindingPtr parent;
Binding(BindingPtr parent);
ClassInfo(PIdent *ident, ClassInfoPtr parent = nullptr);
ClassInfoPtr getParent() const { return static_pointer_cast<ClassInfo>(parent); };
};
......@@ -67,6 +60,7 @@ class Scope {
public:
InfoList<ClassInfo> classes;
InfoList<FunctionInfo> functions;
BindingPtr currentBinding;
ClassInfoPtr currentClass;
FunctionInfoPtr currentFunction;
Scope();
......
#ifndef INFOLIST_HEADER
#define INFOLIST_HEADER
#include "Info.h"
#include "ParseError.h"
#include "TypeDefs.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;
}
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; }
// bool operator() (const T& a, const PIdent *b) const { return a->name < b->string_; }
// bool operator() (const PIdent *a, const T& b) const { return a->string_ < b->name; }
};
......@@ -26,9 +24,11 @@ class InfoList : public set<TPtr, Compare<TPtr>> {
public:
InfoList<T> *parent;
InfoList(InfoList<T> *parent = nullptr) : parent(parent) {};
TPtr operator[](string name) const {
TPtr ret = local(name);
return ret ? ret : parent ? (*parent)[name] : nullptr;
TPtr operator[](PIdent *ident) const {
TPtr ret = local(ident->string_);
if (ret) return ret;
if (parent) return (*parent)[ident];
throw UndefinedError(ident);
}
TPtr local(string name) const {
......@@ -37,7 +37,10 @@ public:
}
void add(TPtr obj) {
this->emplace(obj);
auto ret = this->emplace(obj);
if (!ret.second) {
throw RedefinedError(obj->ident, *ret.first);
}
}
InfoList<T>& operator<<(TPtr obj) {
......@@ -45,3 +48,5 @@ public:
return *this;
}
};
#endif
CC=g++
CCFLAGS=-g -W -Wall -O3 -std=c++2a -Wno-unused-parameter
CCFLAGS=-g -W -Wall -O0 -std=c++2a -Wno-unused-parameter
FLEX=flex
FLEX_OPTS=-PGrammar
......
#include "ParseError.h"
#include "Info.h"
#include "Printer.h"
#include <sstream>
......
#ifndef ERROR_HEADER
#define ERROR_HEADER
#include "Absyn.h"
#include "Info.h"
#ifndef PARSEERROR_HEADER
#define PARSEERROR_HEADER
#include "TypeDefs.h"
#include <string>
#include <stdexcept>
#include <memory>
#include <vector>
using namespace std;
// class VarInfo;
// using VarInfoPtr = shared_ptr<VarInfo>;
class ParseError : std::runtime_error {
protected:
string msg;
......
......@@ -8,19 +8,14 @@ void TypeCheck::visitFunDef(FunDef *t) {} //abstract class
void TypeCheck::visitArg(Arg *t) {} //abstract class
void TypeCheck::visitClassDef(ClassDef *t) {
const string name = t->getName()->string_;
ClassInfoPtr c = scope.classes[name];
if (c) {
throw RedefinedError(t->getName(), c);
}
ClassInfoPtr parent = NULL;
if (t->getParent()) {
const string parentName = t->getParent()->string_;
parent = scope.classes[parentName];
parent = scope.classes[t->getParent()];
if (!parent) {
throw UndefinedError(t->getParent());
}
}
c = make_shared<ClassInfo>(t->getName(), parent);
auto c = make_shared<ClassInfo>(t->getName(), parent);
scope.classes << c;
scope.currentClass = c;
......@@ -28,9 +23,6 @@ void TypeCheck::visitClassDef(ClassDef *t) {
scope.currentClass = NULL;
}
void TypeCheck::visitClassBlock(ClassBlock *t) {} //abstract class
void TypeCheck::visitClassBlockDef(ClassBlockDef *t) {} //abstract class
void TypeCheck::visitBlock(Block *t) {} //abstract class
void TypeCheck::visitStmt(Stmt *t) {} //abstract class
void TypeCheck::visitItem(Item *t) {} //abstract class
void TypeCheck::visitType(Type *t) {} //abstract class
......@@ -41,11 +33,8 @@ void TypeCheck::visitRelOp(RelOp *t) {} //abstract class
void TypeCheck::visitPIdent(PIdent *p_ident)
{
/* Code For PIdent Goes Here */
visitString(p_ident->string_);
visitInteger(p_ident->integer_);
}
void TypeCheck::visitProg(Prog *prog)
......@@ -74,13 +63,7 @@ void TypeCheck::visitClDef(ClDef *cl_def)
void TypeCheck::visitFuncDef(FuncDef *def)
{
const string name = def->pident_->string_;
FunctionInfoPtr f = scope.functions[name];
if (f) {
throw RedefinedError(def->pident_, f);
}
f = make_shared<FunctionInfo>(def->pident_, scope.currentClass);
FunctionInfoPtr f = make_shared<FunctionInfo>(def->pident_, scope.currentClass);
f->block = def->block_;
auto &targetVector = scope.currentClass ? scope.currentClass->functions : scope.functions;
......@@ -133,34 +116,33 @@ void TypeCheck::visitClassFld(ClassFld *class_fld)
void TypeCheck::visitBlk(Blk *blk)
{
/* Code For Blk Goes Here */
blk->liststmt_->accept(this);
}
BindingPtr binding = blk->binding.lock();
if (!binding) {
binding = make_shared<Binding>(scope.currentBinding);
blk->binding = binding;
}
void TypeCheck::visitEmpty(Empty *empty)
{
/* Code For Empty Goes Here */
scope.currentBinding = binding;
blk->liststmt_->accept(this);
scope.currentBinding = binding->getParent();
}
void TypeCheck::visitBStmt(BStmt *b_stmt)
{
/* Code For BStmt Goes Here */
b_stmt->block_->accept(this);
}
void TypeCheck::visitDecl(Decl *decl)
{
/* Code For Decl Goes Here */
decl->type_->accept(this);
decl->listitem_->accept(this);
for (auto el : *decl->listitem_) {
el->pident_->accept(this);
el->expr_->accept(this);
}
}
void TypeCheck::visitAss(Ass *ass)
......@@ -278,23 +260,6 @@ void TypeCheck::visitForEach(ForEach *for_each)
}
void TypeCheck::visitNoInit(NoInit *no_init)
{
/* Code For NoInit Goes Here */
no_init->pident_->accept(this);
}
void TypeCheck::visitInit(Init *init)
{
/* Code For Init Goes Here */
init->pident_->accept(this);
init->expr_->accept(this);
}
void TypeCheck::visitInt(Int *int_)
{
/* Code For Int Goes Here */
......@@ -606,37 +571,16 @@ void TypeCheck::visitInteger(Integer x)
/* Code for Integer Goes Here */
}
void TypeCheck::visitChar(Char x)
{
/* Code for Char Goes Here */
}
void TypeCheck::visitDouble(Double x)
{
/* Code for Double Goes Here */
}
void TypeCheck::visitString(String x)
{
/* Code for String Goes Here */
}
void TypeCheck::visitIdent(Ident x)
{
/* Code for Ident Goes Here */
}
TypeCheck::TypeCheck()
: state(initialized)
{
}
void TypeCheck::check(Visitable *v)
{
if (state != State::initialized) {
......@@ -646,9 +590,13 @@ void TypeCheck::check(Visitable *v)
v->accept(this);
for (auto c : scope.classes) {
for (auto f : c->functions) {
scope.currentClass = c;
for (auto f : c->functions) {
checkFunction(f);
}
scope.currentClass = nullptr;
}
}
......@@ -656,6 +604,13 @@ void TypeCheck::checkFunction(FunctionInfoPtr f)
{
scope.currentFunction = f;
BindingPtr binding = make_shared<Binding>(nullptr);
f->binding = binding;
for (auto arg : f->arguments) {
binding->variables << arg;
}
f->block->accept(this);
scope.currentFunction = nullptr;
......
......@@ -28,11 +28,7 @@ protected:
void visitFunDef(FunDef *p);
void visitArg(Arg *p);
void visitClassDef(ClassDef *p);
void visitClassBlock(ClassBlock *p);
void visitClassBlockDef(ClassBlockDef *p);
void visitBlock(Block *p);
void visitStmt(Stmt *p);
void visitItem(Item *p);
void visitType(Type *p);
void visitExpr(Expr *p);
void visitAddOp(AddOp *p);
......@@ -49,11 +45,8 @@ protected:
void visitClassMthd(ClassMthd *p);
void visitClassFld(ClassFld *p);
void visitBlk(Blk *p);
void visitEmpty(Empty *p);
void visitBStmt(BStmt *p);
void visitDecl(Decl *p);
void visitNoInit(NoInit *p);
void visitInit(Init *p);
void visitAss(Ass *p);
void visitTableAss(TableAss *p);
void visitTableIncr(TableIncr *p);
......@@ -94,6 +87,33 @@ protected:
void visitERel(ERel *p);
void visitEAnd(EAnd *p);
void visitEOr(EOr *p);
void visitListTopDef(ListTopDef *p);
void visitListArg(ListArg *p);
void visitListClassBlockDef(ListClassBlockDef *p);
void visitListStmt(ListStmt *p);
void visitListItem(ListItem *p);
void visitListType(ListType *p);
void visitListExpr(ListExpr *p);
void visitPIdent(PIdent *p);
void visitInteger(Integer x);
void visitChar(Char x) {};
void visitString(String x);
// abstract
// topdefs
void visitBlock(Block *t) {};
void visitClassBlock(ClassBlock *p) {};
void visitClassBlockDef(ClassBlockDef *p) {};
void visitItem(Item *p);
// stmts
void visitEmpty(Empty *p) {};
void visitNoInit(NoInit *p) {};
void visitInit(Init *p) {};
// exprs
void visitPlus(Plus *p) {};
void visitMinus(Minus *p) {};
void visitTimes(Times *p) {};
......@@ -105,20 +125,10 @@ protected:
void visitGE(GE *p) {};
void visitEQU(EQU *p) {};
void visitNE(NE *p) {};
void visitListTopDef(ListTopDef *p);
void visitListArg(ListArg *p);
void visitListClassBlockDef(ListClassBlockDef *p);
void visitListStmt(ListStmt *p);
void visitListItem(ListItem *p);
void visitListType(ListType *p);
void visitListExpr(ListExpr *p);
void visitPIdent(PIdent *p);
void visitInteger(Integer x);
void visitChar(Char x);
void visitDouble(Double x);
void visitString(String x);
void visitIdent(Ident x);
// terminals
void visitDouble(Double x) {};
void visitIdent(Ident x) {};
};
#endif
#ifndef TYPEDEFS_HEADER
#define TYPEDEFS_HEADER
#include <memory>
using namespace std;
class VarInfo;
class FunctionInfo;
class ClassInfo;
class Binding;
class Type;
using VarInfoPtr = shared_ptr<VarInfo>;
using FunctionInfoPtr = shared_ptr<FunctionInfo>;
using ClassInfoPtr = shared_ptr<ClassInfo>;
using BindingPtr = shared_ptr<Binding>;
class Program;
class TopDef;
class FunDef;
class Arg;
class ClassDef;
class ClassBlock;
class ClassBlockDef;
class Block;
class Stmt;
class Item;
class Type;
class Expr;
class AddOp;
class MulOp;
class RelOp;
class Prog;
class FnDef;
class ClDef;
class FuncDef;
class Ar;
class ClassDefN;
class ClassDefE;
class ClassBl;
class ClassMthd;
class ClassFld;
class Blk;
class Empty;
class BStmt;
class Decl;
using NoInit = Item;
using Init = Item;
class Ass;
class TableAss;
class TableIncr;
class TableDecr;
class Incr;
class Decr;
class Ret;
class VRet;
class Cond;
class CondElse;
class While;
class SExp;
class ForEach;
class Int;
class Str;
class Bool;
class Void;
class Array;
class ClassT;
class Fun;
class EVar;
class ELitInt;
class ELitTrue;
class ELitFalse;
class EApp;
class EString;
class ENewArray;
class ENewClass;
class EClsMmbr;
class EClsMthd;
class Null;
class EIndexAcc;
class ECast;
class Neg;
class Not;
class EMul;
class EAdd;
class ERel;
class EAnd;
class EOr;
class Plus;
class Minus;
class Times;
class Div;
class Mod;
class LTH;
class LE;
class GTH;
class GE;
class EQU;
class NE;
class ListTopDef;
class ListArg;
class ListClassBlockDef;
class ListStmt;
class ListItem;
class ListType;
class ListExpr;
class PIdent;
class Binding;
class VarInfo;
class FunctionInfo;
class ClassInfo;
#endif
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