Select Git revision
Sa2ts.java
Forked from
NASR Alexis / 2024_compilation
7 commits behind, 18 commits ahead of the upstream repository.
BAUQUIN Niels authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Sa2ts.java 5.86 KiB
package ts;
import sa.*;
import util.Error;
import util.Type;
public class Sa2ts extends SaDepthFirstVisitor <Void> {
enum Context {
LOCAL,
GLOBAL,
PARAM
}
private Ts tableGlobale;
private Ts tableLocaleCourante;
private Context context;
public Ts getTableGlobale(){return this.tableGlobale;}
public Sa2ts()
{
tableGlobale = new Ts();
tableLocaleCourante = null;
context = Context.GLOBAL;
}
@Override
public Void visit(SaDecTab node) throws Exception {
String idTab = node.getNom();
Type typeTab = node.getType();
int tailleTab = node.getTaille();
if (context == Context.GLOBAL) {
if (tableGlobale.getVar(idTab) != null) {
throw new ErrorException(Error.TS, "Le tableau " + idTab + " est déjà défini.");
}
node.tsItem = tableGlobale.addTab(idTab, typeTab, tailleTab);
}
return null;
}
@Override
public Void visit(SaDecFonc node) throws Exception {
String idFct = node.getNom();
SaLDecVar parameters = node.getParametres();
SaLDecVar variable = node.getVariable();
SaInst corps = node.getCorps();
if (tableGlobale.getFct(idFct) != null) {
throw new ErrorException(Error.TS, "La fonction " + idFct + " est déjà définie.");
}
Ts table = new Ts();
tableLocaleCourante = table;
int nbArgs = 0;
if (parameters != null) {
context = Context.PARAM;
parameters.accept(this);
nbArgs = parameters.length();
}
if (variable != null) {
context = Context.LOCAL;
variable.accept(this);
}
node.tsItem = tableGlobale.addFct(idFct, node.getTypeRetour(), nbArgs, table, node);
//besoin de rajouter un equals pour le SaInstBloc
SaInstBloc tempCorps = (SaInstBloc) corps;
if (corps != null && tempCorps.getVal() != null) {
context = Context.LOCAL;
corps.accept(this);
}
context = Context.GLOBAL;
return null;
}
@Override
public Void visit(SaDecVar node) throws Exception {
String idVar = node.getNom();
Type typeVar = node.getType();
if (context == Context.LOCAL) {
if (tableLocaleCourante.getVar(idVar) != null) {
throw new ErrorException(Error.TS, "La variable " + idVar + " est déjà définie dans la fonction.");
}
node.setTsItem(tableLocaleCourante.addVar(idVar, typeVar));
} else if (context == Context.PARAM) {
if (tableLocaleCourante.getVar(idVar) != null) {
throw new ErrorException(Error.TS, "La variable " + idVar + " est déjà définie dans les parametres.");
}
node.setTsItem(tableLocaleCourante.addParam(idVar, typeVar));
} else if (context == Context.GLOBAL) {
if (tableGlobale.getVar(idVar) != null) {
throw new ErrorException(Error.TS, "La variable " + idVar + " est déjà définie dans la table globale.");
}
node.setTsItem(tableGlobale.addVar(idVar, typeVar));
}
return null;
}
@Override
public Void visit(SaVarSimple node) throws Exception {
String idVar = node.getNom();
TsItemVarSimple varSimple = node.getTsItem();
if (context == Context.LOCAL) {
if (tableLocaleCourante.getVar(idVar) == null && tableGlobale.getVar(idVar) == null) {
throw new ErrorException(Error.TS, "La variable " + idVar + " n'est pas définie.");
}
if (tableLocaleCourante.getVar(idVar) != null) {
node.tsItem = (TsItemVarSimple) tableLocaleCourante.getVar(idVar);
} else {
node.tsItem = (TsItemVarSimple) tableGlobale.getVar(idVar);
}
} else if (context == Context.PARAM) {
if (tableLocaleCourante.getVar(idVar) == null) {
throw new ErrorException(Error.TS, "La variable " + idVar + " n'est pas définie.");
}
} else if (context == Context.GLOBAL) {
if (tableGlobale.getVar(idVar) == null || varSimple.getTaille() > 1) {
throw new ErrorException(Error.TS, "La variable " + idVar + " n'est pas définie.");
}
node.tsItem = (TsItemVarSimple) tableGlobale.getVar(idVar);
}
return null;
}
@Override
public Void visit(SaAppel node) throws Exception {
String idFct = node.getNom();
SaLExp arguments = node.getArguments();
int nbArgs = 0;
if (arguments != null) {
arguments.accept(this);
nbArgs = arguments.length();
}
if (tableGlobale.getFct(idFct) == null) {
throw new ErrorException(Error.TS, "La fonction " + idFct + " n'est pas définie.");
}
if (this.tableGlobale.getFct(idFct).getNbArgs() != nbArgs) {
throw new ErrorException(Error.TS, "Le nombre d'arguments est incorrect."
+ this.tableGlobale.getFct(idFct).getNbArgs() + " requis.");
}
node.tsItem = tableGlobale.getFct(node.getNom());
return null;
}
@Override
public Void visit(SaVarIndicee node) throws Exception {
String idVar = node.getNom();
SaExp indice = node.getIndice();
if (tableGlobale.getVar(idVar) == null && tableGlobale.getVar(idVar).getTaille() == 1)
throw new ErrorException(Error.TS, "indice " + idVar + " incorrect.");
node.tsItem = tableGlobale.getVar(node.getNom());
indice.accept(this);
return null;
}
public void defaultIn(SaNode node)
{
//System.out.println("<" + node.getClass().getSimpleName() + ">");
}
public void defaultOut(SaNode node)
{
//System.out.println("</" + node.getClass().getSimpleName() + ">");
}
}