Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
feature-procedures: ajout compilation procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
Feelzor committed Nov 5, 2019
1 parent e19fee6 commit 96deda1
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Procedure.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "Procedure.h"
#include "Visiteur.h"

Procedure::Procedure(TableSymboles table, Noeud* sequence, std::vector<SymboleValue*> parametres)
: m_table(table), m_sequence(sequence), m_params(parametres) {}

void Procedure::accepter(Visiteur& visiteur) {
m_sequence->accepter(visiteur);
visiteur.visiterProcedure(this);
}

void Procedure::empiler(std::vector<Valeur*> parametres) {
Expand Down
7 changes: 7 additions & 0 deletions Procedure.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ class Procedure : public Noeud {
Procedure(TableSymboles table, Noeud* sequence, std::vector<SymboleValue*> parametres);
void accepter(Visiteur& visiteur) override;
void empiler(std::vector<Valeur*> parametres);

inline void setNom(std::string nom) { m_nom = nom; }

inline std::string getNom() const { return m_nom; }
inline Noeud* getSequence() const { return m_sequence; }
inline std::vector<SymboleValue*> getParametres() const { return m_params; }
private:
TableSymboles m_table;
Noeud* m_sequence;
std::vector<SymboleValue*> m_params;
std::string m_nom;
};

#endif /* PROCEDURE_H */
Expand Down
10 changes: 9 additions & 1 deletion TableProcedures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ TableProcedures* TableProcedures::getTable() {

void TableProcedures::ajoutProcedure(Symbole nom, Procedure* procedure) {
std::string nomProcedure(nom.getChaine());
m_noms.push_back(nomProcedure);
m_procedures.push_back(procedure);
m_noms.push_back(nom.getChaine());
procedure->setNom(nomProcedure);
}

void TableProcedures::executerProcedure(Symbole nom, std::vector<Noeud*> parametres, VisiteurExecuter& v) {
Expand All @@ -30,4 +31,11 @@ void TableProcedures::executerProcedure(Symbole nom, std::vector<Noeud*> paramet
}
p->empiler(params);
p->accepter(v);
}

void TableProcedures::compilerProcedures(VisiteurCompiler& v) {
for (int i = 0; i < m_procedures.size(); i++) {
Procedure* procedure = m_procedures[i];
procedure->accepter(v);
}
}
2 changes: 2 additions & 0 deletions TableProcedures.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include "Symbole.h"
#include "Procedure.h"
#include "VisiteurExecuter.h"
#include "VisiteurCompiler.h"

class TableProcedures {
public:
static TableProcedures* getTable();
void ajoutProcedure(Symbole nom, Procedure* procedure);
void executerProcedure(Symbole nom, std::vector<Noeud*> parametres, VisiteurExecuter& v);
void compilerProcedures(VisiteurCompiler& v);
private:
TableProcedures();
static TableProcedures* table;
Expand Down
2 changes: 2 additions & 0 deletions Visiteur.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

#include "ArbreAbstrait.h"
#include "SymboleValue.h"
#include "Procedure.h"

class Visiteur {
public:
virtual void visiterProcedure(Procedure* procedure) = 0;
virtual void visiterNoeudSeqInst(NoeudSeqInst* noeud) = 0;
virtual void visiterNoeudAffectation(NoeudAffectation* noeud) = 0;
virtual void visiterNoeudOperateurBinaire(NoeudOperateurBinaire* noeud) = 0;
Expand Down
14 changes: 14 additions & 0 deletions VisiteurCompiler.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#include "VisiteurCompiler.h"
#include "Procedure.h"

VisiteurCompiler::VisiteurCompiler(std::ostream& out, int indentation)
: m_out(out), m_indentation(indentation) {}

void VisiteurCompiler::visiterProcedure(Procedure* procedure) {
m_out << "def " << procedure->getNom() << "(";
bool first = true;
for (SymboleValue* param : procedure->getParametres()) {
if (!first) { m_out << ", "; first = false; }
param->accepter(*this);
}
m_out << "):" << endl;
m_indentation++;
procedure->getSequence()->accepter(*this);
m_indentation--;
}

void VisiteurCompiler::visiterNoeudSeqInst(NoeudSeqInst* noeud) {
for (unsigned int i = 0; i < noeud->getInstructions().size(); i++) {
m_out << std::string(m_indentation * 4, ' ');
Expand Down
1 change: 1 addition & 0 deletions VisiteurCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class VisiteurCompiler : public Visiteur {
public:
VisiteurCompiler(std::ostream& out, int indentation);

void visiterProcedure(Procedure* procedure) override;
void visiterNoeudSeqInst(NoeudSeqInst* noeud) override;
void visiterNoeudAffectation(NoeudAffectation* noeud) override;
void visiterNoeudOperateurBinaire(NoeudOperateurBinaire* noeud) override;
Expand Down
4 changes: 4 additions & 0 deletions VisiteurExecuter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "ValeurEntiere.h"
#include "TableProcedures.h"

void VisiteurExecuter::visiterProcedure(Procedure* procedure) {
procedure->getSequence()->accepter(*this);
}

void VisiteurExecuter::visiterNoeudSeqInst(NoeudSeqInst* noeud) {
for (unsigned int i = 0; i < noeud->getInstructions().size(); i++)
noeud->getInstructions()[i]->accepter(*this); // on exécute chaque instruction de la séquence
Expand Down
1 change: 1 addition & 0 deletions VisiteurExecuter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class VisiteurExecuter : public Visiteur {
public:
void visiterProcedure(Procedure* procedure) override;
void visiterNoeudSeqInst(NoeudSeqInst* noeud) override;
void visiterNoeudAffectation(NoeudAffectation* noeud) override;
void visiterNoeudOperateurBinaire(NoeudOperateurBinaire* noeud) override;
Expand Down
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using namespace std;
#include "VisiteurCompiler.h"

#include "ValeurEntiere.h"
#include "TableProcedures.h"

void compiler(ostream& out, const TableSymboles& symboles, Noeud* arbre);

Expand Down Expand Up @@ -73,5 +74,6 @@ int main(int argc, char* argv[]) {

void compiler(ostream& out, const TableSymboles& symboles, Noeud* arbre) {
VisiteurCompiler visiteur(out, 0);
TableProcedures::getTable()->compilerProcedures(visiteur);
arbre->accepter(visiteur);
}

0 comments on commit 96deda1

Please sign in to comment.