From 9905831b24aeac270a25678ec93b17636357a582 Mon Sep 17 00:00:00 2001 From: trochas Date: Thu, 10 Apr 2025 11:13:09 +0200 Subject: [PATCH] todo declaration c pt --- src/main/java/TP2/asd/Interface.java | 20 +++++-- .../java/TP2/asd/PrettyprinterVisitor.java | 23 ++++---- src/main/java/TP2/asd/Program.java | 18 ++++--- src/main/java/TP2/asd/toLLVM_Visitor.java | 54 +++++++++++-------- tests/aLaMain.vsl | 2 +- 5 files changed, 72 insertions(+), 45 deletions(-) diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index a008a23..35af4d0 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -4,7 +4,7 @@ import TP2.asd.Program.*; import TP2.llvm.ProgramLLVM.*; public interface Interface{ - //////////Program + //PROGRAM public interface ProgramI { public S accept(ProgramVisitor v, H h); public String prettyprinter(); @@ -16,7 +16,7 @@ public interface Interface{ } - //////////Function + //FUNCTION public interface Function { public S accept(FunctionVisitor v, H h); } @@ -25,16 +25,26 @@ public interface Interface{ public S visitFunction(FunctionImp fun, H h); } + //DECLARATION + + public interface Declaration{ + public S accept(DeclVisitor v, H h); + } + + public interface DeclVisitor{ + public S visitDeclaration(DeclarationImp instr,H h); + } + + //INSTRUCTION - //////////Instruction public interface Instruction { public S accept(InstrVisitor v, H h); } + public interface InstrVisitor{ public S visitReturn(Return_instrImp 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 visitRead(ReadImp 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 //but each implement will be different for prettyprinter public interface Expression { diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java index 99e93af..d227de3 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -5,6 +5,7 @@ import TP2.asd.Program.*; public class PrettyprinterVisitor implements ProgramVisitor, FunctionVisitor, + DeclVisitor, InstrVisitor, ExprVisitor, TypeVisitor @@ -36,6 +37,18 @@ public class PrettyprinterVisitor implements ProgramVisitor, return str; } + //DELCARATION + + @Override + public String visitDeclaration(DeclarationImp instr, String indent) { + String str = indent +instr.t().accept(this,"") + " "; + for(int i = 0; i, 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 s) implements Declaration{ + @Override + public S accept(DeclVisitor v, H h) { + return v.visitDeclaration(this, h); + } + } + //Instructions public static record Return_instrImp(Expression e) implements Instruction{ public S accept(InstrVisitor v, H h) { return v.visitReturn(this,h); } } - + public static record AssignImp(String t, Expression e) implements Instruction{ public S accept(InstrVisitor v, H h) { return v.visitAssign(this, h); } } - - public static record DeclarationImp(Type t, ArrayList s) implements Instruction{ - @Override - public S accept(InstrVisitor v, H h) { - return v.visitDeclaration(this, h); - } - } public static record PrintImp(ArrayList t) implements Instruction{ @Override diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 29f5bd7..08db214 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -10,6 +10,7 @@ import TP2.llvm.ProgramLLVM.*; public class toLLVM_Visitor implements ProgramVisitor, FunctionVisitor, + DeclVisitor, InstrVisitor>, ExprVisitor, TypeVisitor @@ -22,15 +23,23 @@ public class toLLVM_Visitor implements ProgramVisitor une simplement un val (var ou const) ou un binop */ public static class InstrAndVal{ - public ArrayList instr = null; - public ValLLVM val = null; + public ArrayList instrs; + public ValLLVM val; public InstrAndVal(ArrayList instr, ValLLVM val){ - this.instr = instr; + this.instrs = instr; this.val = val; } } + public static class InstrAndSymTable{ + public ArrayList instrs; + public SymTable symTable; + public InstrAndSymTable(ArrayList instrs, SymTable symTable){ + this.instrs = instrs; + this.symTable = symTable; + } + } //PROGRAM @Override @@ -53,6 +62,22 @@ public class toLLVM_Visitor implements ProgramVisitor return new DefineLLVMImpl(fun.nom(), fun.type().accept(this, h), instrLLVM); } + //DECLARATION + + @Override + public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) { + ArrayList list = new ArrayList<>(); + for(int i = 0; i InstructionLLVM r = new ReturnLLVMImpl(var.getType(),var); ArrayList result = new ArrayList<>(); - result.addAll(res.instr); + result.addAll(res.instrs); result.add(r); return result; @@ -73,26 +98,13 @@ public class toLLVM_Visitor implements ProgramVisitor InstrAndVal res = instr.e().accept(this,h); ValLLVM var = res.val; ArrayList 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 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); return result; } - @Override - public ArrayList visitDeclaration(DeclarationImp instr, SymTable h) { - ArrayList list = new ArrayList<>(); - for(int i = 0; i visitPrint(PrintImp instr, SymTable h) { @@ -151,8 +163,8 @@ public class toLLVM_Visitor implements ProgramVisitor ValLLVM val1 = res1.val; ValLLVM val2 = res2.val; - list.addAll(res1.instr); - list.addAll(res2.instr); + list.addAll(res1.instrs); + list.addAll(res2.instrs); TypeLLVM type = val1.getType(); diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index 2d883f3..08ecbb3 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -3,5 +3,5 @@ FUNC INT main() { b:=3 c:=1 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 } \ No newline at end of file