git ingnore, modification de toString

This commit is contained in:
trochas
2025-04-01 16:58:01 +02:00
parent 0d27726f12
commit cde7a34c7d
2 changed files with 53 additions and 26 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ build
bin bin
*.ll *.ll
*.DS_Store *.DS_Store
**/.antlr/

View File

@@ -22,12 +22,23 @@ public class LLVM {
sealed interface ListInstr{ sealed interface ListInstr{
String toString(String string);} String toString(String string);}
sealed interface Par{} sealed interface Par{}
sealed interface Expression{} sealed interface Expression{
Type getType();
}
sealed static interface Op{}
sealed interface Var extends Expression{
};
sealed interface Val extends Expression{
};
public static void main(String[] args) { public static void main(String[] args) {
Expression exp1 = new Add(new Add(new Val(0),new Val(1)),new Val(2)); Add ADD = new Add();
Modulo MODULO = new Modulo();
Expression exp1 = new BinOp(ADD,new BinOp(ADD,new ValInt(0),new ValInt(1)),new ValInt(2));
ListPar param = new ListParImpl(new ArrayList<>()); ListPar param = new ListParImpl(new ArrayList<>());
ArrayList<Instr> l = new ArrayList<>(); ArrayList<Instr> l = new ArrayList<>();
l.add(new Return(exp1)); l.add(new Return(exp1));
@@ -39,8 +50,9 @@ public class LLVM {
System.out.println("\n"); System.out.println("\n");
Var a = new Var("a");
Var b = new Var("b"); Var a = new VarInt("a");
Var b = new VarInt("b");
Prog p2 = new ProgImpl(List.of( Prog p2 = new ProgImpl(List.of(
f, f,
new FonctionImpl(new INT(),"maFonction",new ListParImpl(List.of(new ParImpl(new INT(),a),new ParImpl(new INT(),b) )),List.of( new FonctionImpl(new INT(),"maFonction",new ListParImpl(List.of(new ParImpl(new INT(),a),new ParImpl(new INT(),b) )),List.of(
@@ -50,9 +62,9 @@ public class LLVM {
new Print(List.of("b = ",b)) new Print(List.of("b = ",b))
)), )),
new While(b, List.of( new While(b, List.of(
new Aff(b, new Modulo(b,new Val(2))) new Aff(b, new BinOp(MODULO,b,new ValInt(2)))
)), )),
new Return(new Add(a,b)) new Return(new BinOp(ADD,a,b))
)) ))
@@ -125,16 +137,13 @@ public class LLVM {
} }
} }
record Read(List<Object> items) implements Instr{ //TODO pas bon record Read(List<Var> items) implements Instr{ //TODO pas bon
public String toString(String indent){ public String toString(String indent){
String str = indent + "READ"; String str = indent + "READ";
for(int i = 0; i<items.size(); i++){ for(int i = 0; i<items.size(); i++){
if(items.get(i) instanceof Exception || items.get(i) instanceof String){
str += items.get(i).toString(); str += items.get(i).toString();
if(i<items.size()-1) str += ", "; if(i<items.size()-1) str += ", ";
} }
else System.err.println("Item de mauvais type, READ");
}
return str; return str;
} }
@@ -190,7 +199,7 @@ public class LLVM {
} }
} }
record ListDeclImpl(Type type, List<String> vars) implements ListDecl{ record ListDeclImpl(Type type, List<String> vars) implements ListDecl{ //erreur
public String toString(String indent){ public String toString(String indent){
String str = indent + type.toString() + " "; String str = indent + type.toString() + " ";
for(int i = 0; i<vars.size(); i++){ for(int i = 0; i<vars.size(); i++){
@@ -231,47 +240,64 @@ public class LLVM {
} }
} }
record BinOp(Op op,Expression e1, Expression e2) implements Expression{
record Add(Expression e1, Expression e2) implements Expression{
public String toString(){ public String toString(){
return "("+ e1 + " + " + e2 + ")"; return "("+ e1 + " " + op.toString() + " " + e2 + ")";
}
public Type getType(){
Type t1 = e1.getType();
Type t2 = e2.getType();
if(t1 == t2) return t1;
else return null;
} }
} }
record Sub(Expression e1, Expression e2) implements Expression{ record Add() implements Op{
public String toString(){ public String toString(){
return "("+ e1 + " - " + e2 + ")"; return "+";
} }
} }
record Modulo(Expression e1, Expression e2) implements Expression{ record Sub() implements Op{
public String toString(){ public String toString(){
return "("+ e1 + " % " + e2 + ")"; return "-";
} }
} }
record Mult(Expression e1, Expression e2) implements Expression{ record Modulo() implements Op{
public String toString(){ public String toString(){
return "("+ e1 + " * " + e2 + ")"; return "%";
} }
} }
record Div(Expression e1, Expression e2) implements Expression{ record Mult() implements Op{
public String toString(){ public String toString(){
return "("+ e1 + " / " + e2 + ")"; return "*";
} }
} }
record Val(int val) implements Expression{ record Div() implements Op{
public String toString(){
return "/";
}
}
record ValInt(int val) implements Val{
public String toString(){ public String toString(){
return val+""; return val+"";
} }
public Type getType(){
return new INT();
}
} }
record Var(String nom) implements Expression{ record VarInt(String nom) implements Var{
public String toString(){ public String toString(){
return nom; return nom;
} }
public Type getType(){
return new INT();
}
} }