diff --git a/CMakeLists.txt b/CMakeLists.txt index c9a7d3f..c2244e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,9 +60,10 @@ SET(SCULLY_SOURCE src/AST/ASTElement.cpp src/AST/ASTVisitor.cpp + src/AST/ParameterList.cpp src/AST/Statement.cpp - src/AST/VariableDefinition.cpp src/AST/Type.cpp + src/AST/VariableDefinition.cpp ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp ) diff --git a/grammar/grammar.y b/grammar/grammar.y index efe35e1..227d46c 100644 --- a/grammar/grammar.y +++ b/grammar/grammar.y @@ -8,9 +8,10 @@ #include "Token.h" #include "AST/ASTElement.h" +#include "AST/ParameterList.h" #include "AST/Statement.h" -#include "AST/VariableDefinition.h" #include "AST/Type.h" +#include "AST/VariableDefinition.h" } @@ -37,14 +38,14 @@ %type program {int} program ::= fundefs(F). { std::cout << F << std::endl; } -program ::= expr(E). { std::cout << E << std::endl; } +//program ::= expr(E). { std::cout << E << std::endl; } %type fundefs {int} fundefs(A) ::= . { A = 0; } fundefs(A) ::= fundefs fundef(B). { A = A + B; } %type fundef {int} -fundef(A) ::= type(T) T_IDENTIFIER(ID) params(P) T_BEGIN statements(S) T_END. { A = 1 + 1 + P + S; /* T ID */ } +fundef(A) ::= type(T) T_IDENTIFIER(ID) T_LPAREN params(P) T_RPAREN T_BEGIN statements(S) T_END. { A = 1 + 1 + 1 + S; /* T P ID */ } %type type {Type*} type(A) ::= T_BOOL. { A = new Type("bool"); } @@ -52,10 +53,10 @@ type(A) ::= T_INT. { A = new Type("int"); } type(A) ::= T_STRING. { A = new Type("string"); } type(A) ::= T_VOID. { A = new Type("void"); } -%type params {int} -params(A) ::= . { A = 0; } -params(A) ::= type(T) T_IDENTIFIER(ID). { A = 1 + 1; /* T ID */ } -params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID). { A = B + 1 + 1; /* T ID */ } +%type params {ParameterList*} +params(A) ::= . { A = new ParameterList(); } +params(A) ::= type(T) T_IDENTIFIER(ID). { A = new ParameterList(); A->addParameter(T, ID->getText()); } +params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID). { B->addParameter(T, ID->getText()); A = B; } %type statement {int} statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S). { A = E + S; } diff --git a/inc/AST/ASTVisitor.h b/inc/AST/ASTVisitor.h index 35ebec0..8e4ee8e 100644 --- a/inc/AST/ASTVisitor.h +++ b/inc/AST/ASTVisitor.h @@ -2,12 +2,14 @@ #define ASTVISITOR_H #include "VariableDefinition.h" +#include "ParameterList.h" class ASTVisitor { public: ASTVisitor(); virtual ~ASTVisitor(); + virtual void visit(ParameterList* e) = 0; virtual void visit(VariableDefinition* e) = 0; }; diff --git a/inc/AST/ParameterList.h b/inc/AST/ParameterList.h new file mode 100644 index 0000000..6209396 --- /dev/null +++ b/inc/AST/ParameterList.h @@ -0,0 +1,23 @@ +#ifndef PARAMETERLIST_H +#define PARAMETERLIST_H + +#include "AST/ASTElement.h" +#include "AST/Type.h" +#include + +typedef std::pair Parameter; + +class ParameterList : public ASTElement { +public: + ParameterList(); + virtual ~ParameterList(); + + virtual void accept(ASTVisitor* visitor); + + void addParameter(Type* type, std::string name); + std::vector getParameters(); +private: + std::vector params_; +}; + +#endif // PARAMETERLIST_H diff --git a/src/AST/ParameterList.cpp b/src/AST/ParameterList.cpp new file mode 100644 index 0000000..de07112 --- /dev/null +++ b/src/AST/ParameterList.cpp @@ -0,0 +1,24 @@ +#include "AST/ParameterList.h" +#include "AST/ASTVisitor.h" +#include + +ParameterList::ParameterList() { + // +} + +ParameterList::~ParameterList() { + // +} + +void ParameterList::accept(ASTVisitor* visitor) { + visitor->visit(this); +} + +void ParameterList::addParameter(Type* type, std::string name) { + std::cout << "added parameter of type" << type->getName() << " and name " << name << std::endl; + params_.push_back(Parameter(type, name)); +} + +std::vector ParameterList::getParameters() { + return params_; +} diff --git a/src/AST/Type.cpp b/src/AST/Type.cpp index 5504402..6c48e28 100644 --- a/src/AST/Type.cpp +++ b/src/AST/Type.cpp @@ -3,3 +3,7 @@ Type::Type(std::string name) : name_(name) { // } + +std::string Type::getName() { + return name_; +}