Add ParameterList

This commit is contained in:
Markus Hauschild
2013-06-01 00:38:20 +02:00
parent a778d524b1
commit e2ab5af964
6 changed files with 63 additions and 8 deletions

View File

@@ -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
)

View File

@@ -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; }

View File

@@ -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;
};

23
inc/AST/ParameterList.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef PARAMETERLIST_H
#define PARAMETERLIST_H
#include "AST/ASTElement.h"
#include "AST/Type.h"
#include <vector>
typedef std::pair<Type*, std::string> Parameter;
class ParameterList : public ASTElement {
public:
ParameterList();
virtual ~ParameterList();
virtual void accept(ASTVisitor* visitor);
void addParameter(Type* type, std::string name);
std::vector<Parameter> getParameters();
private:
std::vector<Parameter> params_;
};
#endif // PARAMETERLIST_H

24
src/AST/ParameterList.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "AST/ParameterList.h"
#include "AST/ASTVisitor.h"
#include <iostream>
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<Parameter> ParameterList::getParameters() {
return params_;
}

View File

@@ -3,3 +3,7 @@
Type::Type(std::string name) : name_(name) {
//
}
std::string Type::getName() {
return name_;
}