Commit 0df5b04c authored by zygzagZ's avatar zygzagZ

Extend abstract ClassDef, add ParseError, WIP TypeCheck

parent 1f3906a4
...@@ -258,6 +258,9 @@ public: ...@@ -258,6 +258,9 @@ public:
class ClassDef : public Visitable class ClassDef : public Visitable
{ {
public: public:
virtual PIdent *getName() const = 0;
virtual PIdent *getParent() const = 0;
virtual ClassBlock *getBlock() const = 0;
virtual ClassDef *clone() const = 0; virtual ClassDef *clone() const = 0;
}; };
...@@ -436,6 +439,10 @@ public: ...@@ -436,6 +439,10 @@ public:
virtual void accept(Visitor *v); virtual void accept(Visitor *v);
virtual ClassDefN *clone() const; virtual ClassDefN *clone() const;
void swap(ClassDefN &); void swap(ClassDefN &);
virtual PIdent *getName() const { return pident_; };
virtual PIdent *getParent() const { return NULL; };
virtual ClassBlock *getBlock() const { return classblock_; };
}; };
class ClassDefE : public ClassDef class ClassDefE : public ClassDef
...@@ -452,6 +459,10 @@ public: ...@@ -452,6 +459,10 @@ public:
virtual void accept(Visitor *v); virtual void accept(Visitor *v);
virtual ClassDefE *clone() const; virtual ClassDefE *clone() const;
void swap(ClassDefE &); void swap(ClassDefE &);
virtual PIdent *getName() const { return pident_1; };
virtual PIdent *getParent() const { return pident_2; };
virtual ClassBlock *getBlock() const { return classblock_; };
}; };
class ClassBl : public ClassBlock class ClassBl : public ClassBlock
......
#ifndef INFO_HEADER
#define INFO_HEADER
#include "Absyn.h"
using namespace std;
class VarInfo {
string name;
string type;
bool operator<(const VarInfo &other) const {
return name != other.name ? name < other.name : type < other.type;
}
}
class FunctionInfo : VarInfo {
vector<VarInfo> arguments;
};
class ClassInfo : VarInfo
{
ClassInfo * parent;
public:
ClassInfo * getParent() const { return parent; };
vector<FunctionInfo> functions;
vector<VarInfo> variables;
};
vector<ClassInfo> classes;
vector<FunctionInfo> functions;
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "Printer.h" #include "Printer.h"
#include "Absyn.h" #include "Absyn.h"
// #include "Compiler.h" // #include "Compiler.h"
#include "ParseError.h"
#include "TypeCheck.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
...@@ -63,6 +65,12 @@ int main(int argc, char ** argv) ...@@ -63,6 +65,12 @@ int main(int argc, char ** argv)
binaryPath = argv[0]; binaryPath = argv[0];
std::filesystem::path file(filename ? filename : "Instant"); std::filesystem::path file(filename ? filename : "Instant");
TypeCheck checker;
try {
checker.check(parse_tree);
} catch (ParseError &e) {
std::cerr << e.what() << std::endl;
}
/*Compiler c(file); /*Compiler c(file);
std::string out = c.compile(parse_tree); std::string out = c.compile(parse_tree);
......
CC=g++ CC=g++
CCFLAGS=-g -W -Wall -O3 -std=c++1z CCFLAGS=-g -W -Wall -O3 -std=c++1z -Wno-unused-parameter
FLEX=flex FLEX=flex
FLEX_OPTS=-PGrammar FLEX_OPTS=-PGrammar
...@@ -7,7 +7,7 @@ FLEX_OPTS=-PGrammar ...@@ -7,7 +7,7 @@ FLEX_OPTS=-PGrammar
BISON=bison BISON=bison
BISON_OPTS=-t -pGrammar BISON_OPTS=-t -pGrammar
OBJS=Absyn.o Lexer.o Parser.o Printer.o OBJS=Absyn.o Lexer.o Parser.o Printer.o TypeCheck.o
.PHONY : clean distclean .PHONY : clean distclean
...@@ -44,5 +44,8 @@ Printer.o : Printer.cpp Printer.h Absyn.h ...@@ -44,5 +44,8 @@ Printer.o : Printer.cpp Printer.h Absyn.h
Skeleton.o : Skeleton.cpp Skeleton.h Absyn.h Skeleton.o : Skeleton.cpp Skeleton.h Absyn.h
${CC} ${CCFLAGS} -Wno-unused-parameter -c Skeleton.cpp ${CC} ${CCFLAGS} -Wno-unused-parameter -c Skeleton.cpp
Latte.o : Latte.cpp Parser.h Printer.h Absyn.h Latte.o : Latte.cpp Parser.h Printer.h Absyn.h ParseError.h
${CC} ${CCFLAGS} -c Latte.cpp ${CC} ${CCFLAGS} -c Latte.cpp
TypeCheck.o : TypeCheck.cpp TypeCheck.h Absyn.h ParseError.h
${CC} ${CCFLAGS} -c TypeCheck.cpp
#ifndef ERROR_HEADER
#define ERROR_HEADER
#include <string>
using namespace std;
class ParseError : std::exception {
string msg;
public:
ParseError(string reason) : msg(reason) {}
const char * what() { return msg.data(); }
};
#endif
\ No newline at end of file
/*** BNFC-Generated Visitor Design Pattern Skeleton. ***/
/* This implements the common visitor design pattern.
Note that this method uses Visitor-traversal of lists, so
List->accept() does NOT traverse the list. This allows different
algorithms to use context information differently. */
#include "TypeCheck.h" #include "TypeCheck.h"
#include <stdexcept>
void TypeCheck::visitProgram(Program *t) {} //abstract class
void Skeleton::visitProgram(Program *t) {} //abstract class void TypeCheck::visitTopDef(TopDef *t) {} //abstract class
void Skeleton::visitTopDef(TopDef *t) {} //abstract class void TypeCheck::visitFunDef(FunDef *t) {} //abstract class
void Skeleton::visitFunDef(FunDef *t) {} //abstract class void TypeCheck::visitArg(Arg *t) {} //abstract class
void Skeleton::visitArg(Arg *t) {} //abstract class void TypeCheck::visitClassDef(ClassDef *t) {
void Skeleton::visitClassDef(ClassDef *t) {} //abstract class t->getName()->accept(this);
void Skeleton::visitClassBlock(ClassBlock *t) {} //abstract class if (t->getParent())
void Skeleton::visitClassBlockDef(ClassBlockDef *t) {} //abstract class t->getParent()->accept(this);
void Skeleton::visitBlock(Block *t) {} //abstract class t->getBlock()->accept(this);
void Skeleton::visitStmt(Stmt *t) {} //abstract class }
void Skeleton::visitItem(Item *t) {} //abstract class
void Skeleton::visitType(Type *t) {} //abstract class void TypeCheck::visitClassBlock(ClassBlock *t) {} //abstract class
void Skeleton::visitExpr(Expr *t) {} //abstract class void TypeCheck::visitClassBlockDef(ClassBlockDef *t) {} //abstract class
void Skeleton::visitAddOp(AddOp *t) {} //abstract class void TypeCheck::visitBlock(Block *t) {} //abstract class
void Skeleton::visitMulOp(MulOp *t) {} //abstract class void TypeCheck::visitStmt(Stmt *t) {} //abstract class
void Skeleton::visitRelOp(RelOp *t) {} //abstract class void TypeCheck::visitItem(Item *t) {} //abstract class
void TypeCheck::visitType(Type *t) {} //abstract class
void Skeleton::visitPIdent(PIdent *p_ident) void TypeCheck::visitExpr(Expr *t) {} //abstract class
void TypeCheck::visitAddOp(AddOp *t) {} //abstract class
void TypeCheck::visitMulOp(MulOp *t) {} //abstract class
void TypeCheck::visitRelOp(RelOp *t) {} //abstract class
void TypeCheck::visitPIdent(PIdent *p_ident)
{ {
/* Code For PIdent Goes Here */ /* Code For PIdent Goes Here */
...@@ -33,7 +32,7 @@ void Skeleton::visitPIdent(PIdent *p_ident) ...@@ -33,7 +32,7 @@ void Skeleton::visitPIdent(PIdent *p_ident)
} }
void Skeleton::visitProg(Prog *prog) void TypeCheck::visitProg(Prog *prog)
{ {
/* Code For Prog Goes Here */ /* Code For Prog Goes Here */
...@@ -41,7 +40,7 @@ void Skeleton::visitProg(Prog *prog) ...@@ -41,7 +40,7 @@ void Skeleton::visitProg(Prog *prog)
} }
void Skeleton::visitFnDef(FnDef *fn_def) void TypeCheck::visitFnDef(FnDef *fn_def)
{ {
/* Code For FnDef Goes Here */ /* Code For FnDef Goes Here */
...@@ -49,7 +48,7 @@ void Skeleton::visitFnDef(FnDef *fn_def) ...@@ -49,7 +48,7 @@ void Skeleton::visitFnDef(FnDef *fn_def)
} }
void Skeleton::visitClDef(ClDef *cl_def) void TypeCheck::visitClDef(ClDef *cl_def)
{ {
/* Code For ClDef Goes Here */ /* Code For ClDef Goes Here */
...@@ -57,7 +56,7 @@ void Skeleton::visitClDef(ClDef *cl_def) ...@@ -57,7 +56,7 @@ void Skeleton::visitClDef(ClDef *cl_def)
} }
void Skeleton::visitFuncDef(FuncDef *func_def) void TypeCheck::visitFuncDef(FuncDef *func_def)
{ {
/* Code For FuncDef Goes Here */ /* Code For FuncDef Goes Here */
...@@ -68,7 +67,7 @@ void Skeleton::visitFuncDef(FuncDef *func_def) ...@@ -68,7 +67,7 @@ void Skeleton::visitFuncDef(FuncDef *func_def)
} }
void Skeleton::visitAr(Ar *ar) void TypeCheck::visitAr(Ar *ar)
{ {
/* Code For Ar Goes Here */ /* Code For Ar Goes Here */
...@@ -77,26 +76,17 @@ void Skeleton::visitAr(Ar *ar) ...@@ -77,26 +76,17 @@ void Skeleton::visitAr(Ar *ar)
} }
void Skeleton::visitClassDefN(ClassDefN *class_def_n) void TypeCheck::visitClassDefN(ClassDefN *class_def_n)
{ {
/* Code For ClassDefN Goes Here */ visitClassDef(class_def_n);
class_def_n->pident_->accept(this);
class_def_n->classblock_->accept(this);
} }
void Skeleton::visitClassDefE(ClassDefE *class_def_e) void TypeCheck::visitClassDefE(ClassDefE *class_def_e)
{ {
/* Code For ClassDefE Goes Here */ visitClassDef(class_def_e);
class_def_e->pident_1->accept(this);
class_def_e->pident_2->accept(this);
class_def_e->classblock_->accept(this);
} }
void Skeleton::visitClassBl(ClassBl *class_bl) void TypeCheck::visitClassBl(ClassBl *class_bl)
{ {
/* Code For ClassBl Goes Here */ /* Code For ClassBl Goes Here */
...@@ -104,7 +94,7 @@ void Skeleton::visitClassBl(ClassBl *class_bl) ...@@ -104,7 +94,7 @@ void Skeleton::visitClassBl(ClassBl *class_bl)
} }
void Skeleton::visitClassMthd(ClassMthd *class_mthd) void TypeCheck::visitClassMthd(ClassMthd *class_mthd)
{ {
/* Code For ClassMthd Goes Here */ /* Code For ClassMthd Goes Here */
...@@ -112,7 +102,7 @@ void Skeleton::visitClassMthd(ClassMthd *class_mthd) ...@@ -112,7 +102,7 @@ void Skeleton::visitClassMthd(ClassMthd *class_mthd)
} }
void Skeleton::visitClassFld(ClassFld *class_fld) void TypeCheck::visitClassFld(ClassFld *class_fld)
{ {
/* Code For ClassFld Goes Here */ /* Code For ClassFld Goes Here */
...@@ -121,7 +111,7 @@ void Skeleton::visitClassFld(ClassFld *class_fld) ...@@ -121,7 +111,7 @@ void Skeleton::visitClassFld(ClassFld *class_fld)
} }
void Skeleton::visitBlk(Blk *blk) void TypeCheck::visitBlk(Blk *blk)
{ {
/* Code For Blk Goes Here */ /* Code For Blk Goes Here */
...@@ -129,14 +119,14 @@ void Skeleton::visitBlk(Blk *blk) ...@@ -129,14 +119,14 @@ void Skeleton::visitBlk(Blk *blk)
} }
void Skeleton::visitEmpty(Empty *empty) void TypeCheck::visitEmpty(Empty *empty)
{ {
/* Code For Empty Goes Here */ /* Code For Empty Goes Here */
} }
void Skeleton::visitBStmt(BStmt *b_stmt) void TypeCheck::visitBStmt(BStmt *b_stmt)
{ {
/* Code For BStmt Goes Here */ /* Code For BStmt Goes Here */
...@@ -144,7 +134,7 @@ void Skeleton::visitBStmt(BStmt *b_stmt) ...@@ -144,7 +134,7 @@ void Skeleton::visitBStmt(BStmt *b_stmt)
} }
void Skeleton::visitDecl(Decl *decl) void TypeCheck::visitDecl(Decl *decl)
{ {
/* Code For Decl Goes Here */ /* Code For Decl Goes Here */
...@@ -153,7 +143,7 @@ void Skeleton::visitDecl(Decl *decl) ...@@ -153,7 +143,7 @@ void Skeleton::visitDecl(Decl *decl)
} }
void Skeleton::visitAss(Ass *ass) void TypeCheck::visitAss(Ass *ass)
{ {
/* Code For Ass Goes Here */ /* Code For Ass Goes Here */
...@@ -162,7 +152,7 @@ void Skeleton::visitAss(Ass *ass) ...@@ -162,7 +152,7 @@ void Skeleton::visitAss(Ass *ass)
} }
void Skeleton::visitTableAss(TableAss *table_ass) void TypeCheck::visitTableAss(TableAss *table_ass)
{ {
/* Code For TableAss Goes Here */ /* Code For TableAss Goes Here */
...@@ -172,7 +162,7 @@ void Skeleton::visitTableAss(TableAss *table_ass) ...@@ -172,7 +162,7 @@ void Skeleton::visitTableAss(TableAss *table_ass)
} }
void Skeleton::visitTableIncr(TableIncr *table_incr) void TypeCheck::visitTableIncr(TableIncr *table_incr)
{ {
/* Code For TableIncr Goes Here */ /* Code For TableIncr Goes Here */
...@@ -181,7 +171,7 @@ void Skeleton::visitTableIncr(TableIncr *table_incr) ...@@ -181,7 +171,7 @@ void Skeleton::visitTableIncr(TableIncr *table_incr)
} }
void Skeleton::visitTableDecr(TableDecr *table_decr) void TypeCheck::visitTableDecr(TableDecr *table_decr)
{ {
/* Code For TableDecr Goes Here */ /* Code For TableDecr Goes Here */
...@@ -190,7 +180,7 @@ void Skeleton::visitTableDecr(TableDecr *table_decr) ...@@ -190,7 +180,7 @@ void Skeleton::visitTableDecr(TableDecr *table_decr)
} }
void Skeleton::visitIncr(Incr *incr) void TypeCheck::visitIncr(Incr *incr)
{ {
/* Code For Incr Goes Here */ /* Code For Incr Goes Here */
...@@ -198,7 +188,7 @@ void Skeleton::visitIncr(Incr *incr) ...@@ -198,7 +188,7 @@ void Skeleton::visitIncr(Incr *incr)
} }
void Skeleton::visitDecr(Decr *decr) void TypeCheck::visitDecr(Decr *decr)
{ {
/* Code For Decr Goes Here */ /* Code For Decr Goes Here */
...@@ -206,7 +196,7 @@ void Skeleton::visitDecr(Decr *decr) ...@@ -206,7 +196,7 @@ void Skeleton::visitDecr(Decr *decr)
} }
void Skeleton::visitRet(Ret *ret) void TypeCheck::visitRet(Ret *ret)
{ {
/* Code For Ret Goes Here */ /* Code For Ret Goes Here */
...@@ -214,14 +204,14 @@ void Skeleton::visitRet(Ret *ret) ...@@ -214,14 +204,14 @@ void Skeleton::visitRet(Ret *ret)
} }
void Skeleton::visitVRet(VRet *v_ret) void TypeCheck::visitVRet(VRet *v_ret)
{ {
/* Code For VRet Goes Here */ /* Code For VRet Goes Here */
} }
void Skeleton::visitCond(Cond *cond) void TypeCheck::visitCond(Cond *cond)
{ {
/* Code For Cond Goes Here */ /* Code For Cond Goes Here */
...@@ -230,7 +220,7 @@ void Skeleton::visitCond(Cond *cond) ...@@ -230,7 +220,7 @@ void Skeleton::visitCond(Cond *cond)
} }
void Skeleton::visitCondElse(CondElse *cond_else) void TypeCheck::visitCondElse(CondElse *cond_else)
{ {
/* Code For CondElse Goes Here */ /* Code For CondElse Goes Here */
...@@ -240,7 +230,7 @@ void Skeleton::visitCondElse(CondElse *cond_else) ...@@ -240,7 +230,7 @@ void Skeleton::visitCondElse(CondElse *cond_else)
} }
void Skeleton::visitWhile(While *while_) void TypeCheck::visitWhile(While *while_)
{ {
/* Code For While Goes Here */ /* Code For While Goes Here */
...@@ -249,7 +239,7 @@ void Skeleton::visitWhile(While *while_) ...@@ -249,7 +239,7 @@ void Skeleton::visitWhile(While *while_)
} }
void Skeleton::visitSExp(SExp *s_exp) void TypeCheck::visitSExp(SExp *s_exp)
{ {
/* Code For SExp Goes Here */ /* Code For SExp Goes Here */
...@@ -257,7 +247,7 @@ void Skeleton::visitSExp(SExp *s_exp) ...@@ -257,7 +247,7 @@ void Skeleton::visitSExp(SExp *s_exp)
} }
void Skeleton::visitForEach(ForEach *for_each) void TypeCheck::visitForEach(ForEach *for_each)
{ {
/* Code For ForEach Goes Here */ /* Code For ForEach Goes Here */
...@@ -268,7 +258,7 @@ void Skeleton::visitForEach(ForEach *for_each) ...@@ -268,7 +258,7 @@ void Skeleton::visitForEach(ForEach *for_each)
} }
void Skeleton::visitNoInit(NoInit *no_init) void TypeCheck::visitNoInit(NoInit *no_init)
{ {
/* Code For NoInit Goes Here */ /* Code For NoInit Goes Here */
...@@ -276,7 +266,7 @@ void Skeleton::visitNoInit(NoInit *no_init) ...@@ -276,7 +266,7 @@ void Skeleton::visitNoInit(NoInit *no_init)
} }
void Skeleton::visitInit(Init *init) void TypeCheck::visitInit(Init *init)
{ {
/* Code For Init Goes Here */ /* Code For Init Goes Here */
...@@ -285,35 +275,35 @@ void Skeleton::visitInit(Init *init) ...@@ -285,35 +275,35 @@ void Skeleton::visitInit(Init *init)
} }
void Skeleton::visitInt(Int *int_) void TypeCheck::visitInt(Int *int_)
{ {
/* Code For Int Goes Here */ /* Code For Int Goes Here */
} }
void Skeleton::visitStr(Str *str) void TypeCheck::visitStr(Str *str)
{ {
/* Code For Str Goes Here */ /* Code For Str Goes Here */
} }
void Skeleton::visitBool(Bool *bool_) void TypeCheck::visitBool(Bool *bool_)
{ {
/* Code For Bool Goes Here */ /* Code For Bool Goes Here */
} }
void Skeleton::visitVoid(Void *void_) void TypeCheck::visitVoid(Void *void_)
{ {
/* Code For Void Goes Here */ /* Code For Void Goes Here */
} }
void Skeleton::visitArray(Array *array) void TypeCheck::visitArray(Array *array)
{ {
/* Code For Array Goes Here */ /* Code For Array Goes Here */
...@@ -321,7 +311,7 @@ void Skeleton::visitArray(Array *array) ...@@ -321,7 +311,7 @@ void Skeleton::visitArray(Array *array)
} }
void Skeleton::visitClassT(ClassT *class_t) void TypeCheck::visitClassT(ClassT *class_t)
{ {
/* Code For ClassT Goes Here */ /* Code For ClassT Goes Here */
...@@ -329,7 +319,7 @@ void Skeleton::visitClassT(ClassT *class_t) ...@@ -329,7 +319,7 @@ void Skeleton::visitClassT(ClassT *class_t)
} }
void Skeleton::visitFun(Fun *fun) void TypeCheck::visitFun(Fun *fun)
{ {
/* Code For Fun Goes Here */ /* Code For Fun Goes Here */
...@@ -338,7 +328,7 @@ void Skeleton::visitFun(Fun *fun) ...@@ -338,7 +328,7 @@ void Skeleton::visitFun(Fun *fun)
} }
void Skeleton::visitEVar(EVar *e_var) void TypeCheck::visitEVar(EVar *e_var)
{ {
/* Code For EVar Goes Here */ /* Code For EVar Goes Here */
...@@ -346,7 +336,7 @@ void Skeleton::visitEVar(EVar *e_var) ...@@ -346,7 +336,7 @@ void Skeleton::visitEVar(EVar *e_var)
} }
void Skeleton::visitELitInt(ELitInt *e_lit_int) void TypeCheck::visitELitInt(ELitInt *e_lit_int)
{ {
/* Code For ELitInt Goes Here */ /* Code For ELitInt Goes Here */
...@@ -354,21 +344,21 @@ void Skeleton::visitELitInt(ELitInt *e_lit_int) ...@@ -354,21 +344,21 @@ void Skeleton::visitELitInt(ELitInt *e_lit_int)
} }
void Skeleton::visitELitTrue(ELitTrue *e_lit_true) void TypeCheck::visitELitTrue(ELitTrue *e_lit_true)
{ {
/* Code For ELitTrue Goes Here */ /* Code For ELitTrue Goes Here */
} }
void Skeleton::visitELitFalse(ELitFalse *e_lit_false) void TypeCheck::visitELitFalse(ELitFalse *e_lit_false)
{ {
/* Code For ELitFalse Goes Here */ /* Code For ELitFalse Goes Here */
} }
void Skeleton::visitEApp(EApp *e_app) void TypeCheck::visitEApp(EApp *e_app)
{ {
/* Code For EApp Goes Here */ /* Code For EApp Goes Here */
...@@ -377,7 +367,7 @@ void Skeleton::visitEApp(EApp *e_app) ...@@ -377,7 +367,7 @@ void Skeleton::visitEApp(EApp *e_app)
} }
void Skeleton::visitEString(EString *e_string) void TypeCheck::visitEString(EString *e_string)
{ {
/* Code For EString Goes Here */ /* Code For EString Goes Here */
...@@ -385,7 +375,7 @@ void Skeleton::visitEString(EString *e_string) ...@@ -385,7 +375,7 @@ void Skeleton::visitEString(EString *e_string)
} }
void Skeleton::visitENewArray(ENewArray *e_new_array) void TypeCheck::visitENewArray(ENewArray *e_new_array)
{ {
/* Code For ENewArray Goes Here */ /* Code For ENewArray Goes Here */
...@@ -394,7 +384,7 @@ void Skeleton::visitENewArray(ENewArray *e_new_array) ...@@ -394,7 +384,7 @@ void Skeleton::visitENewArray(ENewArray *e_new_array)
} }
void Skeleton::visitENewClass(ENewClass *e_new_class) void TypeCheck::visitENewClass(ENewClass *e_new_class)
{ {
/* Code For ENewClass Goes Here */ /* Code For ENewClass Goes Here */
...@@ -402,7 +392,7 @@ void Skeleton::visitENewClass(ENewClass *e_new_class) ...@@ -402,7 +392,7 @@ void Skeleton::visitENewClass(ENewClass *e_new_class)
} }
void Skeleton::visitEClsMmbr(EClsMmbr *e_cls_mmbr) void TypeCheck::visitEClsMmbr(EClsMmbr *e_cls_mmbr)
{ {
/* Code For EClsMmbr Goes Here */ /* Code For EClsMmbr Goes Here */
...@@ -411,7 +401,7 @@ void Skeleton::visitEClsMmbr(EClsMmbr *e_cls_mmbr) ...@@ -411,7 +401,7 @@ void Skeleton::visitEClsMmbr(EClsMmbr *e_cls_mmbr)
} }
void Skeleton::visitEClsMthd(EClsMthd *e_cls_mthd) void TypeCheck::visitEClsMthd(EClsMthd *e_cls_mthd)
{ {
/* Code For EClsMthd Goes Here */ /* Code For EClsMthd Goes Here */
...@@ -421,14 +411,14 @@ void Skeleton::visitEClsMthd(EClsMthd *e_cls_mthd) ...@@ -421,14 +411,14 @@ void Skeleton::visitEClsMthd(EClsMthd *e_cls_mthd)
} }
void Skeleton::visitNull(Null *null) void TypeCheck::visitNull(Null *null)
{ {
/* Code For Null Goes Here */ /* Code For Null Goes Here */
} }
void Skeleton::visitEIndexAcc(EIndexAcc *e_index_acc) void TypeCheck::visitEIndexAcc(EIndexAcc *e_index_acc)
{ {
/* Code For EIndexAcc Goes Here */ /* Code For EIndexAcc Goes Here */
...@@ -437,7 +427,7 @@ void Skeleton::visitEIndexAcc(EIndexAcc *e_index_acc) ...@@ -437,7 +427,7 @@ void Skeleton::visitEIndexAcc(EIndexAcc *e_index_acc)
} }
void Skeleton::visitECast(ECast *e_cast) void TypeCheck::visitECast(ECast *e_cast)
{ {
/* Code For ECast Goes Here */ /* Code For ECast Goes Here */
...@@ -446,7 +436,7 @@ void Skeleton::visitECast(ECast *e_cast) ...@@ -446,7 +436,7 @@ void Skeleton::visitECast(ECast *e_cast)
} }
void Skeleton::visitNeg(Neg *neg) void TypeCheck::visitNeg(Neg *neg)
{ {
/* Code For Neg Goes Here */ /* Code For Neg Goes Here */
...@@ -454,7 +444,7 @@ void Skeleton::visitNeg(Neg *neg) ...@@ -454,7 +444,7 @@ void Skeleton::visitNeg(Neg *neg)
} }
void Skeleton::visitNot(Not *not_) void TypeCheck::visitNot(Not *not_)
{ {
/* Code For Not Goes Here */ /* Code For Not Goes Here */
...@@ -462,7 +452,7 @@ void Skeleton::visitNot(Not *not_) ...@@ -462,7 +452,7 @@ void Skeleton::visitNot(Not *not_)
} }
void Skeleton::visitEMul(EMul *e_mul) void TypeCheck::visitEMul(EMul *e_mul)
{ {
/* Code For EMul Goes Here */ /* Code For EMul Goes Here */
...@@ -472,7 +462,7 @@ void Skeleton::visitEMul(EMul *e_mul) ...@@ -472,7 +462,7 @@ void Skeleton::visitEMul(EMul *e_mul)
} }
void Skeleton::visitEAdd(EAdd *e_add) void TypeCheck::visitEAdd(EAdd *e_add)
{ {
/* Code For EAdd Goes Here */ /* Code For EAdd Goes Here */
...@@ -482,7 +472,7 @@ void Skeleton::visitEAdd(EAdd *e_add) ...@@ -482,7 +472,7 @@ void Skeleton::visitEAdd(EAdd *e_add)
} }
void Skeleton::visitERel(ERel *e_rel) void TypeCheck::visitERel(ERel *e_rel)
{ {
/* Code For ERel Goes Here */ /* Code For ERel Goes Here */
...@@ -492,7 +482,7 @@ void Skeleton::visitERel(ERel *e_rel) ...@@ -492,7 +482,7 @@ void Skeleton::visitERel(ERel *e_rel)
} }
void Skeleton::visitEAnd(EAnd *e_and) void TypeCheck::visitEAnd(EAnd *e_and)
{ {
/* Code For EAnd Goes Here */ /* Code For EAnd Goes Here */
...@@ -501,7 +491,7 @@ void Skeleton::visitEAnd(EAnd *e_and) ...@@ -501,7 +491,7 @@ void Skeleton::visitEAnd(EAnd *e_and)
} }
void Skeleton::visitEOr(EOr *e_or) void TypeCheck::visitEOr(EOr *e_or)
{ {
/* Code For EOr Goes Here */ /* Code For EOr Goes Here */
...@@ -510,77 +500,77 @@ void Skeleton::visitEOr(EOr *e_or) ...@@ -510,77 +500,77 @@ void Skeleton::visitEOr(EOr *e_or)
} }
void Skeleton::visitPlus(Plus *plus) void TypeCheck::visitPlus(Plus *plus)
{ {
/* Code For Plus Goes Here */ /* Code For Plus Goes Here */
} }
void Skeleton::visitMinus(Minus *minus) void TypeCheck::visitMinus(Minus *minus)
{ {
/* Code For Minus Goes Here */ /* Code For Minus Goes Here */
} }
void Skeleton::visitTimes(Times *times) void TypeCheck::visitTimes(Times *times)
{ {
/* Code For Times Goes Here */ /* Code For Times Goes Here */
} }
void Skeleton::visitDiv(Div *div) void TypeCheck::visitDiv(Div *div)
{ {
/* Code For Div Goes Here */ /* Code For Div Goes Here */
} }
void Skeleton::visitMod(Mod *mod) void TypeCheck::visitMod(Mod *mod)
{ {
/* Code For Mod Goes Here */ /* Code For Mod Goes Here */
} }
void Skeleton::visitLTH(LTH *lth) void TypeCheck::visitLTH(LTH *lth)
{ {
/* Code For LTH Goes Here */ /* Code For LTH Goes Here */
} }
void Skeleton::visitLE(LE *le) void TypeCheck::visitLE(LE *le)
{ {
/* Code For LE Goes Here */ /* Code For LE Goes Here */
} }
void Skeleton::visitGTH(GTH *gth) void TypeCheck::visitGTH(GTH *gth)
{ {
/* Code For GTH Goes Here */ /* Code For GTH Goes Here */
} }
void Skeleton::visitGE(GE *ge) void TypeCheck::visitGE(GE *ge)
{ {
/* Code For GE Goes Here */ /* Code For GE Goes Here */
} }
void Skeleton::visitEQU(EQU *equ) void TypeCheck::visitEQU(EQU *equ)
{ {
/* Code For EQU Goes Here */ /* Code For EQU Goes Here */
} }
void Skeleton::visitNE(NE *ne) void TypeCheck::visitNE(NE *ne)
{ {
/* Code For NE Goes Here */ /* Code For NE Goes Here */
...@@ -588,7 +578,7 @@ void Skeleton::visitNE(NE *ne) ...@@ -588,7 +578,7 @@ void Skeleton::visitNE(NE *ne)
} }
void Skeleton::visitListTopDef(ListTopDef *list_top_def) void TypeCheck::visitListTopDef(ListTopDef *list_top_def)
{ {
for (ListTopDef::iterator i = list_top_def->begin() ; i != list_top_def->end() ; ++i) for (ListTopDef::iterator i = list_top_def->begin() ; i != list_top_def->end() ; ++i)
{ {
...@@ -596,7 +586,7 @@ void Skeleton::visitListTopDef(ListTopDef *list_top_def) ...@@ -596,7 +586,7 @@ void Skeleton::visitListTopDef(ListTopDef *list_top_def)
} }
} }
void Skeleton::visitListArg(ListArg *list_arg) void TypeCheck::visitListArg(ListArg *list_arg)
{ {
for (ListArg::iterator i = list_arg->begin() ; i != list_arg->end() ; ++i) for (ListArg::iterator i = list_arg->begin() ; i != list_arg->end() ; ++i)
{ {
...@@ -604,7 +594,7 @@ void Skeleton::visitListArg(ListArg *list_arg) ...@@ -604,7 +594,7 @@ void Skeleton::visitListArg(ListArg *list_arg)
} }
} }
void Skeleton::visitListClassBlockDef(ListClassBlockDef *list_class_block_def) void TypeCheck::visitListClassBlockDef(ListClassBlockDef *list_class_block_def)
{ {
for (ListClassBlockDef::iterator i = list_class_block_def->begin() ; i != list_class_block_def->end() ; ++i) for (ListClassBlockDef::iterator i = list_class_block_def->begin() ; i != list_class_block_def->end() ; ++i)
{ {
...@@ -612,7 +602,7 @@ void Skeleton::visitListClassBlockDef(ListClassBlockDef *list_class_block_def) ...@@ -612,7 +602,7 @@ void Skeleton::visitListClassBlockDef(ListClassBlockDef *list_class_block_def)
} }
} }
void Skeleton::visitListStmt(ListStmt *list_stmt) void TypeCheck::visitListStmt(ListStmt *list_stmt)
{ {
for (ListStmt::iterator i = list_stmt->begin() ; i != list_stmt->end() ; ++i) for (ListStmt::iterator i = list_stmt->begin() ; i != list_stmt->end() ; ++i)
{ {
...@@ -620,7 +610,7 @@ void Skeleton::visitListStmt(ListStmt *list_stmt) ...@@ -620,7 +610,7 @@ void Skeleton::visitListStmt(ListStmt *list_stmt)
} }
} }
void Skeleton::visitListItem(ListItem *list_item) void TypeCheck::visitListItem(ListItem *list_item)
{ {
for (ListItem::iterator i = list_item->begin() ; i != list_item->end() ; ++i) for (ListItem::iterator i = list_item->begin() ; i != list_item->end() ; ++i)
{ {
...@@ -628,7 +618,7 @@ void Skeleton::visitListItem(ListItem *list_item) ...@@ -628,7 +618,7 @@ void Skeleton::visitListItem(ListItem *list_item)
} }
} }
void Skeleton::visitListType(ListType *list_type) void TypeCheck::visitListType(ListType *list_type)
{ {
for (ListType::iterator i = list_type->begin() ; i != list_type->end() ; ++i) for (ListType::iterator i = list_type->begin() ; i != list_type->end() ; ++i)
{ {
...@@ -636,7 +626,7 @@ void Skeleton::visitListType(ListType *list_type) ...@@ -636,7 +626,7 @@ void Skeleton::visitListType(ListType *list_type)
} }
} }
void Skeleton::visitListExpr(ListExpr *list_expr) void TypeCheck::visitListExpr(ListExpr *list_expr)
{ {
for (ListExpr::iterator i = list_expr->begin() ; i != list_expr->end() ; ++i) for (ListExpr::iterator i = list_expr->begin() ; i != list_expr->end() ; ++i)
{ {
...@@ -645,30 +635,43 @@ void Skeleton::visitListExpr(ListExpr *list_expr) ...@@ -645,30 +635,43 @@ void Skeleton::visitListExpr(ListExpr *list_expr)
} }
void Skeleton::visitInteger(Integer x) void TypeCheck::visitInteger(Integer x)
{ {
/* Code for Integer Goes Here */ /* Code for Integer Goes Here */
} }
void Skeleton::visitChar(Char x) void TypeCheck::visitChar(Char x)
{ {
/* Code for Char Goes Here */ /* Code for Char Goes Here */
} }
void Skeleton::visitDouble(Double x) void TypeCheck::visitDouble(Double x)
{ {
/* Code for Double Goes Here */ /* Code for Double Goes Here */
} }
void Skeleton::visitString(String x) void TypeCheck::visitString(String x)
{ {
/* Code for String Goes Here */ /* Code for String Goes Here */
} }
void Skeleton::visitIdent(Ident x) void TypeCheck::visitIdent(Ident x)
{ {
/* Code for Ident Goes Here */ /* Code for Ident Goes Here */
} }
void TypeCheck::check(Visitable *v)
{
if (state != State::initialized) {
throw std::runtime_error("already initialized");
}
state = State::buildInfo;
v->accept(this);
state = State::checkType;
v->accept(this);
}
\ No newline at end of file
...@@ -5,9 +5,17 @@ ...@@ -5,9 +5,17 @@
#include "Absyn.h" #include "Absyn.h"
class Skeleton : public Visitor class TypeCheck : public Visitor
{ {
enum State {
initialized,
buildInfo,
checkType
} state;
public: public:
TypeCheck() : state(initialized) {};
void check(Visitable *v);
protected:
void visitProgram(Program *p); void visitProgram(Program *p);
void visitTopDef(TopDef *p); void visitTopDef(TopDef *p);
void visitFunDef(FunDef *p); void visitFunDef(FunDef *p);
...@@ -104,8 +112,6 @@ public: ...@@ -104,8 +112,6 @@ public:
void visitDouble(Double x); void visitDouble(Double x);
void visitString(String x); void visitString(String x);
void visitIdent(Ident x); void visitIdent(Ident x);
}; };
#endif #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