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

Commit

Permalink
feature-visiteur: implémentation pattern visiteur pour exécuter
Browse files Browse the repository at this point in the history
  • Loading branch information
Feelzor committed Oct 30, 2019
1 parent d3a2170 commit 287d95f
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 116 deletions.
132 changes: 37 additions & 95 deletions ArbreAbstrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Symbole.h"
#include "SymboleValue.h"
#include "Exceptions.h"
#include "Visiteur.h"

////////////////////////////////////////////////////////////////////////////////
// NoeudSeqInst
Expand All @@ -12,13 +13,6 @@
NoeudSeqInst::NoeudSeqInst() : m_instructions() {
}

int NoeudSeqInst::executer() {
for (unsigned int i = 0; i < m_instructions.size(); i++)
m_instructions[i]->executer(); // on exécute chaque instruction de la séquence
return 0; // La valeur renvoyée ne représente rien !
}


void NoeudSeqInst::compiler(ostream & out, int indentation) {
for (unsigned int i = 0; i < m_instructions.size(); i++) {
cout << std::string(indentation * 4, ' ');
Expand All @@ -27,6 +21,10 @@ void NoeudSeqInst::compiler(ostream & out, int indentation) {
}
}

void NoeudSeqInst::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudSeqInst(this);
}

void NoeudSeqInst::ajoute(Noeud* instruction) {
if (instruction!=nullptr) m_instructions.push_back(instruction);
}
Expand All @@ -39,12 +37,6 @@ NoeudAffectation::NoeudAffectation(Noeud* variable, Noeud* expression)
: m_variable(variable), m_expression(expression) {
}

