/** class Expr, our internal representation of an expression * in the OG1 language. * See http://www.radford.edu/itec380/2022fall-ibarland/Homeworks/Project/ * * @author Ian Barland * @version 2024.Nov.11 */ import java.util.*; /** An `Expr` represents (the parse tree of) an OG0 expression. * See its implementations `BinOp` etc, for its subtypes/variants. */ interface Expr { /** Evaluate a given Expr. * @return the Value this Expr evaluates to. * (In OG0, all values are numbers (doubles), but * in OG3 that will change, which is why we have * pre-emptively made the return type 'Value'.) */ abstract public Value eval(); /** 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. * @return a String representation of this Expr. */ abstract public String toString(); /** Return (our internal representation of) the first expression in s. * * @param s The source code for exactly one Expr. Must by syntactically correct. * @return (our internal representation of) the expression s. */ public static Expr parse(String s) { return Expr.parse(new java.util.Scanner(s)); } public static final String PUNCTUATION = "@=^{}><;!~#:?|\\(\\)\\[\\]/\\\\"; //as a regex-class // Note: Beware cautious '-' or '.' or '+' as punctuation, // if it introduces ambiguity with numbers. /** Return (our internal representation of) the first expression in s. * Side-effect: consume *exactly* the chars of that expr from the front of `s`. * @param s A scanner starting with the source code for an Expr. * @pre The Expr source must be syntactically correct. * @return (our internal representation of) the expression at front of `s`. */ public static Expr parse(Scanner s) { return parse(s,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`. * @param s A scanner starting with the source code for an Expr. * @param punctuation A string of the chars to be interpreted as puncutation (*and* not part larger tokens). * @pre The Expr source must be syntactically correct. * @return (our internal representation of) the expression at front of `s`. * @see {@link Expr#parse(Scanner)} */ public static Expr parse(java.util.Scanner s, String punctuation) { if (UtilIan.hasNextDoubleSplittingBy(s,punctuation)) { return Num.parse(s,punctuation); } else if (UtilIan.hasNextSplittingBy(s, Paren.START_TOKEN, punctuation) ) { return Paren.parse(s,punctuation); } else if (UtilIan.hasNextSplittingBy(s, BinOp.START_TOKEN,punctuation) ) { return BinOp.parse(s,punctuation); } else if (UtilIan.hasNextSplittingBy(s, IfZero.START_TOKEN, punctuation) ) { return IfZero.parse(s,punctuation); } else { /* Unknown syntax! */ String tokens = ""; while (s.hasNext()) { tokens += UtilIan.nextSplittingBy(s,punctuation) + " "; } throw new java.util.InputMismatchException( "Expr.parse: something has gone awry! Encountered \"" + tokens + "\""); } } }