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

Commit

Permalink
feature-variables-typees: ajout compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Feelzor committed Nov 4, 2019
1 parent a1967ec commit d9c6ccb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 58 deletions.
87 changes: 40 additions & 47 deletions VisiteurCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ void VisiteurCompiler::visiterNoeudSeqInst(NoeudSeqInst* noeud) {
}

void VisiteurCompiler::visiterNoeudAffectation(NoeudAffectation* noeud) {
bool instructionSeule = true;
if (m_indentation < 0) {
instructionSeule = false;
}

int indent = m_indentation;
m_indentation = -1;
noeud->getVariable()->accepter(*this);
m_out << "=";
noeud->getExpression()->accepter(*this);

m_indentation = indent;
if (instructionSeule) m_out << ";";
}

void VisiteurCompiler::visiterNoeudOperateurBinaire(NoeudOperateurBinaire* noeud) {
Expand All @@ -34,11 +28,11 @@ void VisiteurCompiler::visiterNoeudOperateurBinaire(NoeudOperateurBinaire* noeud

m_out << "(";
if (op == "non") {
m_out << "!";
m_out << "not ";
noeud->getOperandeGauche()->accepter(*this);
} else {
if (op == "et") op = "&&";
else if (op == "ou") op = "||";
if (op == "et") op = " and ";
else if (op == "ou") op = " or ";

noeud->getOperandeGauche()->accepter(*this);
m_out << op;
Expand All @@ -52,23 +46,22 @@ void VisiteurCompiler::visiterNoeudOperateurBinaire(NoeudOperateurBinaire* noeud
void VisiteurCompiler::visiterNoeudInstSi(NoeudInstSi* noeud) {
std::string indent(m_indentation * 4, ' ');
if (noeud->getCondition() != nullptr) {
if (!noeud->isPremiereCondition()) { m_out << ' '; }
m_out << "if (";
m_out << "if ";
int indentOrigine = m_indentation;
m_indentation = -1;
noeud->getCondition()->accepter(*this);
m_indentation = indentOrigine;
m_out << ")";
} else {
m_out << "se";
}

m_out << " {" << endl;
m_out << ":" << endl;
m_indentation++;
noeud->getSequence()->accepter(*this);
m_indentation--;
m_out << indent << "}";

if (noeud->getProchaineCondition() != nullptr) {
m_out << " else";
m_out << "el";
noeud->getProchaineCondition()->accepter(*this);
} else {
m_out << endl;
Expand All @@ -77,73 +70,73 @@ void VisiteurCompiler::visiterNoeudInstSi(NoeudInstSi* noeud) {

void VisiteurCompiler::visiterNoeudInstRepeter(NoeudInstRepeter* noeud) {
std::string indent(m_indentation * 4, ' ');
m_out << "do {" << endl;
m_indentation++;
noeud->getSequence()->accepter(*this);
m_indentation--;
m_out << indent << "} while (!(";
m_out << indent << "transpilation_dowhile_boolean = 0" << endl;
m_out << "while not (";
int indentOrigine = m_indentation;
m_indentation = -1;
noeud->getCondition()->accepter(*this);
m_indentation = indentOrigine;
m_out << "));" << endl;
m_out << ") or transpilation_dowhile_boolean == 0:" << endl;
m_indentation++;
m_out << indent << " transpilation_dowhile_boolean+=1" << endl;
noeud->getSequence()->accepter(*this);
m_indentation--;
}

void VisiteurCompiler::visiterNoeudInstPour(NoeudInstPour* noeud) {
std::string indent(4 * m_indentation, ' ');
int indentOrigine = m_indentation;
m_indentation = -1;
m_out << "for (";

if (noeud->getInit() != nullptr) noeud->getInit()->accepter(*this);
m_out << ";";
m_out << endl;
m_indentation = -1;
m_out << "while";
noeud->getCondition()->accepter(*this);
m_out << ";";
if (noeud->getAffectation() != nullptr) noeud->getAffectation()->accepter(*this);
m_out << ") {" << endl;
m_out << ":" << endl;
m_indentation = indentOrigine + 1;
noeud->getSequence()->accepter(*this);
m_out << indent << " ";
if (noeud->getAffectation() != nullptr) noeud->getAffectation()->accepter(*this);
m_indentation--;
m_out << indent << "}" << endl;
}

void VisiteurCompiler::visiterNoeudInstTantQue(NoeudInstTantQue* noeud) {
std::string indent(m_indentation * 4, ' ');
m_out << "while " ;
noeud->getCondition()->accepter(*this);
m_out << " {" << endl;
m_out << ":" << endl;
m_indentation++;
noeud->getSequence()->accepter(*this);
m_indentation--;
m_out << indent << "}" << endl;
}

void VisiteurCompiler::visiterNoeudInstLire(NoeudInstLire* noeud) {
m_out << "std::cin";
std::string indent(m_indentation * 4, ' ');
for (Noeud* var : noeud->getVariables()) {
m_out << " >> ";
int indent = m_indentation;
m_out << indent;
int currIndent = m_indentation;
m_indentation = -1;
var->accepter(*this);
m_indentation = indent;
m_indentation = currIndent;
m_out << " = input()" << endl;
}

m_out << ";";
}

void VisiteurCompiler::visiterNoeudInstEcrire(NoeudInstEcrire* noeud) {
m_out << "std::cout";
std::string indent(m_indentation * 4, ' ');
bool first = true;
for (Noeud* inst : noeud->getEcritures()) {
m_out << " << ";
if ((typeid (*inst) == typeid (SymboleValue) && *((SymboleValue*) inst) == "<CHAINE>")) {
m_out << ((SymboleValue*) inst)->getChaine();
} else {
int indent = m_indentation;
m_indentation = -1;
inst->accepter(*this);
m_indentation = indent;
}
if (first) m_out << "print(";
else m_out << indent << "print(";

int currIndent = m_indentation;
m_indentation = -1;
inst->accepter(*this);
m_indentation = currIndent;
m_out << ", end = '')" << endl;
first = false;
}
m_out << ";";
m_out << indent << "print('')" << endl;
}

void VisiteurCompiler::visiterSymboleValue(SymboleValue* symbole) {
Expand Down
13 changes: 2 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int main(int argc, char* argv[]) {
cout << endl << "================ Syntaxe Correcte" << endl;
} else {
cout << endl << "================ Syntaxe Incorrecte" << endl;
return 1;
}
if (compile) {
compiler(*out, interpreteur.getTable(), interpreteur.getArbre());
Expand Down Expand Up @@ -71,16 +72,6 @@ int main(int argc, char* argv[]) {
}

void compiler(ostream& out, const TableSymboles& symboles, Noeud* arbre) {
std::string indentationBasique(4, ' ');
out << "#include <iostream>" << endl << endl;
out << "int main() {" << endl;
for (int i = 0; i < symboles.getTaille(); i++) {
const SymboleValue s = symboles[i];
if (s == "<VARIABLE>") {
out << indentationBasique << "int " << s.getChaine() << ";" << endl;
}
}
VisiteurCompiler visiteur(out, 1);
VisiteurCompiler visiteur(out, 0);
arbre->accepter(visiteur);
out << "}" << endl;
}

0 comments on commit d9c6ccb

Please sign in to comment.