JStatementExpression.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 that appears as a statement. Only the 7 * expressions that have a side-effect are valid statement expressions. 8 */ 9 10 class JStatementExpression extends JStatement { 11 12 /** The expression. */ 13 JExpression expr; 14 15 /** 16 * Construct an AST node for a statement expression given its line number, 17 * and expression. 18 * 19 * @param line 20 * line in which the expression occurs in the source file. 21 * @param expr 22 * the expression. 23 */ 24 25 public JStatementExpression(int line, JExpression expr) { 26 super(line); 27 this.expr = expr; 28 } 29 30 /** 31 * Analysis involves analyzing the encapsulated expression if indeed it is a 32 * statement expression, i.e., one with a side effect. 33 * 34 * @param context 35 * context in which names are resolved. 36 * @return the analyzed (and possibly rewritten) AST subtree. 37 */ 38 39 public JStatement analyze(Context context) { 40 if (expr.isStatementExpression) { 41 expr = expr.analyze(context); 42 } 43 return this; 44 } 45 46 /** 47 * Generating code for the statement expression involves simply generating 48 * code for the encapsulated expression. 49 * 50 * @param output 51 * the code emitter (basically an abstraction for producing the 52 * .class file). 53 */ 54 55 public void codegen(CLEmitter output) { 56 expr.codegen(output); 57 } 58 59 /** 60 * @inheritDoc 61 */ 62 63 public void writeToStdOut(PrettyPrinter p) { 64 p.printf("<JStatementExpression line=\"%d\">\n", line()); 65 p.indentRight(); 66 expr.writeToStdOut(p); 67 p.indentLeft(); 68 p.printf("</JStatementExpression>\n"); 69 } 70 71 } 72