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,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