Skip to content
Snippets Groups Projects
Commit 1053b835 authored by lucien.noel's avatar lucien.noel
Browse files

analyse des relation (<, >, etc...) terminée + amélioration du code

parent becd94b9
No related branches found
No related tags found
No related merge requests found
......@@ -15,19 +15,18 @@ public class SemanticAnalyzer implements ASTVisitor {
public Object visit(Addition node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
// test si la partie de gauche est un nombre ou un Idf qui pointe vers un entier
if (!(node.getGauche() instanceof Nombre ||
(node.getGauche() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getGauche()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer l'addition car la valeur de gauche n'est pas un nombre !");
if (!isExpressionNumber(node.getGauche())) {
throw new RuntimeException(
"Impossible d'effectuer l'addition car la valeur de gauche n'est pas un nombre !");
}
// test si la partie de droite est un nombre ou un Idf qui pointe vers un entier
if (!(node.getDroite() instanceof Nombre ||
(node.getDroite() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getDroite()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer l'addition car la valeur de droite n'est pas un nombre !");
if (!isExpressionNumber(node.getDroite())) {
throw new RuntimeException(
"Impossible d'effectuer l'addition car la valeur de droite n'est pas un nombre !");
}
return null;
}
......@@ -51,13 +50,16 @@ public class SemanticAnalyzer implements ASTVisitor {
public Object visit(Condition node) {
node.getCondition().accept(this);
Expression cond = (node.getCondition() instanceof Parentheses)? ((Parentheses)node.getCondition()).getExpression() : node.getCondition();
Expression cond = (node.getCondition() instanceof Parentheses)
? ((Parentheses) node.getCondition()).getExpression()
: node.getCondition();
if (!(cond instanceof Vrai || cond instanceof Faux ||
(cond instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)cond).getNom())).getType() instanceof Booleen) ||
(cond instanceof Idf
&& TDS.getInstance().identifier(new Entree(((Idf) cond).getNom())).getType() instanceof Booleen)
||
cond instanceof Egal || cond instanceof Diff ||
cond instanceof Inferieur || cond instanceof InfEgal ||
cond instanceof Superieur || cond instanceof SupEgal
)){
cond instanceof Superieur || cond instanceof SupEgal)) {
// throw new RuntimeException("classe invalide :"+node.getCondition().getClass().toString());
throw new RuntimeException("la condition passée n'est pas un booleen !");
}
......@@ -93,6 +95,18 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this);
node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression()
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) {
throw new RuntimeException(
"Impossible d'effectuer une comparaison entre 2 valeur qui ne sont pas du même type !");
}
return null;
}
......@@ -101,16 +115,14 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getDroite().accept(this);
// test si la partie de gauche est un nombre ou un Idf qui pointe vers un entier
if (!(node.getGauche() instanceof Nombre ||
(node.getGauche() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getGauche()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer la divions car la valeur de gauche n'est pas un nombre !");
if (!isExpressionNumber(node.getGauche())) {
throw new RuntimeException(
"Impossible d'effectuer la divions car la valeur de gauche n'est pas un nombre !");
}
// test si la partie de droite est un nombre ou un Idf qui pointe vers un entier
if (!(node.getDroite() instanceof Nombre ||
(node.getDroite() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getDroite()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer la division car la valeur de droite n'est pas un nombre !");
if (!isExpressionNumber(node.getDroite())) {
throw new RuntimeException(
"Impossible d'effectuer la division car la valeur de droite n'est pas un nombre !");
}
return null;
}
......@@ -124,32 +136,16 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this);
node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses)? ((Parentheses)node.getGauche()).getExpression(): node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses)? ((Parentheses)node.getDroite()).getExpression(): node.getDroite();
boolean isComparaisonOk = false;
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression()
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
if ((gauche instanceof Nombre && droite instanceof Nombre) ||
(gauche instanceof Nombre && (droite instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)droite).getNom())).getType() instanceof Entier)) ||
((gauche instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)gauche).getNom())).getType() instanceof Entier) && droite instanceof Nombre) ||
((gauche instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)gauche).getNom())).getType() instanceof Entier) && (droite instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)droite).getNom())).getType() instanceof Entier))) {
//dans le cas ou on compare 2 nombres
isComparaisonOk = true;
}else if (
(gauche instanceof Vrai || gauche instanceof Faux ||
(gauche instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)gauche).getNom())).getType() instanceof Booleen) ||
gauche instanceof Relation)
&&
(droite instanceof Vrai || droite instanceof Faux ||
(droite instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)droite).getNom())).getType() instanceof Booleen) ||
droite instanceof Relation)
) {
//dans le cas où on compare 2 booleans
isComparaisonOk = true;
}
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) {
throw new RuntimeException("Impossible d'effectuer une comparaison entre 2 valeur qui ne sont pas du même type !");
throw new RuntimeException(
"Impossible d'effectuer une comparaison entre 2 valeur qui ne sont pas du même type !");
}
return null;
}
......@@ -171,12 +167,38 @@ public class SemanticAnalyzer implements ASTVisitor {
public Object visit(InfEgal node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression()
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) {
throw new RuntimeException(
"Impossible d'effectuer une comparaison entre 2 valeur qui ne sont pas du même type !");
}
return null;
}
public Object visit(Inferieur node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression()
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) {
throw new RuntimeException(
"Impossible d'effectuer une comparaison entre 2 valeur qui ne sont pas du même type !");
}
return null;
}
......@@ -224,16 +246,14 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getDroite().accept(this);
// test si la partie de gauche est un nombre ou un Idf qui pointe vers un entier
if (!(node.getGauche() instanceof Nombre ||
(node.getGauche() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getGauche()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer la multiplication car la valeur de gauche n'est pas un nombre !");
if (!isExpressionNumber(node.getGauche())) {
throw new RuntimeException(
"Impossible d'effectuer la multiplication car la valeur de gauche n'est pas un nombre !");
}
// test si la partie de droite est un nombre ou un Idf qui pointe vers un entier
if (!(node.getDroite() instanceof Nombre ||
(node.getDroite() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getDroite()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer la multiplication car la valeur de droite n'est pas un nombre !");
if (!isExpressionNumber(node.getDroite())) {
throw new RuntimeException(
"Impossible d'effectuer la multiplication car la valeur de droite n'est pas un nombre !");
}
return null;
}
......@@ -247,16 +267,14 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getDroite().accept(this);
// test si la partie de gauche est un nombre ou un Idf qui pointe vers un entier
if (!(node.getGauche() instanceof Nombre ||
(node.getGauche() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getGauche()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer la soustraction car la valeur de gauche n'est pas un nombre !");
if (!isExpressionNumber(node.getGauche())) {
throw new RuntimeException(
"Impossible d'effectuer la soustraction car la valeur de gauche n'est pas un nombre !");
}
// test si la partie de droite est un nombre ou un Idf qui pointe vers un entier
if (!(node.getDroite() instanceof Nombre ||
(node.getDroite() instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf)node.getDroite()).getNom())).getType() instanceof Entier))
) {
throw new RuntimeException("Impossible d'effectuer la soustraction car la valeur de droite n'est pas un nombre !");
if (!isExpressionNumber(node.getDroite())) {
throw new RuntimeException(
"Impossible d'effectuer la soustraction car la valeur de droite n'est pas un nombre !");
}
return null;
}
......@@ -264,12 +282,38 @@ public class SemanticAnalyzer implements ASTVisitor {
public Object visit(SupEgal node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression()
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) {
throw new RuntimeException(
"Impossible d'effectuer une comparaison entre 2 valeur qui ne sont pas du même type !");
}
return null;
}
public Object visit(Superieur node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression()
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) {
throw new RuntimeException(
"Impossible d'effectuer une comparaison entre 2 valeur qui ne sont pas du même type !");
}
return null;
}
......@@ -284,4 +328,54 @@ public class SemanticAnalyzer implements ASTVisitor {
public Object visit(Vrai node) {
return null;
}
// ==============================================================
// privates methodes
// ==============================================================
/**
* Indique si les expressions de gauche est de droite peuvent être comparées.
*
* @param gauche
* @param droite
* @return true si les expressions peuvent être comparées sinon false
*/
private boolean isRelationOk(Expression gauche, Expression droite) {
boolean ok = false;
if ((gauche instanceof Nombre && droite instanceof Nombre) ||
(gauche instanceof Nombre && (droite instanceof Idf && TDS.getInstance()
.identifier(new Entree(((Idf) droite).getNom())).getType() instanceof Entier))
||
((gauche instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf) gauche).getNom()))
.getType() instanceof Entier) && droite instanceof Nombre)
||
((gauche instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf) gauche).getNom()))
.getType() instanceof Entier)
&& (droite instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf) droite).getNom()))
.getType() instanceof Entier))) {
// dans le cas ou on compare 2 nombres
ok = true;
} else if ((gauche instanceof Vrai || gauche instanceof Faux ||
(gauche instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf) gauche).getNom()))
.getType() instanceof Booleen)
||
gauche instanceof Relation)
&&
(droite instanceof Vrai || droite instanceof Faux ||
(droite instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf) droite).getNom()))
.getType() instanceof Booleen)
||
droite instanceof Relation)) {
// dans le cas où on compare 2 booleans
ok = true;
}
return ok;
}
private boolean isExpressionNumber(Expression expr) {
return expr instanceof Nombre || (expr instanceof Idf
&& TDS.getInstance().identifier(new Entree(((Idf) expr).getNom())).getType() instanceof Entier);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment