error LL1 in function and proto in Parser. too weird.
This commit is contained in:
@@ -52,17 +52,31 @@ functions returns [ArrayList<Function> out]
|
|||||||
;
|
;
|
||||||
|
|
||||||
proto returns [Function out]:
|
proto returns [Function out]:
|
||||||
PROTOTYPE t=type i=ident ParO v=liste_param ParF
|
PROTOTYPE t=type i=ident ParO
|
||||||
|
(
|
||||||
|
v=liste_param ParF
|
||||||
{
|
{
|
||||||
$out=new PrototypeImp($t.return_type,$i.out, $v.out);
|
$out=new PrototypeImp($t.return_type,$i.out, $v.out);
|
||||||
}
|
}
|
||||||
|
| ParF
|
||||||
|
{
|
||||||
|
$out=new PrototypeImp($t.return_type,$i.out, new ArrayList<>());
|
||||||
|
}
|
||||||
|
)
|
||||||
;
|
;
|
||||||
|
|
||||||
function returns [Function out]:
|
function returns [Function out]:
|
||||||
FUNCTION t=type i=ident ParO v=liste_param ParF instr= instruction
|
FUNCTION t=type i=ident ParO
|
||||||
|
(
|
||||||
|
v=liste_param ParF instr= instruction
|
||||||
{
|
{
|
||||||
$out=new FunctionImp($t.return_type, $i.out, $v.out, $instr.out);
|
$out=new FunctionImp($t.return_type, $i.out, $v.out, $instr.out);
|
||||||
}
|
}
|
||||||
|
| ParF instr= instruction
|
||||||
|
{
|
||||||
|
$out=new FunctionImp($t.return_type, $i.out, new ArrayList<>(), $instr.out);
|
||||||
|
}
|
||||||
|
)
|
||||||
;
|
;
|
||||||
|
|
||||||
list_decls returns [ArrayList<Declaration> out]
|
list_decls returns [ArrayList<Declaration> out]
|
||||||
@@ -207,7 +221,7 @@ lit returns [Expression out]:
|
|||||||
}
|
}
|
||||||
| t=ident
|
| t=ident
|
||||||
(
|
(
|
||||||
ParO (para=list_expression) ParF {$out = new AppealImp($t.out,$para.out);}
|
ParO (para=list_expression)? ParF {$out = new AppealImp($t.out,$para.out);}
|
||||||
| {$out=new VarImp($t.out);}
|
| {$out=new VarImp($t.out);}
|
||||||
)
|
)
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -8,18 +8,18 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImp;
|
|||||||
public class SymTable {
|
public class SymTable {
|
||||||
|
|
||||||
private PStack<PMap<String,ValueVarMap>> varMap;
|
private PStack<PMap<String,ValueVarMap>> varMap;
|
||||||
private PMap<String,ValueFunMap> fuctionsMap;
|
private PMap<String,ValueFunMap> functionsMap;
|
||||||
private int id=1;
|
private int id=1;
|
||||||
private int idLabel = 1;
|
private int idLabel = 1;
|
||||||
|
|
||||||
public SymTable(){
|
public SymTable(){
|
||||||
this.varMap= ConsPStack.singleton(HashTreePMap.empty());
|
this.varMap= ConsPStack.singleton(HashTreePMap.empty());
|
||||||
this.fuctionsMap = HashTreePMap.empty();
|
this.functionsMap = HashTreePMap.empty();
|
||||||
}
|
}
|
||||||
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> fuctionsMap, int id, int idLabel){
|
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> functionsMap, int id, int idLabel){
|
||||||
this.varMap= varMap;
|
this.varMap= varMap;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.fuctionsMap = fuctionsMap;
|
this.functionsMap = functionsMap;
|
||||||
this.idLabel = idLabel;
|
this.idLabel = idLabel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,12 +34,12 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SymTable newBlock() {
|
public SymTable newBlock() {
|
||||||
return new SymTable(varMap.plus(HashTreePMap.empty()), fuctionsMap, id, idLabel);
|
return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id, idLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SymTable outBlock() {
|
public SymTable outBlock() {
|
||||||
if (varMap.size() > 1) {
|
if (varMap.size() > 1) {
|
||||||
return new SymTable(varMap.minus(0), fuctionsMap, id, idLabel);
|
return new SymTable(varMap.minus(0), functionsMap, id, idLabel);
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Vide");
|
System.err.println("Vide");
|
||||||
return this;
|
return this;
|
||||||
@@ -73,9 +73,9 @@ public class SymTable {
|
|||||||
|
|
||||||
|
|
||||||
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
|
public SymTable addFunction(DefineLLVMImp function, Boolean isProto){
|
||||||
ValueFunMap value = this.fuctionsMap.get(function.name());
|
ValueFunMap value = this.functionsMap.get(function.name());
|
||||||
if(value == null || (value!=null && value.isProto && !isProto)){
|
if(value == null || (value!=null && value.isProto && !isProto)){
|
||||||
return new SymTable(this.varMap, this.fuctionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel);
|
return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà");
|
if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà");
|
||||||
@@ -86,7 +86,7 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ValueFunMap getFunction(String name){
|
public ValueFunMap getFunction(String name){
|
||||||
return this.fuctionsMap.get(name);
|
return this.functionsMap.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId(){
|
public int getId(){
|
||||||
@@ -129,7 +129,7 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
String realName = nomVar + id;
|
String realName = nomVar + id;
|
||||||
top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id, false));
|
top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id, false));
|
||||||
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), fuctionsMap, id+1, idLabel);
|
SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel);
|
||||||
return new Result(newSym, realName);
|
return new Result(newSym, realName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,12 +150,10 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Boolean isPresentVar(String nomVar){
|
public Boolean isPresentVar(String nomVar){
|
||||||
boolean x= false;
|
for (PMap<String,ValueVarMap> scope : varMap) {
|
||||||
while(!x){
|
if (scope.containsKey(nomVar)) {
|
||||||
for (PMap<String,ValueVarMap> scope : varMap){
|
return true;
|
||||||
x=scope.containsKey(nomVar);
|
|
||||||
}
|
}
|
||||||
if (x) return true;
|
|
||||||
}
|
}
|
||||||
System.err.println(nomVar+" n'est pas trouvé");
|
System.err.println(nomVar+" n'est pas trouvé");
|
||||||
return false;
|
return false;
|
||||||
@@ -189,7 +187,7 @@ public class SymTable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
top = top.plus(nomVar, new ValueVarMap(type, id, isParam));
|
top = top.plus(nomVar, new ValueVarMap(type, id, isParam));
|
||||||
return new SymTable(varMap.minus(0).plus(0, top), fuctionsMap, id+1, idLabel);
|
return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String print_all(){
|
public String print_all(){
|
||||||
@@ -199,7 +197,7 @@ public class SymTable {
|
|||||||
|
|
||||||
int scopeLevel = varMap.size();
|
int scopeLevel = varMap.size();
|
||||||
for (PMap<String, ValueVarMap> scope : varMap) {
|
for (PMap<String, ValueVarMap> scope : varMap) {
|
||||||
str.append("Scope Level ").append(scopeLevel--).append(":\n");
|
str.append("Block Level ").append(scopeLevel--).append(":\n");
|
||||||
for (String varName : scope.keySet()) {
|
for (String varName : scope.keySet()) {
|
||||||
ValueVarMap value = scope.get(varName);
|
ValueVarMap value = scope.get(varName);
|
||||||
str.append("Name: ").append(varName)
|
str.append("Name: ").append(varName)
|
||||||
@@ -211,7 +209,7 @@ public class SymTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
str.append("FUNCTIONS:\n");
|
str.append("FUNCTIONS:\n");
|
||||||
for (String funcName : fuctionsMap.keySet()) {
|
for (String funcName : functionsMap.keySet()) {
|
||||||
str.append(" Name: ").append(funcName).append("\n");
|
str.append(" Name: ").append(funcName).append("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -336,15 +336,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
|
ArrayList<ValLLVM> paramsLLVM = new ArrayList<>();
|
||||||
for(Expression param : instr.params()){
|
for(Expression param : instr.params()){
|
||||||
InstrAndVal result = param.accept(this,h);
|
InstrAndVal result = param.accept(this,h);
|
||||||
l.add((InstructionLLVM) result.instrs);
|
l.addAll( result.instrs);
|
||||||
paramsLLVM.add(result.val);
|
paramsLLVM.add(result.val);
|
||||||
}
|
}
|
||||||
ValueFunMap fLLVM = h.getFunction(instr.fName()); //on récupère la fonction LLVM dans la table des Symboles
|
ValueFunMap fLLVM = h.getFunction(instr.fName()); //on récupère la fonction LLVM dans la table des Symboles
|
||||||
if(fLLVM == null){
|
if(fLLVM == null){
|
||||||
System.err.println("[VSL compile error] : la fonction n'existe pas, veuillez vous assurer de l'avoir déclarée avant l'appel");
|
System.err.println("[VSL compile error] : la fonction n'existe pas, veuillez vous assurer de l'avoir déclarée avant l'appel");
|
||||||
|
return new InstrAndVal(l, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Pour c=func(x,y)
|
||||||
|
Result res = h.addNewTempVar();
|
||||||
|
h = res.symTable;
|
||||||
|
VarLLVMImp var = new VarLLVMImp(fLLVM.define.type(), res.var);
|
||||||
|
|
||||||
l.add(new CallLLVMImp(fLLVM.define,paramsLLVM,""));
|
l.add(new CallLLVMImp(fLLVM.define,paramsLLVM,""));
|
||||||
return new InstrAndVal(l, null);
|
return new InstrAndVal(l, var);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
PROTO INT add(x,y)
|
PROTO INT add()
|
||||||
|
|
||||||
FUNC INT add(x,y) {
|
FUNC INT add() {
|
||||||
INT z
|
INT z
|
||||||
z := x+y
|
z := 1
|
||||||
RETURN z
|
RETURN z
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ FUNC INT main(x,y) {
|
|||||||
x := 5
|
x := 5
|
||||||
minh := x * y
|
minh := x * y
|
||||||
b:=3
|
b:=3
|
||||||
c:=add(x,b)
|
c:=add()
|
||||||
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b
|
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b
|
||||||
WHILE b - 1
|
WHILE b - 1
|
||||||
DO{
|
DO{
|
||||||
|
|||||||
Reference in New Issue
Block a user