This commit is contained in:
Vu Tuan Minh
2025-04-01 17:52:45 +02:00
parent 2a901e4a37
commit 3f9072c7bd
7 changed files with 161 additions and 178 deletions

View File

@@ -1,23 +0,0 @@
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();
}
}
}

View File

@@ -2,37 +2,39 @@ package TP2.asd;
import java.util.Map;
interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h);
}
public interface Interface{
public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h);
}
interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h);
}
public interface Instruction {
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);
}
public interface ProgramVisitor<H,S> {
public S visitProgram(Program p, H h);
public S visitFunction(Program.Function f, H h);
}
interface InstrVisitor<H,S>{
public S visitReturn(Return_instr e, H h);
}
public interface InstrVisitor<H,S>{
public S visitReturn(Program.Return_instr e, H h);
}
interface ExprVisitor<H,S> {
public S visitConst(Const e,H h);
public S visitBinOp(BinopExpression e, H h);
}
public interface ExprVisitor<H,S> {
public S visitConst(Program.Const e,H h);
public S visitBinOp(Program.BinopExpression e, H h);
}
interface Type{
public String prettyprinter();
}
public interface Type{
public String prettyprinter();
}
enum Op {PLUS, MINUS, TIMES,DIV}
public enum Op {PLUS, MINUS, TIMES,DIV}
//Eval
interface ExprEval extends ExprVisitor<Map<String, Integer>, Integer> {
}
//Eval
public interface ExprEval extends ExprVisitor<Map<String, Integer>, Integer> {
}
interface TypeCheck extends ExprVisitor<Map<String, Type>, Type> {
public interface TypeCheck extends ExprVisitor<Map<String, Type>, Type> {
}
}

View File

@@ -1,49 +1,48 @@
package TP2.asd;
import java.util.ArrayList;
import TP2.asd.Interface.*;
public record Program(ArrayList<Function> functions) {
}
record Function(Type type, String nom, ArrayList<Instruction> instructions){
public Function(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }});
public class Program{
public static record Function(Type type, String nom, ArrayList<Instruction> instructions){
public Function(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }});
}
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
return v.visitFunction(this, h);
}
}
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
return v.visitFunction(this, h);
public static record Const(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h);
}
}
public static 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);
}
}
public static record Return_instr(Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitReturn(this,h);
}
}
public static record Type_void() implements Type{
public String prettyprinter() {
return "VOID";
}
}
public static record Type_int() implements Type{
public String prettyprinter() {
return "INT";
}
}
}
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);
}
}
record Type_void() implements Type{
public String prettyprinter() {
return "VOID";
}
}
record Type_int() implements Type{
public String prettyprinter() {
return "INT";
}
}