add parser
This commit is contained in:
@@ -113,7 +113,15 @@ instruction returns [Instruction out]:
|
||||
read.add(new VarImp($i2.out));
|
||||
})*
|
||||
{$out = new ReadImp(read);}
|
||||
|
||||
| //IF THEN ELSE FIN
|
||||
IF ex1=expression THEN ins1=instruction
|
||||
(FIN
|
||||
{$out= new IfThenImp($ex1.out, $ins1.out);}
|
||||
| ELSE ins2=instruction FIN
|
||||
{$out= new IfThenElseImp($ex1.out, $ins1.out,$ins2.out); }
|
||||
)
|
||||
| WHILE exp1=expression DO ins3=instruction DONE
|
||||
{$out = new WhileImp($exp1.out,$ins3.out);}
|
||||
;
|
||||
|
||||
//Priorité lit(val, const ou paranthese) -> td_exp (*/%) -> exp (+-)
|
||||
|
||||
@@ -1,8 +1,34 @@
|
||||
package TP2.Error;
|
||||
|
||||
import TP2.asd.SymTable;
|
||||
import TP2.asd.Interface.ExprVisitor;
|
||||
import TP2.asd.Program.BinopExpressionImp;
|
||||
import TP2.asd.Program.ConstImp;
|
||||
import TP2.asd.Program.VarImp;
|
||||
|
||||
public class TypeChecking {
|
||||
private SymTable st;
|
||||
|
||||
|
||||
/*
|
||||
public class TypeCheckExpr implements ExprVisitor<SymTable ,TypeCheckExprDiag>{
|
||||
|
||||
@Override
|
||||
public TypeCheckExprDiag visitConst(ConstImp e, SymTable h) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitConst'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckExprDiag visitVar(VarImp e, SymTable h) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitVar'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckExprDiag visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||
TypeCheckExprDiag tce1 = e.e1().accept(this, h) ;
|
||||
TypeCheckExprDiag tce2 = e.e2().accept(this, h) ;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -37,6 +37,9 @@ public interface Interface{
|
||||
public S visitDeclaration(DeclarationImp instr,H h);
|
||||
public S visitPrint(PrintImp instr, H h);
|
||||
public S visitRead(ReadImp instr,H h);
|
||||
public S visitIfThen(IfThenImp instr, H h);
|
||||
public S visitIfThenElse(IfThenElseImp instr, H h);
|
||||
public S visitWhile(WhileImp instr, H h);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -74,6 +74,23 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||
return str;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitIfThen(IfThenImp instr, String indent) {
|
||||
String str = indent + "IF ";
|
||||
str +=(instr.e().accept(this, ""));
|
||||
str +=" THEN " + (instr.i1().accept(this, ""));
|
||||
return str;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitIfThenElse(IfThenElseImp instr, String indent) {
|
||||
String str = indent + "IF ";
|
||||
str +=(instr.e().accept(this, ""));
|
||||
str +=" THEN " + (instr.i1().accept(this, ""));
|
||||
str +=" ELSE "+ (instr.i2().accept(this, ""));
|
||||
return str;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitRead(ReadImp instr, String indent) {
|
||||
String str = indent+"READ ";
|
||||
@@ -84,6 +101,15 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
||||
return str;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitWhile(WhileImp instr, String indent) {
|
||||
String str = indent+"WHILE ";
|
||||
str += (instr.e().accept(this, ""));
|
||||
str+= " DO "+ (instr.i1().accept(this, ""));
|
||||
str+= " DONE";
|
||||
return str;
|
||||
}
|
||||
|
||||
//EXPRESSION
|
||||
|
||||
@Override
|
||||
|
||||
@@ -88,6 +88,27 @@ public class Program{
|
||||
}
|
||||
}
|
||||
|
||||
public static record IfThenImp(Expression e, Instruction i1) implements Instruction {
|
||||
@Override
|
||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||
return v.visitIfThen(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
public static record IfThenElseImp(Expression e, Instruction i1, Instruction i2) implements Instruction {
|
||||
@Override
|
||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||
return v.visitIfThenElse(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
public static record WhileImp(Expression e, Instruction i1) implements Instruction {
|
||||
@Override
|
||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||
return v.visitWhile(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
//Type
|
||||
public static record Type_voidImp() implements Type{
|
||||
@Override
|
||||
@@ -103,4 +124,5 @@ public class Program{
|
||||
return v.visitInt(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -105,6 +105,25 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
||||
return l;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitIfThen(IfThenImp instr, SymTable h) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitIfThen'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitIfThenElse(IfThenElseImp instr, SymTable h) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitIfThenElse'");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ArrayList<InstructionLLVM> visitWhile(WhileImp instr, SymTable h) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitWhile'");
|
||||
}
|
||||
|
||||
//EXPRESSION
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user