génération du fichier .ll sans passer par la sorti Print, pour controller le type d'encodage
This commit is contained in:
56
README.md
56
README.md
@@ -1,17 +1,55 @@
|
|||||||
# TP2 PDS - VSL+
|
# TP2 PDS - VSL+
|
||||||
|
|
||||||
Ce projet VSCode contient tout le nécessaire pour commencer à programmer le compilateur.
|
Réalisé par Thibaut ROCHAS et Tuan Minh VU
|
||||||
|
|
||||||
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,)
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 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é
|
## Compatibilité
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,10 @@ BacO: '{'
|
|||||||
;
|
;
|
||||||
BacF: '}'
|
BacF: '}'
|
||||||
;
|
;
|
||||||
|
CroO: '['
|
||||||
|
;
|
||||||
|
CroF:']'
|
||||||
|
;
|
||||||
PLUS: '+'
|
PLUS: '+'
|
||||||
;
|
;
|
||||||
MINUS: '-'
|
MINUS: '-'
|
||||||
|
|||||||
@@ -87,17 +87,32 @@ list_decls returns [ArrayList<Declaration> out]
|
|||||||
})+
|
})+
|
||||||
;
|
;
|
||||||
|
|
||||||
declaration returns [Declaration out]:
|
declaration returns [Declaration out]
|
||||||
t=type i=ident
|
@init{
|
||||||
|
ArrayList<VarDeclImp> vars = new ArrayList<>();
|
||||||
|
}:
|
||||||
|
t=type v1=var_decl
|
||||||
|
{
|
||||||
|
vars.add($v1.out);
|
||||||
|
}
|
||||||
|
(VIRGULE v2=var_decl
|
||||||
|
{
|
||||||
|
vars.add($v2.out);
|
||||||
|
}
|
||||||
|
)*
|
||||||
{
|
{
|
||||||
ArrayList<String> declare= new ArrayList<String>();
|
$out= new DeclarationImp($t.return_type, vars);
|
||||||
declare.add($i.out);
|
}
|
||||||
}(VIRGULE i2=ident
|
;
|
||||||
{
|
|
||||||
declare.add($i2.out);
|
var_decl returns [VarDeclImp out]:
|
||||||
})*
|
id=ident
|
||||||
{$out = new DeclarationImp($t.return_type, declare);}
|
(CroO n=NUMBER CroF
|
||||||
;
|
{$out= new VarDeclImp($id.out,$n.int);}
|
||||||
|
|
|
||||||
|
{$out= new VarDeclImp($id.out, null);}
|
||||||
|
)
|
||||||
|
;
|
||||||
|
|
||||||
list_instr returns [ArrayList<Instruction> out]
|
list_instr returns [ArrayList<Instruction> out]
|
||||||
@init{
|
@init{
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ public interface Interface{
|
|||||||
|
|
||||||
public interface DeclVisitor<H,S>{
|
public interface DeclVisitor<H,S>{
|
||||||
public S visitDeclaration(DeclarationImp instr,H h);
|
public S visitDeclaration(DeclarationImp instr,H h);
|
||||||
|
public S visitVarDecl(VarDeclImp instr, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
//INSTRUCTION
|
//INSTRUCTION
|
||||||
@@ -79,8 +80,5 @@ public interface Interface{
|
|||||||
public S visitVoid(Type_voidImp t, H h);
|
public S visitVoid(Type_voidImp t, H h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public enum Op {PLUS, MINUS, TIMES, DIV, MOD}
|
public enum Op {PLUS, MINUS, TIMES, DIV, MOD}
|
||||||
}
|
}
|
||||||
@@ -58,12 +58,22 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
public String visitDeclaration(DeclarationImp instr, String indent) {
|
public String visitDeclaration(DeclarationImp instr, String indent) {
|
||||||
String str = indent +instr.t().accept(this,"") + " ";
|
String str = indent +instr.t().accept(this,"") + " ";
|
||||||
for(int i = 0; i<instr.s().size();i++){
|
for(int i = 0; i<instr.s().size();i++){
|
||||||
str += instr.s().get(i);
|
str += instr.s().get(i).accept(this, indent);
|
||||||
if(i<instr.s().size()-1) str += ",";
|
if(i<instr.s().size()-1) str += ",";
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitVarDecl(VarDeclImp instr, String indent){
|
||||||
|
String str = "";
|
||||||
|
if(instr.size()==null){
|
||||||
|
str+= instr.nom();
|
||||||
|
}else{
|
||||||
|
str+= instr.nom()+ "["+instr.size()+"]";
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
//INSTRUCTION
|
//INSTRUCTION
|
||||||
@Override
|
@Override
|
||||||
public String visitReturn(Return_instrImp instr, String indent) {
|
public String visitReturn(Return_instrImp instr, String indent) {
|
||||||
|
|||||||
@@ -70,13 +70,20 @@ public class Program{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Declaration
|
//Declaration
|
||||||
public static record DeclarationImp(Type t, ArrayList<String> s) implements Declaration{
|
public static record DeclarationImp(Type t, ArrayList<VarDeclImp> s) implements Declaration{
|
||||||
@Override
|
@Override
|
||||||
public <H, S> S accept(DeclVisitor<H, S> v, H h) {
|
public <H, S> S accept(DeclVisitor<H, S> v, H h) {
|
||||||
return v.visitDeclaration(this, h);
|
return v.visitDeclaration(this, h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static record VarDeclImp(String nom, Integer size) implements Declaration{
|
||||||
|
@Override
|
||||||
|
public <H, S> S accept(DeclVisitor<H, S> v, H h) {
|
||||||
|
return v.visitVarDecl(this, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Instructions
|
//Instructions
|
||||||
public static record Return_instrImp(Expression e) implements Instruction{
|
public static record Return_instrImp(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) {
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
@Override
|
@Override
|
||||||
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
|
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
|
||||||
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
ArrayList<InstructionLLVM> list = new ArrayList<>();
|
||||||
|
/*
|
||||||
for(int i = 0; i<instr.s().size();i++){
|
for(int i = 0; i<instr.s().size();i++){
|
||||||
TypeLLVM t2 = instr.t().accept(this,h);
|
TypeLLVM t2 = instr.t().accept(this,h);
|
||||||
Result r = h.addVar(instr.s().get(i),t2);
|
Result r = h.addVar(instr.s().get(i),t2);
|
||||||
@@ -111,6 +112,14 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
h = r.symTable;
|
h = r.symTable;
|
||||||
list.add(new AssignLLVMImp(new VarLLVMImp(t2, name,false),new allocaLLVMImp(t2)));
|
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<InstructionLLVM> list = new ArrayList<>();
|
||||||
|
//TODO
|
||||||
return new InstrAndSymTable(list,h);
|
return new InstrAndSymTable(list,h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,6 +297,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
String labelThen= "then"+h.getNewIdLabel();
|
String labelThen= "then"+h.getNewIdLabel();
|
||||||
String labelFin= "fi"+h.getNewIdLabel();
|
String labelFin= "fi"+h.getNewIdLabel();
|
||||||
|
|
||||||
|
l.add(new BrLLVMImp(labelIf));
|
||||||
l.add(new LabelLLVMImp(labelIf));
|
l.add(new LabelLLVMImp(labelIf));
|
||||||
InstrAndVal temp = instr.e().accept(this,h);
|
InstrAndVal temp = instr.e().accept(this,h);
|
||||||
l.addAll(temp.instrs);
|
l.addAll(temp.instrs);
|
||||||
@@ -317,6 +327,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
|
|||||||
String labelElse= "else"+h.getNewIdLabel();
|
String labelElse= "else"+h.getNewIdLabel();
|
||||||
String labelFin= "fi"+h.getNewIdLabel();
|
String labelFin= "fi"+h.getNewIdLabel();
|
||||||
|
|
||||||
|
l.add(new BrLLVMImp(labelIf));
|
||||||
l.add(new LabelLLVMImp(labelIf));
|
l.add(new LabelLLVMImp(labelIf));
|
||||||
InstrAndVal temp = instr.e().accept(this,h);
|
InstrAndVal temp = instr.e().accept(this,h);
|
||||||
l.addAll(temp.instrs);
|
l.addAll(temp.instrs);
|
||||||
|
|||||||
@@ -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) {
|
FUNC INT main(x,y) {
|
||||||
INT a,b,c,minh
|
INT a,b[10],c,d[11]
|
||||||
x := 5
|
a:=1
|
||||||
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)
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
tests/fragment2/call2
Executable file
BIN
tests/fragment2/call2
Executable file
Binary file not shown.
BIN
tests/fragment2/call3expr
Executable file
BIN
tests/fragment2/call3expr
Executable file
Binary file not shown.
BIN
tests/fragment2/call4if
Executable file
BIN
tests/fragment2/call4if
Executable file
Binary file not shown.
BIN
tests/fragment2/proto2
Executable file
BIN
tests/fragment2/proto2
Executable file
Binary file not shown.
BIN
tests/testsAdvanced/portee2
Executable file
BIN
tests/testsAdvanced/portee2
Executable file
Binary file not shown.
Reference in New Issue
Block a user