diff --git a/README.md b/README.md index c0610f9..5153d64 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,55 @@ # TP2 PDS - VSL+ -Ce projet VSCode contient tout le nécessaire pour commencer à programmer le compilateur. - -Vous pouvez forker ce dépôt, mais devez impérativement garder votre dépôt privé. - -Ce fichier `README.md` doit être complété au fur et à mesure de votre avancement. - - -func::=Func(String,) - +Réalisé par Thibaut ROCHAS et Tuan Minh VU +#### Program +``` +Program ::= Program(Function+) +``` +#### Function +``` +Function ::= Function(Type, String, Var*, Instruction) + | Prototype(Type, String, Var*) +``` +#### Var +``` +Var ::= Var(String) +``` +#### Declaration +``` +Declaration ::= Declaration(Type, String+) +``` +#### Instruction +``` +Instruction ::= Assign(String, Expression) + | Return(Expression) + | Bloc(Instruction+) + | BlocWithDecl(Declaration+, Instruction+) + | VoidFunctionCall(String, Expression*) + | IfThen(Expression, Instruction) + | IfThenElse(Expression, Instruction, Instruction) + | While(Expression, Instruction) + | Read(Var+) + | Print(ExpressionOuText+) +``` +#### Expression +``` +ExpressionOuText ::= Expression + | Text(String) +Expression ::= Binop(Op, Expression, Expression) + | Const(Int) + | Var(String) + | Call(String, Expression*) + | Paren(Expression) +``` +#### Autres +``` +Op ::= PLUS | MINUS | TIMES | DIV | MOD +Type ::= INT | VOID +``` +## Etat ## Compatibilité diff --git a/a.out b/a.out index f0013e8..4d71261 100755 Binary files a/a.out and b/a.out differ diff --git a/src/main/antlr/VSLLexer.g b/src/main/antlr/VSLLexer.g index 19ffdfa..87f7413 100644 --- a/src/main/antlr/VSLLexer.g +++ b/src/main/antlr/VSLLexer.g @@ -50,6 +50,10 @@ BacO: '{' ; BacF: '}' ; +CroO: '[' + ; +CroF:']' + ; PLUS: '+' ; MINUS: '-' diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index d6abee3..144ca9d 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -87,17 +87,32 @@ list_decls returns [ArrayList out] })+ ; -declaration returns [Declaration out]: - t=type i=ident +declaration returns [Declaration out] + @init{ + ArrayList vars = new ArrayList<>(); + }: + t=type v1=var_decl + { + vars.add($v1.out); + } + (VIRGULE v2=var_decl + { + vars.add($v2.out); + } + )* { - ArrayList declare= new ArrayList(); - declare.add($i.out); - }(VIRGULE i2=ident - { - declare.add($i2.out); - })* - {$out = new DeclarationImp($t.return_type, declare);} - ; + $out= new DeclarationImp($t.return_type, vars); + } +; + +var_decl returns [VarDeclImp out]: + id=ident + (CroO n=NUMBER CroF + {$out= new VarDeclImp($id.out,$n.int);} + | + {$out= new VarDeclImp($id.out, null);} + ) +; list_instr returns [ArrayList out] @init{ diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index 47d1480..b13ef5c 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -34,6 +34,7 @@ public interface Interface{ public interface DeclVisitor{ public S visitDeclaration(DeclarationImp instr,H h); + public S visitVarDecl(VarDeclImp instr, H h); } //INSTRUCTION @@ -79,8 +80,5 @@ public interface Interface{ public S visitVoid(Type_voidImp t, H h); } - - - public enum Op {PLUS, MINUS, TIMES, DIV, MOD} } \ No newline at end of file diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java index 8b6293b..4d0fd0f 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -58,12 +58,22 @@ public class PrettyprinterVisitor implements ProgramVisitor, public String visitDeclaration(DeclarationImp instr, String indent) { String str = indent +instr.t().accept(this,"") + " "; for(int i = 0; i s) implements Declaration{ + public static record DeclarationImp(Type t, ArrayList s) implements Declaration{ @Override public S accept(DeclVisitor v, H h) { return v.visitDeclaration(this, h); } } + public static record VarDeclImp(String nom, Integer size) implements Declaration{ + @Override + public S accept(DeclVisitor v, H h) { + return v.visitVarDecl(this, h); + } + } + //Instructions public static record Return_instrImp(Expression e) implements Instruction{ public S accept(InstrVisitor v, H h) { diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index 4223f8d..be65866 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -104,6 +104,7 @@ public class toLLVM_Visitor implements ProgramVisitor, @Override public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) { ArrayList list = new ArrayList<>(); + /* for(int i = 0; i, h = r.symTable; list.add(new AssignLLVMImp(new VarLLVMImp(t2, name,false),new allocaLLVMImp(t2))); } + */ + return new InstrAndSymTable(list,h); + } + + @Override + public InstrAndSymTable visitVarDecl(VarDeclImp instr, SymTable h) { + ArrayList list = new ArrayList<>(); + //TODO return new InstrAndSymTable(list,h); } @@ -288,6 +297,7 @@ public class toLLVM_Visitor implements ProgramVisitor, String labelThen= "then"+h.getNewIdLabel(); String labelFin= "fi"+h.getNewIdLabel(); + l.add(new BrLLVMImp(labelIf)); l.add(new LabelLLVMImp(labelIf)); InstrAndVal temp = instr.e().accept(this,h); l.addAll(temp.instrs); @@ -317,6 +327,7 @@ public class toLLVM_Visitor implements ProgramVisitor, String labelElse= "else"+h.getNewIdLabel(); String labelFin= "fi"+h.getNewIdLabel(); + l.add(new BrLLVMImp(labelIf)); l.add(new LabelLLVMImp(labelIf)); InstrAndVal temp = instr.e().accept(this,h); l.addAll(temp.instrs); diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index 2820ad9..0666e8d 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -1,34 +1,4 @@ -PROTO INT add(a,b) - -FUNC INT add(a,b) { - INT z - z := a+b - RETURN z -} - -FUNC VOID affiche(x){ - PRINT x -} - FUNC INT main(x,y) { - INT a,b,c,minh - x := 5 - minh := x * y - b:=3 - affiche(b) - c:=add(x,b) - PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b, x - PRINT "test",a - WHILE b - 1 - DO{ - b := b - 1 - c := c + 1 - } - DONE - IF c -1 - THEN READ a ELSE READ b - FI - b:=c+1 - RETURN 4 + 6 * 5 + 2 } - -PROTO INT type(x,y) \ No newline at end of file + INT a,b[10],c,d[11] + a:=1 +} \ No newline at end of file diff --git a/tests/fragment1/if1 b/tests/fragment1/if1 index 2966266..b5231f4 100755 Binary files a/tests/fragment1/if1 and b/tests/fragment1/if1 differ diff --git a/tests/fragment1/if2 b/tests/fragment1/if2 index 03a3d5e..8271f70 100755 Binary files a/tests/fragment1/if2 and b/tests/fragment1/if2 differ diff --git a/tests/fragment1/while1 b/tests/fragment1/while1 index 9709d1a..43b8db4 100755 Binary files a/tests/fragment1/while1 and b/tests/fragment1/while1 differ diff --git a/tests/fragment2/call2 b/tests/fragment2/call2 new file mode 100755 index 0000000..8be4c43 Binary files /dev/null and b/tests/fragment2/call2 differ diff --git a/tests/fragment2/call3expr b/tests/fragment2/call3expr new file mode 100755 index 0000000..4d71261 Binary files /dev/null and b/tests/fragment2/call3expr differ diff --git a/tests/fragment2/call4if b/tests/fragment2/call4if new file mode 100755 index 0000000..be1419c Binary files /dev/null and b/tests/fragment2/call4if differ diff --git a/tests/fragment2/proto2 b/tests/fragment2/proto2 new file mode 100755 index 0000000..25a79ed Binary files /dev/null and b/tests/fragment2/proto2 differ diff --git a/tests/testsAdvanced/portee2 b/tests/testsAdvanced/portee2 new file mode 100755 index 0000000..0fc701b Binary files /dev/null and b/tests/testsAdvanced/portee2 differ