diff --git a/src/Compiler.java b/src/Compiler.java index 6d78befcd03da15d07a93737416e5d4dc7f39749..6a87a9259bb5cd80e8135de893f074d8c75f69b0 100644 --- a/src/Compiler.java +++ b/src/Compiler.java @@ -32,9 +32,10 @@ public class Compiler System.out.println("[BUILD SC] "); buildSc(); System.out.println("[BUILD SA] "); - buildSa();/* + buildSa(); System.out.println("[BUILD TS] "); buildTs(); + /* System.out.println("[TYPE CHECKING]"); typeCheck(); System.out.println("[BUILD C3A] "); diff --git a/src/ts/Sa2ts.java b/src/ts/Sa2ts.java index 50a80a6c17ef87000261b8a1dbf16dd07675709e..3838506778de3f84e420f4b308981246c8ea2d1e 100644 --- a/src/ts/Sa2ts.java +++ b/src/ts/Sa2ts.java @@ -1,6 +1,7 @@ package ts; import sa.*; import util.Error; +import util.Type; public class Sa2ts extends SaDepthFirstVisitor <Void> { enum Context { @@ -22,14 +23,142 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { context = Context.GLOBAL; } + @Override + public Void visit(SaDecTab node) throws Exception { + String nomTableau = node.getNom(); + Type typeTableau = node.getType(); + int tailleTableau = node.getTaille(); + if (context == Context.GLOBAL) { + if (tableGlobale.getVar(nomTableau) != null) { + throw new ErrorException(Error.TS, "Le tableau " + nomTableau + " est déjà défini."); + } + tableGlobale.addTab(nomTableau, typeTableau, tailleTableau); + } + return null; + } + + @Override + public Void visit(SaDecFonc node) throws Exception { + String nomFonction = node.getNom(); + SaLDecVar parameters = node.getParametres(); + SaLDecVar variable = node.getVariable(); + SaInst corps = node.getCorps(); + if (tableGlobale.getFct(nomFonction) != null) { + throw new ErrorException(Error.TS, "La fonction " + nomFonction + " est déjà définie."); + } + Ts table = new Ts(); + tableLocaleCourante = table; + int param = 0; + if (parameters != null) { + context = Context.PARAM; + parameters.accept(this); + param = parameters.length(); + } + if (variable != null) { + context = Context.LOCAL; + variable.accept(this); + } + node.tsItem = tableGlobale.addFct(nomFonction, node.getTypeRetour(), param, table, node); + if (corps != null) { + context = Context.LOCAL; + corps.accept(this); + } + context = Context.GLOBAL; + return null; + } + + @Override + public Void visit(SaDecVar node) throws Exception { + String nomVar = node.getNom(); + Type typeVar = node.getType(); + if (context == Context.LOCAL) { + if (tableLocaleCourante.getVar(nomVar) != null) { + throw new ErrorException(Error.TS, "La variable " + nomVar + " est déjà définie."); + } + node.setTsItem(tableLocaleCourante.addVar(nomVar, typeVar)); + } else if (context == Context.PARAM) { + if (tableLocaleCourante.getVar(nomVar) != null) { + throw new ErrorException(Error.TS, "La variable " + nomVar + " est déjà définie."); + } + node.setTsItem(tableLocaleCourante.addParam(nomVar, typeVar)); + } else if (context == Context.GLOBAL) { + if (tableGlobale.getVar(nomVar) != null) { + throw new ErrorException(Error.TS, "La variable " + nomVar + " est déjà définie."); + } + node.setTsItem(tableGlobale.addVar(nomVar, typeVar)); + } + return null; + } + + @Override + public Void visit(SaVarSimple node) throws Exception { + String nomVar = node.getNom(); + TsItemVarSimple varSimple = node.getTsItem(); + if (context == Context.LOCAL) { + if (tableLocaleCourante.getVar(nomVar) == null && tableGlobale.getVar(nomVar) == null) { + throw new ErrorException(Error.TS, "La variable " + nomVar + " n'est pas définie."); + } + if (tableLocaleCourante.getVar(nomVar) != null) { + node.tsItem = (TsItemVarSimple) tableLocaleCourante.getVar(nomVar); + } else { + node.tsItem = (TsItemVarSimple) tableGlobale.getVar(nomVar); + } + } else if (context == Context.PARAM) { + if (tableLocaleCourante.getVar(nomVar) == null) { + throw new ErrorException(Error.TS, "La variable " + nomVar + " n'est pas définie."); + } + } else if (context == Context.GLOBAL) { + if (tableGlobale.getVar(nomVar) == null || varSimple.getTaille() > 1) { + throw new ErrorException(Error.TS, "La variable " + nomVar + " n'est pas définie."); + } + node.tsItem = (TsItemVarSimple) tableGlobale.getVar(nomVar); + } + return null; + } + + @Override + public Void visit(SaAppel node) throws Exception { + String nomFonction = node.getNom(); + SaLExp arguments = node.getArguments(); + int args = 0; + if (arguments != null) { + arguments.accept(this); + args = arguments.length(); + } + + if (tableGlobale.getFct(nomFonction) == null) { + throw new ErrorException(Error.TS, "La fonction " + nomFonction + " n'est pas définie."); + } + + if (this.tableGlobale.getFct(nomFonction).getNbArgs() != args) { + throw new ErrorException(Error.TS, "Le nombre d'arguments est incorrect." + + this.tableGlobale.getFct(nomFonction).getNbArgs() + " requis."); + } + + node.tsItem = tableGlobale.getFct(node.getNom()); + return null; + } + + @Override + public Void visit(SaVarIndicee node) throws Exception { + String nomVar = node.getNom(); + SaExp indice = node.getIndice(); + if (tableGlobale.getVar(nomVar) == null && tableGlobale.getVar(nomVar).getTaille() == 1) + throw new ErrorException(Error.TS, "indice " + nomVar + " incorrect."); + + node.tsItem = tableGlobale.getVar(node.getNom()); + indice.accept(this); + return null; + } + public void defaultIn(SaNode node) { - // System.out.println("<" + node.getClass().getSimpleName() + ">"); + System.out.println("<" + node.getClass().getSimpleName() + ">"); } public void defaultOut(SaNode node) { - // System.out.println("</" + node.getClass().getSimpleName() + ">"); + System.out.println("</" + node.getClass().getSimpleName() + ">"); } diff --git a/src/ts/Ts.java b/src/ts/Ts.java index 770b95fb929f894e506bf2c7cf1e10774fd3e1aa..e61eafc549d6a17fda3e77346f95a5b3f49b45c1 100644 --- a/src/ts/Ts.java +++ b/src/ts/Ts.java @@ -23,11 +23,7 @@ public class Ts public TsItemVar addVar(String identif, Type type) { - for (String id : variables.keySet()) { - if (id == identif && variables.get(id).isParam) { - return null; - } - } + TsItemVar item = new TsItemVarSimple(identif, type); item.portee = this; item.adresse = this.adrVarCourante; @@ -39,11 +35,7 @@ public class Ts public TsItemVar addParam(String identif, Type type) { - for (String id : variables.keySet()) { - if (id == identif && variables.get(id).isParam) { - return null; - } - } + TsItemVar item = new TsItemVarSimple(identif, type); item.portee = this; item.adresse = this.adrArgCourante; @@ -55,7 +47,7 @@ public class Ts public TsItemVar addTab(String identif, Type type, int taille) { - //Verifier qu'on est en global + TsItemVar item = new TsItemVarTab(identif, type, taille); item.portee = this; item.adresse = this.adrVarCourante; diff --git a/src/ts/TsItemVar.java b/src/ts/TsItemVar.java index 7dae2bb896d63d316875f99ff8a3a45023110c49..2a7f1e576253f8354265059dd3de8d4d809fdac5 100644 --- a/src/ts/TsItemVar.java +++ b/src/ts/TsItemVar.java @@ -9,6 +9,7 @@ public abstract class TsItemVar { public boolean isParam; public Type type; + public int getAdresse(){return this.adresse;} public String getIdentif(){return this.identif;} public Ts getPortee() {return this.portee;}