Skip to content
Snippets Groups Projects
Commit 086b4789 authored by BAUQUIN Niels's avatar BAUQUIN Niels
Browse files

geg

parent bf675260
No related branches found
No related tags found
No related merge requests found
...@@ -32,9 +32,10 @@ public class Compiler ...@@ -32,9 +32,10 @@ public class Compiler
System.out.println("[BUILD SC] "); System.out.println("[BUILD SC] ");
buildSc(); buildSc();
System.out.println("[BUILD SA] "); System.out.println("[BUILD SA] ");
buildSa();/* buildSa();
System.out.println("[BUILD TS] "); System.out.println("[BUILD TS] ");
buildTs(); buildTs();
/*
System.out.println("[TYPE CHECKING]"); System.out.println("[TYPE CHECKING]");
typeCheck(); typeCheck();
System.out.println("[BUILD C3A] "); System.out.println("[BUILD C3A] ");
......
package ts; package ts;
import sa.*; import sa.*;
import util.Error; import util.Error;
import util.Type;
public class Sa2ts extends SaDepthFirstVisitor <Void> { public class Sa2ts extends SaDepthFirstVisitor <Void> {
enum Context { enum Context {
...@@ -22,14 +23,142 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> { ...@@ -22,14 +23,142 @@ public class Sa2ts extends SaDepthFirstVisitor <Void> {
context = Context.GLOBAL; 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) public void defaultIn(SaNode node)
{ {
// System.out.println("<" + node.getClass().getSimpleName() + ">"); System.out.println("<" + node.getClass().getSimpleName() + ">");
} }
public void defaultOut(SaNode node) public void defaultOut(SaNode node)
{ {
// System.out.println("</" + node.getClass().getSimpleName() + ">"); System.out.println("</" + node.getClass().getSimpleName() + ">");
} }
......
...@@ -23,11 +23,7 @@ public class Ts ...@@ -23,11 +23,7 @@ public class Ts
public TsItemVar addVar(String identif, Type type) 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); TsItemVar item = new TsItemVarSimple(identif, type);
item.portee = this; item.portee = this;
item.adresse = this.adrVarCourante; item.adresse = this.adrVarCourante;
...@@ -39,11 +35,7 @@ public class Ts ...@@ -39,11 +35,7 @@ public class Ts
public TsItemVar addParam(String identif, Type type) 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); TsItemVar item = new TsItemVarSimple(identif, type);
item.portee = this; item.portee = this;
item.adresse = this.adrArgCourante; item.adresse = this.adrArgCourante;
...@@ -55,7 +47,7 @@ public class Ts ...@@ -55,7 +47,7 @@ public class Ts
public TsItemVar addTab(String identif, Type type, int taille) public TsItemVar addTab(String identif, Type type, int taille)
{ {
//Verifier qu'on est en global
TsItemVar item = new TsItemVarTab(identif, type, taille); TsItemVar item = new TsItemVarTab(identif, type, taille);
item.portee = this; item.portee = this;
item.adresse = this.adrVarCourante; item.adresse = this.adrVarCourante;
......
...@@ -9,6 +9,7 @@ public abstract class TsItemVar { ...@@ -9,6 +9,7 @@ public abstract class TsItemVar {
public boolean isParam; public boolean isParam;
public Type type; public Type type;
public int getAdresse(){return this.adresse;} public int getAdresse(){return this.adresse;}
public String getIdentif(){return this.identif;} public String getIdentif(){return this.identif;}
public Ts getPortee() {return this.portee;} public Ts getPortee() {return this.portee;}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment