This commit is contained in:
Vu Tuan Minh
2025-04-30 13:44:12 +02:00
parent 854b2387c4
commit 88c5047e5f
18 changed files with 66 additions and 51 deletions

View File

@@ -61,12 +61,12 @@ public class Main {
//System.err.println("todo " + ast);
//System.out.println("\n\n PRETTYPRINTER VSL : \n--------------\n");
//System.out.println(ast.prettyprinter());
System.out.println(ast.prettyprinter());
//System.out.println("\n\n PRETTYPRINTER VSL : \n--------------\n");
// Verify the program semantic
// Generate the intermediate representation
//System.out.println("\n\n");
System.out.println("\n\n");
//PrintWriter out2 = new PrintWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8), true);
ProgramLLVMImp astLLVM = ast.toLLVM();

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);