Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Technique de compilation - TP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
lucien.noel
Technique de compilation - TP
Commits
1053b835
Commit
1053b835
authored
2 years ago
by
lucien.noel
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Visitors/SemanticAnalyzer.java
+167
-73
167 additions, 73 deletions
Visitors/SemanticAnalyzer.java
with
167 additions
and
73 deletions
Visitors/SemanticAnalyzer.java
+
167
−
73
View file @
1053b835
...
...
@@ -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
.
get
Droit
e
();
boolean
isComparaisonOk
=
false
;
Expression
gauche
=
(
node
.
getGauche
()
instanceof
Parentheses
)
?
((
Parentheses
)
node
.
getGauche
()).
getExpression
()
:
node
.
get
Gauch
e
();
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
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment