Make Type an enum class (C++11 ftw)
This commit is contained in:
@@ -63,11 +63,11 @@ program ::= expr(E). { PrintVisitor* pv = new PrintVisitor; E->accept(pv)
|
|||||||
fundef(A) ::= type(T) T_IDENTIFIER(ID) T_LPAREN params(P) T_RPAREN T_BEGIN statements(S) T_END.
|
fundef(A) ::= type(T) T_IDENTIFIER(ID) T_LPAREN params(P) T_RPAREN T_BEGIN statements(S) T_END.
|
||||||
{ A = new FunctionDefinition(T, ID->getText(), P, S); }
|
{ A = new FunctionDefinition(T, ID->getText(), P, S); }
|
||||||
|
|
||||||
%type type {Type*}
|
%type type {Type}
|
||||||
type(A) ::= T_BOOL. { A = new Type("bool"); }
|
type(A) ::= T_BOOL. { A = Type::BOOL; }
|
||||||
type(A) ::= T_INT. { A = new Type("int"); }
|
type(A) ::= T_INT. { A = Type::INT; }
|
||||||
type(A) ::= T_STRING. { A = new Type("string"); }
|
type(A) ::= T_STRING. { A = Type::STRING; }
|
||||||
type(A) ::= T_VOID. { A = new Type("void"); }
|
type(A) ::= T_VOID. { A = Type::VOID; }
|
||||||
|
|
||||||
%type params {ParameterList*}
|
%type params {ParameterList*}
|
||||||
params(A) ::= . { A = new ParameterList(); }
|
params(A) ::= . { A = new ParameterList(); }
|
||||||
|
|||||||
@@ -9,17 +9,17 @@
|
|||||||
|
|
||||||
class FunctionDefinition : public ASTElement {
|
class FunctionDefinition : public ASTElement {
|
||||||
public:
|
public:
|
||||||
FunctionDefinition(Type *type, std::string name, ParameterList* params, StatementList* sl);
|
FunctionDefinition(Type type, std::string name, ParameterList* params, StatementList* sl);
|
||||||
virtual ~FunctionDefinition();
|
virtual ~FunctionDefinition();
|
||||||
|
|
||||||
virtual void accept(ASTVisitor* visitor);
|
virtual void accept(ASTVisitor* visitor);
|
||||||
|
|
||||||
Type* getType();
|
Type getType();
|
||||||
std::string getName();
|
std::string getName();
|
||||||
ParameterList* getParams();
|
ParameterList* getParams();
|
||||||
StatementList* getSl();
|
StatementList* getSl();
|
||||||
private:
|
private:
|
||||||
Type* type_;
|
Type type_;
|
||||||
std::string name_;
|
std::string name_;
|
||||||
ParameterList* params_;
|
ParameterList* params_;
|
||||||
StatementList* sl_;
|
StatementList* sl_;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "AST/Type.h"
|
#include "AST/Type.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
typedef std::pair<Type*, std::string> Parameter;
|
typedef std::pair<Type, std::string> Parameter;
|
||||||
|
|
||||||
class ParameterList : public ASTElement {
|
class ParameterList : public ASTElement {
|
||||||
public:
|
public:
|
||||||
@@ -14,7 +14,7 @@ public:
|
|||||||
|
|
||||||
virtual void accept(ASTVisitor* visitor);
|
virtual void accept(ASTVisitor* visitor);
|
||||||
|
|
||||||
void addParameter(Type* type, std::string name);
|
void addParameter(Type type, std::string name);
|
||||||
std::vector<Parameter> getParameters();
|
std::vector<Parameter> getParameters();
|
||||||
private:
|
private:
|
||||||
std::vector<Parameter> params_;
|
std::vector<Parameter> params_;
|
||||||
|
|||||||
@@ -5,4 +5,6 @@
|
|||||||
|
|
||||||
enum class Type { BOOL, INT, STRING, VOID };
|
enum class Type { BOOL, INT, STRING, VOID };
|
||||||
|
|
||||||
|
std::string typeToString(Type type);
|
||||||
|
|
||||||
#endif // TYPE_H
|
#endif // TYPE_H
|
||||||
|
|||||||
@@ -9,15 +9,15 @@
|
|||||||
class VariableDefinition : public Statement
|
class VariableDefinition : public Statement
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VariableDefinition(Type *type, std::string name);
|
VariableDefinition(Type type, std::string name);
|
||||||
virtual ~VariableDefinition();
|
virtual ~VariableDefinition();
|
||||||
|
|
||||||
virtual void accept(ASTVisitor* visitor);
|
virtual void accept(ASTVisitor* visitor);
|
||||||
|
|
||||||
Type* getType();
|
Type getType();
|
||||||
std::string getName();
|
std::string getName();
|
||||||
private:
|
private:
|
||||||
Type* type_;
|
Type type_;
|
||||||
std::string name_;
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "AST/FunctionDefinition.h"
|
#include "AST/FunctionDefinition.h"
|
||||||
#include "AST/ASTVisitor.h"
|
#include "AST/ASTVisitor.h"
|
||||||
|
|
||||||
FunctionDefinition::FunctionDefinition(Type *type, std::string name, ParameterList *params, StatementList *sl) : type_(type), name_(name), params_(params), sl_(sl) {
|
FunctionDefinition::FunctionDefinition(Type type, std::string name, ParameterList *params, StatementList *sl) : type_(type), name_(name), params_(params), sl_(sl) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ void FunctionDefinition::accept(ASTVisitor* visitor) {
|
|||||||
visitor->visit(this);
|
visitor->visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Type* FunctionDefinition::getType() {
|
Type FunctionDefinition::getType() {
|
||||||
return type_;
|
return type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ void ParameterList::accept(ASTVisitor* visitor) {
|
|||||||
visitor->visit(this);
|
visitor->visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParameterList::addParameter(Type* type, std::string name) {
|
void ParameterList::addParameter(Type type, std::string name) {
|
||||||
params_.push_back(Parameter(type, name));
|
params_.push_back(Parameter(type, name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ void PrintVisitor::visit(FunctionDefinition* e) {
|
|||||||
println(ss.str());
|
println(ss.str());
|
||||||
ss.str("");
|
ss.str("");
|
||||||
ss.clear();
|
ss.clear();
|
||||||
ss << "Type: " << e->getType();
|
ss << "Type: " << typeToString(e->getType());
|
||||||
println(ss.str());
|
println(ss.str());
|
||||||
ParameterList* params = e->getParams();
|
ParameterList* params = e->getParams();
|
||||||
if (params) {
|
if (params) {
|
||||||
@@ -140,7 +140,7 @@ void PrintVisitor::visit(ParameterList* e) {
|
|||||||
println(ss.str());
|
println(ss.str());
|
||||||
ss.str("");
|
ss.str("");
|
||||||
ss.clear();
|
ss.clear();
|
||||||
ss << "Type: " << p.first->getName();
|
ss << "Type: " << typeToString(p.first);
|
||||||
println(ss.str());
|
println(ss.str());
|
||||||
}
|
}
|
||||||
level_--;
|
level_--;
|
||||||
@@ -216,7 +216,7 @@ void PrintVisitor::visit(VariableDefinition* e) {
|
|||||||
println(ss.str());
|
println(ss.str());
|
||||||
ss.str("");
|
ss.str("");
|
||||||
ss.clear();
|
ss.clear();
|
||||||
ss << "Type: " << e->getType()->getName();
|
ss << "Type: " << typeToString(e->getType());
|
||||||
println(ss.str());
|
println(ss.str());
|
||||||
level_--;
|
level_--;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,16 @@
|
|||||||
#include "AST/Type.h"
|
#include "AST/Type.h"
|
||||||
|
|
||||||
|
std::string typeToString(Type type) {
|
||||||
|
switch (type) {
|
||||||
|
case Type::BOOL:
|
||||||
|
return "bool";
|
||||||
|
case Type::INT:
|
||||||
|
return "int";
|
||||||
|
case Type::STRING:
|
||||||
|
return "string";
|
||||||
|
case Type::VOID:
|
||||||
|
return "void";
|
||||||
|
default:
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "AST/VariableDefinition.h"
|
#include "AST/VariableDefinition.h"
|
||||||
#include "AST/ASTVisitor.h"
|
#include "AST/ASTVisitor.h"
|
||||||
|
|
||||||
VariableDefinition::VariableDefinition(Type* type, std::string name) : type_(type), name_(name) {
|
VariableDefinition::VariableDefinition(Type type, std::string name) : type_(type), name_(name) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ void VariableDefinition::accept(ASTVisitor* visitor) {
|
|||||||
visitor->visit(this);
|
visitor->visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Type* VariableDefinition::getType() {
|
Type VariableDefinition::getType() {
|
||||||
return type_;
|
return type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user