Commit 0df5b04c authored by zygzagZ's avatar zygzagZ

Extend abstract ClassDef, add ParseError, WIP TypeCheck

parent 1f3906a4
......@@ -258,6 +258,9 @@ public:
class ClassDef : public Visitable
{
public:
virtual PIdent *getName() const = 0;
virtual PIdent *getParent() const = 0;
virtual ClassBlock *getBlock() const = 0;
virtual ClassDef *clone() const = 0;
};
......@@ -436,6 +439,10 @@ public:
virtual void accept(Visitor *v);
virtual ClassDefN *clone() const;
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
......@@ -452,6 +459,10 @@ public:
virtual void accept(Visitor *v);
virtual ClassDefE *clone() const;
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
......
#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 @@
#include "Printer.h"
#include "Absyn.h"
// #include "Compiler.h"
#include "ParseError.h"
#include "TypeCheck.h"
#include <fstream>
#include <iostream>
#include <filesystem>
......@@ -63,6 +65,12 @@ int main(int argc, char ** argv)
binaryPath = argv[0];
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);
std::string out = c.compile(parse_tree);
......
CC=g++
CCFLAGS=-g -W -Wall -O3 -std=c++1z
CCFLAGS=-g -W -Wall -O3 -std=c++1z -Wno-unused-parameter
FLEX=flex
FLEX_OPTS=-PGrammar
......@@ -7,7 +7,7 @@ FLEX_OPTS=-PGrammar
BISON=bison
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
......@@ -44,5 +44,8 @@ Printer.o : Printer.cpp Printer.h Absyn.h
Skeleton.o : Skeleton.cpp Skeleton.h Absyn.h
${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
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 @@
#include "Absyn.h"
class Skeleton : public Visitor
class TypeCheck : public Visitor
{
enum State {
initialized,
buildInfo,
checkType
} state;
public:
void visitProgram(Program *p);
void visitTopDef(TopDef *p);
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);
void visitMulOp(MulOp *p);
void visitRelOp(RelOp *p);
void visitProg(Prog *p);
void visitFnDef(FnDef *p);
void visitClDef(ClDef *p);
void visitFuncDef(FuncDef *p);
void visitAr(Ar *p);
void visitClassDefN(ClassDefN *p);
void visitClassDefE(ClassDefE *p);
void visitClassBl(ClassBl *p);
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);
void visitTableDecr(TableDecr *p);
void visitIncr(Incr *p);
void visitDecr(Decr *p);
void visitRet(Ret *p);
void visitVRet(VRet *p);
void visitCond(Cond *p);
void visitCondElse(CondElse *p);
void visitWhile(While *p);
void visitSExp(SExp *p);
void visitForEach(ForEach *p);
void visitInt(Int *p);
void visitStr(Str *p);
void visitBool(Bool *p);
void visitVoid(Void *p);
void visitArray(Array *p);
void visitClassT(ClassT *p);
void visitFun(Fun *p);
void visitEVar(EVar *p);
void visitELitInt(ELitInt *p);
void visitELitTrue(ELitTrue *p);
void visitELitFalse(ELitFalse *p);
void visitEApp(EApp *p);
void visitEString(EString *p);
void visitENewArray(ENewArray *p);
void visitENewClass(ENewClass *p);
void visitEClsMmbr(EClsMmbr *p);
void visitEClsMthd(EClsMthd *p);
void visitNull(Null *p);
void visitEIndexAcc(EIndexAcc *p);
void visitECast(ECast *p);
void visitNeg(Neg *p);
void visitNot(Not *p);
void visitEMul(EMul *p);
void visitEAdd(EAdd *p);
void visitERel(ERel *p);
void visitEAnd(EAnd *p);
void visitEOr(EOr *p);
void visitPlus(Plus *p);
void visitMinus(Minus *p);
void visitTimes(Times *p);
void visitDiv(Div *p);
void visitMod(Mod *p);
void visitLTH(LTH *p);
void visitLE(LE *p);
void visitGTH(GTH *p);
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);
TypeCheck() : state(initialized) {};
void check(Visitable *v);
protected:
void visitProgram(Program *p);
void visitTopDef(TopDef *p);
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);
void visitMulOp(MulOp *p);
void visitRelOp(RelOp *p);
void visitProg(Prog *p);
void visitFnDef(FnDef *p);
void visitClDef(ClDef *p);
void visitFuncDef(FuncDef *p);
void visitAr(Ar *p);
void visitClassDefN(ClassDefN *p);
void visitClassDefE(ClassDefE *p);
void visitClassBl(ClassBl *p);
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);
void visitTableDecr(TableDecr *p);
void visitIncr(Incr *p);
void visitDecr(Decr *p);
void visitRet(Ret *p);
void visitVRet(VRet *p);
void visitCond(Cond *p);
void visitCondElse(CondElse *p);
void visitWhile(While *p);
void visitSExp(SExp *p);
void visitForEach(ForEach *p);
void visitInt(Int *p);
void visitStr(Str *p);
void visitBool(Bool *p);
void visitVoid(Void *p);
void visitArray(Array *p);
void visitClassT(ClassT *p);
void visitFun(Fun *p);
void visitEVar(EVar *p);
void visitELitInt(ELitInt *p);
void visitELitTrue(ELitTrue *p);
void visitELitFalse(ELitFalse *p);
void visitEApp(EApp *p);
void visitEString(EString *p);
void visitENewArray(ENewArray *p);
void visitENewClass(ENewClass *p);
void visitEClsMmbr(EClsMmbr *p);
void visitEClsMthd(EClsMthd *p);
void visitNull(Null *p);
void visitEIndexAcc(EIndexAcc *p);
void visitECast(ECast *p);
void visitNeg(Neg *p);
void visitNot(Not *p);
void visitEMul(EMul *p);
void visitEAdd(EAdd *p);
void visitERel(ERel *p);
void visitEAnd(EAnd *p);
void visitEOr(EOr *p);
void visitPlus(Plus *p);
void visitMinus(Minus *p);
void visitTimes(Times *p);
void visitDiv(Div *p);
void visitMod(Mod *p);
void visitLTH(LTH *p);
void visitLE(LE *p);
void visitGTH(GTH *p);
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);
};
#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