FunctionDefinition plus use more stuff in the grammar
This commit is contained in:
@@ -65,15 +65,16 @@ SET(SCULLY_SOURCE
|
||||
src/AST/ConstantExpression.cpp
|
||||
src/AST/Expression.cpp
|
||||
src/AST/ExpressionStatement.cpp
|
||||
src/AST/FunctionDefinition.cpp
|
||||
src/AST/IfStatement.cpp
|
||||
src/AST/ParameterList.cpp
|
||||
src/AST/ReturnStatement.cpp
|
||||
src/AST/Scope.cpp
|
||||
src/AST/Statement.cpp
|
||||
src/AST/StatementList.cpp
|
||||
src/AST/Type.cpp
|
||||
src/AST/VariableDefinition.cpp
|
||||
src/AST/StatementList.cpp
|
||||
src/AST/ValueList.cpp
|
||||
src/AST/IfStatement.cpp
|
||||
src/AST/ReturnStatement.cpp
|
||||
|
||||
${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp
|
||||
)
|
||||
|
||||
@@ -12,8 +12,11 @@
|
||||
#include "AST/BinOp.h"
|
||||
#include "AST/ConstantExpression.h"
|
||||
#include "AST/Expression.h"
|
||||
#include "AST/ExpressionStatement.h"
|
||||
#include "AST/FunctionDefinition.h"
|
||||
#include "AST/IfStatement.h"
|
||||
#include "AST/ParameterList.h"
|
||||
#include "AST/ReturnStatement.h"
|
||||
#include "AST/Scope.h"
|
||||
#include "AST/Statement.h"
|
||||
#include "AST/StatementList.h"
|
||||
@@ -45,16 +48,12 @@
|
||||
}
|
||||
|
||||
%type program {int}
|
||||
program ::= fundefs(F). { std::cout << F << std::endl; }
|
||||
program ::= fundef(F). { std::cout << F << 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}
|
||||
%type fundef {FunctionDefinition*}
|
||||
fundef(A) ::= type(T) T_IDENTIFIER(ID) T_LPAREN params(P) T_RPAREN T_BEGIN statements(S) T_END.
|
||||
{ A = 1 + 1 + 1 + 1; /* T ID P S */ }
|
||||
{ A = new FunctionDefinition(T, ID->getText(), P, S); }
|
||||
|
||||
%type type {Type*}
|
||||
type(A) ::= T_BOOL. { A = new Type("bool"); }
|
||||
@@ -77,10 +76,10 @@ statement(A) ::= T_FOR T_LPAREN expr(INIT) T_SEMICOLON expr(COND) T_SEMICOLON ex
|
||||
{ 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 = 0; /* INIT P STEP S */ }
|
||||
statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = 0; /* E */ }
|
||||
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 = 0; /* V */ }
|
||||
statement(A) ::= expr(E) T_SEMICOLON. { A = 0; /* E */ }
|
||||
statement(A) ::= vardef(V) T_SEMICOLON. { A = V; /* V */ }
|
||||
statement(A) ::= expr(E) T_SEMICOLON. { A = new ExpressionStatement(E); }
|
||||
|
||||
%type statements {StatementList*}
|
||||
statements(A) ::= . { A = new StatementList(); }
|
||||
|
||||
@@ -4,14 +4,15 @@
|
||||
#include "AssignmentExpression.h"
|
||||
#include "BinOp.h"
|
||||
#include "ConstantExpression.h"
|
||||
#include "ExpressionStatement.h"
|
||||
#include "FunctionDefinition.h"
|
||||
#include "IfStatement.h"
|
||||
#include "ParameterList.h"
|
||||
#include "ReturnStatement.h"
|
||||
#include "Scope.h"
|
||||
#include "StatementList.h"
|
||||
#include "ValueList.h"
|
||||
#include "VariableDefinition.h"
|
||||
#include "ExpressionStatement.h"
|
||||
#include "ReturnStatement.h"
|
||||
|
||||
|
||||
class ASTVisitor {
|
||||
@@ -23,13 +24,14 @@ public:
|
||||
virtual void visit(BinOp* e) = 0;
|
||||
virtual void visit(ConstantExpression* e) = 0;
|
||||
virtual void visit(ExpressionStatement* e) = 0;
|
||||
virtual void visit(FunctionDefinition* e) = 0;
|
||||
virtual void visit(IfStatement* e) = 0;
|
||||
virtual void visit(ParameterList* e) = 0;
|
||||
virtual void visit(Scope* e) = 0;
|
||||
virtual void visit(StatementList* e) = 0;
|
||||
virtual void visit(ReturnStatement* e) = 0;
|
||||
virtual void visit(Scope* e) = 0;
|
||||
virtual void visit(StatementList* e) = 0;
|
||||
virtual void visit(ValueList* e) = 0;
|
||||
virtual void visit(VariableDefinition* e) = 0;
|
||||
virtual void visit(ReturnStatement* e) = 0;
|
||||
};
|
||||
|
||||
#endif // ASTVISITOR_H
|
||||
|
||||
28
inc/AST/FunctionDefinition.h
Normal file
28
inc/AST/FunctionDefinition.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef FUNCTIONDEFINITION_H
|
||||
#define FUNCTIONDEFINITION_H
|
||||
|
||||
#include "AST/ASTElement.h"
|
||||
#include "AST/ParameterList.h"
|
||||
#include "AST/StatementList.h"
|
||||
#include "AST/Type.h"
|
||||
#include <string>
|
||||
|
||||
class FunctionDefinition : public ASTElement {
|
||||
public:
|
||||
FunctionDefinition(Type *type, std::string name, ParameterList* params, StatementList* sl);
|
||||
virtual ~FunctionDefinition();
|
||||
|
||||
virtual void accept(ASTVisitor* visitor);
|
||||
|
||||
Type* getType();
|
||||
std::string getName();
|
||||
ParameterList* getParams();
|
||||
StatementList* getSl();
|
||||
private:
|
||||
Type* type_;
|
||||
std::string name_;
|
||||
ParameterList* params_;
|
||||
StatementList* sl_;
|
||||
};
|
||||
|
||||
#endif // FUNCTIONDEFINITION_H
|
||||
30
src/AST/FunctionDefinition.cpp
Normal file
30
src/AST/FunctionDefinition.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "AST/FunctionDefinition.h"
|
||||
#include "AST/ASTVisitor.h"
|
||||
|
||||
FunctionDefinition::FunctionDefinition(Type *type, std::string name, ParameterList *params, StatementList *sl) : type_(type), name_(name), params_(params), sl_(sl) {
|
||||
//
|
||||
}
|
||||
|
||||
FunctionDefinition::~FunctionDefinition() {
|
||||
//
|
||||
}
|
||||
|
||||
void FunctionDefinition::accept(ASTVisitor* visitor) {
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
Type* FunctionDefinition::getType() {
|
||||
return type_;
|
||||
}
|
||||
|
||||
std::string FunctionDefinition::getName() {
|
||||
return name_;
|
||||
}
|
||||
|
||||
ParameterList* FunctionDefinition::getParams() {
|
||||
return params_;
|
||||
}
|
||||
|
||||
StatementList* FunctionDefinition::getSl() {
|
||||
return sl_;
|
||||
}
|
||||
Reference in New Issue
Block a user