Interface Expr

All Known Subinterfaces:
Expr.Value
All Known Implementing Classes:
Expr.BinOp, Expr.IfZero, Expr.Num, Expr.Paren

public sealed interface Expr permits Expr.Value, Expr.Paren, Expr.BinOp, Expr.IfZero
An `Expr` represents (the parse tree of) an expression in E0. See the internal records like `BinOp` etc, for specific variants.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final record 
    Our internal representation of a BinOp in the E0 language.
    static final record 
    Our internal representation of a IfZero expr in the E0 language.
    static final record 
    Our internal representation of a number in the E0 language.
    static final record 
    Our internal representation of a Paren in the E0 language.
    static interface 
    Abstract class Value - The type of all Expressions which self-evaluate: Numbers, and (later, in E3) functions.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
     
    static final String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    default Expr.Value
    Evaluate a given Expr.
    default String
    Return a String representation of this Expr.
    static Expr
    Return (our internal representation of) the first expression in s.
    static Expr
    Return (our internal representation of) the first expression in s.
    static Expr
    parse(Scanner s, String punctuation)
    Return (our internal representation of) the first expression in s.
  • Field Details

  • Method Details

    • eval

      default Expr.Value eval()
      Evaluate a given Expr.
      Returns:
      the Value this Expr evaluates to. (In E0, all values are numbers (doubles), but in E3 that will change, which is why we have pre-emptively made the return type 'Value'.)
    • myToString

      default String myToString()
      Return a String representation of this Expr. The result will be something which can be passed into 'parse(String)' to get the same Expr back. That is, toString and parse are inverses of each other.
      Returns:
      a String representation of this Expr.
    • parse

      static Expr parse(String s)
      Return (our internal representation of) the first expression in s.
      Parameters:
      s - The source code for exactly one Expr. Must by syntactically correct.
      Returns:
      (our internal representation of) the expression s.
    • parse

      static Expr parse(Scanner s)
      Return (our internal representation of) the first expression in s. Side-effect: consume *exactly* the chars of that expr from the front of `s`.
      Parameters:
      s - A scanner starting with the source code for an Expr.
      Returns:
      (our internal representation of) the expression at front of `s`.
    • parse

      static Expr parse(Scanner s, String punctuation)
      Return (our internal representation of) the first expression in s. Side-effect: consume *exactly* the chars of that expr from the front of `s`.
      Parameters:
      s - A scanner starting with the source code for an Expr.
      punctuation - A string of the chars to be interpreted as puncutation (*and* not part larger tokens).
      Returns:
      (our internal representation of) the expression at front of `s`.