diff --git a/src/Compiler.java b/src/Compiler.java index 41d8b22805d94c4f529c43924fa078c2a51ac52b..c59404dda1027e12400b1b6661614dfc511f6283 100644 --- a/src/Compiler.java +++ b/src/Compiler.java @@ -35,10 +35,10 @@ public class Compiler buildSa(); System.out.println("[BUILD TS] "); buildTs(); -/* + System.out.println("[TYPE CHECKING]"); typeCheck(); - +/* System.out.println("[BUILD C3A] "); buildC3a(); System.out.println("[BUILD PRE NASM] "); diff --git a/src/sa/SaDepthFirstVisitor.java b/src/sa/SaDepthFirstVisitor.java index ded93ad194c4d1bc1f81c6f76a48c900262ce527..d3820c5541e592a6f1bd6d1db5101842514513de 100644 --- a/src/sa/SaDepthFirstVisitor.java +++ b/src/sa/SaDepthFirstVisitor.java @@ -148,12 +148,17 @@ 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; @@ -164,8 +169,10 @@ 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; } @@ -340,10 +347,13 @@ public class SaDepthFirstVisitor <T> implements SaVisitor <T>{ public T visit(SaInstBloc node) throws Exception { defaultIn(node); - node.getVal().accept(this); - defaultOut(node); + if (node.getVal() != null) { + node.getVal().accept(this); + }defaultOut(node); return null; } + + public T visit(SaInstSi node) throws Exception { diff --git a/src/sa/SaTypeCheck.java b/src/sa/SaTypeCheck.java index 00c093f8875ee56778fdfa5c15bdfc206c97f5bc..9ab31967ceb530bbd55c055ae1886956de98ed0b 100644 --- a/src/sa/SaTypeCheck.java +++ b/src/sa/SaTypeCheck.java @@ -14,17 +14,6 @@ public class SaTypeCheck extends SaDepthFirstVisitor <Void>{ public SaTypeCheck(SaNode root) { 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); @@ -52,13 +41,10 @@ public class SaTypeCheck extends SaDepthFirstVisitor <Void>{ 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)) { + if (!Type.checkCompatibility(node.getLhs().getTsItem().getType(), node.getRhs().getType())) { 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; @@ -136,18 +122,47 @@ public class SaTypeCheck extends SaDepthFirstVisitor <Void>{ return null; } + @Override - public Void visit(SaInstRetour node) throws Exception { + public Void visit(SaDecFonc node) throws Exception { + fonctionCourante = node.tsItem; + return super.visit(node); + } + @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"); + SaLExp listCalledArgs = node.getArguments(); + List<Type> typeList = new ArrayList<>(); + while (typeList.size() <= listCalledArgs.length()) { + typeList.add(listCalledArgs.getTete().getType()); + if (listCalledArgs.getQueue() == null) { + break; + } + listCalledArgs = listCalledArgs.getQueue(); + } + + SaLDecVar params = node.tsItem.saDecFonc.getParametres(); + + for (int i = 0; i <= params.length(); i ++) { + if (!Type.checkCompatibility(params.getTete().getType(), typeList.get(i))) { + throw new ErrorException(Error.TYPE, "Les types des arguments doivent être les mêmes que ceux spécifiés dans la fonction"); + } + if (params.getQueue() == null) { + break; + } + params = params.getQueue(); + } + + return null; }