Add ParameterList
This commit is contained in:
@@ -60,9 +60,10 @@ SET(SCULLY_SOURCE
|
|||||||
|
|
||||||
src/AST/ASTElement.cpp
|
src/AST/ASTElement.cpp
|
||||||
src/AST/ASTVisitor.cpp
|
src/AST/ASTVisitor.cpp
|
||||||
|
src/AST/ParameterList.cpp
|
||||||
src/AST/Statement.cpp
|
src/AST/Statement.cpp
|
||||||
src/AST/VariableDefinition.cpp
|
|
||||||
src/AST/Type.cpp
|
src/AST/Type.cpp
|
||||||
|
src/AST/VariableDefinition.cpp
|
||||||
|
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
|
${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -8,9 +8,10 @@
|
|||||||
#include "Token.h"
|
#include "Token.h"
|
||||||
|
|
||||||
#include "AST/ASTElement.h"
|
#include "AST/ASTElement.h"
|
||||||
|
#include "AST/ParameterList.h"
|
||||||
#include "AST/Statement.h"
|
#include "AST/Statement.h"
|
||||||
#include "AST/VariableDefinition.h"
|
|
||||||
#include "AST/Type.h"
|
#include "AST/Type.h"
|
||||||
|
#include "AST/VariableDefinition.h"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,14 +38,14 @@
|
|||||||
|
|
||||||
%type program {int}
|
%type program {int}
|
||||||
program ::= fundefs(F). { std::cout << F << std::endl; }
|
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}
|
%type fundefs {int}
|
||||||
fundefs(A) ::= . { A = 0; }
|
fundefs(A) ::= . { A = 0; }
|
||||||
fundefs(A) ::= fundefs fundef(B). { A = A + B; }
|
fundefs(A) ::= fundefs fundef(B). { A = A + B; }
|
||||||
|
|
||||||
%type fundef {int}
|
%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 type {Type*}
|
||||||
type(A) ::= T_BOOL. { A = new Type("bool"); }
|
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_STRING. { A = new Type("string"); }
|
||||||
type(A) ::= T_VOID. { A = new Type("void"); }
|
type(A) ::= T_VOID. { A = new Type("void"); }
|
||||||
|
|
||||||
%type params {int}
|
%type params {ParameterList*}
|
||||||
params(A) ::= . { A = 0; }
|
params(A) ::= . { A = new ParameterList(); }
|
||||||
params(A) ::= type(T) T_IDENTIFIER(ID). { A = 1 + 1; /* T ID */ }
|
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). { A = B + 1 + 1; /* T ID */ }
|
params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID). { B->addParameter(T, ID->getText()); A = B; }
|
||||||
|
|
||||||
%type statement {int}
|
%type statement {int}
|
||||||
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S). { A = E + S; }
|
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S). { A = E + S; }
|
||||||
|
|||||||
@@ -2,12 +2,14 @@
|
|||||||
#define ASTVISITOR_H
|
#define ASTVISITOR_H
|
||||||
|
|
||||||
#include "VariableDefinition.h"
|
#include "VariableDefinition.h"
|
||||||
|
#include "ParameterList.h"
|
||||||
|
|
||||||
class ASTVisitor {
|
class ASTVisitor {
|
||||||
public:
|
public:
|
||||||
ASTVisitor();
|
ASTVisitor();
|
||||||
virtual ~ASTVisitor();
|
virtual ~ASTVisitor();
|
||||||
|
|
||||||
|
virtual void visit(ParameterList* e) = 0;
|
||||||
virtual void visit(VariableDefinition* e) = 0;
|
virtual void visit(VariableDefinition* e) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
23
inc/AST/ParameterList.h
Normal file
23
inc/AST/ParameterList.h
Normal 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
24
src/AST/ParameterList.cpp
Normal 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_;
|
||||||
|
}
|
||||||
@@ -3,3 +3,7 @@
|
|||||||
Type::Type(std::string name) : name_(name) {
|
Type::Type(std::string name) : name_(name) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Type::getName() {
|
||||||
|
return name_;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user