todo declaration c pt
This commit is contained in:
@@ -4,7 +4,7 @@ import TP2.asd.Program.*;
|
|||||||
import TP2.llvm.ProgramLLVM.*;
|
import TP2.llvm.ProgramLLVM.*;
|
||||||
|
|
||||||
public interface Interface{
|
public interface Interface{
|
||||||
//////////Program
|
//PROGRAM
|
||||||
public interface ProgramI {
|
public interface ProgramI {
|
||||||
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
|
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
|
||||||
public String prettyprinter();
|
public String prettyprinter();
|
||||||
@@ -16,7 +16,7 @@ public interface Interface{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////Function
|
//FUNCTION
|
||||||
public interface Function {
|
public interface Function {
|
||||||
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
|
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
|
||||||
}
|
}
|
||||||
@@ -25,16 +25,26 @@ public interface Interface{
|
|||||||
public S visitFunction(FunctionImp fun, H h);
|
public S visitFunction(FunctionImp fun, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DECLARATION
|
||||||
|
|
||||||
|
public interface Declaration{
|
||||||
|
public <H,S> S accept(DeclVisitor<H,S> v, H h);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface DeclVisitor<H,S>{
|
||||||
|
public S visitDeclaration(DeclarationImp instr,H h);
|
||||||
|
}
|
||||||
|
|
||||||
|
//INSTRUCTION
|
||||||
|
|
||||||
//////////Instruction
|
|
||||||
public interface Instruction {
|
public interface Instruction {
|
||||||
public <H,S> S accept(InstrVisitor<H,S> v, H h);
|
public <H,S> S accept(InstrVisitor<H,S> v, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public interface InstrVisitor<H,S>{
|
public interface InstrVisitor<H,S>{
|
||||||
public S visitReturn(Return_instrImp instr, H h);
|
public S visitReturn(Return_instrImp instr, H h);
|
||||||
public S visitAssign(AssignImp instr, H h);
|
public S visitAssign(AssignImp instr, H h);
|
||||||
public S visitDeclaration(DeclarationImp instr,H h);
|
|
||||||
public S visitPrint(PrintImp instr, H h);
|
public S visitPrint(PrintImp instr, H h);
|
||||||
public S visitRead(ReadImp instr,H h);
|
public S visitRead(ReadImp instr,H h);
|
||||||
public S visitIfThen(IfThenImp instr, H h);
|
public S visitIfThen(IfThenImp instr, H h);
|
||||||
@@ -43,7 +53,7 @@ public interface Interface{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////Expression
|
//EXPRESSION
|
||||||
//We put prettyprinter here beause each expr will have to implement it like accept visitor
|
//We put prettyprinter here beause each expr will have to implement it like accept visitor
|
||||||
//but each implement will be different for prettyprinter
|
//but each implement will be different for prettyprinter
|
||||||
public interface Expression {
|
public interface Expression {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import TP2.asd.Program.*;
|
|||||||
|
|
||||||
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||||
FunctionVisitor<String,String>,
|
FunctionVisitor<String,String>,
|
||||||
|
DeclVisitor<String,String>,
|
||||||
InstrVisitor<String,String>,
|
InstrVisitor<String,String>,
|
||||||
ExprVisitor<String,String>,
|
ExprVisitor<String,String>,
|
||||||
TypeVisitor<String,String>
|
TypeVisitor<String,String>
|
||||||
@@ -36,6 +37,18 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DELCARATION
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitDeclaration(DeclarationImp instr, String indent) {
|
||||||
|
String str = indent +instr.t().accept(this,"") + " ";
|
||||||
|
for(int i = 0; i<instr.s().size();i++){
|
||||||
|
str += instr.s().get(i);
|
||||||
|
if(i<instr.s().size()-1) str += ",";
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
//INSTRUCTION
|
//INSTRUCTION
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -48,16 +61,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
return indent + instr.t()+ " := " + instr.e().accept(this,"");
|
return indent + instr.t()+ " := " + instr.e().accept(this,"");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String visitDeclaration(DeclarationImp instr, String indent) {
|
|
||||||
String str = indent +instr.t().accept(this,"") + " ";
|
|
||||||
for(int i = 0; i<instr.s().size();i++){
|
|
||||||
str += instr.s().get(i);
|
|
||||||
if(i<instr.s().size()-1) str += ",";
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visitPrint(PrintImp instr, String indent) {
|
public String visitPrint(PrintImp instr, String indent) {
|
||||||
String str = indent + "PRINT ";
|
String str = indent + "PRINT ";
|
||||||
|
|||||||
@@ -54,6 +54,15 @@ public class Program{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Declaration
|
||||||
|
|
||||||
|
public static record DeclarationImp(Type t, ArrayList<String> s) implements Declaration{
|
||||||
|
@Override
|
||||||
|
public <H, S> S accept(DeclVisitor<H, S> v, H h) {
|
||||||
|
return v.visitDeclaration(this, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Instructions
|
//Instructions
|
||||||
public static record Return_instrImp(Expression e) implements Instruction{
|
public static record Return_instrImp(Expression e) implements Instruction{
|
||||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||||
@@ -67,13 +76,6 @@ public class Program{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static record DeclarationImp(Type t, ArrayList<String> s) implements Instruction{
|
|
||||||
@Override
|
|
||||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
|
||||||
return v.visitDeclaration(this, h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static record PrintImp(ArrayList<Object> t) implements Instruction{
|
public static record PrintImp(ArrayList<Object> t) implements Instruction{
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import TP2.llvm.ProgramLLVM.*;
|
|||||||
|
|
||||||
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>,
|
||||||
FunctionVisitor<SymTable,DefineLLVM>,
|
FunctionVisitor<SymTable,DefineLLVM>,
|
||||||
|
DeclVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndSymTable>,
|
||||||
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
InstrVisitor<SymTable,ArrayList<InstructionLLVM>>,
|
||||||
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
|
ExprVisitor<SymTable,TP2.asd.toLLVM_Visitor.InstrAndVal>,
|
||||||
TypeVisitor<SymTable,TypeLLVM>
|
TypeVisitor<SymTable,TypeLLVM>
|
||||||
@@ -22,15 +23,23 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
une simplement un val (var ou const) ou un binop
|
une simplement un val (var ou const) ou un binop
|
||||||
*/
|
*/
|
||||||
public static class InstrAndVal{
|
public static class InstrAndVal{
|
||||||
public ArrayList<AssignLVMImpl> instr = null;
|
public ArrayList<AssignLVMImpl> instrs;
|
||||||
public ValLLVM val = null;
|
public ValLLVM val;
|
||||||
|
|
||||||
public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
|
public InstrAndVal(ArrayList<AssignLVMImpl> instr, ValLLVM val){
|
||||||
this.instr = instr;
|
this.instrs = instr;
|
||||||
this.val = val;
|
this.val = val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class InstrAndSymTable{
|
||||||
|
public ArrayList<InstructionLLVM> instrs;
|
||||||
|
public SymTable symTable;
|
||||||
|
public InstrAndSymTable(ArrayList<InstructionLLVM> instrs, SymTable symTable){
|
||||||
|
this.instrs = instrs;
|
||||||
|
this.symTable = symTable;
|
||||||
|
}
|
||||||
|
}
|
||||||
//PROGRAM
|
//PROGRAM
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,6 +62,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM);
|
return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DECLARATION
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
|
||||||
|
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
||||||
|
for(int i = 0; i<instr.s().size();i++){
|
||||||
|
TypeLLVM t2 = instr.t().accept(this,h);
|
||||||
|
//String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i));
|
||||||
|
Result r = h.addVar(instr.s().get(i));
|
||||||
|
String name = r.var;
|
||||||
|
h = r.symTable;
|
||||||
|
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
|
||||||
|
}
|
||||||
|
return new InstrAndSymTable(list,h);
|
||||||
|
}
|
||||||
|
|
||||||
//INSTRUCTION
|
//INSTRUCTION
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,7 +87,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
|
|
||||||
InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var);
|
InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var);
|
||||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||||
result.addAll(res.instr);
|
result.addAll(res.instrs);
|
||||||
result.add(r);
|
result.add(r);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -73,26 +98,13 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
InstrAndVal res = instr.e().accept(this,h);
|
InstrAndVal res = instr.e().accept(this,h);
|
||||||
ValLLVM var = res.val;
|
ValLLVM var = res.val;
|
||||||
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
ArrayList<InstructionLLVM> result = new ArrayList<>();
|
||||||
result.addAll(res.instr);
|
result.addAll(res.instrs);
|
||||||
//InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
//InstructionLLVM r = new AssignLVMImp(new VarLLVMImpl(var.getType(),instr.t()),var);
|
||||||
InstructionLLVM r = new StoreLLVMImpl(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),instr.t()/*"h.getVar(instr.t())*/));
|
InstructionLLVM r = new StoreLLVMImpl(var.getType(),var,var.getType(),new VarLLVMImpl(var.getType(),h.getVar(instr.t())));
|
||||||
result.add(r);
|
result.add(r);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ArrayList<InstructionLLVM> visitDeclaration(DeclarationImp instr, SymTable h) {
|
|
||||||
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
|
||||||
for(int i = 0; i<instr.s().size();i++){
|
|
||||||
TypeLLVM t2 = instr.t().accept(this,h);
|
|
||||||
//String name = instr.s().get(i);//h.addVarLLVM(instr.s().get(i));
|
|
||||||
Result r = h.addVar(instr.s().get(i));
|
|
||||||
String name = r.var;
|
|
||||||
h = r.symTable;
|
|
||||||
list.add(new AssignLVMImpl(new VarLLVMImpl(t2, name),new allocaLLVMImpl(t2)));
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
|
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
|
||||||
@@ -151,8 +163,8 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
ValLLVM val1 = res1.val;
|
ValLLVM val1 = res1.val;
|
||||||
ValLLVM val2 = res2.val;
|
ValLLVM val2 = res2.val;
|
||||||
|
|
||||||
list.addAll(res1.instr);
|
list.addAll(res1.instrs);
|
||||||
list.addAll(res2.instr);
|
list.addAll(res2.instrs);
|
||||||
|
|
||||||
|
|
||||||
TypeLLVM type = val1.getType();
|
TypeLLVM type = val1.getType();
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ FUNC INT main() {
|
|||||||
b:=3
|
b:=3
|
||||||
c:=1
|
c:=1
|
||||||
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
|
||||||
READ a, b, n, t, m
|
READ a, b, c, d
|
||||||
RETURN 4 + 6 * 5 + 2 }
|
RETURN 4 + 6 * 5 + 2 }
|
||||||
Reference in New Issue
Block a user