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

dfg

parent 68ab49e9
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,7 @@ public class Compiler ...@@ -38,6 +38,7 @@ public class Compiler
/* /*
System.out.println("[TYPE CHECKING]"); System.out.println("[TYPE CHECKING]");
typeCheck(); typeCheck();
System.out.println("[BUILD C3A] "); System.out.println("[BUILD C3A] ");
buildC3a(); buildC3a();
System.out.println("[BUILD PRE NASM] "); System.out.println("[BUILD PRE NASM] ");
......
...@@ -16,4 +16,6 @@ public class SaInstBloc implements SaInst{ ...@@ -16,4 +16,6 @@ public class SaInstBloc implements SaInst{
public String toString() { public String toString() {
return "(" + this.getClass().getSimpleName() + " " + val + ")"; return "(" + this.getClass().getSimpleName() + " " + val + ")";
} }
} }
...@@ -25,4 +25,5 @@ public class SaLDecFonc implements SaNode{ ...@@ -25,4 +25,5 @@ public class SaLDecFonc implements SaNode{
return "(" + this.getClass().getSimpleName() + " " + tete + " " + queue + ")"; return "(" + this.getClass().getSimpleName() + " " + tete + " " + queue + ")";
} }
} }
...@@ -3,13 +3,31 @@ import util.Type; ...@@ -3,13 +3,31 @@ import util.Type;
import util.Error; import util.Error;
import ts.*; import ts.*;
import java.util.ArrayList;
import java.util.List;
public class SaTypeCheck extends SaDepthFirstVisitor <Void>{ public class SaTypeCheck extends SaDepthFirstVisitor <Void>{
private TsItemFct fonctionCourante; private TsItemFct fonctionCourante;
private List<TsItemFct> fonctions;
public SaTypeCheck(SaNode root) public SaTypeCheck(SaNode root)
{ {
try{ try{
//BANCAL
/*
fonctions = new ArrayList<>();
SaProg prog = (SaProg) root;
SaLDecFonc list = prog.getFonctions();
for (int i = 0; i < list.length(); i++) {
fonctions.add(list.getTete().tsItem);
list = list.getQueue();
}
fonctionCourante = fonctions.get(0);
*/
root.accept(this); root.accept(this);
} catch(ErrorException e){ } catch(ErrorException e){
System.err.print("ERREUR DE TYPAGE : "); System.err.print("ERREUR DE TYPAGE : ");
System.err.println(e.getMessage()); System.err.println(e.getMessage());
...@@ -17,14 +35,132 @@ public class SaTypeCheck extends SaDepthFirstVisitor <Void>{ ...@@ -17,14 +35,132 @@ public class SaTypeCheck extends SaDepthFirstVisitor <Void>{
} catch(Exception e){} } catch(Exception e){}
} }
@Override
public Void visit(SaExpAdd node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.BOOL) || Type.checkCompatibility(node.getOp2().getType(), Type.BOOL)) {
throw new ErrorException(Error.TYPE, "Veuillez additioner uniquement des entiers");
}
return null;
}
@Override
public Void visit(SaInstTantQue node) throws Exception {
if (Type.checkCompatibility(node.getTest().getType(), Type.ENTIER)) {
throw new ErrorException(Error.TYPE, "Le résultat du test de la condition doit être un booléen");
}
return null;
}
@Override
public Void visit(SaInstAffect node) throws Exception {
String id = node.getLhs().getTsItem().identif;
Ts table = fonctionCourante.getTable();
Type type = table.getVar(id).getType();
if (!Type.checkCompatibility(node.getRhs().getType(), type)) {
throw new ErrorException(Error.TYPE, "Le type affecté doit être le même que celui spécifié à la déclaration de la variable");
}
return null;
}
@Override
public Void visit(SaExpSub node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.BOOL) || Type.checkCompatibility(node.getOp2().getType(), Type.BOOL)) {
throw new ErrorException(Error.TYPE, "Veuillez soustraire uniquement des entiers");
}
return null;
}
@Override
public Void visit(SaExpMult node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.BOOL) || Type.checkCompatibility(node.getOp2().getType(), Type.BOOL)) {
throw new ErrorException(Error.TYPE, "Veuillez multiplier uniquement des entiers");
}
return null;
}
@Override
public Void visit(SaExpDiv node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.BOOL) || Type.checkCompatibility(node.getOp2().getType(), Type.BOOL)) {
throw new ErrorException(Error.TYPE, "Veuillez diviser uniquement des entiers");
}
return null;
}
@Override
public Void visit(SaExpInf node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.BOOL) || Type.checkCompatibility(node.getOp2().getType(), Type.BOOL)) {
throw new ErrorException(Error.TYPE, "L'opérateur '<' marche uniquement sur des entiers");
}
return null;
}
@Override
public Void visit(SaExpEqual node) throws Exception {
if (!Type.checkCompatibility(node.getOp1().getType(), node.getOp2().getType())) {
throw new ErrorException(Error.TYPE, "Veuillez comparer uniquement des operande du même type");
}
return null;
}
@Override
public Void visit(SaExpAnd node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.ENTIER) || Type.checkCompatibility(node.getOp2().getType(), Type.ENTIER)) {
throw new ErrorException(Error.TYPE, "Veuillez 'And' uniquement des booléens");
}
return null;
}
@Override
public Void visit(SaExpOr node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.ENTIER) || Type.checkCompatibility(node.getOp2().getType(), Type.ENTIER)) {
throw new ErrorException(Error.TYPE, "Veuillez 'Or' uniquement des booléens");
}
return null;
}
@Override
public Void visit(SaExpNot node) throws Exception {
if (Type.checkCompatibility(node.getOp1().getType(), Type.ENTIER)) {
throw new ErrorException(Error.TYPE, "Veuillez 'Not' uniquement des booléens");
}
return null;
}
@Override
public Void visit(SaInstSi node) throws Exception {
if (Type.checkCompatibility(node.getTest().getType(), Type.ENTIER)) {
throw new ErrorException(Error.TYPE, "Le résultat du test de la condition doit être un booléen");
}
return null;
}
@Override
public Void visit(SaInstRetour node) throws Exception {
if (!Type.checkCompatibility(node.getType(), fonctionCourante.getTypeRetour())) {
throw new ErrorException(Error.TYPE, "Le type retourné doit être le même que celui de la fonction");
}
return null;
}
@Override
public Void visit(SaAppel node) throws Exception {
throw new ErrorException(Error.TYPE, "nique");
}
public void defaultIn(SaNode node) public void defaultIn(SaNode node)
{ {
// System.out.println("<" + node.getClass().getSimpleName() + ">"); System.out.println("<" + node.getClass().getSimpleName() + ">");
} }
public void defaultOut(SaNode node) public void defaultOut(SaNode node)
{ {
// System.out.println("</" + node.getClass().getSimpleName() + ">"); System.out.println("</" + node.getClass().getSimpleName() + ">");
} }
......
...@@ -41,30 +41,43 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { ...@@ -41,30 +41,43 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> {
@Override @Override
public Void visit(SaDecFonc node) throws Exception { public Void visit(SaDecFonc node) throws Exception {
String idFct = node.getNom(); String idFct = node.getNom();
SaLDecVar parameters = node.getParametres(); SaLDecVar parameters = node.getParametres();
SaLDecVar variable = node.getVariable(); SaLDecVar variable = node.getVariable();
SaInst corps = node.getCorps(); SaInst corps = node.getCorps();
if (tableGlobale.getFct(idFct) != null) { if (tableGlobale.getFct(idFct) != null) {
throw new ErrorException(Error.TS, "La fonction " + idFct + " est déjà définie."); throw new ErrorException(Error.TS, "La fonction " + idFct + " est déjà définie.");
} }
Ts table = new Ts(); Ts table = new Ts();
tableLocaleCourante = table; tableLocaleCourante = table;
int nbArgs = 0; int nbArgs = 0;
if (parameters != null) { if (parameters != null) {
context = Context.PARAM; context = Context.PARAM;
parameters.accept(this); parameters.accept(this);
nbArgs = parameters.length(); nbArgs = parameters.length();
} }
if (variable != null) { if (variable != null) {
context = Context.LOCAL; context = Context.LOCAL;
variable.accept(this); variable.accept(this);
} }
node.tsItem = tableGlobale.addFct(idFct, node.getTypeRetour(), nbArgs, table, node); node.tsItem = tableGlobale.addFct(idFct, node.getTypeRetour(), nbArgs, table, node);
if (corps != null) {
//besoin de rajouter un equals pour le SaInstBloc
SaInstBloc tempCorps = (SaInstBloc) corps;
if (corps != null && tempCorps.getVal() != null) {
context = Context.LOCAL; context = Context.LOCAL;
corps.accept(this); corps.accept(this);
} }
context = Context.GLOBAL; context = Context.GLOBAL;
return null; return null;
} }
...@@ -131,7 +144,6 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { ...@@ -131,7 +144,6 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> {
if (tableGlobale.getFct(idFct) == null) { if (tableGlobale.getFct(idFct) == null) {
throw new ErrorException(Error.TS, "La fonction " + idFct + " n'est pas définie."); throw new ErrorException(Error.TS, "La fonction " + idFct + " n'est pas définie.");
} }
if (this.tableGlobale.getFct(idFct).getNbArgs() != nbArgs) { if (this.tableGlobale.getFct(idFct).getNbArgs() != nbArgs) {
throw new ErrorException(Error.TS, "Le nombre d'arguments est incorrect." throw new ErrorException(Error.TS, "Le nombre d'arguments est incorrect."
+ this.tableGlobale.getFct(idFct).getNbArgs() + " requis."); + this.tableGlobale.getFct(idFct).getNbArgs() + " requis.");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment