add check type

This commit is contained in:
Vu Tuan Minh
2025-04-24 06:07:25 +02:00
parent 2948c372dd
commit 3b2314c236
7 changed files with 234 additions and 19 deletions

View File

@@ -14,7 +14,15 @@ public interface Interface{
public interface ProgramVisitor<H,S> {
public S visitProgram(ProgramImp prog, H h);
}
//PROTOTYPE
public interface Prototype{
public <H,S> S accept(PrototypeVisitor<H,S> v, H h);
}
public interface PrototypeVisitor<H,S> {
public S visitPrototype(PrototypeImp proto, H h);
}
//FUNCTION
public interface Function {

View File

@@ -4,6 +4,7 @@ import TP2.asd.Interface.*;
import TP2.asd.Program.*;
public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
PrototypeVisitor<String,String>,
FunctionVisitor<String,String>,
DeclVisitor<String,String>,
InstrVisitor<String,String>,
@@ -18,6 +19,11 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
@Override
public String visitProgram(ProgramImp prog, String indent) {
String str ="";
for(int i= 0; i<prog.protos().size();i++){
str+=prog.protos().get(i).accept(this, INDENT);
if(i<prog.protos().size()-1) str += "\n";
}
str+="\n";
for(int i = 0; i<prog.fonctions().size(); i++){
str += prog.fonctions().get(i).accept(this,INDENT);
if(i<prog.fonctions().size()-1) str += "\n";
@@ -25,8 +31,20 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
return str;
}
//FUNCTION
//PROTOTYPE
@Override
public String visitPrototype(PrototypeImp proto, String indent){
String str= indent + "PROTO "+proto.type().accept(this, "")+ " "+ proto.nom() + "(";
for(int i=0; i<proto.s().size();i++){
str+=proto.s().get(i);
if((i<proto.s().size()-1)){
str+=", ";
}
}
return str+")";
}
//FUNCTION
@Override
public String visitFunction(FunctionImp fun, String indent) {
String str = indent+"FUNC " + fun.type().accept(this,"")+ " " + fun.nom() +"() ";
@@ -35,7 +53,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
}
//DELCARATION
@Override
public String visitDeclaration(DeclarationImp instr, String indent) {
String str = indent +instr.t().accept(this,"") + " ";
@@ -47,7 +64,6 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
}
//INSTRUCTION
@Override
public String visitReturn(Return_instrImp instr, String indent) {
return indent+"RETURN " + instr.e().accept(this,"");

View File

@@ -7,7 +7,7 @@ import TP2.llvm.ProgramLLVM.*;
public class Program{
//Prog
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{
public static record ProgramImp(ArrayList<Function> fonctions,ArrayList<Prototype> protos) implements ProgramI{
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
return v.visitProgram(this, h);
}
@@ -23,6 +23,15 @@ public class Program{
return this.accept(llvmVisitor,new SymTable());
}
}
//Prototype
public static record PrototypeImp(Type type, String nom, ArrayList<String> s) implements Prototype{
@Override
public <H, S> S accept(PrototypeVisitor<H, S> v, H h) {
return v.visitPrototype(this,h);
}
}
//Fonction
public static record FunctionImp(Type type, String nom, Instruction instruction)implements Function {