Skip to content
Snippets Groups Projects
Commit 5f38b401 authored by BAUQUIN Niels's avatar BAUQUIN Niels
Browse files

Exam test 20/20

parent 62d5e4f0
No related branches found
No related tags found
No related merge requests found
...@@ -176,10 +176,56 @@ public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> { ...@@ -176,10 +176,56 @@ public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> {
@Override @Override
public C3aOperand visit(SaLDecVar node) throws Exception { public C3aOperand visit(SaLDecVar node) throws Exception {
defaultIn(node); defaultIn(node);
defaultOut(node); defaultOut(node);
return super.visit(node); return super.visit(node);
} }
@Override
public C3aOperand visit(SaIncr node) throws Exception {
defaultIn(node);
C3aOperand var = node.getLhs().accept(this);
C3aOperand exp = node.getRhs().accept(this);
System.out.println(var);
System.out.println(exp);
C3aTemp temp = c3a.newTemp();
c3a.ajouteInst(new C3aInstAdd(var, exp, temp, ""));
c3a.ajouteInst(new C3aInstAffect(temp, var, ""));
defaultOut(node);
defaultOut(node);
return null;
}
@Override
public C3aOperand visit(SaExpModulo node) throws Exception {
defaultIn(node);
C3aOperand op1 = node.getOp1().accept(this);
C3aOperand op2 = node.getOp2().accept(this);
C3aOperand temp1 = c3a.newTemp();
c3a.ajouteInst(new C3aInstDiv(op1, op2, temp1, ""));
C3aOperand temp2 = c3a.newTemp();
c3a.ajouteInst(new C3aInstMult(op2, temp1, temp2, ""));
C3aOperand temp3 = c3a.newTemp();
c3a.ajouteInst(new C3aInstSub(op1, temp2, temp3, ""));
defaultOut(node);
return temp3;
}
@Override @Override
public C3aOperand visit(SaLDecFonc node) throws Exception { public C3aOperand visit(SaLDecFonc node) throws Exception {
defaultIn(node); defaultIn(node);
......
...@@ -12,6 +12,7 @@ Tokens ...@@ -12,6 +12,7 @@ Tokens
espaces = (' ' | 13 | 10)+; espaces = (' ' | 13 | 10)+;
commentaire= '#' [[0 .. 0xffff] - [10 + 13]]* (10 | 13 | 10 13); commentaire= '#' [[0 .. 0xffff] - [10 + 13]]* (10 | 13 | 10 13);
add = '+'; add = '+';
incr = '+=';
sub = '-'; sub = '-';
mult = '*'; mult = '*';
div = '/'; div = '/';
...@@ -30,6 +31,7 @@ gcro = '['; ...@@ -30,6 +31,7 @@ gcro = '[';
dcro = ']'; dcro = ']';
pvirg = ';'; pvirg = ';';
virg = ','; virg = ',';
mod = '%';
si = 'si'; si = 'si';
alors = 'alors'; alors = 'alors';
sinon = 'sinon'; sinon = 'sinon';
...@@ -60,7 +62,7 @@ exp2 = {egal} exp2 egal exp3 | {inf} exp2 inf exp3 | {exp3} exp3; ...@@ -60,7 +62,7 @@ exp2 = {egal} exp2 egal exp3 | {inf} exp2 inf exp3 | {exp3} exp3;
exp3 = {add} exp3 add exp4 | {sub} exp3 sub exp4 | {exp4} exp4; exp3 = {add} exp3 add exp4 | {sub} exp3 sub exp4 | {exp4} exp4;
exp4 = {mult} exp4 mult exp5 | {div} exp4 div exp5 | {exp5} exp5; exp4 = {mult} exp4 mult exp5 | {div} exp4 div exp5 | {modulo} exp4 mod exp5 | {exp5} exp5;
exp5 = {not} not exp5 | {exp6} exp6; exp5 = {not} not exp5 | {exp6} exp6;
...@@ -68,6 +70,7 @@ exp6 = {parenthesis} gpar exp dpar | {nombre} nombre | {vrai} vrai | {faux} faux ...@@ -68,6 +70,7 @@ exp6 = {parenthesis} gpar exp dpar | {nombre} nombre | {vrai} vrai | {faux} faux
ins = {affectation} var egal exp pvirg| ins = {affectation} var egal exp pvirg|
{incrementation} var incr exp pvirg |
{fairetantque} tantque exp faire bloc | {fairetantque} tantque exp faire bloc |
{fonction} id gpar lexp dpar pvirg| {fonction} id gpar lexp dpar pvirg|
{sialors} si exp alors bloc | {sialors} si exp alors bloc |
......
...@@ -30,12 +30,14 @@ saexpinf = 'SaExpInf'; ...@@ -30,12 +30,14 @@ saexpinf = 'SaExpInf';
saexpint = 'SaExpInt'; saexpint = 'SaExpInt';
saexplire = 'SaExpLire'; saexplire = 'SaExpLire';
saexpmult = 'SaExpMult'; saexpmult = 'SaExpMult';
saexpmodulo = 'SaExpModulo';
saexpnot = 'SaExpNot'; saexpnot = 'SaExpNot';
saexpor = 'SaExpOr'; saexpor = 'SaExpOr';
saexpsub = 'SaExpSub'; saexpsub = 'SaExpSub';
saexpvar = 'SaExpVar'; saexpvar = 'SaExpVar';
saexpvrai = 'SaExpVrai'; saexpvrai = 'SaExpVrai';
sainstaffect = 'SaInstAffect'; sainstaffect = 'SaInstAffect';
saincr= 'SaIncr';
sainstbloc = 'SaInstBloc'; sainstbloc = 'SaInstBloc';
sainstecriture = 'SaInstEcriture'; sainstecriture = 'SaInstEcriture';
sainstretour = 'SaInstRetour'; sainstretour = 'SaInstRetour';
...@@ -73,6 +75,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf ...@@ -73,6 +75,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf
| {equal} po saexpequal [op1]:exp [op2]:exp pf | {equal} po saexpequal [op1]:exp [op2]:exp pf
| {inf} po saexpinf [op1]:exp [op2]:exp pf | {inf} po saexpinf [op1]:exp [op2]:exp pf
| {mult} po saexpmult [op1]:exp [op2]:exp pf | {mult} po saexpmult [op1]:exp [op2]:exp pf
| {modulo} po saexpmodulo [op1]:exp [op2]:exp pf
| {or} po saexpor [op1]:exp [op2]:exp pf | {or} po saexpor [op1]:exp [op2]:exp pf
| {sub} po saexpsub [op1]:exp [op2]:exp pf | {sub} po saexpsub [op1]:exp [op2]:exp pf
| {not} po saexpnot exp pf | {not} po saexpnot exp pf
...@@ -85,6 +88,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf ...@@ -85,6 +88,7 @@ exp = {add} po saexpadd [op1]:exp [op2]:exp pf
; ;
inst = {affect} po sainstaffect var exp pf inst = {affect} po sainstaffect var exp pf
| {incr} po saincr var exp pf
| {bloc} po sainstbloc linst pf | {bloc} po sainstbloc linst pf
| {ecriture} po sainstecriture exp pf | {ecriture} po sainstecriture exp pf
| {retour} po sainstretour exp pf | {retour} po sainstretour exp pf
......
...@@ -159,6 +159,19 @@ public class LoadSa extends DepthFirstAdapter { ...@@ -159,6 +159,19 @@ public class LoadSa extends DepthFirstAdapter {
outAMultExp(node); outAMultExp(node);
} }
@Override
public void caseAModuloExp(AModuloExp node)
{
inAModuloExp(node);
node.getOp1().apply(this);
SaExp op1 = (SaExp) returnValue;
node.getOp2().apply(this);
SaExp op2 = (SaExp) returnValue;
returnValue = new SaExpModulo(op1,op2);
outAModuloExp(node);
}
// exp = {or} po saexpor [op1]:exp [op2]:exp pf // exp = {or} po saexpor [op1]:exp [op2]:exp pf
@Override @Override
public void caseAOrExp(AOrExp node) public void caseAOrExp(AOrExp node)
...@@ -267,6 +280,20 @@ public class LoadSa extends DepthFirstAdapter { ...@@ -267,6 +280,20 @@ public class LoadSa extends DepthFirstAdapter {
outAAffectInst(node); outAAffectInst(node);
} }
@Override
public void caseAIncrInst(AIncrInst node)
{
inAIncrInst(node);
node.getVar().apply(this);
SaVar var = (SaVar) returnValue;
node.getExp().apply(this);
SaExp exp = (SaExp) returnValue;
returnValue = new SaIncr(var, exp);
outAIncrInst(node);
}
// inst = {bloc} po sainstbloc linst pf // inst = {bloc} po sainstbloc linst pf
@Override @Override
public void caseABlocInst(ABlocInst node) public void caseABlocInst(ABlocInst node)
......
...@@ -148,17 +148,12 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ ...@@ -148,17 +148,12 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
defaultOut(node); defaultOut(node);
return null; return null;
} }
public T visit(SaLInst node) throws Exception public T visit(SaLInst node) throws Exception
{ {
defaultIn(node); defaultIn(node);
if(node != null){ if(node != null){
if(node.getTete() != null) { if(node.getTete() != null)node.getTete().accept(this);
node.getTete().accept(this); if(node.getQueue() != null) node.getQueue().accept(this);
}
if(node.getQueue() != null) {
node.getQueue().accept(this); }
} }
defaultOut(node); defaultOut(node);
return null; return null;
...@@ -169,10 +164,8 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ ...@@ -169,10 +164,8 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
{ {
defaultIn(node); defaultIn(node);
if(node.getParametres() != null) node.getParametres().accept(this); if(node.getParametres() != null) node.getParametres().accept(this);
if(node.getVariable() != null) {node.getVariable().accept(this);} if(node.getVariable() != null) node.getVariable().accept(this);
if(node.getCorps() != null) { if(node.getCorps() != null) node.getCorps().accept(this);
node.getCorps().accept(this);
}
defaultOut(node); defaultOut(node);
return null; return null;
} }
...@@ -194,6 +187,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ ...@@ -194,6 +187,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
return null; return null;
} }
public T visit(SaIncr node) throws Exception
{
defaultIn(node);
node.getLhs().accept(this);
node.getRhs().accept(this);
defaultOut(node);
return null;
}
// LDEC -> DEC LDEC // LDEC -> DEC LDEC
// LDEC -> null // LDEC -> null
/* public T visit(SaLDec node) throws Exception /* public T visit(SaLDec node) throws Exception
...@@ -276,6 +278,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ ...@@ -276,6 +278,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
return null; return null;
} }
public T visit(SaExpModulo node) throws Exception
{
defaultIn(node);
node.getOp1().accept(this);
node.getOp2().accept(this);
defaultOut(node);
return null;
}
// EXP -> div EXP EXP // EXP -> div EXP EXP
public T visit(SaExpDiv node) throws Exception public T visit(SaExpDiv node) throws Exception
{ {
...@@ -347,14 +358,11 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ ...@@ -347,14 +358,11 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
public T visit(SaInstBloc node) throws Exception public T visit(SaInstBloc node) throws Exception
{ {
defaultIn(node); defaultIn(node);
if (node.getVal() != null) {
node.getVal().accept(this); node.getVal().accept(this);
}defaultOut(node); defaultOut(node);
return null; return null;
} }
public T visit(SaInstSi node) throws Exception public T visit(SaInstSi node) throws Exception
{ {
defaultIn(node); defaultIn(node);
......
...@@ -23,9 +23,7 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> { ...@@ -23,9 +23,7 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
try{ try{
appelMain.accept(this); appelMain.accept(this);
} catch(Exception e){ } catch(Exception e){}
e.printStackTrace();
}
} }
public TypeVal getVar(SaVarSimple saVar){ public TypeVal getVar(SaVarSimple saVar){
...@@ -248,6 +246,21 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> { ...@@ -248,6 +246,21 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
defaultOut(node); defaultOut(node);
return null; return null;
} }
public TypeVal visit(SaIncr node) throws Exception
{
defaultIn(node);
TypeVal incr = node.getRhs().accept(this);
if(node.getLhs() instanceof SaVarIndicee){ // c'est une case de tableau, donc forcément globale
SaVarIndicee lhsIndicee = (SaVarIndicee) node.getLhs();
TypeVal indice = lhsIndicee.getIndice().accept(this);
setVarGlobIndicee(lhsIndicee, indice, new TypeVal(getVarGlobIndicee(lhsIndicee, indice).valInt + incr.valInt));
} else{
setVar((SaVarSimple) node.getLhs(), new TypeVal(getVar((SaVarSimple) node.getLhs()).valInt + incr.valInt));
}
defaultOut(node);
return null;
}
// LDEC -> DEC LDEC // LDEC -> DEC LDEC
// LDEC -> null // LDEC -> null
...@@ -344,6 +357,15 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> { ...@@ -344,6 +357,15 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
return new TypeVal(op1.valInt * op2.valInt); return new TypeVal(op1.valInt * op2.valInt);
} }
public TypeVal visit(SaExpModulo node) throws Exception
{
defaultIn(node);
TypeVal op1 = node.getOp1().accept(this);
TypeVal op2 = node.getOp2().accept(this);
defaultOut(node);
return new TypeVal(op1.valInt % op2.valInt);
}
// EXP -> div EXP EXP // EXP -> div EXP EXP
public TypeVal visit(SaExpDiv node) throws Exception public TypeVal visit(SaExpDiv node) throws Exception
{ {
......
...@@ -28,6 +28,7 @@ interface SaVisitor <T> { ...@@ -28,6 +28,7 @@ interface SaVisitor <T> {
public T visit(SaExpAdd node) throws Exception; public T visit(SaExpAdd node) throws Exception;
public T visit(SaExpSub node) throws Exception; public T visit(SaExpSub node) throws Exception;
public T visit(SaExpMult node) throws Exception; public T visit(SaExpMult node) throws Exception;
public T visit(SaExpModulo node) throws Exception;
public T visit(SaExpDiv node) throws Exception; public T visit(SaExpDiv node) throws Exception;
public T visit(SaExpInf node) throws Exception; public T visit(SaExpInf node) throws Exception;
public T visit(SaExpEqual node) throws Exception; public T visit(SaExpEqual node) throws Exception;
...@@ -35,4 +36,6 @@ interface SaVisitor <T> { ...@@ -35,4 +36,6 @@ interface SaVisitor <T> {
public T visit(SaExpOr node) throws Exception; public T visit(SaExpOr node) throws Exception;
public T visit(SaExpNot node) throws Exception; public T visit(SaExpNot node) throws Exception;
public T visit(SaLExp node) throws Exception; public T visit(SaLExp node) throws Exception;
public T visit(SaIncr node) throws Exception;
} }
...@@ -607,5 +607,32 @@ public class Sc2sa extends DepthFirstAdapter ...@@ -607,5 +607,32 @@ public class Sc2sa extends DepthFirstAdapter
this.returnValue = null; this.returnValue = null;
outAEpsilon(node); outAEpsilon(node);
} }
@Override
public void caseAIncrementationIns(AIncrementationIns node) {
inAIncrementationIns(node);
SaVar lhs = null;
SaExp rhs = null;
node.getVar().apply(this);
lhs = (SaVar) this.returnValue;
node.getExp().apply(this);
rhs = (SaExp) this.returnValue;
this.returnValue = new SaIncr(lhs, rhs);
outAIncrementationIns(node);
}
@Override
public void caseAModuloExp4(AModuloExp4 node) {
inAModuloExp4(node);
SaExp op1 = null;
SaExp op2 = null;
node.getExp4().apply(this);
op1 = (SaExp) this.returnValue;
node.getExp5().apply(this);
op2 = (SaExp) this.returnValue;
this.returnValue = new SaExpModulo(op1, op2);
outAModuloExp4(node);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment