génération du fichier .ll sans passer par la sorti Print, pour controller le type d'encodage

This commit is contained in:
Rochas
2025-04-30 14:02:19 +02:00
17 changed files with 110 additions and 57 deletions

View File

@@ -50,6 +50,10 @@ BacO: '{'
;
BacF: '}'
;
CroO: '['
;
CroF:']'
;
PLUS: '+'
;
MINUS: '-'

View File

@@ -87,17 +87,32 @@ list_decls returns [ArrayList<Declaration> out]
})+
;
declaration returns [Declaration out]:
t=type i=ident
declaration returns [Declaration out]
@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>();
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<Instruction> out]
@init{

View File

@@ -34,6 +34,7 @@ public interface Interface{
public interface DeclVisitor<H,S>{
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}
}

View File

@@ -58,12 +58,22 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
public String visitDeclaration(DeclarationImp instr, String indent) {
String str = indent +instr.t().accept(this,"") + " ";
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 += ",";
}
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
@Override
public String visitReturn(Return_instrImp instr, String indent) {

View File

@@ -70,13 +70,20 @@ public class Program{
}
//Declaration
public static record DeclarationImp(Type t, ArrayList<String> s) implements Declaration{
public static record DeclarationImp(Type t, ArrayList<VarDeclImp> s) implements Declaration{
@Override
public <H, S> S accept(DeclVisitor<H, S> v, H 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
public static record Return_instrImp(Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {

View File

@@ -104,6 +104,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
@Override
public InstrAndSymTable visitDeclaration(DeclarationImp instr, SymTable h) {
ArrayList<InstructionLLVM> list = new ArrayList<>();
/*
for(int i = 0; i<instr.s().size();i++){
TypeLLVM t2 = instr.t().accept(this,h);
Result r = h.addVar(instr.s().get(i),t2);
@@ -111,6 +112,14 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
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<InstructionLLVM> list = new ArrayList<>();
//TODO
return new InstrAndSymTable(list,h);
}
@@ -288,6 +297,7 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
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<SymTable,ProgramLLVMImp>,
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);