/* La classe astratta Expr definisce parse tree per le espressioni. Le sottoclassi concrete si riferiscono ai tipi particolari di espressioni. Il metodo generaCodice permette di generare il codice per calcolare l'espressione. */ import lt.macchina.Codice; import static lt.macchina.Macchina.*; abstract class Expr { public abstract void generaCodice(Codice c); } class PiuExpr extends Expr { private Expr sx, dx; public PiuExpr(Expr sx, Expr dx) { this.sx = sx; this.dx = dx; } public void generaCodice(Codice c) { sx.generaCodice(c); dx.generaCodice(c); c.genera(ADD); } public String toString() { return sx.toString() + " " + dx.toString() + " +"; } } class MenoExpr extends Expr { private Expr sx, dx; public MenoExpr(Expr sx, Expr dx) { this.sx = sx; this.dx = dx; } public void generaCodice(Codice c) { sx.generaCodice(c); dx.generaCodice(c); c.genera(SUB); } public String toString() { return sx.toString() + " " + dx.toString() + " -"; } } class PerExpr extends Expr { private Expr sx, dx; public PerExpr(Expr sx, Expr dx) { this.sx = sx; this.dx = dx; } public void generaCodice(Codice c) { sx.generaCodice(c); dx.generaCodice(c); c.genera(MUL); } public String toString() { return sx.toString() + " " + dx.toString() + " *"; } } class DivisoExpr extends Expr { private Expr sx, dx; public DivisoExpr(Expr sx, Expr dx) { this.sx = sx; this.dx = dx; } public void generaCodice(Codice c) { sx.generaCodice(c); dx.generaCodice(c); c.genera(DIV); } public String toString() { return sx.toString() + " " + dx.toString() + " /"; } } class UnMenoExpr extends Expr { private Expr e; public UnMenoExpr(Expr e) { this.e = e; } public void generaCodice(Codice c) { c.genera(PUSHIMM, 0); e.generaCodice(c); c.genera(SUB); } public String toString() { return e.toString() + "-"; } } class UnPiuExpr extends Expr { private Expr e; public UnPiuExpr(Expr e) { this.e = e; } public void generaCodice(Codice c) { e.generaCodice(c); } public String toString() { return e.toString() + "+"; } } class NumExpr extends Expr { private Integer num; public NumExpr(Integer num) { this.num = num; } public void generaCodice(Codice c) { c.genera(PUSHIMM, num.intValue()); } public String toString() { return num.toString(); } } class IdExpr extends Expr { private Descrittore descrittore; public IdExpr(Descrittore d) { descrittore = d; } public void generaCodice(Codice c) { c.genera(PUSH, descrittore.getIndirizzo()); } public String toString() { return descrittore.getIdentificatore(); } }