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
d1298322
Commit
d1298322
authored
2 years ago
by
lucien.noel
Browse files
Options
Downloads
Patches
Plain Diff
bytecode pour la méthode Ecrire fait
parent
8dc52bfa
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/ByteCodeGenerator.java
+102
-10
102 additions, 10 deletions
Visitors/ByteCodeGenerator.java
with
102 additions
and
10 deletions
Visitors/ByteCodeGenerator.java
+
102
−
10
View file @
d1298322
...
...
@@ -9,6 +9,7 @@ import java.util.*;
public
class
ByteCodeGenerator
implements
ASTVisitor
{
private
String
bytecode
=
""
;
private
HashMap
<
String
,
Integer
>
varValues
=
new
HashMap
<
String
,
Integer
>();
public
Object
visit
(
Addition
node
)
{
return
null
;
...
...
@@ -51,7 +52,13 @@ public class ByteCodeGenerator implements ASTVisitor {
return
null
;
}
public
Object
visit
(
Ecrire
node
)
{
return
null
;
}
public
Object
visit
(
Ecrire
node
)
{
node
.
getSource
().
accept
(
this
);
bytecode
+=
"getstatic java/lang/System/out Ljava/io/PrintStream;"
;
String
toWrite
=
getValueInExpressionToString
(
node
.
getSource
());
bytecode
+=
"ldc "
+
toWrite
;
bytecode
+=
"invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V"
;
return
null
;
}
public
Object
visit
(
Egal
node
)
{
return
null
;
...
...
@@ -111,14 +118,99 @@ public class ByteCodeGenerator implements ASTVisitor {
// erreur de Jasmin quand le bytecode est vide.
public
String
getByteCode
()
{
return
bytecode
;
// return """
// .class public Program
// .super java/lang/Object
// .method public static main([Ljava/lang/String;)V
// .limit stack 20000
// .limit locals 1
// return
// .end method
// """;
}
private
String
getValueInExpressionToString
(
Expression
expr
){
String
res
=
null
;
if
(
expr
instanceof
Chaine
){
res
=
((
Chaine
)
expr
).
getValeur
();
}
else
if
(
isExpressionBooleen
(
expr
)){
res
=
""
+
getBoolInExpression
(
expr
);
}
else
if
(
isExpressionNumber
(
expr
)){
res
=
""
+
getIntInExpression
(
expr
);
}
return
res
;
}
private
int
getIntInExpression
(
Expression
expr
){
int
res
=
0
;
if
(
expr
instanceof
Parentheses
){
expr
=
((
Parentheses
)
expr
).
getExpression
();
}
if
(
expr
instanceof
Nombre
){
res
=
((
Nombre
)
expr
).
getValeur
();
}
else
if
(
expr
instanceof
Addition
){
res
=
getIntInExpression
(((
Addition
)
expr
).
getGauche
())
+
getIntInExpression
(((
Addition
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Soustraction
){
res
=
getIntInExpression
(((
Addition
)
expr
).
getGauche
())
-
getIntInExpression
(((
Addition
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Produit
){
res
=
getIntInExpression
(((
Addition
)
expr
).
getGauche
())
*
getIntInExpression
(((
Addition
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Division
){
res
=(
int
)
(
getIntInExpression
(((
Addition
)
expr
).
getGauche
())
/
getIntInExpression
(((
Addition
)
expr
).
getDroite
()));
}
else
if
(
expr
instanceof
Moins
){
res
=
-
getIntInExpression
(((
Moins
)
expr
).
getOperand
());
}
else
if
(
expr
instanceof
Idf
&&
TDS
.
getInstance
().
identifier
(
new
Entree
(((
Idf
)
expr
).
getNom
())).
getType
()
instanceof
Entier
){
res
=
varValues
.
get
(((
Idf
)
expr
).
getNom
());
}
return
res
;
}
private
boolean
getBoolInExpression
(
Expression
expr
){
boolean
res
=
false
;
if
(
expr
instanceof
Parentheses
){
expr
=
((
Parentheses
)
expr
).
getExpression
();
}
if
(
expr
instanceof
Vrai
){
res
=
true
;
}
else
if
(
expr
instanceof
Faux
){
res
=
false
;
}
else
if
(
expr
instanceof
Non
){
res
=
getBoolInExpression
(((
Non
)
expr
).
getOperand
());
}
else
if
(
expr
instanceof
Et
){
res
=
getBoolInExpression
(((
Et
)
expr
).
getGauche
())
&&
getBoolInExpression
(((
Et
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Ou
){
res
=
getBoolInExpression
(((
Ou
)
expr
).
getGauche
())
||
getBoolInExpression
(((
Ou
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Egal
){
res
=
getBoolInExpression
(((
Egal
)
expr
).
getGauche
())
==
getBoolInExpression
(((
Egal
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Diff
){
res
=
getBoolInExpression
(((
Diff
)
expr
).
getGauche
())
!=
getBoolInExpression
(((
Diff
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Superieur
){
res
=
getIntInExpression
(((
Superieur
)
expr
).
getGauche
())
>
getIntInExpression
(((
Superieur
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
SupEgal
){
res
=
getIntInExpression
(((
SupEgal
)
expr
).
getGauche
())
>=
getIntInExpression
(((
SupEgal
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Inferieur
){
res
=
getIntInExpression
(((
Inferieur
)
expr
).
getGauche
())
<
getIntInExpression
(((
Inferieur
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
InfEgal
){
res
=
getIntInExpression
(((
InfEgal
)
expr
).
getGauche
())
<=
getIntInExpression
(((
InfEgal
)
expr
).
getDroite
());
}
else
if
(
expr
instanceof
Idf
&&
TDS
.
getInstance
().
identifier
(
new
Entree
(((
Idf
)
expr
).
getNom
())).
getType
()
instanceof
Booleen
){
res
=
(
varValues
.
get
(((
Idf
)
expr
).
getNom
())
==
0
)?
false
:
true
;
}
return
res
;
}
/**
* Indique si l'expression passée est un nombre
*
* @param expr
* @return true si l'expression passée est un nombre
*/
private
boolean
isExpressionNumber
(
Expression
expr
)
{
expr
=
(
expr
instanceof
Parentheses
)
?
((
Parentheses
)
expr
).
getExpression
()
:
expr
;
return
expr
instanceof
Nombre
||
expr
instanceof
Arithmetique
||
expr
instanceof
Moins
||
(
expr
instanceof
Idf
&&
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
Non
||
expr
instanceof
Et
||
expr
instanceof
Ou
||
(
expr
instanceof
Idf
&&
TDS
.
getInstance
().
identifier
(
new
Entree
(((
Idf
)
expr
).
getNom
()))
.
getType
()
instanceof
Booleen
);
}
}
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