JExpression.java |
1 // Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas 2 3 package jminusminus; 4 5 /** 6 * The AST node for an expression. The syntax says all expressions are 7 * statements, but a semantic check throws some (those without a side-effect) 8 * out. 9 * 10 * Every expression has a type and a flag saying whether or not it's a 11 * statement-expression. 12 */ 13 14 abstract class JExpression extends JStatement { 15 16 /** Expression type. */ 17 protected Type type; 18 19 /** Whether or not this expression is a statement. */ 20 protected boolean isStatementExpression; 21 22 /** 23 * Construct an AST node for an expression given its line number. 24 * 25 * @param line 26 * line in which the expression occurs in the source file. 27 */ 28 29 protected JExpression(int line) { 30 super(line); 31 isStatementExpression = false; // by default 32 } 33 34 /** 35 * Return the expression type. 36 * 37 * @return the expression type. 38 */ 39 40 public Type type() { 41 return type; 42 } 43 44 /** 45 * Is this a statementRxpression? 46 * 47 * @return whether or not this is being used as a statement. 48 */ 49 50 public boolean isStatementExpression() { 51 return isStatementExpression; 52 } 53 54 /** 55 * The analysis of any JExpression returns a JExpression. That's all this 56 * (re-)declaration of analyze() says. 57 * 58 * @param context 59 * context in which names are resolved. 60 * @return the analyzed (and possibly rewritten) AST subtree. 61 */ 62 63 public abstract JExpression analyze(Context context); 64 65 /** 66 * Perform (short-circuit) code generation for a boolean expression, given 67 * the code emitter, a target label, and whether we branch to that label on 68 * true or on false. 69 * 70 * @param output 71 * the code emitter (basically an abstraction for producing the 72 * .class file). 73 * @param targetLabel 74 * the label to which we should branch. 75 * @param onTrue 76 * do we branch on true? 77 */ 78 79 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) { 80 // We should never reach here, i.e., all boolean 81 // (including 82 // identifier) expressions must override this method. 83 System.err.println("Error in code generation"); 84 } 85 86 } 87