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
This diff is collapsed.
...@@ -5,107 +5,113 @@ ...@@ -5,107 +5,113 @@
#include "Absyn.h" #include "Absyn.h"
class Skeleton : public Visitor class TypeCheck : public Visitor
{ {
enum State {
initialized,
buildInfo,
checkType
} state;
public: public:
void visitProgram(Program *p); TypeCheck() : state(initialized) {};
void visitTopDef(TopDef *p); void check(Visitable *v);
void visitFunDef(FunDef *p); protected:
void visitArg(Arg *p); void visitProgram(Program *p);
void visitClassDef(ClassDef *p); void visitTopDef(TopDef *p);
void visitClassBlock(ClassBlock *p); void visitFunDef(FunDef *p);
void visitClassBlockDef(ClassBlockDef *p); void visitArg(Arg *p);
void visitBlock(Block *p); void visitClassDef(ClassDef *p);
void visitStmt(Stmt *p); void visitClassBlock(ClassBlock *p);
void visitItem(Item *p); void visitClassBlockDef(ClassBlockDef *p);
void visitType(Type *p); void visitBlock(Block *p);
void visitExpr(Expr *p); void visitStmt(Stmt *p);
void visitAddOp(AddOp *p); void visitItem(Item *p);
void visitMulOp(MulOp *p); void visitType(Type *p);
void visitRelOp(RelOp *p); void visitExpr(Expr *p);
void visitProg(Prog *p); void visitAddOp(AddOp *p);
void visitFnDef(FnDef *p); void visitMulOp(MulOp *p);
void visitClDef(ClDef *p); void visitRelOp(RelOp *p);
void visitFuncDef(FuncDef *p); void visitProg(Prog *p);
void visitAr(Ar *p); void visitFnDef(FnDef *p);
void visitClassDefN(ClassDefN *p); void visitClDef(ClDef *p);
void visitClassDefE(ClassDefE *p); void visitFuncDef(FuncDef *p);
void visitClassBl(ClassBl *p); void visitAr(Ar *p);
void visitClassMthd(ClassMthd *p); void visitClassDefN(ClassDefN *p);
void visitClassFld(ClassFld *p); void visitClassDefE(ClassDefE *p);
void visitBlk(Blk *p); void visitClassBl(ClassBl *p);
void visitEmpty(Empty *p); void visitClassMthd(ClassMthd *p);
void visitBStmt(BStmt *p); void visitClassFld(ClassFld *p);
void visitDecl(Decl *p); void visitBlk(Blk *p);
void visitNoInit(NoInit *p); void visitEmpty(Empty *p);
void visitInit(Init *p); void visitBStmt(BStmt *p);
void visitAss(Ass *p); void visitDecl(Decl *p);
void visitTableAss(TableAss *p); void visitNoInit(NoInit *p);
void visitTableIncr(TableIncr *p); void visitInit(Init *p);
void visitTableDecr(TableDecr *p); void visitAss(Ass *p);
void visitIncr(Incr *p); void visitTableAss(TableAss *p);
void visitDecr(Decr *p); void visitTableIncr(TableIncr *p);
void visitRet(Ret *p); void visitTableDecr(TableDecr *p);
void visitVRet(VRet *p); void visitIncr(Incr *p);
void visitCond(Cond *p); void visitDecr(Decr *p);
void visitCondElse(CondElse *p); void visitRet(Ret *p);
void visitWhile(While *p); void visitVRet(VRet *p);
void visitSExp(SExp *p); void visitCond(Cond *p);
void visitForEach(ForEach *p); void visitCondElse(CondElse *p);
void visitInt(Int *p); void visitWhile(While *p);
void visitStr(Str *p); void visitSExp(SExp *p);
void visitBool(Bool *p); void visitForEach(ForEach *p);
void visitVoid(Void *p); void visitInt(Int *p);
void visitArray(Array *p); void visitStr(Str *p);
void visitClassT(ClassT *p); void visitBool(Bool *p);
void visitFun(Fun *p); void visitVoid(Void *p);
void visitEVar(EVar *p); void visitArray(Array *p);
void visitELitInt(ELitInt *p); void visitClassT(ClassT *p);
void visitELitTrue(ELitTrue *p); void visitFun(Fun *p);
void visitELitFalse(ELitFalse *p); void visitEVar(EVar *p);
void visitEApp(EApp *p); void visitELitInt(ELitInt *p);
void visitEString(EString *p); void visitELitTrue(ELitTrue *p);
void visitENewArray(ENewArray *p); void visitELitFalse(ELitFalse *p);
void visitENewClass(ENewClass *p); void visitEApp(EApp *p);
void visitEClsMmbr(EClsMmbr *p); void visitEString(EString *p);
void visitEClsMthd(EClsMthd *p); void visitENewArray(ENewArray *p);
void visitNull(Null *p); void visitENewClass(ENewClass *p);
void visitEIndexAcc(EIndexAcc *p); void visitEClsMmbr(EClsMmbr *p);
void visitECast(ECast *p); void visitEClsMthd(EClsMthd *p);
void visitNeg(Neg *p); void visitNull(Null *p);
void visitNot(Not *p); void visitEIndexAcc(EIndexAcc *p);
void visitEMul(EMul *p); void visitECast(ECast *p);
void visitEAdd(EAdd *p); void visitNeg(Neg *p);
void visitERel(ERel *p); void visitNot(Not *p);
void visitEAnd(EAnd *p); void visitEMul(EMul *p);
void visitEOr(EOr *p); void visitEAdd(EAdd *p);
void visitPlus(Plus *p); void visitERel(ERel *p);
void visitMinus(Minus *p); void visitEAnd(EAnd *p);
void visitTimes(Times *p); void visitEOr(EOr *p);
void visitDiv(Div *p); void visitPlus(Plus *p);
void visitMod(Mod *p); void visitMinus(Minus *p);
void visitLTH(LTH *p); void visitTimes(Times *p);
void visitLE(LE *p); void visitDiv(Div *p);
void visitGTH(GTH *p); void visitMod(Mod *p);
void visitGE(GE *p); void visitLTH(LTH *p);
void visitEQU(EQU *p); void visitLE(LE *p);
void visitNE(NE *p); void visitGTH(GTH *p);
void visitListTopDef(ListTopDef *p); void visitGE(GE *p);
void visitListArg(ListArg *p); void visitEQU(EQU *p);
void visitListClassBlockDef(ListClassBlockDef *p); void visitNE(NE *p);
void visitListStmt(ListStmt *p); void visitListTopDef(ListTopDef *p);
void visitListItem(ListItem *p); void visitListArg(ListArg *p);
void visitListType(ListType *p); void visitListClassBlockDef(ListClassBlockDef *p);
void visitListExpr(ListExpr *p); void visitListStmt(ListStmt *p);
void visitPIdent(PIdent *p); void visitListItem(ListItem *p);
void visitListType(ListType *p);
void visitInteger(Integer x); void visitListExpr(ListExpr *p);
void visitChar(Char x); void visitPIdent(PIdent *p);
void visitDouble(Double x);
void visitString(String x);
void visitIdent(Ident x);
void visitInteger(Integer x);
void visitChar(Char x);
void visitDouble(Double x);
void visitString(String 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