Merge branch 'master' of git.tuxzone.org:woc2013
Conflicts: CMakeLists.txt inc/AST/ASTVisitor.h
This commit is contained in:
@@ -69,6 +69,8 @@ SET(SCULLY_SOURCE
|
||||
src/AST/Type.cpp
|
||||
src/AST/VariableDefinition.cpp
|
||||
src/AST/StatementList.cpp
|
||||
src/AST/ValueList.cpp
|
||||
src/AST/IfStatement.cpp
|
||||
|
||||
${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
|
||||
)
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "AST/Statement.h"
|
||||
#include "AST/StatementList.h"
|
||||
#include "AST/Type.h"
|
||||
#include "AST/ValueList.h"
|
||||
#include "AST/VariableDefinition.h"
|
||||
|
||||
}
|
||||
@@ -50,7 +51,8 @@ fundefs(A) ::= . { A = 0; }
|
||||
fundefs(A) ::= fundefs fundef(B). { A = A + B; }
|
||||
|
||||
%type fundef {int}
|
||||
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 */ }
|
||||
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"); }
|
||||
@@ -61,11 +63,14 @@ type(A) ::= T_VOID. { A = new Type("void"); }
|
||||
%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; }
|
||||
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 = 1 + S; /* E */ }
|
||||
statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S). { A = 1 + S; /* P */ }
|
||||
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S).
|
||||
{ A = 1 + S; /* E */ }
|
||||
statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S).
|
||||
{ A = 1 + S; /* P */ }
|
||||
statement(A) ::= T_FOR T_LPAREN expr(INIT) T_SEMICOLON expr(COND) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
|
||||
{ A = 1 + 1 + 1 + S; /* INIT COND STEP */ }
|
||||
statement(A) ::= T_RFOR T_LPAREN expr(INIT) T_SEMICOLON T_CINT(P) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
|
||||
@@ -90,13 +95,14 @@ expr(A) ::= expr(B) T_DIV expr(C). { A = new BinOp(B, "/", C); }
|
||||
expr(A) ::= T_CINT(I). { A = new ConstantExpression(I->getText()); }
|
||||
expr(A) ::= T_TRUE. { A = new ConstantExpression("true"); }
|
||||
expr(A) ::= T_FALSE. { A = new ConstantExpression("false"); }
|
||||
expr(A) ::= T_IDENTIFIER(ID) T_LPAREN values(V) T_RPAREN. { A = 0; /* ID V */ }
|
||||
expr(A) ::= T_IDENTIFIER(ID) T_LPAREN values(V) T_RPAREN.
|
||||
{ A = 0; /* ID V */ }
|
||||
|
||||
%type vardef {VariableDefinition*}
|
||||
vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = new VariableDefinition(T, ID->getText()); }
|
||||
|
||||
%type values {int}
|
||||
values(A) ::= . { A = 0; }
|
||||
values(A) ::= expr(E). { A = 1; /* E */ }
|
||||
values(A) ::= values(B) T_COMMA expr(E). { A = B + 1; /* E */ }
|
||||
%type values {ValueList*}
|
||||
values(A) ::= . { A = new ValueList(); }
|
||||
values(A) ::= expr(E). { A = new ValueList(); A->addValue(E); }
|
||||
values(A) ::= values(B) T_COMMA expr(E). { B->addValue(E); A = B; }
|
||||
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
#include "AssignmentExpression.h"
|
||||
#include "BinOp.h"
|
||||
#include "ConstantExpression.h"
|
||||
#include "VariableDefinition.h"
|
||||
#include "IfStatement.h"
|
||||
#include "ParameterList.h"
|
||||
#include "StatementList.h"
|
||||
#include "ValueList.h"
|
||||
#include "VariableDefinition.h"
|
||||
|
||||
class ASTVisitor {
|
||||
public:
|
||||
@@ -16,7 +18,9 @@ public:
|
||||
virtual void visit(AssignmentExpression* e) = 0;
|
||||
virtual void visit(BinOp* e) = 0;
|
||||
virtual void visit(ConstantExpression* e) = 0;
|
||||
virtual void visit(IfStatement* e) = 0;
|
||||
virtual void visit(ParameterList* e) = 0;
|
||||
virtual void visit(ValueList* e) = 0;
|
||||
virtual void visit(VariableDefinition* e) = 0;
|
||||
virtual void visit(StatementList* e) = 0;
|
||||
};
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
#include "AST/Expression.h"
|
||||
#include <string>
|
||||
|
||||
class AssignmentExpression : public Expression
|
||||
{
|
||||
class AssignmentExpression : public Expression {
|
||||
public:
|
||||
AssignmentExpression(std::string id, Expression* expr);
|
||||
virtual ~AssignmentExpression();
|
||||
|
||||
23
inc/AST/IfStatement.h
Normal file
23
inc/AST/IfStatement.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef IFSTATEMENT_H
|
||||
#define IFSTATEMENT_H
|
||||
|
||||
#include "AST/Expression.h"
|
||||
#include "AST/Statement.h"
|
||||
|
||||
class IfStatement : public Statement
|
||||
{
|
||||
public:
|
||||
IfStatement(Expression* cond, Statement* stmt);
|
||||
virtual ~IfStatement();
|
||||
|
||||
virtual void accept(ASTVisitor *visitor);
|
||||
|
||||
Expression* getCond();
|
||||
Statement* getStmt();
|
||||
private:
|
||||
Expression* cond_;
|
||||
Statement* stmt_;
|
||||
|
||||
};
|
||||
|
||||
#endif // IFSTATEMENT_H
|
||||
21
inc/AST/ValueList.h
Normal file
21
inc/AST/ValueList.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef VALUELIST_H
|
||||
#define VALUELIST_H
|
||||
|
||||
#include "AST/ASTElement.h"
|
||||
#include "AST/Expression.h"
|
||||
#include <vector>
|
||||
|
||||
class ValueList : public ASTElement {
|
||||
public:
|
||||
ValueList();
|
||||
~ValueList();
|
||||
|
||||
virtual void accept(ASTVisitor* visitor);
|
||||
|
||||
void addValue(Expression* expr);
|
||||
std::vector<Expression*> getValues();
|
||||
private:
|
||||
std::vector<Expression*> values_;
|
||||
};
|
||||
|
||||
#endif // VALUELIST_H
|
||||
@@ -1,12 +1,12 @@
|
||||
#include "AST/AssignmentExpression.h"
|
||||
#include "AST/ASTVisitor.h"
|
||||
|
||||
AssignmentExpression::AssignmentExpression(std::string id, Expression *expr)
|
||||
{
|
||||
AssignmentExpression::AssignmentExpression(std::string id, Expression *expr) : id_(id), expr_(expr) {
|
||||
//
|
||||
}
|
||||
|
||||
AssignmentExpression::~AssignmentExpression() {
|
||||
//
|
||||
}
|
||||
|
||||
void AssignmentExpression::accept(ASTVisitor *visitor) {
|
||||
|
||||
23
src/AST/IfStatement.cpp
Normal file
23
src/AST/IfStatement.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "AST/IfStatement.h"
|
||||
#include "AST/ASTVisitor.h"
|
||||
|
||||
IfStatement::IfStatement(Expression *cond, Statement *stmt) : cond_(cond),stmt_(stmt)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
IfStatement::~IfStatement(){
|
||||
//
|
||||
}
|
||||
|
||||
void IfStatement::accept(ASTVisitor *visitor) {
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
Expression* IfStatement::getCond() {
|
||||
return cond_;
|
||||
}
|
||||
|
||||
Statement* IfStatement::getStmt() {
|
||||
return stmt_;
|
||||
}
|
||||
22
src/AST/ValueList.cpp
Normal file
22
src/AST/ValueList.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "AST/ValueList.h"
|
||||
#include "AST/ASTVisitor.h"
|
||||
|
||||
ValueList::ValueList() {
|
||||
//
|
||||
}
|
||||
|
||||
ValueList::~ValueList() {
|
||||
//
|
||||
}
|
||||
|
||||
void ValueList::accept(ASTVisitor* visitor) {
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
void ValueList::addValue(Expression* expr) {
|
||||
values_.push_back(expr);
|
||||
}
|
||||
|
||||
std::vector<Expression*> ValueList::getValues() {
|
||||
return values_;
|
||||
}
|
||||
Reference in New Issue
Block a user