| JExpression.java |
1 // Copyright 2012- 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 statements, but a semantic
7 * check throws some (those without a side-effect) out. Every expression has a type and a flag
8 * indicating whether or not it's a statement-expression.
9 */
10 abstract class JExpression extends JStatement {
11 /**
12 * Expression type.
13 */
14 protected Type type;
15
16 /**
17 * Whether or not this expression is a statement.
18 */
19 protected boolean isStatementExpression;
20
21 /**
22 * Constructs an AST node for an expression.
23 *
24 * @param line line in which the expression occurs in the source file.
25 */
26 protected JExpression(int line) {
27 super(line);
28 isStatementExpression = false; // by default
29 }
30
31 /**
32 * Returns the expression type.
33 *
34 * @return the expression type.
35 */
36 public Type type() {
37 return type;
38 }
39
40 /**
41 * Returns true if this expression is being used as a statement, and false otherwise.
42 *
43 * @return true if this expression is being used as a statement, and false otherwise.
44 */
45 public boolean isStatementExpression() {
46 return isStatementExpression;
47 }
48
49 /**
50 * Analyzes and returns a JExpression.
51 *
52 * @param context context in which names are resolved.
53 * @return the analyzed (and possibly rewritten) AST subtree.
54 */
55 public abstract JExpression analyze(Context context);
56
57 /**
58 * Performs short-circuit code generation for a boolean expression, given the code emitter,
59 * a target label, and whether we branch to that label on true or on false.
60 *
61 * @param output the code emitter.
62 * @param targetLabel the label to which we should branch.
63 * @param onTrue do we branch on true?
64 */
65 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
66 // We should never reach here, since all boolean (including identifier) expressions must
67 // override this method.
68 System.err.println("Error in short-circuit code generation");
69 }
70 }
71