int NoeudAffectation::executer() {
int valeur = m_expression->executer(); // On exécute (évalue) l'expression
((SymboleValue*) m_variable)->setValeur(valeur); // On affecte la variable
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudAffectation::compiler(ostream & out, int indentation) {
bool instructionSeule = true;
if (indentation < 0) {
Expand All @@ -58,6 +50,10 @@ void NoeudAffectation::compiler(ostream & out, int indentation) {
if (instructionSeule) out << ";";
}

void NoeudAffectation::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudAffectation(this);
}

////////////////////////////////////////////////////////////////////////////////
// NoeudOperateurBinaire
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -66,30 +62,6 @@ NoeudOperateurBinaire::NoeudOperateurBinaire(Symbole operateur, Noeud* operandeG
: m_operateur(operateur), m_operandeGauche(operandeGauche), m_operandeDroit(operandeDroit) {
}

int NoeudOperateurBinaire::executer() {
int og, od, valeur;
if (m_operandeGauche != nullptr) og = m_operandeGauche->executer(); // On évalue l'opérande gauche
if (m_operandeDroit != nullptr) od = m_operandeDroit->executer(); // On évalue l'opérande droit
// Et on combine les deux opérandes en fonctions de l'opérateur
if (this->m_operateur == "+") valeur = (og + od);
else if (this->m_operateur == "-") valeur = (og - od);
else if (this->m_operateur == "*") valeur = (og * od);
else if (this->m_operateur == "==") valeur = (og == od);
else if (this->m_operateur == "!=") valeur = (og != od);
else if (this->m_operateur == "<") valeur = (og < od);
else if (this->m_operateur == ">") valeur = (og > od);
else if (this->m_operateur == "<=") valeur = (og <= od);
else if (this->m_operateur == ">=") valeur = (og >= od);
else if (this->m_operateur == "et") valeur = (og && od);
else if (this->m_operateur == "ou") valeur = (og || od);
else if (this->m_operateur == "non") valeur = (!og);
else if (this->m_operateur == "/") {
if (od == 0) throw DivParZeroException();
valeur = og / od;
}
return valeur; // On retourne la valeur calculée
}

void NoeudOperateurBinaire::compiler(ostream & out, int indentation) {
std::string op = m_operateur.getChaine();

Expand All @@ -108,6 +80,10 @@ void NoeudOperateurBinaire::compiler(ostream & out, int indentation) {
out << ")";
}

void NoeudOperateurBinaire::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudOperateurBinaire(this);
}

////////////////////////////////////////////////////////////////////////////////
// NoeudInstSi
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -116,14 +92,6 @@ NoeudInstSi::NoeudInstSi(Noeud* condition, Noeud* sequence)
: m_condition(condition), m_sequence(sequence), m_prochaineCondition(nullptr), m_isPremiereCondition(true) {
}

int NoeudInstSi::executer() {
if (m_condition != nullptr && m_condition->executer()) m_sequence->executer();
else if (m_prochaineCondition != nullptr) {
m_prochaineCondition->executer();
}
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudInstSi::compiler(ostream & out, int indentation) {
std::string indent(indentation * 4, ' ');
if (m_condition != nullptr) {
Expand Down Expand Up @@ -154,6 +122,10 @@ void NoeudInstSi::ajoute(Noeud* condition) {
}
}

void NoeudInstSi::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudInstSi(this);
}

void NoeudInstSi::setIsPremiereCondition(bool value) {
m_isPremiereCondition = value;
}
Expand All @@ -166,13 +138,6 @@ NoeudInstRepeter::NoeudInstRepeter(Noeud* instruction, Noeud* condition) : m_seq

}

int NoeudInstRepeter::executer(){
do{ m_sequence->executer();
}while( ! m_condition->executer());

return 0;
}

void NoeudInstRepeter::compiler(ostream & out, int indentation) {
std::string indent(indentation * 4, ' ');
out << "do {" << endl;
Expand All @@ -182,6 +147,10 @@ void NoeudInstRepeter::compiler(ostream & out, int indentation) {
out << "));" << endl;
}

void NoeudInstRepeter::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudInstRepeter(this);
}

////////////////////////////////////////////////////////////////////////////////
// NoeudInstPour
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -190,15 +159,6 @@ NoeudInstPour::NoeudInstPour(Noeud* init, Noeud* condition, Noeud* affect, Noeud
: m_init(init), m_condition(condition), m_affectation(affect), m_sequence(sequence) {
}

int NoeudInstPour::executer() {
if (m_init != nullptr) m_init->executer();
while (m_condition->executer()) {
m_sequence->executer();
if (m_affectation != nullptr) m_affectation->executer();
}
return 0;
}

void NoeudInstPour::compiler(ostream & out, int indentation) {
std::string indent(4 * indentation, ' ');
out << "for (";
Expand All @@ -212,6 +172,10 @@ void NoeudInstPour::compiler(ostream & out, int indentation) {
out << indent << "}" << endl;
}

void NoeudInstPour::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudInstPour(this);
}

////////////////////////////////////////////////////////////////////////////////
// NoeudInstTantQue
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -220,11 +184,6 @@ NoeudInstTantQue::NoeudInstTantQue(Noeud* condition, Noeud* sequence)
: m_condition(condition), m_sequence(sequence) {
}

int NoeudInstTantQue::executer() {
while (m_condition->executer()) m_sequence->executer();
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudInstTantQue::compiler(ostream & out, int indentation) {
std::string indent(indentation * 4, ' ');
out << "while " ;
Expand All @@ -234,30 +193,17 @@ void NoeudInstTantQue::compiler(ostream & out, int indentation) {
out << indent << "}" << endl;
}

void NoeudInstTantQue::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudInstTantQue(this);
}

////////////////////////////////////////////////////////////////////////////////
// NoeudInstLire
////////////////////////////////////////////////////////////////////////////////
NoeudInstLire::NoeudInstLire() {
m_variables = vector<Noeud*>();
}

int NoeudInstLire::executer() {
for(Noeud* var : m_variables){
int val;
cout << "Saisir un entier : ";
cin >> val;
if(!cin.fail()){
((SymboleValue*)var)->setValeur(val);
} else {
cout << "La valeur que vous avez saisie n'est pas un entier. La valeur zéro a été attribuée par défaut." << endl;
cin.clear();
cin.ignore(10000,'\n');
((SymboleValue*)var)->setValeur(0);
}
}
return 0;
}

void NoeudInstLire::compiler(ostream & out, int indentation) {
out << "std::cin";
for (Noeud* var : m_variables) {
Expand All @@ -268,6 +214,10 @@ void NoeudInstLire::compiler(ostream & out, int indentation) {
out << ";";
}

void NoeudInstLire::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudInstLire(this);
}

void NoeudInstLire::ajoute(Noeud* var){
m_variables.push_back(var);
}
Expand All @@ -283,18 +233,6 @@ void NoeudInstEcrire::ajoute(Noeud* instruction) {
m_ecritures.push_back(instruction);
}

int NoeudInstEcrire::executer() {
for (Noeud* inst : m_ecritures) {
if ((typeid (*inst) == typeid (SymboleValue) && *((SymboleValue*) inst) == "<CHAINE>")) {
string s = ((SymboleValue*) inst)->getChaine();
cout << s.substr(1, s.size() - 2); // On retire le premier et le dernier caractère (les ")
} else {
cout << inst->executer();
}
}
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudInstEcrire::compiler(ostream & out, int indentation) {
out << "std::cout";
for (Noeud* inst : m_ecritures) {
Expand All @@ -307,3 +245,7 @@ void NoeudInstEcrire::compiler(ostream & out, int indentation) {
}
out << ";";
}

void NoeudInstEcrire::accepter(Visiteur& visiteur) {
visiteur.visiterNoeudInstEcrire(this);
}
Loading

0 comments on commit 287d95f

Please sign in to comment.