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/Type.cpp
|
||||||
src/AST/VariableDefinition.cpp
|
src/AST/VariableDefinition.cpp
|
||||||
src/AST/StatementList.cpp
|
src/AST/StatementList.cpp
|
||||||
|
src/AST/ValueList.cpp
|
||||||
|
src/AST/IfStatement.cpp
|
||||||
|
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
|
${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include "AST/Statement.h"
|
#include "AST/Statement.h"
|
||||||
#include "AST/StatementList.h"
|
#include "AST/StatementList.h"
|
||||||
#include "AST/Type.h"
|
#include "AST/Type.h"
|
||||||
|
#include "AST/ValueList.h"
|
||||||
#include "AST/VariableDefinition.h"
|
#include "AST/VariableDefinition.h"
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -42,61 +43,66 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
%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) 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 type {Type*}
|
||||||
type(A) ::= T_BOOL. { A = new Type("bool"); }
|
type(A) ::= T_BOOL. { A = new Type("bool"); }
|
||||||
type(A) ::= T_INT. { A = new Type("int"); }
|
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 {ParameterList*}
|
%type params {ParameterList*}
|
||||||
params(A) ::= . { A = new ParameterList(); }
|
params(A) ::= . { A = new ParameterList(); }
|
||||||
params(A) ::= type(T) T_IDENTIFIER(ID). { A = new ParameterList(); A->addParameter(T, ID->getText()); }
|
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}
|
%type statement {int}
|
||||||
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S). { A = 1 + S; /* E */ }
|
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S).
|
||||||
statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S). { A = 1 + S; /* P */ }
|
{ 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).
|
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 */ }
|
{ 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).
|
statement(A) ::= T_RFOR T_LPAREN expr(INIT) T_SEMICOLON T_CINT(P) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
|
||||||
{ A = 1 + 1 + 1 + S; /* INIT P STEP */ }
|
{ A = 1 + 1 + 1 + S; /* INIT P STEP */ }
|
||||||
statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = 1; /* E */ }
|
statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = 1; /* E */ }
|
||||||
statement(A) ::= T_BEGIN statements(S) T_END. { A = S; }
|
statement(A) ::= T_BEGIN statements(S) T_END. { A = S; }
|
||||||
statement(A) ::= vardef(V) T_SEMICOLON. { A = 1; /* V */ }
|
statement(A) ::= vardef(V) T_SEMICOLON. { A = 1; /* V */ }
|
||||||
statement(A) ::= expr(E) T_SEMICOLON. { A = 1; /* E */ }
|
statement(A) ::= expr(E) T_SEMICOLON. { A = 1; /* E */ }
|
||||||
|
|
||||||
%type statements {StatementList*}
|
%type statements {StatementList*}
|
||||||
statements(A) ::= . { A = new StatementList(); }
|
statements(A) ::= . { A = new StatementList(); }
|
||||||
statements(A) ::= statements(B) statement(C). { B->addStatement(C); A = B; }
|
statements(A) ::= statements(B) statement(C). { B->addStatement(C); A = B; }
|
||||||
|
|
||||||
%type expr {Expression*}
|
%type expr {Expression*}
|
||||||
expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = new AssignmentExpression(ID->getText(), E); }
|
expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = new AssignmentExpression(ID->getText(), E); }
|
||||||
expr(A) ::= expr(B) T_EQUALS expr(C). { A = new BinOp(B, "==", C); }
|
expr(A) ::= expr(B) T_EQUALS expr(C). { A = new BinOp(B, "==", C); }
|
||||||
expr(A) ::= expr(B) T_LESS expr(C). { A = new BinOp(B, "<", C); }
|
expr(A) ::= expr(B) T_LESS expr(C). { A = new BinOp(B, "<", C); }
|
||||||
expr(A) ::= expr(B) T_PLUS expr(C). { A = new BinOp(B, "+", C); }
|
expr(A) ::= expr(B) T_PLUS expr(C). { A = new BinOp(B, "+", C); }
|
||||||
expr(A) ::= expr(B) T_MINUS expr(C). { A = new BinOp(B, "-", C); }
|
expr(A) ::= expr(B) T_MINUS expr(C). { A = new BinOp(B, "-", C); }
|
||||||
expr(A) ::= expr(B) T_TIMES expr(C). { A = new BinOp(B, "*", C); }
|
expr(A) ::= expr(B) T_TIMES expr(C). { A = new BinOp(B, "*", C); }
|
||||||
expr(A) ::= expr(B) T_DIV expr(C). { A = new BinOp(B, "/", C); }
|
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_CINT(I). { A = new ConstantExpression(I->getText()); }
|
||||||
expr(A) ::= T_TRUE. { A = new ConstantExpression("true"); }
|
expr(A) ::= T_TRUE. { A = new ConstantExpression("true"); }
|
||||||
expr(A) ::= T_FALSE. { A = new ConstantExpression("false"); }
|
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*}
|
%type vardef {VariableDefinition*}
|
||||||
vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = new VariableDefinition(T, ID->getText()); }
|
vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = new VariableDefinition(T, ID->getText()); }
|
||||||
|
|
||||||
%type values {int}
|
%type values {ValueList*}
|
||||||
values(A) ::= . { A = 0; }
|
values(A) ::= . { A = new ValueList(); }
|
||||||
values(A) ::= expr(E). { A = 1; /* E */ }
|
values(A) ::= expr(E). { A = new ValueList(); A->addValue(E); }
|
||||||
values(A) ::= values(B) T_COMMA expr(E). { A = B + 1; /* E */ }
|
values(A) ::= values(B) T_COMMA expr(E). { B->addValue(E); A = B; }
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,11 @@
|
|||||||
#include "AssignmentExpression.h"
|
#include "AssignmentExpression.h"
|
||||||
#include "BinOp.h"
|
#include "BinOp.h"
|
||||||
#include "ConstantExpression.h"
|
#include "ConstantExpression.h"
|
||||||
#include "VariableDefinition.h"
|
#include "IfStatement.h"
|
||||||
#include "ParameterList.h"
|
#include "ParameterList.h"
|
||||||
#include "StatementList.h"
|
#include "StatementList.h"
|
||||||
|
#include "ValueList.h"
|
||||||
|
#include "VariableDefinition.h"
|
||||||
|
|
||||||
class ASTVisitor {
|
class ASTVisitor {
|
||||||
public:
|
public:
|
||||||
@@ -16,7 +18,9 @@ public:
|
|||||||
virtual void visit(AssignmentExpression* e) = 0;
|
virtual void visit(AssignmentExpression* e) = 0;
|
||||||
virtual void visit(BinOp* e) = 0;
|
virtual void visit(BinOp* e) = 0;
|
||||||
virtual void visit(ConstantExpression* e) = 0;
|
virtual void visit(ConstantExpression* e) = 0;
|
||||||
|
virtual void visit(IfStatement* e) = 0;
|
||||||
virtual void visit(ParameterList* e) = 0;
|
virtual void visit(ParameterList* e) = 0;
|
||||||
|
virtual void visit(ValueList* e) = 0;
|
||||||
virtual void visit(VariableDefinition* e) = 0;
|
virtual void visit(VariableDefinition* e) = 0;
|
||||||
virtual void visit(StatementList* e) = 0;
|
virtual void visit(StatementList* e) = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,19 +4,18 @@
|
|||||||
#include "AST/Expression.h"
|
#include "AST/Expression.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class AssignmentExpression : public Expression
|
class AssignmentExpression : public Expression {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
AssignmentExpression(std::string id, Expression* expr);
|
AssignmentExpression(std::string id, Expression* expr);
|
||||||
virtual ~AssignmentExpression();
|
virtual ~AssignmentExpression();
|
||||||
|
|
||||||
virtual void accept(ASTVisitor *visitor);
|
virtual void accept(ASTVisitor *visitor);
|
||||||
|
|
||||||
std::string getId();
|
std::string getId();
|
||||||
Expression* getExpr();
|
Expression* getExpr();
|
||||||
private:
|
private:
|
||||||
std::string id_;
|
std::string id_;
|
||||||
Expression* expr_;
|
Expression* expr_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ASSIGNMENTEXPRESSION_H
|
#endif // ASSIGNMENTEXPRESSION_H
|
||||||
|
|||||||
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,22 +1,22 @@
|
|||||||
#include "AST/AssignmentExpression.h"
|
#include "AST/AssignmentExpression.h"
|
||||||
#include "AST/ASTVisitor.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() {
|
AssignmentExpression::~AssignmentExpression() {
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssignmentExpression::accept(ASTVisitor *visitor) {
|
void AssignmentExpression::accept(ASTVisitor *visitor) {
|
||||||
visitor->visit(this);
|
visitor->visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string AssignmentExpression::getId() {
|
std::string AssignmentExpression::getId() {
|
||||||
return id_;
|
return id_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression* AssignmentExpression::getExpr() {
|
Expression* AssignmentExpression::getExpr() {
|
||||||
return expr_;
|
return expr_;
|
||||||
}
|
}
|
||||||
|
|||||||
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