parser + asd non testé non builder
This commit is contained in:
@@ -31,7 +31,7 @@ public class Main {
|
||||
VSLParser parser = new VSLParser(tokens);
|
||||
|
||||
// Parse
|
||||
Program ast = parser.program();
|
||||
ProgramImpl ast = parser.program();
|
||||
|
||||
// Pretty-print the program (to debug parsing)
|
||||
System.err.println("todo " + ast);
|
||||
|
||||
23
src/main/java/TP2/asd/ExprEval.java
Normal file
23
src/main/java/TP2/asd/ExprEval.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ExprEval implements ExprVisitor<Map<String, Type>,Integer> {
|
||||
|
||||
@Override
|
||||
public Integer visitConst(Const c, Map<String, Type> h) {
|
||||
return c.c();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visitBinOp(BinopExpression e, Map<String, Type> h) {
|
||||
switch(e.op()){
|
||||
case Op.PLUS: return e.e1().accept(this, h)+e.e2().accept(this, h);
|
||||
case Op.MINUS: return e.e1().accept(this, h)-e.e2().accept(this, h);
|
||||
case Op.TIMES: return e.e1().accept(this, h)*e.e2().accept(this, h);
|
||||
case Op.DIV: return e.e1().accept(this, h)/e.e2().accept(this, h);
|
||||
default: throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,38 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
interface Expression {
|
||||
public <H,S> S accept(ExprVisitor<H,S> v, H h);
|
||||
}
|
||||
|
||||
interface Instruction {
|
||||
public String prettyprinter();
|
||||
public <H,S> S accept(InstrVisitor<H,S> v, H h);
|
||||
}
|
||||
|
||||
interface ProgramVisitor<H,S> {
|
||||
public S visitProgram(Program p, H h);
|
||||
public S visitFunction(Function f, H h);
|
||||
}
|
||||
|
||||
interface InstrVisitor<H,S>{
|
||||
public S visitReturn(Return_instr e, H h);
|
||||
}
|
||||
|
||||
interface ExprVisitor<H,S> {
|
||||
public S visitConst(Const e,H h);
|
||||
public S visitBinOp(BinopExpression e, H h);
|
||||
}
|
||||
|
||||
interface Type{
|
||||
public String prettyprinter();
|
||||
}
|
||||
|
||||
enum Op {PLUS, MINUS, TIMES,DIV}
|
||||
|
||||
//Eval
|
||||
interface ExprEval extends ExprVisitor<Map<String, Integer>, Integer> {
|
||||
}
|
||||
|
||||
interface TypeCheck extends ExprVisitor<Map<String, Type>, Type> {
|
||||
}
|
||||
@@ -1,29 +1,40 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public record Program(ArrayList<Function> functions) {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
record Function(Type type, String nom, ArrayList<Instruction> instructions){
|
||||
public String prettyprinter(){
|
||||
return "FUNC " +type.prettyprinter() +" "+ nom
|
||||
+ instructions.stream().map(Instruction::prettyprinter).collect(Collectors.joining("\n"));
|
||||
// Map: appel prettyprinter pour isntruction, combiner dans 1 paragraph avec \n au milieu
|
||||
public Function(Type type, String name, Instruction instruction) {
|
||||
this(type, name, new ArrayList<>() {{ add(instruction); }});
|
||||
}
|
||||
}
|
||||
record Return_instr(Expression e) implements Instruction{
|
||||
public String prettyprinter(){
|
||||
return "RETURN" +e.prettyprinter();
|
||||
|
||||
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
|
||||
return v.visitFunction(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
record Expression(){
|
||||
public String prettyprinter(){
|
||||
return "";
|
||||
record Const(int c) implements Expression{
|
||||
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||
return v.visitConst(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
record BinopExpression(Op op,Expression e1, Expression e2) implements Expression{
|
||||
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||
return v.visitBinOp(this, h);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
record Return_instr(Expression e) implements Instruction{
|
||||
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
|
||||
return v.visitReturn(this,h);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,4 +48,4 @@ record Type_int() implements Type{
|
||||
public String prettyprinter() {
|
||||
return "INT";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
7
src/main/java/TP2/asd/SymTable.java
Normal file
7
src/main/java/TP2/asd/SymTable.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package TP2.asd;
|
||||
|
||||
public class SymTable {
|
||||
public SymTable(){
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user