diff --git a/src/ts/Sa2ts.java b/src/ts/Sa2ts.java index 492f8c5794a4af559baa071d11d0314ef981d632..238982db82e3bd7e4674caf6c1bca0ad005ff6fa 100644 --- a/src/ts/Sa2ts.java +++ b/src/ts/Sa2ts.java @@ -1,6 +1,4 @@ package ts; -import lParser.node.AType; -import lParser.node.ATypeType; import sa.*; import util.Error; @@ -11,7 +9,7 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { PARAM } - private Ts tableGlobale; + private final Ts tableGlobale; private Ts tableLocaleCourante; private Context context; @@ -25,20 +23,29 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { context = Context.GLOBAL; } - public Void visit(SaDecVar node) { - if (tableLocaleCourante.getVar(node.getNom()) == null) { - if (node.getTsItem() == null) { - tableLocaleCourante.addVar(node.getNom(),node.getType()); + public Void visit(SaDecVar node) throws Exception{ + defaultIn(node); + TsItemVar ts = null; + try { + if (context == Context.PARAM) { + ts = tableLocaleCourante.addParam(node.getNom(), node.getType()); + } else if (context == Context.LOCAL) { + if (tableLocaleCourante.getVar(node.getNom()) != null) + throw new ErrorException(Error.TS, "Variable locale"); + ts = tableLocaleCourante.addVar(node.getNom(), node.getType()); } else { - if (node.getTsItem().isParam) { - tableLocaleCourante.addParam(node.getNom(),node.getType()); - } else { - tableLocaleCourante.addVar(node.getNom(),node.getType()); - } + if (tableGlobale.getVar(node.getNom()) != null) + throw new ErrorException(Error.TS, "Variable globale"); + ts = tableLocaleCourante.addVar(node.getNom(),node.getType()); } - } else { - System.err.println("La variable/ le paramètre" + node.getNom() + "existe déjà !"); + }catch(ErrorException e){ + System.err.print("Erreur dans la declaration de variable"); + System.err.println(e.getMessage()); + System.exit(e.getCode()); } + node.setTsItem(ts); + defaultOut(node); + System.out.println(tableGlobale.variables); return null; } @@ -100,32 +107,39 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { node.tsItem = tsItemVar; return null; } - public Void visit(SaVarIndicee node){ - TsItemVar var = tableGlobale.getVar(node.getNom()); - if(var == null) - System.err.println("Introuvable"); - else - node.tsItem = var; + public Void visit(SaVarIndicee node) throws Exception { + defaultIn(node); + if(tableGlobale.getVar(node.getNom()) != null){ + node.tsItem = tableGlobale.getVar(node.getNom()); + node.getIndice().accept(this); + } + else if(tableLocaleCourante.getVar(node.getNom()) != null){ + node.tsItem = tableLocaleCourante.getVar(node.getNom()); + node.getIndice().accept(this); + } + else{ + throw new ErrorException(Error.TS,""); + } + defaultOut(node); return null; - } - public Void visit(SaAppel node){ - String nom = node.getNom(); - int nbArgs; - if(node.getArguments() == null) - nbArgs = 0; - else - nbArgs = node.getArguments().length(); - TsItemFct tsItemFct = tableGlobale.getFct(nom); - - if(tableGlobale.getFct(nom) == null) - throw new RuntimeException(("Erreur")); - - if(nbArgs != tsItemFct.nbArgs) - throw new RuntimeException("Erreur"); - node.tsItem = tsItemFct; + public Void visit(SaAppel node) throws Exception { + defaultIn(node); + if(node.getArguments().length() == tableGlobale.getFct(node.getNom()).getNbArgs()){ + node.getArguments().accept(this); + node.tsItem = tableGlobale.getFct(node.getNom()); + } + else if(node.getArguments() == null && tableGlobale.getFct(node.getNom()).getNbArgs() == 0) { + node.tsItem = tableGlobale.getFct(node.getNom()); + } + else { + throw new ErrorException(Error.TS,""); + } + context = Context.LOCAL; + node.tsItem = tableGlobale.getFct(node.getNom()); + context = Context.GLOBAL; + defaultOut(node); return null; - }