blocks en cours

This commit is contained in:
trochas
2025-04-10 12:04:42 +02:00
8 changed files with 144 additions and 42 deletions

View File

@@ -39,11 +39,11 @@ functions returns [ArrayList<Function> out]
function returns [Function out]: function returns [Function out]:
FUNCTION t=type i=ident ParO ParF FUNCTION t=type i=ident ParO ParF
( BacO
BacO instrs=list_instr BacF (instrs=list_instr
{ {
$out=new FunctionImp($t.return_type, $i.out, $instrs.out); $out=new FunctionImp($t.return_type, $i.out, $instrs.out);
} } BacF
| instr= instruction | instr= instruction
{ {
$out=new FunctionImp($t.return_type, $i.out, $instr.out); $out=new FunctionImp($t.return_type, $i.out, $instr.out);
@@ -51,6 +51,18 @@ function returns [Function out]:
) )
; ;
declaration returns [Declaration out]:
(t=type i=ident
{
ArrayList<String> declare= new ArrayList<String>();
declare.add($i.out);
}(VIRGULE i2=ident
{
declare.add($i2.out);
})*
{$out = new DeclarationImp($t.return_type, declare);})+
;
list_instr returns [ArrayList<Instruction> out] list_instr returns [ArrayList<Instruction> out]
@init{ @init{
$out = new ArrayList<>(); $out = new ArrayList<>();
@@ -65,21 +77,21 @@ instruction returns [Instruction out]:
RETURN e=expression RETURN e=expression
{ {
$out =new Return_instrImp($e.out);} $out =new Return_instrImp($e.out);}
| //BLOC
BacO
(dec=declaration instr4=list_instr
{
$out=new BlocDec($dec.out,$insts4.out);
}
| instr3=list_instr
{ $out= new Bloc($instr3.out);
}
) BacF
| //ASSIGN | //ASSIGN
i=ident ASSIGN e=expression i=ident ASSIGN e=expression
{ {
$out = new AssignImp($i.out, $e.out); $out = new AssignImp($i.out, $e.out);
} }
| //DECLARATION
t=type i=ident
{
ArrayList<String> declare= new ArrayList<String>();
declare.add($i.out);
}(VIRGULE i2=ident
{
declare.add($i2.out);
})*
{$out = new DeclarationImp($t.return_type, declare);}
| // PRINT | // PRINT
PRINT PRINT
{ArrayList<Object> printer= new ArrayList<Object>();} {ArrayList<Object> printer= new ArrayList<Object>();}
@@ -114,13 +126,13 @@ instruction returns [Instruction out]:
})* })*
{$out = new ReadImp(read);} {$out = new ReadImp(read);}
| //IF THEN ELSE FIN | //IF THEN ELSE FIN
IF ex1=expression THEN ins1=instruction IF ex1=expression THEN ins1=list_instr
(FIN (FIN
{$out= new IfThenImp($ex1.out, $ins1.out);} {$out= new IfThenImp($ex1.out, $ins1.out);}
| ELSE ins2=instruction FIN | ELSE ins2=list_instr FIN
{$out= new IfThenElseImp($ex1.out, $ins1.out,$ins2.out); } {$out= new IfThenElseImp($ex1.out, $ins1.out,$ins2.out); }
) )
| WHILE exp1=expression DO ins3=instruction DONE | WHILE exp1=expression DO ins3=list_instr DONE
{$out = new WhileImp($exp1.out,$ins3.out);} {$out = new WhileImp($exp1.out,$ins3.out);}
; ;

View File

@@ -44,6 +44,8 @@ public interface Interface{
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 visitBloc(BlocImp instr, H h);
public S visitBlocDec (BlocDecImp instr, H h);
public S visitAssign(AssignImp instr, H h); public S visitAssign(AssignImp 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);
@@ -52,10 +54,8 @@ public interface Interface{
public S visitWhile(WhileImp instr, H h); public S visitWhile(WhileImp instr, H h);
} }
//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 { public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h); public <H,S> S accept(ExprVisitor<H,S> v, H h);
} }

View File

@@ -29,12 +29,9 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override @Override
public String visitFunction(FunctionImp fun, String indent) { public String visitFunction(FunctionImp fun, String indent) {
String str = indent+"FUNC " + fun.type().accept(this,"")+ " " + fun.nom() +"(){\n"; String str = indent+"FUNC " + fun.type().accept(this,"")+ " " + fun.nom() +"() ";
for(int i = 0; i<fun.instructions().size(); i++){ str += fun.instruction().accept(this,indent)+"\n";
str += fun.instructions().get(i).accept(this,indent+INDENT)+"\n"; return str;
}
str+= indent+"}";
return str;
} }
//DELCARATION //DELCARATION
@@ -56,6 +53,30 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
return indent+"RETURN " + instr.e().accept(this,""); return indent+"RETURN " + instr.e().accept(this,"");
} }
@Override
public String visitBloc(BlocImp instr, String indent) {
String str = "{";
for(int i = 0; i<instr.instrs().size(); i++){
str+=instr.instrs().get(i).accept(this, indent+INDENT)+"\n";
}
str += indent+"}";
return str;
}
@Override
public String visitBlocDec(BlocDecImp instr, String indent) {
String str = "{";
for(int i = 0; i<instr.decls().size(); i++){
str+=instr.decls().get(i).accept(this, indent+INDENT)+"\n";
}
for(int i = 0; i<instr.instrs().size(); i++){
str+=instr.instrs().get(i).accept(this, indent+INDENT)+"\n";
}
str += indent+"}";
return str;
}
@Override @Override
public String visitAssign(AssignImp instr, String indent) { public String visitAssign(AssignImp instr, String indent) {
return indent + instr.t()+ " := " + instr.e().accept(this,""); return indent + instr.t()+ " := " + instr.e().accept(this,"");
@@ -81,7 +102,10 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
public String visitIfThen(IfThenImp instr, String indent) { public String visitIfThen(IfThenImp instr, String indent) {
String str = indent + "IF "; String str = indent + "IF ";
str +=(instr.e().accept(this, "")); str +=(instr.e().accept(this, ""));
str +=" THEN " + (instr.i1().accept(this, "")); str +=" THEN ";
for(int i=0; i<instr.i1().size();i++){
str+= instr.i1().get(i).accept(this, "");
}
return str; return str;
} }
@@ -89,8 +113,14 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
public String visitIfThenElse(IfThenElseImp instr, String indent) { public String visitIfThenElse(IfThenElseImp instr, String indent) {
String str = indent + "IF "; String str = indent + "IF ";
str +=(instr.e().accept(this, "")); str +=(instr.e().accept(this, ""));
str +=" THEN " + (instr.i1().accept(this, "")); str +=" THEN ";
str +=" ELSE "+ (instr.i2().accept(this, "")); for(int i=0; i<instr.i1().size();i++){
str+= instr.i1().get(i).accept(this, "");
}
str +=" ELSE ";
for(int i=0; i<instr.i2().size();i++){
str+= instr.i2().get(i).accept(this, "");
}
return str; return str;
} }
@@ -108,7 +138,10 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
public String visitWhile(WhileImp instr, String indent) { public String visitWhile(WhileImp instr, String indent) {
String str = indent+"WHILE "; String str = indent+"WHILE ";
str += (instr.e().accept(this, "")); str += (instr.e().accept(this, ""));
str+= " DO "+ (instr.i1().accept(this, "")); str+= " DO ";
for(int i=0; i<instr.i1().size();i++){
str+= instr.i1().get(i).accept(this, "");
}
str+= " DONE"; str+= " DONE";
return str; return str;
} }

View File

@@ -25,10 +25,10 @@ public class Program{
} }
//Fonction //Fonction
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function { public static record FunctionImp(Type type, String nom, Instruction instruction)implements Function {
public FunctionImp(Type type, String name, Instruction instruction) { //public FunctionImp(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }}); // this(type, name, new ArrayList<>() {{ add(instruction); }}); C KOI ?
} //}
public <H, S> S accept(FunctionVisitor<H, S> v, H h) { public <H, S> S accept(FunctionVisitor<H, S> v, H h) {
return v.visitFunction(this, h); return v.visitFunction(this, h);
@@ -69,7 +69,19 @@ public class Program{
return v.visitReturn(this,h); return v.visitReturn(this,h);
} }
} }
public static record BlocDecImp(ArrayList<Declaration> decls, ArrayList<Instruction> instrs) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitBlocDec(this, h);
}
}
public static record BlocImp(ArrayList<Instruction> instrs) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitBloc(this, h);
}
}
public static record AssignImp(String t, Expression e) implements Instruction{ public static record AssignImp(String t, 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) {
return v.visitAssign(this, h); return v.visitAssign(this, h);
@@ -90,21 +102,21 @@ public class Program{
} }
} }
public static record IfThenImp(Expression e, Instruction i1) implements Instruction { public static record IfThenImp(Expression e, ArrayList<Instruction> i1) 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) {
return v.visitIfThen(this, h); return v.visitIfThen(this, h);
} }
} }
public static record IfThenElseImp(Expression e, Instruction i1, Instruction i2) implements Instruction { public static record IfThenElseImp(Expression e, ArrayList<Instruction> i1, ArrayList<Instruction> i2) 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) {
return v.visitIfThenElse(this, h); return v.visitIfThenElse(this, h);
} }
} }
public static record WhileImp(Expression e, Instruction i1) implements Instruction { public static record WhileImp(Expression e, ArrayList<Instruction> i1) 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) {
return v.visitWhile(this, h); return v.visitWhile(this, h);

View File

@@ -93,6 +93,18 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return result; return result;
} }
@Override
public ArrayList<InstructionLLVM> visitBloc(BlocImp instr, SymTable h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitBloc'");
}
@Override
public ArrayList<InstructionLLVM> visitBlocDec(BlocDecImp instr, SymTable h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitBlocDec'");
}
@Override @Override
public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) { public ArrayList<InstructionLLVM> visitAssign(AssignImp instr, SymTable h) {
InstrAndVal res = instr.e().accept(this,h); InstrAndVal res = instr.e().accept(this,h);
@@ -132,11 +144,10 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
throw new UnsupportedOperationException("Unimplemented method 'visitIfThenElse'"); throw new UnsupportedOperationException("Unimplemented method 'visitIfThenElse'");
} }
@Override @Override
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) { public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
// TODO Auto-generated method stub ArrayList<InstructionLLVM> l = new ArrayList<>();
throw new UnsupportedOperationException("Unimplemented method 'visitWhile'"); return l;
} }
//EXPRESSION //EXPRESSION
@@ -149,7 +160,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
@Override @Override
public InstrAndVal visitVar(VarImp e, SymTable h) { public InstrAndVal visitVar(VarImp e, SymTable h) {
ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), h.getVar(e.name())); ValLLVM val = new VarLLVMImpl(new IntLLVMImpl(), e.name());
return new InstrAndVal(new ArrayList<>(), val); return new InstrAndVal(new ArrayList<>(), val);
} }

View File

@@ -1,4 +1,5 @@
package TP2.llvm; package TP2.llvm;
import TP2.asd.Program.IfThenElseImp;
import TP2.llvm.ProgramLLVM.*; import TP2.llvm.ProgramLLVM.*;
public interface Interface { public interface Interface {
@@ -31,6 +32,9 @@ public interface Interface {
public S visitStoreLLVM(StoreLLVMImpl instr, H h); public S visitStoreLLVM(StoreLLVMImpl instr, H h);
public S visitPrintLLVM(PrintLLVMImpl instr, H h); public S visitPrintLLVM(PrintLLVMImpl instr, H h);
public S visitReadLLVM(ReadLLVMImpl instr, H h); public S visitReadLLVM(ReadLLVMImpl instr, H h);
public S visitIfThenElseLLVM(IfThenElseImp instr, H h);
public S visitIfThenLLVM(IfThenImp instr, H h);
public S visitWhileLLVM(WhileImp instr, H h);
} }
//////////ExpressionLLVM (expression) //////////ExpressionLLVM (expression)

View File

@@ -74,9 +74,35 @@ public class ProgramLLVM {
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) { public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
return v.visitReadLLVM(this, h); return v.visitReadLLVM(this, h);
} }
} }
public static record IfThenLLVMImp() implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'accept'");
}
}
public static record IfThenElseLLVMImp() implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'accept'");
}
}
public static record WhileLLVMImp() implements InstructionLLVM{
@Override
public <H, S> S accept(InstructionLLVMVisitor<H, S> v, H h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'accept'");
}
}
//Expression : //Expression :
public static record BinOpLLVMImpl(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{ public static record BinOpLLVMImpl(TypeLLVM type,Op op, ValLLVM val1,ValLLVM val2) implements ExpressionLLVM{
@Override @Override

View File

@@ -3,5 +3,9 @@ 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
IF 1 THEN a:=a+1 FI
IF 2 THEN b:=c+1 ELSE
b:=c+1
READ a, b, c, d READ a, b, c, d
FI
RETURN 4 + 6 * 5 + 2 } RETURN 4 + 6 * 5 + 2 }