Add ForStatement

This commit is contained in:
Markus Hauschild
2013-06-01 03:02:14 +02:00
parent a2ee427f57
commit 71abd2b56f
5 changed files with 65 additions and 5 deletions

View File

@@ -65,6 +65,7 @@ SET(SCULLY_SOURCE
src/AST/ConstantExpression.cpp
src/AST/Expression.cpp
src/AST/ExpressionStatement.cpp
src/AST/ForStatement.cpp
src/AST/FunctionDefinition.cpp
src/AST/IfStatement.cpp
src/AST/ParameterList.cpp

View File

@@ -13,9 +13,11 @@
#include "AST/ConstantExpression.h"
#include "AST/Expression.h"
#include "AST/ExpressionStatement.h"
#include "AST/ForStatement.h"
#include "AST/FunctionDefinition.h"
#include "AST/IfStatement.h"
#include "AST/ParameterList.h"
#include "AST/RandomIfStatement.h"
#include "AST/ReturnStatement.h"
#include "AST/Scope.h"
#include "AST/Statement.h"
@@ -70,15 +72,15 @@ params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID).
%type statement {Statement*}
statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S).
{ A = new IfStatement(E, S); }
statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S).
{ A = 0; /* P S */ }
statement(A) ::= T_RIF T_LPAREN expr(P) T_RPAREN statement(S).
{ A = new RandomIfStatement(P, S); }
statement(A) ::= T_FOR T_LPAREN expr(INIT) T_SEMICOLON expr(COND) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
{ A = 0; /* INIT COND STEP S */ }
statement(A) ::= T_RFOR T_LPAREN expr(INIT) T_SEMICOLON T_CINT(P) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
{ A = new ForStatement(INIT, COND, STEP, S); }
statement(A) ::= T_RFOR T_LPAREN expr(INIT) T_SEMICOLON expr(P) T_SEMICOLON expr(STEP) T_RPAREN statement(S).
{ A = 0; /* INIT P STEP S */ }
statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = new ReturnStatement(E); }
statement(A) ::= T_BEGIN statements(S) T_END. { A = new Scope(S); }
statement(A) ::= vardef(V) T_SEMICOLON. { A = V; /* V */ }
statement(A) ::= vardef(V) T_SEMICOLON. { A = V; }
statement(A) ::= expr(E) T_SEMICOLON. { A = new ExpressionStatement(E); }
%type statements {StatementList*}

View File

@@ -5,6 +5,7 @@
#include "BinOp.h"
#include "ConstantExpression.h"
#include "ExpressionStatement.h"
#include "ForStatement.h"
#include "FunctionDefinition.h"
#include "IfStatement.h"
#include "ParameterList.h"
@@ -25,6 +26,7 @@ public:
virtual void visit(BinOp* e) = 0;
virtual void visit(ConstantExpression* e) = 0;
virtual void visit(ExpressionStatement* e) = 0;
virtual void visit(ForStatement* e) = 0;
virtual void visit(FunctionDefinition* e) = 0;
virtual void visit(IfStatement* e) = 0;
virtual void visit(ParameterList* e) = 0;

25
inc/AST/ForStatement.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef FORSTATEMENT_H
#define FORSTATEMENT_H
#include "AST/Expression.h"
#include "AST/Statement.h"
class ForStatement : public Statement {
public:
ForStatement(Expression* init, Expression* cond, Expression* step, Statement* stmt);
virtual ~ForStatement();
virtual void accept(ASTVisitor* visitor);
Expression* getInit();
Expression* getCond();
Expression* getStep();
Statement* GetStmt();
private:
Expression* init_;
Expression* cond_;
Expression* step_;
Statement* stmt_;
};
#endif // FORSTATEMENT_H

30
src/AST/ForStatement.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "AST/ForStatement.h"
#include "AST/ASTVisitor.h"
ForStatement::ForStatement(Expression *init, Expression *cond, Expression *step, Statement *stmt) : init_(init), cond_(cond), step_(step), stmt_(stmt) {
//
}
ForStatement::~ForStatement() {
//
}
void ForStatement::accept(ASTVisitor* visitor) {
visitor->visit(this);
}
Expression* ForStatement::getInit() {
return init_;
}
Expression* ForStatement::getCond() {
return cond_;
}
Expression* ForStatement::getStep() {
return step_;
}
Statement* ForStatement::GetStmt() {
return stmt_;
}