Select Git revision
Printable.class
Forked from
COUETOUX Basile / FirefighterStarter
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Sa2ts.java 4.92 KiB
package ts;
import sa.*;
import util.Error;
public class Sa2ts extends SaDepthFirstVisitor <Void> {
enum Context {
LOCAL,
GLOBAL,
PARAM
}
private final Ts tableGlobale;
private Ts tableLocaleCourante;
private Context context;
public Ts getTableGlobale() {
return this.tableGlobale;
}
public Sa2ts() {
tableGlobale = new Ts();
tableLocaleCourante = null;
context = Context.GLOBAL;
}
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 (tableGlobale.getVar(node.getNom()) != null)
throw new ErrorException(Error.TS, "Variable globale");
ts = tableLocaleCourante.addVar(node.getNom(),node.getType());
}
}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;
}
public Void visit(SaDecTab node) {
defaultIn(node);
if (tableGlobale.getVar(node.getNom()) != null)
node.setTsItem(this.tableGlobale.addTab(node.getNom(),node.getType(),node.getTaille()));
else
throw new RuntimeException("Erreur");
defaultOut(node);
return null;
}
public Void visit(SaDecFonc node) throws Exception {
defaultIn(node);
tableLocaleCourante = new Ts();
int nbArgs = 0;
this.context = Context.GLOBAL;
if(tableGlobale.getFct(node.getNom()) == null){
tableLocaleCourante.addFct(node.getNom(),node.getTypeRetour(),nbArgs,tableLocaleCourante, node);
}
else{
nbArgs = node.getParametres().length();
tableLocaleCourante.addFct(node.getNom(),node.getTypeRetour(),nbArgs,tableLocaleCourante, node);
}
this.context = Context.PARAM;
if(node.getParametres() == null){
node.getParametres().accept(this);
}
this.context = Context.LOCAL;
if(node.getVariable() == null){
node.getParametres().accept(this);
}
if(node.getCorps() == null){
node.getCorps().accept(this);
}
defaultOut(node);
return null;
}
public Void visit(SaDecVarSimple node) {
boolean isGlobal = false;
if (tableLocaleCourante.getVar(node.getNom()) != null) {
if (tableGlobale.getVar(node.getNom()) != null) {
throw new RuntimeException("Erreur");
} else {
isGlobal = true;
}
}
TsItemVar tsItemVar;
if(isGlobal){
tsItemVar = tableGlobale.getVar(node.getNom());
}else{
tsItemVar = tableLocaleCourante.getVar(node.getNom());
}
node.tsItem = tsItemVar;
return null;
}
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) 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;
}
public void defaultIn(SaNode node)
{
// System.out.println("<" + node.getClass().getSimpleName() + ">");
}
public void defaultOut(SaNode node)
{
// System.out.println("</" + node.getClass().getSimpleName() + ">");
}
}