/** class Expr, our internal representation of an expression * in the S0 language. * See http://www.radford.edu/itec380/2017fall-ibarland/Homeworks/Project/ * * @author Ian Barland * @version 2016.Jul.15 */ import java.util.*; public abstract class Expr { /** Evaluate a given Expr. * @return the Value this Expr evaluates to. * (In S0, 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 = "\\?|!:/\\(\\)\\{\\}"; //as a regex // Note: Beware declaring '-' or '.' or '+' as punctuation -- that means // those chars can't appear as part of a number. /** 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.hasNextChar(s,'|') ) { // a Paren UtilIan.nextChar(s,'|'); // Consume the opening '|' and continue. Expr theInsideExpr = parse(s); UtilIan.nextChar(s,'|'); // consume the closing '|' return new Paren(theInsideExpr); } else if (UtilIan.hasNextChar(s,'?') ) { // a BinOp UtilIan.nextChar(s,'?'); // Consume the opening "?" and continue. String operator = UtilIan.nextSplittingBy(s,PUNCTUATION); if (!BinOp.OPS.contains(operator)) throw new InputMismatchException(String.format("Unknown operator \"%s\".",operator)); Expr lefty = parse(s); Expr righty = parse(s); return new BinOp(lefty, righty, operator ); } else if (UtilIan.hasNextChar(s, '(')) { UtilIan.nextChar(s, '('); // consume the "(" Expr theCond = parse(s); assert UtilIan.hasNextChar(s, ')') : "')' must close "+Ifnz.TOKEN; UtilIan.nextChar(s,')'); // consume the ")" UtilIan.nextChar(s,'!'); // consume the "!" UtilIan.nextChar(s,'0'); // consume the "0" Expr theNzBranch = parse(s); UtilIan.nextChar(s,':'); Expr theZBranch = parse(s); return new Ifnz(theCond,theNzBranch,theZBranch); } else { /* Unknown syntax! */ String tokens = ""; while (s.hasNext()) { tokens += s.next(); } throw new java.util.InputMismatchException( "Expr.parse: something has gone awry! Encountered \"" + 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)0x314d2ef361bcd159L; } }