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

amélioration du code

parent e122f40d
No related branches found
No related tags found
No related merge requests found
...@@ -60,7 +60,8 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -60,7 +60,8 @@ public class SemanticAnalyzer implements ASTVisitor {
cond instanceof Egal || cond instanceof Diff || cond instanceof Egal || cond instanceof Diff ||
cond instanceof Inferieur || cond instanceof InfEgal || 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("classe invalide
// :"+node.getCondition().getClass().toString());
throw new RuntimeException("la condition passée n'est pas un booleen !"); throw new RuntimeException("la condition passée n'est pas un booleen !");
} }
...@@ -95,12 +96,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -95,12 +96,7 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this); node.getGauche().accept(this);
node.getDroite().accept(this); node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression() boolean isComparaisonOk = isRelationOk(node.getGauche(), node.getDroite());
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) { if (!isComparaisonOk) {
throw new RuntimeException( throw new RuntimeException(
...@@ -136,12 +132,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -136,12 +132,7 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this); node.getGauche().accept(this);
node.getDroite().accept(this); node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression() boolean isComparaisonOk = isRelationOk(node.getGauche(), node.getDroite());
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) { if (!isComparaisonOk) {
throw new RuntimeException( throw new RuntimeException(
...@@ -168,12 +159,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -168,12 +159,7 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this); node.getGauche().accept(this);
node.getDroite().accept(this); node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression() boolean isComparaisonOk = isRelationOk(node.getGauche(), node.getDroite());
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) { if (!isComparaisonOk) {
throw new RuntimeException( throw new RuntimeException(
...@@ -187,12 +173,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -187,12 +173,7 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this); node.getGauche().accept(this);
node.getDroite().accept(this); node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression() boolean isComparaisonOk = isRelationOk(node.getGauche(), node.getDroite());
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) { if (!isComparaisonOk) {
throw new RuntimeException( throw new RuntimeException(
...@@ -224,6 +205,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -224,6 +205,7 @@ public class SemanticAnalyzer implements ASTVisitor {
public Object visit(Ou node) { public Object visit(Ou node) {
node.getGauche().accept(this); node.getGauche().accept(this);
node.getDroite().accept(this); node.getDroite().accept(this);
return null; return null;
} }
...@@ -283,12 +265,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -283,12 +265,7 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this); node.getGauche().accept(this);
node.getDroite().accept(this); node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression() boolean isComparaisonOk = isRelationOk(node.getGauche(), node.getDroite());
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) { if (!isComparaisonOk) {
throw new RuntimeException( throw new RuntimeException(
...@@ -302,12 +279,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -302,12 +279,7 @@ public class SemanticAnalyzer implements ASTVisitor {
node.getGauche().accept(this); node.getGauche().accept(this);
node.getDroite().accept(this); node.getDroite().accept(this);
Expression gauche = (node.getGauche() instanceof Parentheses) ? ((Parentheses) node.getGauche()).getExpression() boolean isComparaisonOk = isRelationOk(node.getGauche(), node.getDroite());
: node.getGauche();
Expression droite = (node.getDroite() instanceof Parentheses) ? ((Parentheses) node.getDroite()).getExpression()
: node.getDroite();
boolean isComparaisonOk = isRelationOk(gauche, droite);
if (!isComparaisonOk) { if (!isComparaisonOk) {
throw new RuntimeException( throw new RuntimeException(
...@@ -342,32 +314,7 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -342,32 +314,7 @@ public class SemanticAnalyzer implements ASTVisitor {
*/ */
private boolean isRelationOk(Expression gauche, Expression droite) { private boolean isRelationOk(Expression gauche, Expression droite) {
boolean ok = false; boolean ok = false;
if ((isExpressionNumber(gauche) && isExpressionNumber(droite)) || (isExpressionBooleen(gauche) && isExpressionBooleen(droite))) {
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; ok = true;
} }
return ok; return ok;
...@@ -375,12 +322,26 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -375,12 +322,26 @@ public class SemanticAnalyzer implements ASTVisitor {
/** /**
* Indique si l'expression passée est un nombre * Indique si l'expression passée est un nombre
*
* @param expr * @param expr
* @return true si l'expression passée est un nombre * @return true si l'expression passée est un nombre
*/ */
private boolean isExpressionNumber(Expression expr) { private boolean isExpressionNumber(Expression expr) {
expr = (expr instanceof Parentheses)?((Parentheses)expr).getExpression():expr;
return expr instanceof Nombre || (expr instanceof Idf return expr instanceof Nombre || (expr instanceof Idf
&& TDS.getInstance().identifier(new Entree(((Idf) expr).getNom())).getType() instanceof Entier); && TDS.getInstance().identifier(new Entree(((Idf) expr).getNom())).getType() instanceof Entier);
} }
/**
* Indique si l'expression passée est un booleen
*
* @param expr
* @return true si l'expression passée est un booleen
*/
private boolean isExpressionBooleen(Expression expr) {
expr = (expr instanceof Parentheses)?((Parentheses)expr).getExpression():expr;
return expr instanceof Vrai || expr instanceof Faux || expr instanceof Relation || (expr instanceof Idf
&& TDS.getInstance().identifier(new Entree(((Idf) expr).getNom())).getType() instanceof Booleen);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment