fix prototype

This commit is contained in:
Vu Tuan Minh
2025-04-26 22:05:05 +02:00
parent a34802db55
commit 49c4382343
4 changed files with 14 additions and 11 deletions

View File

@@ -28,16 +28,16 @@ program returns [ProgramImp p] :
{$p = new ProgramImp($func.out,$pro.out);} {$p = new ProgramImp($func.out,$pro.out);}
; ;
prototypes returns [ArrayList<Prototype> out] prototypes returns [ArrayList<Function> out]
@init{ @init{
$out = new ArrayList<Prototype>(); $out = new ArrayList<Function>();
}: }:
(proto { (proto {
$out.add($proto.out); $out.add($proto.out);
})* })*
; ;
proto returns [Prototype 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);

View File

@@ -18,6 +18,11 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override @Override
public String visitProgram(ProgramImp prog, String indent) { public String visitProgram(ProgramImp prog, String indent) {
String str =""; String str ="";
for(int i= 0; i<prog.protos().size();i++){
str+=prog.protos().get(i).accept(this, INDENT);
if(i<prog.protos().size()-1) str += "\n";
}
str+="\n";
for(int i = 0; i<prog.fonctions().size(); i++){ for(int i = 0; i<prog.fonctions().size(); i++){
str += prog.fonctions().get(i).accept(this,INDENT); str += prog.fonctions().get(i).accept(this,INDENT);
if(i<prog.fonctions().size()-1) str += "\n"; if(i<prog.fonctions().size()-1) str += "\n";
@@ -29,7 +34,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override @Override
public String visitPrototype(PrototypeImp proto, String indent){ public String visitPrototype(PrototypeImp proto, String indent){
String str= indent + "PROTO "+proto.type().accept(this, "")+ " "+ proto.nom() + "("; String str= indent + "PROTO "+proto.type().accept(this, "")+ " "+ proto.nom() + "(";
for(int i=0; i<proto.params().size();i++){ for(int i=0; i<proto.params().size();i++){
str+=proto.params().get(i).accept(this, ""); str+=proto.params().get(i).accept(this, "");
if((i<proto.params().size()-1)){ if((i<proto.params().size()-1)){
@@ -43,7 +47,6 @@ 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() +"("; String str = indent+"FUNC " + fun.type().accept(this,"")+ " " + fun.nom() +"(";
for(int i=0; i<fun.params().size();i++){ for(int i=0; i<fun.params().size();i++){
str+=fun.params().get(i).accept(this, ""); str+=fun.params().get(i).accept(this, "");
if((i<fun.params().size()-1)){ if((i<fun.params().size()-1)){

View File

@@ -7,7 +7,7 @@ import TP2.llvm.ProgramLLVM.*;
public class Program{ public class Program{
//Prog //Prog
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{ public static record ProgramImp(ArrayList<Function> fonctions,ArrayList<Function> protos) implements ProgramI{
public <H, S> S accept(ProgramVisitor<H, S> v, H h) { public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
return v.visitProgram(this, h); return v.visitProgram(this, h);
} }

View File

@@ -1,7 +1,7 @@
PROTO INT add(x,y) PROTO INT add(x,y)
FUNC INT main(x,y) { FUNC INT main(x,y) {
INT a,b,c,minh INT a,b,c,minh
x = 5 x := 5
minh := x * y minh := x * y
b:=3 b:=3
c:=1 c:=1