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

Commit

Permalink
feautre-compile-seqinst: fix ; à la fin d'une affectation imbriquée d…
Browse files Browse the repository at this point in the history
…ans un bloc de structure
  • Loading branch information
Feelzor committed Oct 24, 2019
1 parent 0cea53b commit 3e2b5c6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
51 changes: 28 additions & 23 deletions ArbreAbstrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int NoeudSeqInst::executer() {
}


void NoeudSeqInst::compiler(ostream & out, unsigned int indentation) {
void NoeudSeqInst::compiler(ostream & out, int indentation) {
for (unsigned int i = 0; i < m_instructions.size(); i++) {
cout << std::string(indentation * 4, ' ');
m_instructions[i]->compiler(out, indentation);
Expand All @@ -45,12 +45,17 @@ int NoeudAffectation::executer() {
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudAffectation::compiler(ostream & out, unsigned int indentation) {
std::string indent(indentation * 4 , ' ');
m_variable->compiler(out,0);
void NoeudAffectation::compiler(ostream & out, int indentation) {
bool instructionSeule = true;
if (indentation < 0) {
indentation = 0;
instructionSeule = false;
}

m_variable->compiler(out, -1);
out << "=";
m_expression->compiler(out,0);
out << ";";
m_expression->compiler(out, -1);
if (instructionSeule) out << ";";
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -85,20 +90,20 @@ int NoeudOperateurBinaire::executer() {
return valeur; // On retourne la valeur calculée
}

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

out << "(";
if (op == "non") {
out << "!";
m_operandeGauche->compiler(out, 0);
m_operandeGauche->compiler(out, -1);
} else {
if (op == "et") op = "&&";
else if (op == "ou") op = "||";

m_operandeGauche->compiler(out, 0);
m_operandeGauche->compiler(out, -1);
out << op;
m_operandeDroit->compiler(out, 0);
m_operandeDroit->compiler(out, -1);
}
out << ")";
}
Expand All @@ -119,12 +124,12 @@ int NoeudInstSi::executer() {
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudInstSi::compiler(ostream & out, unsigned int indentation) {
void NoeudInstSi::compiler(ostream & out, int indentation) {
std::string indent(indentation * 4, ' ');
if (m_condition != nullptr) {
if (!m_isPremiereCondition) { out << ' '; }
out << "if (";
m_condition->compiler(out, 0);
m_condition->compiler(out, -1);
out << ")";
}

Expand Down Expand Up @@ -168,12 +173,12 @@ int NoeudInstRepeter::executer(){
return 0;
}

void NoeudInstRepeter::compiler(ostream & out, unsigned int indentation) {
void NoeudInstRepeter::compiler(ostream & out, int indentation) {
std::string indent(indentation * 4, ' ');
out << "do {" << endl;
m_sequence->compiler(out, indentation + 1);
out << indent << "} while (!(";
m_condition->compiler(out, 0);
m_condition->compiler(out, -1);
out << "));" << endl;
}

Expand All @@ -194,14 +199,14 @@ int NoeudInstPour::executer() {
return 0;
}

void NoeudInstPour::compiler(ostream & out, unsigned int indentation) {
void NoeudInstPour::compiler(ostream & out, int indentation) {
std::string indent(4 * indentation, ' ');
out << "for (";
if (m_init != nullptr) m_init->compiler(out, 0);
if (m_init != nullptr) m_init->compiler(out, -1);
out << ";";
m_condition->compiler(out, 0);
m_condition->compiler(out, -1);
out << ";";
if (m_affectation != nullptr) m_affectation->compiler(out, 0);
if (m_affectation != nullptr) m_affectation->compiler(out, -1);
out << ") {" << endl;
m_sequence->compiler(out, indentation + 1);
out << indent << "}" << endl;
Expand All @@ -220,7 +225,7 @@ int NoeudInstTantQue::executer() {
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudInstTantQue::compiler(ostream & out, unsigned int indentation) {
void NoeudInstTantQue::compiler(ostream & out, int indentation) {
std::string indent(indentation * 4, ' ');
out << "while " ;
m_condition->compiler(out,indentation);
Expand Down Expand Up @@ -253,11 +258,11 @@ int NoeudInstLire::executer() {
return 0;
}

void NoeudInstLire::compiler(ostream & out, unsigned int indentation) {
void NoeudInstLire::compiler(ostream & out, int indentation) {
out << "std::cin";
for (Noeud* var : m_variables) {
out << " >> ";
var->compiler(out, indentation);
var->compiler(out, -1);
}

out << ";";
Expand Down Expand Up @@ -290,14 +295,14 @@ int NoeudInstEcrire::executer() {
return 0; // La valeur renvoyée ne représente rien !
}

void NoeudInstEcrire::compiler(ostream & out, unsigned int indentation) {
void NoeudInstEcrire::compiler(ostream & out, int indentation) {
out << "std::cout";
for (Noeud* inst : m_ecritures) {
out << " << ";
if ((typeid (*inst) == typeid (SymboleValue) && *((SymboleValue*) inst) == "<CHAINE>")) {
out << ((SymboleValue*) inst)->getChaine();
} else {
inst->compiler(out, 0);
inst->compiler(out, -1);
}
}
out << ";";
Expand Down
20 changes: 10 additions & 10 deletions ArbreAbstrait.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Noeud {
// Remarque : la classe ne contient aucun constructeur
public:
virtual int executer() =0 ; // Méthode pure (non implémentée) qui rend la classe abstraite
virtual void compiler(ostream & out, unsigned int indentation) =0 ;
virtual void compiler(ostream & out, int indentation) =0 ;
virtual void ajoute(Noeud* instruction) { throw OperationInterditeException(); }
virtual ~Noeud() {} // Présence d'un destructeur virtuel conseillée dans les classes abstraites
};
Expand All @@ -31,7 +31,7 @@ class NoeudSeqInst : public Noeud {
NoeudSeqInst(); // Construit une séquence d'instruction vide
~NoeudSeqInst() {} // A cause du destructeur virtuel de la classe Noeud
int executer() override; // Exécute chaque instruction de la séquence
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;
void ajoute(Noeud* instruction) override; // Ajoute une instruction à la séquence

private:
Expand All @@ -46,7 +46,7 @@ class NoeudAffectation : public Noeud {
NoeudAffectation(Noeud* variable, Noeud* expression); // construit une affectation
~NoeudAffectation() {} // A cause du destructeur virtuel de la classe Noeud
int executer() override; // Exécute (évalue) l'expression et affecte sa valeur à la variable
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

private:
Noeud* m_variable;
Expand All @@ -62,7 +62,7 @@ class NoeudOperateurBinaire : public Noeud {
// Construit une opération binaire : operandeGauche operateur OperandeDroit
~NoeudOperateurBinaire() {} // A cause du destructeur virtuel de la classe Noeud
int executer() override; // Exécute (évalue) l'opération binaire)
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

private:
Symbole m_operateur;
Expand All @@ -79,7 +79,7 @@ class NoeudInstSi : public Noeud {
// Construit une "instruction si" avec sa condition et sa séquence d'instruction
~NoeudInstSi() {} // A cause du destructeur virtuel de la classe Noeud
int executer() override; // Exécute l'instruction si : si condition vraie on exécute la séquence
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

void ajoute(Noeud* condition) override;
void setIsPremiereCondition(bool value);
Expand All @@ -99,7 +99,7 @@ public :

~NoeudInstRepeter() {}
int executer() override;
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

private:
Noeud* m_sequence;
Expand All @@ -117,7 +117,7 @@ class NoeudInstPour : public Noeud {
~NoeudInstPour() {
} // A cause du destructeur virtuel de la classe Noeud
int executer() override; // Exécute l'instruction si : si condition vraie on exécute la séquence
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

private:
Noeud* m_init;
Expand All @@ -137,7 +137,7 @@ class NoeudInstTantQue : public Noeud {
~NoeudInstTantQue() {
} // A cause du destructeur virtuel de la classe Noeud
int executer() override; // Exécute l'instruction tant que : tant que condition vraie on exécute la séquence
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

private:
Noeud* m_condition;
Expand All @@ -155,7 +155,7 @@ class NoeudInstLire : public Noeud {
}

int executer() override;
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

void ajoute(Noeud* var) override;

Expand All @@ -176,7 +176,7 @@ class NoeudInstEcrire : public Noeud {
~NoeudInstEcrire() {
} // A cause du destructeur virtuel de la classe Noeud
int executer() override; // Exécute l'instruction ecrire : ecrire condition vraie on exécute la séquence
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;

private:
std::vector<Noeud*> m_ecritures;
Expand Down
2 changes: 1 addition & 1 deletion SymboleValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int SymboleValue::executer() {
return m_valeur;
}

void SymboleValue::compiler(ostream & out, unsigned int indentation) {
void SymboleValue::compiler(ostream & out, int indentation) {
out << getChaine();
}

Expand Down
2 changes: 1 addition & 1 deletion SymboleValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SymboleValue : public Symbole, // Un symbole valué est un symbole qui a
SymboleValue(const Symbole & s); // Construit un symbole valué à partir d'un symbole existant s
~SymboleValue( ) {}
int executer() override; // exécute le SymboleValue (revoie sa valeur !)
void compiler(ostream & out, unsigned int indentation) override;
void compiler(ostream & out, int indentation) override;
inline void setValeur(int valeur) { this->m_valeur=valeur; m_defini=true; } // accesseur
inline bool estDefini() { return m_defini; } // accesseur

Expand Down

0 comments on commit 3e2b5c6

Please sign in to comment.