From 5f38b4011940a0db2e15c5cca2d5156763de1017 Mon Sep 17 00:00:00 2001
From: b21202065 <niels.bauquin@etu.univ-amu.fr>
Date: Fri, 12 Apr 2024 10:24:02 +0200
Subject: [PATCH] Exam test 20/20

---
 src/c3a/Sa2c3a.java             | 46 ++++++++++++++++++++++++++
 src/l.cfg                       |  5 ++-
 src/sa.cfg                      |  4 +++
 src/sa/LoadSa.java              | 29 ++++++++++++++++-
 src/sa/SaDepthFirstVisitor.java | 58 +++++++++++++++++++--------------
 src/sa/SaEval.java              | 48 +++++++++++++++++++--------
 src/sa/SaVisitor.java           |  3 ++
 src/sa/Sc2sa.java               | 27 +++++++++++++++
 8 files changed, 180 insertions(+), 40 deletions(-)

diff --git a/src/c3a/Sa2c3a.java b/src/c3a/Sa2c3a.java
index 3636648..eeaf5b3 100644
--- a/src/c3a/Sa2c3a.java
+++ b/src/c3a/Sa2c3a.java
@@ -176,10 +176,56 @@ public class Sa2c3a extends SaDepthFirstVisitor <C3aOperand> {
     @Override
     public C3aOperand visit(SaLDecVar node) throws Exception {
         defaultIn(node);
+
+
+
+
+
+
         defaultOut(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
     public C3aOperand visit(SaLDecFonc node) throws Exception {
         defaultIn(node);
diff --git a/src/l.cfg b/src/l.cfg
index bb53371..d673f05 100644
--- a/src/l.cfg
+++ b/src/l.cfg
@@ -12,6 +12,7 @@ Tokens
 espaces = (' ' | 13 | 10)+;
 commentaire= '#' [[0 .. 0xffff] - [10 + 13]]* (10 | 13 | 10 13);
 add = '+';
+incr = '+=';
 sub = '-';
 mult = '*';
 div = '/';
@@ -30,6 +31,7 @@ gcro = '[';
 dcro = ']';
 pvirg = ';';
 virg = ',';
+mod = '%';
 si = 'si';
 alors = 'alors';
 sinon = 'sinon';
@@ -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;
 
-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;
 
@@ -68,6 +70,7 @@ exp6 = {parenthesis} gpar exp dpar | {nombre} nombre | {vrai} vrai | {faux} faux
 
 
 ins = {affectation} var egal exp pvirg|
+{incrementation} var incr exp pvirg |
 {fairetantque} tantque exp faire bloc |
 {fonction} id gpar lexp dpar pvirg|
 {sialors} si exp alors bloc |
diff --git a/src/sa.cfg b/src/sa.cfg
index 60ff9b8..142f147 100644
--- a/src/sa.cfg
+++ b/src/sa.cfg
@@ -30,12 +30,14 @@ saexpinf       = 'SaExpInf';
 saexpint = 'SaExpInt';
 saexplire = 'SaExpLire';
 saexpmult = 'SaExpMult';
+saexpmodulo = 'SaExpModulo';
 saexpnot = 'SaExpNot';
 saexpor = 'SaExpOr';
 saexpsub = 'SaExpSub';
 saexpvar = 'SaExpVar';
 saexpvrai = 'SaExpVrai';
 sainstaffect = 'SaInstAffect';
+saincr= 'SaIncr';
 sainstbloc = 'SaInstBloc';
 sainstecriture = 'SaInstEcriture';
 sainstretour = 'SaInstRetour';
@@ -73,6 +75,7 @@ exp = {add}       po saexpadd   [op1]:exp [op2]:exp pf
     | {equal}     po saexpequal [op1]:exp [op2]:exp pf
     | {inf}       po saexpinf   [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
     | {sub}       po saexpsub   [op1]:exp [op2]:exp pf
     | {not}       po saexpnot         exp pf
@@ -85,6 +88,7 @@ exp = {add}       po saexpadd   [op1]:exp [op2]:exp pf
     ;
 
 inst = {affect}   po sainstaffect   var exp pf
+     | {incr}     po saincr         var exp pf
      | {bloc}     po sainstbloc     linst pf
      | {ecriture} po sainstecriture exp pf
      | {retour}   po sainstretour   exp pf
diff --git a/src/sa/LoadSa.java b/src/sa/LoadSa.java
index 209de9c..758a851 100644
--- a/src/sa/LoadSa.java
+++ b/src/sa/LoadSa.java
@@ -159,7 +159,20 @@ public class LoadSa extends DepthFirstAdapter {
         outAMultExp(node);
     }
 
-// exp = {or}        po saexpor    [op1]:exp [op2]:exp pf
+    @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
     @Override
     public void caseAOrExp(AOrExp node)
     {
@@ -267,6 +280,20 @@ public class LoadSa extends DepthFirstAdapter {
         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
     @Override
     public void caseABlocInst(ABlocInst node)
diff --git a/src/sa/SaDepthFirstVisitor.java b/src/sa/SaDepthFirstVisitor.java
index d3820c5..fb9886c 100644
--- a/src/sa/SaDepthFirstVisitor.java
+++ b/src/sa/SaDepthFirstVisitor.java
@@ -148,17 +148,12 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
 	defaultOut(node);
 	return null;
     }
-
     public T visit(SaLInst node) throws Exception
     {
 	defaultIn(node);
 	if(node != null){
-	    if(node.getTete() != null) {
-			node.getTete().accept(this);
-		}
-		if(node.getQueue() != null) {
-			node.getQueue().accept(this); 		}
-
+	    if(node.getTete() != null)node.getTete().accept(this);
+	    if(node.getQueue() != null) node.getQueue().accept(this);
 	}
 	defaultOut(node);
 	return null;
@@ -169,10 +164,8 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
     {
 	defaultIn(node);
 	if(node.getParametres() != null) node.getParametres().accept(this);
-	if(node.getVariable() != null) {node.getVariable().accept(this);}
-	if(node.getCorps() != null) {
-		node.getCorps().accept(this);
-	}
+	if(node.getVariable() != null) node.getVariable().accept(this);
+	if(node.getCorps() != null) node.getCorps().accept(this);
 	defaultOut(node);
 	return null;
     }
@@ -193,6 +186,15 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
 	defaultOut(node);
 	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 -> null 
@@ -267,16 +269,25 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
     }
 
     // EXP -> mult EXP EXP
-    public T visit(SaExpMult node) throws Exception
-    {
-	defaultIn(node);
-	node.getOp1().accept(this);
-	node.getOp2().accept(this);
-	defaultOut(node);
-	return null;
-    }
+	public T visit(SaExpMult node) throws Exception
+	{
+		defaultIn(node);
+		node.getOp1().accept(this);
+		node.getOp2().accept(this);
+		defaultOut(node);
+		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
     {
 	defaultIn(node);
@@ -347,13 +358,10 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{
     public T visit(SaInstBloc node) throws Exception
     {
 	defaultIn(node);
-	if (node.getVal() != null) {
-		node.getVal().accept(this);
-	}defaultOut(node);
+	node.getVal().accept(this);
+	defaultOut(node);
 	return null;
     }
-
-
     
     public T visit(SaInstSi node) throws Exception
     {
diff --git a/src/sa/SaEval.java b/src/sa/SaEval.java
index 7f284f6..8bba44f 100644
--- a/src/sa/SaEval.java
+++ b/src/sa/SaEval.java
@@ -23,9 +23,7 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
 
 		try{
 			appelMain.accept(this);
-		} catch(Exception e){
-			e.printStackTrace();
-		}
+		} catch(Exception e){}
 	}
 
 	public TypeVal getVar(SaVarSimple saVar){
@@ -232,9 +230,9 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
 		defaultOut(node);
 		return null;
     }
-    
-    public TypeVal visit(SaInstAffect node) throws Exception
-    {
+
+	public TypeVal visit(SaInstAffect node) throws Exception
+	{
 		defaultIn(node);
 		TypeVal typeVal = node.getRhs().accept(this);
 		if(node.getLhs() instanceof SaVarIndicee){ // c'est une case de tableau, donc forcément globale
@@ -247,9 +245,24 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
 
 		defaultOut(node);
 		return null;
-    }
-    
-    // LDEC -> DEC LDEC 
+	}
+	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 -> null 
     /*    public TypeVal visit(SaLDec node) throws Exception
     {
@@ -335,16 +348,25 @@ public class SaEval extends SaDepthFirstVisitor <TypeVal> {
     }
 
     // EXP -> mult EXP EXP
-    public TypeVal visit(SaExpMult node) throws Exception
-    {
+	public TypeVal visit(SaExpMult 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);
-    }
+	}
+
+	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
     {
 		defaultIn(node);
diff --git a/src/sa/SaVisitor.java b/src/sa/SaVisitor.java
index 3d28bb7..962d2f2 100644
--- a/src/sa/SaVisitor.java
+++ b/src/sa/SaVisitor.java
@@ -28,6 +28,7 @@ interface SaVisitor <T> {
     public T visit(SaExpAdd node) throws Exception;
     public T visit(SaExpSub 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(SaExpInf node) throws Exception;
     public T visit(SaExpEqual node) throws Exception;
@@ -35,4 +36,6 @@ interface SaVisitor <T> {
     public T visit(SaExpOr node) throws Exception;
     public T visit(SaExpNot node) throws Exception;
     public T visit(SaLExp node) throws Exception;
+    public T visit(SaIncr node) throws Exception;
+
 }
diff --git a/src/sa/Sc2sa.java b/src/sa/Sc2sa.java
index 90bdf60..4cdb24b 100644
--- a/src/sa/Sc2sa.java
+++ b/src/sa/Sc2sa.java
@@ -607,5 +607,32 @@ public class Sc2sa extends DepthFirstAdapter
         this.returnValue = null;
         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);
+
+    }
 }
 
-- 
GitLab