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

le test 5 passe

parent 115e5620
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ public class ByteCodeGenerator implements ASTVisitor {
private String bytecode = "";
private HashMap<String, Integer> varValues = new HashMap<String, Integer>();
private HashMap<String, Integer> varAddrMemory = new HashMap<String, Integer>();
int nb_label = 0;
public Object visit(Addition node) {
......@@ -22,13 +23,13 @@ public class ByteCodeGenerator implements ASTVisitor {
node.getDestination().accept(this);
node.getSource().accept(this);
String varName = ((Idf)node.getDestination()).getNom();
int value = 0;
if(isExpressionBooleen(node.getSource())){
value = (getBoolInExpression(node.getSource()))?1:0;
loadBoolInByteCode(node.getSource());
}else if(isExpressionNumber(node.getSource())){
value = getIntInExpression(node.getSource());
loadIntInByteCode(node.getSource());
}
varValues.put(varName, value);
int memoryAddr = varAddrMemory.get(varName);
bytecode+="istore "+memoryAddr+"\n";
return null;
}
......@@ -73,7 +74,7 @@ public class ByteCodeGenerator implements ASTVisitor {
node.getDeclaration().accept(this);
node.getInstructions().accept(this);
bytecode += "return\n.end method";
bytecode += "exit_label:\nreturn\n.end method";
return null;
}
......@@ -82,6 +83,9 @@ public class ByteCodeGenerator implements ASTVisitor {
node.getIdentifier(i).accept(this);
varValues.put(node.getIdentifier(i).getNom(), null);
bytecode+=".var "+(varValues.size()-1)+" is "+node.getIdentifier(i).getNom()+" "+((node.id.type instanceof Booleen)?"Z":"I")+"\n";
bytecode+="ldc 0\n";
bytecode+="istore "+varAddrMemory.size()+"\n";
varAddrMemory.put(node.getIdentifier(i).getNom(), varAddrMemory.size());
}
return null;
}
......@@ -100,10 +104,31 @@ public class ByteCodeGenerator implements ASTVisitor {
public Object visit(Ecrire node) {
node.getSource().accept(this);
Expression expr = (node.getSource() instanceof Parentheses)?((Parentheses)node.getSource()).getExpression():node.getSource();
if (expr instanceof Chaine) {
bytecode+="getstatic java/lang/System/out Ljava/io/PrintStream;\n";
String toWrite = getValueInExpressionToString(node.getSource());
String toWrite = ((Chaine)expr).getValeur();
bytecode+="ldc "+toWrite+"\n";
bytecode+="invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V\n";
}else if(isExpressionBooleen(expr)){
bytecode+="getstatic java/lang/System/out Ljava/io/PrintStream;\n";
loadBoolInByteCode(expr);
bytecode+="ifeq label_"+nb_label+"\n";
bytecode+="ldc "+'"'+"vrai"+'"'+"\n";
bytecode+="goto label_"+(nb_label+1)+"\n";
bytecode+="label_"+nb_label+":\n";
bytecode+="ldc "+'"'+"faux"+'"'+"\n";
bytecode+="label_"+(nb_label+1)+":\n";
nb_label+=2;
bytecode+="invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V\n";
}else if(isExpressionNumber(expr)){
bytecode+="getstatic java/lang/System/out Ljava/io/PrintStream;\n";
loadIntInByteCode(expr);
bytecode+="invokevirtual java/io/PrintStream/println(I)V\n";
}else if(expr instanceof Idf){
}
return null; }
public Object visit(Egal node) {
......@@ -201,8 +226,14 @@ public class ByteCodeGenerator implements ASTVisitor {
}
public Object visit(Tantque node) {
bytecode += "label_"+nb_label+":\n";
node.getCondition().accept(this);
bytecode += "ldc "+((getBoolInExpression(node.getCondition()))?"1":"0")+"\n";
bytecode += "ifeq label_"+(nb_label+1)+"\n";
node.getInstruction().accept(this);
bytecode += "goto label_"+nb_label+"\n";
bytecode += "label_"+(nb_label+1)+":\n";
nb_label += 2;
return null;
}
......@@ -216,16 +247,149 @@ public class ByteCodeGenerator implements ASTVisitor {
return bytecode;
}
private String getValueInExpressionToString(Expression expr){
String res = null;
if(expr instanceof Chaine){
res = ((Chaine) expr).getValeur();
}else if(isExpressionBooleen(expr)){
res = '"'+((getBoolInExpression(expr))?"vrai":"faux")+'"';
}else if(isExpressionNumber(expr)){
res = '"'+""+getIntInExpression(expr)+'"';
// private String getValueInExpressionToString(Expression expr){
// String res = null;
// if(expr instanceof Chaine){
// res = ((Chaine) expr).getValeur();
// }else if(isExpressionBooleen(expr)){
// res = '"'+((getBoolInExpression(expr))?"vrai":"faux")+'"';
// }else if(isExpressionNumber(expr)){
// res = '"'+""+getIntInExpression(expr)+'"';
// }
// return res;
// }
private void loadBoolInByteCode(Expression expr){
if(expr instanceof Parentheses){
expr = ((Parentheses)expr).getExpression();
}
if(expr instanceof Vrai){
bytecode+="ldc 1\n";
}else if(expr instanceof Faux){
bytecode+="ldc 0\n";
}else if(expr instanceof Non){
//load la valeur
if(((Non)expr).getOperand() instanceof Idf){
bytecode+="iload "+varAddrMemory.get(((Idf)((Non)expr).getOperand()).getNom())+"\n";
}else{
if(isExpressionBooleen(((Non)expr).getOperand() )){
loadBoolInByteCode(((Non)expr).getOperand() );
}else{
loadIntInByteCode(((Non)expr).getOperand() );
}
}
//génère le bytecode
bytecode+="ifeq label_"+nb_label+"\n";
bytecode+="iconst_0\n";
bytecode+="goto label_"+(nb_label+1)+"\n";
bytecode+="label_"+nb_label+":\n";
bytecode+="iconst_1\n";
bytecode+="label_"+(nb_label+1)+":\n";
nb_label+=2;
}else if(expr instanceof Et){
if(((Et)expr).getGauche() instanceof Idf){
bytecode+="iload "+varAddrMemory.get(((Idf)((Et)expr).getGauche()).getNom())+"\n";
}else{
bytecode+="ldc "+getIntInExpression(((Et)expr).getGauche())+"\n";
}
if(((Et)expr).getDroite() instanceof Idf){
bytecode+="iload "+varAddrMemory.get(((Idf)((Et)expr).getDroite()).getNom())+"\n";
}else{
bytecode+="ldc "+getIntInExpression(((Et)expr).getDroite())+"\n";
}
bytecode+="iand\n";
}else if(expr instanceof Ou){
if(((Ou)expr).getGauche() instanceof Idf){
bytecode+="iload "+varAddrMemory.get(((Idf)((Ou)expr).getGauche()).getNom())+"\n";
}else{
bytecode+="ldc "+getIntInExpression(((Ou)expr).getGauche())+"\n";
}
if(((Ou)expr).getDroite() instanceof Idf){
bytecode+="iload "+varAddrMemory.get(((Idf)((Ou)expr).getDroite()).getNom())+"\n";
}else{
bytecode+="ldc "+getIntInExpression(((Ou)expr).getDroite())+"\n";
}
bytecode+="ior\n";
}else if(expr instanceof Relation){
//load gauche
if(((Relation)expr).getGauche() instanceof Idf){
bytecode+="iload "+varAddrMemory.get(((Idf)((Relation)expr).getGauche()).getNom())+"\n";
}else{
if(isExpressionBooleen(((Relation)expr).getGauche())){
loadBoolInByteCode(((Relation)expr).getGauche());
}else{
loadIntInByteCode(((Relation)expr).getGauche());
}
}
//load droite
if(((Relation)expr).getDroite() instanceof Idf){
bytecode+="iload "+varAddrMemory.get(((Idf)((Relation)expr).getDroite()).getNom())+"\n";
}else{
if(isExpressionBooleen(((Relation)expr).getDroite())){
loadBoolInByteCode(((Relation)expr).getDroite());
}else{
loadIntInByteCode(((Relation)expr).getDroite());
}
}
//écrit la relation
if(expr instanceof Egal){
bytecode+="if_icmpeq label_"+nb_label+"\n";
}else if(expr instanceof Diff){
bytecode+="if_icmpne label_"+nb_label+"\n";
}else if(expr instanceof SupEgal){
bytecode+="if_icmpge label_"+nb_label+"\n";
}else if(expr instanceof Superieur){
bytecode+="if_icmpgt label_"+nb_label+"\n";
}else if(expr instanceof InfEgal){
bytecode+="if_icmple label_"+nb_label+"\n";
}else if(expr instanceof Inferieur){
bytecode+="if_icmplt label_"+nb_label+"\n";
}
bytecode+="iconst_0\n";
bytecode+="goto label_"+(nb_label+1)+"\n";
bytecode+="label_"+nb_label+":\n";
bytecode+="iconst_1\n";
bytecode+="label_"+(nb_label+1)+":\n";
nb_label+=2;
}else if(expr instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf) expr).getNom())).getType() instanceof Booleen){
int memoryAddr = varAddrMemory.get(((Idf) expr).getNom());
bytecode+="iload "+memoryAddr+"\n";
}
}
private void loadIntInByteCode(Expression expr){
if(expr instanceof Parentheses){
expr = ((Parentheses)expr).getExpression();
}
if(expr instanceof Nombre){
bytecode += "ldc "+Math.abs(((Nombre) expr).getValeur())+"\n";
bytecode += (((Nombre) expr).getValeur() < 0)?"ineg\n":"";
}else if(expr instanceof Addition){
loadIntInByteCode(((Addition)expr).getGauche());
loadIntInByteCode(((Addition)expr).getDroite());
bytecode+="iadd\n";
}else if(expr instanceof Soustraction){
loadIntInByteCode(((Soustraction)expr).getGauche());
loadIntInByteCode(((Soustraction)expr).getDroite());
bytecode+="isub\n";
}else if(expr instanceof Produit){
loadIntInByteCode(((Produit)expr).getGauche());
loadIntInByteCode(((Produit)expr).getDroite());
bytecode+="imul\n";
}else if(expr instanceof Division){
loadIntInByteCode(((Division)expr).getGauche());
loadIntInByteCode(((Division)expr).getDroite());
bytecode+="idiv\n";
}else if(expr instanceof Moins){
int i = getIntInExpression(((Moins)expr).getOperand());
bytecode+="ldc "+i+"\n";
bytecode+="ineg\n";
}else if(expr instanceof Idf && TDS.getInstance().identifier(new Entree(((Idf) expr).getNom())).getType() instanceof Entier){
int memoryAddr = varAddrMemory.get(((Idf) expr).getNom());
bytecode+="iload "+memoryAddr+"\n";
}
return res;
}
private int getIntInExpression(Expression expr){
......@@ -245,8 +409,6 @@ public class ByteCodeGenerator implements ASTVisitor {
res =(int) (getIntInExpression(((Division) expr).getGauche()) / getIntInExpression(((Division) 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());
}else if(isExpressionBooleen(expr)){
res =(getBoolInExpression(expr))?1:0;
}
......@@ -288,8 +450,6 @@ public class ByteCodeGenerator implements ASTVisitor {
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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment