tested print

This commit is contained in:
Vu Tuan Minh
2025-04-07 21:31:13 +02:00
parent 425e2d4088
commit b4bafcde5d
8 changed files with 33 additions and 10 deletions

View File

@@ -38,7 +38,7 @@ PRINT : 'PRINT'
;
RETURN: 'RETURN'
;
TEXT : '"' (ASCII)+ '"'
TEXT : '"' (ASCII)+ '"'
;
NUMBER : (DIGIT)+
;

View File

@@ -72,7 +72,6 @@ instruction returns [Instruction out]:
}
| //DECLARATION
t=type i=ident
{
ArrayList<String> declare= new ArrayList<String>();
declare.add($i.out);
@@ -81,6 +80,12 @@ instruction returns [Instruction out]:
declare.add($i2.out);
})*
{$out = new DeclarationImp($t.return_type, declare);}
| // PRINT
PRINT TEXT
{
String text= $TEXT.getText();
$out = new PrintImp(text.substring(1,text.length()-1));
}
;
//Priorité lit(val, const ou paranthese) -> td_exp (*/%) -> exp (+-)

View File

@@ -35,6 +35,7 @@ public interface Interface{
public S visitReturn(Return_instrImp instr, H h);
public S visitAssign(AssignImp instr, H h);
public S visitDeclaration(DeclarationImp instr,H h);
public S visitPrint(PrintImp instr, H h);
}

View File

@@ -58,6 +58,11 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
return str;
}
@Override
public String visitPrint(PrintImp instr, String indent) {
return indent+ "PRINT \""+instr.t() +"\"";
}
//EXPRESSION
@Override

View File

@@ -5,7 +5,7 @@ import TP2.asd.Interface.*;
import TP2.llvm.ProgramLLVM.*;
public class Program{
//Prog
public static record ProgramImp(ArrayList<Function> fonctions) implements ProgramI{
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
return v.visitProgram(this, h);
@@ -23,7 +23,7 @@ public class Program{
}
}
//Fonction
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function {
public FunctionImp(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }});
@@ -34,14 +34,13 @@ public class Program{
}
}
//Expression
public static record ConstImp(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h);
}
}
public static record BinopExpressionImp(Op op,Expression e1, Expression e2) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitBinOp(this, h);
@@ -54,15 +53,13 @@ public class Program{
}
}
//Instructions
public static record Return_instrImp(Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitReturn(this,h);
}
}
public static record AssignImp(String t, Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitAssign(this, h);
@@ -76,6 +73,13 @@ public class Program{
}
}
public static record PrintImp(String t) implements Instruction{
@Override
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitPrint(this, h);
}
}
//Type
public static record Type_voidImp() implements Type{
@Override

View File

@@ -11,5 +11,7 @@ public class test_symtable{
//test_symTable.peppapeek();
test_symTable.addVar("a", Type_intImp);
System.out.print(test_symTable.print_all());
String a =" addsqdqsdqs";
a.substring(1, a.length()-1);
}
}

View File

@@ -91,6 +91,12 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return list;
}
@Override
public ArrayList<InstructionLLVM> visitPrint(PrintImp instr, SymTable h) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'visitPrint'");
}
@Override
public InstrOrVal visitConst(ConstImp e, SymTable h) {
ValLLVM val = new ValLLVMImpl(new IntLLVMImpl(),e.c());

View File

@@ -2,5 +2,5 @@ FUNC INT main() {
INT a,b,c
b:=3
c:=1
a := b+c
PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp"
RETURN 4 + 6 * 5 + 2 }