/** class Expr, our internal representation of an expression * in the R0 language. * See http://www.radford.edu/itec380/2016fall-ibarland/Homeworks/Project/ * * @author Ian Barland * @version 2016.Jul.15 */ public abstract class Expr { /** Evaluate a given Expr. * @return the Value this Expr evaluates to. * (In R0, all values are numbers (doubles), but * in R3 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 expression 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 = "|/\\;()%:<>{}!#$^&~='\"?,#" + java.util.regex.Pattern.quote("[]"); // I am not sure why the [] needs to be quoted, // but nothing can be quoted. It's a bug in UtilIan. --Ian /** Return (our internal representation of) the expression s. * @param s A scanner reading the source code for exactly one Expr. * Must by syntactically correct. * @return (our internal representation of) the expression s. */ public static Expr parse(java.util.Scanner s) { if (UtilIan.hasNextDoubleSplittingBy(s,PUNCTUATION)) { return new Num( UtilIan.nextDoubleSplittingBy(s,PUNCTUATION) ); } else if (UtilIan.hasNextSplittingBy(s, Parity.TOKEN, PUNCTUATION)) { UtilIan.nextSplittingBy(s, PUNCTUATION); // consume the "parity" Expr subExpr1 = parse(s); Expr subExpr2 = parse(s); Expr subExpr3 = parse(s); assert UtilIan.hasNextChar(s,'\\') : "`\\` must close " + Parity.TOKEN; UtilIan.nextChar(s,'\\'); // Consume the closing \ return new Parity(subExpr1,subExpr2,subExpr3); } else if (UtilIan.hasNextChar(s,'<') ) { // a Paren UtilIan.nextChar(s,'<'); // Consume the opening '<' and continue. Expr subExpr1 = parse(s); UtilIan.nextChar(s,'>'); // consume the closing '>' return new Paren(subExpr1); } else if (UtilIan.hasNextChar(s,'|') ) { // a BinOp UtilIan.nextChar(s,'|'); // Consume the opening "<" and continue. Expr subExpr1 = parse(s); Expr subExpr2 = parse(s); String operator = UtilIan.nextSplittingBy(s,PUNCTUATION); // Either "xD" or ";". If ";", read one more punctuation-token: if (operator.equals(";")) { operator += UtilIan.nextSplittingBy(s,PUNCTUATION); } UtilIan.nextChar(s,'|'); // consume the closing '|' return new BinOp(subExpr1, subExpr2, operator ); } else { /* Unknown syntax! */ String tokens = ""; while (s.hasNext()) { tokens += s.next(); } throw new IllegalArgumentException( "Syntax error: Couldn't parse " + tokens ); } } /* Note that I removed some sanity-checks from 'parse', so that students can concentrate on the code/algorithm. E.g. assert UtilIan.hasNextSplittingBy(s,"is0",PUNCTUATION) : "Expected `is0` after `" + subExpr1.toString() + "`, got `" + UtilIan.nextSplittingBy(s,PUNCTUATION) + "`"; */ /** @override. Used for fingerprinting only. */ public int hashCode() { return super.hashCode()+(int)0x31476096bdd65159L; } }