From 3d4750f7e49715282a29d1470a368686ad500f0e Mon Sep 17 00:00:00 2001 From: Vu Tuan Minh Date: Sat, 26 Apr 2025 22:24:54 +0200 Subject: [PATCH] fix proto again --- src/main/antlr/VSLParser.g | 33 ++++++++----------- .../java/TP2/asd/PrettyprinterVisitor.java | 5 --- src/main/java/TP2/asd/Program.java | 2 +- tests/aLaMain.vsl | 4 ++- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index a709044..c752299 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -22,28 +22,11 @@ options { } program returns [ProgramImp p] : - pro=prototypes func=functions EOF - {$p = new ProgramImp($func.out,$pro.out);} + {$p = new ProgramImp($func.out);} ; -prototypes returns [ArrayList out] - @init{ - $out = new ArrayList(); - }: - (proto { - $out.add($proto.out); - })* - ; - -proto returns [Function out]: - PROTOTYPE t=type i=ident ParO v=liste_param ParF - { - $out=new PrototypeImp($t.return_type,$i.out, $v.out); - } - ; - liste_param returns [ArrayList out] @init{ $out = new ArrayList(); @@ -58,11 +41,23 @@ functions returns [ArrayList out] @init{ $out = new ArrayList(); }: + ( (function { $out.add($function.out); - })+ + }) + |(proto { + $out.add($proto.out); + }) + )+ ; +proto returns [Function out]: + PROTOTYPE t=type i=ident ParO v=liste_param ParF + { + $out=new PrototypeImp($t.return_type,$i.out, $v.out); + } + ; + function returns [Function out]: FUNCTION t=type i=ident ParO v=liste_param ParF instr= instruction { diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java index 5031726..2b8c8ff 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -18,11 +18,6 @@ public class PrettyprinterVisitor implements ProgramVisitor, @Override public String visitProgram(ProgramImp prog, String indent) { String str =""; - for(int i= 0; i fonctions,ArrayList protos) implements ProgramI{ + public static record ProgramImp(ArrayList fonctions) implements ProgramI{ public S accept(ProgramVisitor v, H h) { return v.visitProgram(this, h); } diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index 2aa0dd9..47bb4a7 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -16,4 +16,6 @@ FUNC INT main(x,y) { THEN READ a ELSE READ b FI b:=c+1 - RETURN 4 + 6 * 5 + 2 } \ No newline at end of file + RETURN 4 + 6 * 5 + 2 } + +PROTO INT type(x,y) \ No newline at end of file