/** class Expr, our internal representation of an expression * in the L0 language. * See http://www.radford.edu/itec380/2008fall/Hw06/hw06.html * * @author Ian Barland * @version 2008.Nov.30 */ public abstract class Expr { /** 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.hasNextChar(s,'(') ) {// a sum or product UtilIan.nextChar(s,'('); // Consume the opening "(" and continue. Expr subExpr1 = parse(s); String operator = UtilIan.nextSplittingBy(s,PUNCTUATION); Expr subExpr2 = parse(s); assert UtilIan.hasNextChar(s,')') : "Expected a ')' to finish the Expr " + operator.toString(); UtilIan.nextChar(s,')'); // Consume the trailing '}'. if (operator.equals(Sum.TOKEN)) { return new Sum( subExpr1, subExpr2 ); } else if (operator.equals(Prod.TOKEN)) { return new Prod( subExpr1, subExpr2 ); } else { /* Unknown syntax! */ String tokens = ""; while (s.hasNext()) { tokens += s.next(); } throw new IllegalArgumentException( "Couldn't parse " + tokens ); } } else if (UtilIan.hasNextSplittingBy(s,IfZero.TOKEN, PUNCTUATION)) { UtilIan.nextSplittingBy(s, PUNCTUATION); // consume the "ifZero" assert UtilIan.hasNextChar(s,'(') : "'(' must follow " + IfZero.TOKEN; UtilIan.nextChar(s,'('); // Consume the open-paren Expr subExpr1 = parse(s); assert UtilIan.hasNextChar(s,',') : "',' separate " + IfZero.TOKEN + " args."; UtilIan.nextChar(s,','); // Consume the open-paren Expr subExpr2 = parse(s); assert UtilIan.hasNextChar(s,',') : "',' separate " + IfZero.TOKEN + " args."; UtilIan.nextChar(s,','); // Consume the open-paren Expr subExpr3 = parse(s); assert UtilIan.hasNextChar(s,')') : "')' must close " + IfZero.TOKEN; UtilIan.nextChar(s,')'); // Consume the open-paren return new IfZero(subExpr1,subExpr2,subExpr3); } else { /* Unknown syntax! */ String tokens = ""; while (s.hasNext()) { tokens += s.next(); } throw new IllegalArgumentException( "Couldn't parse " + tokens ); } } /** Evaluate a given Expr. * @return the Value this Expr evaluates to. * (In L0, all values are numbers (doubles), but * in L3 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(); }