1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.millscript.expr;
22
23 import org.millscript.millscript.action.NotAction;
24 import org.millscript.millscript.vm.CompilerState;
25
26 /**
27 * This class implements a <code>not</code> expression. This expression always
28 * returns one result.
29 *
30 * @see org.millscript.millscript.syntax.NotSyntax
31 * @see NotAction
32 */
33 public final class NotExpr extends Expr< NotAction > implements OneResult {
34
35 /**
36 * Returns an expression which negates the specified expression. This method
37 * provides some compile-time optimisation, as it avoids creating additional
38 * unnecessary objects in the expression tree. If the specified expression
39 * is already a not expression, we return it's expression to be negated.
40 * Otherwise we return a new not expression.
41 *
42 * @param x the expression whose result to be negated
43 * @return a {@link NotExpr} if the specified expression is not already
44 * {@link NotExpr}, otherwise we return the specified expressions
45 * body expression.
46 */
47 public static Expr< ? > make( final Expr x ) {
48
49
50
51 if ( x instanceof NotExpr ) {
52
53 return ((NotExpr) x).getCont();
54 } else {
55
56 return new NotExpr( x );
57 }
58 }
59
60 /**
61 * The expression whose result should be negated.
62 */
63 private final Expr< ? > cont;
64
65 /**
66 * Creates a new <code>not</code> expression to negate the result of the
67 * specified expression.
68 *
69 * @param x the expression whose result to negate
70 */
71 public NotExpr( final Expr< ? > x ) {
72 cont = CheckExpr.make( x );
73 }
74
75 /**
76 * Returns this instances expression to be negated.
77 *
78 * @return this instances expression to be negated.
79 */
80 Expr< ? > getCont() {
81 return cont;
82 }
83
84 /**
85 * @see org.millscript.millscript.expr.Expr#compileIt()
86 */
87 @Override
88 public NotAction compileIt() {
89 return new NotAction( cont.compile() );
90 }
91
92 /**
93 * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
94 */
95 @Override
96 public void resolve( final CompilerState state ) {
97 this.cont.resolve( state );
98 }
99
100 /**
101 * @see org.millscript.millscript.expr.Expr#showComponents(int)
102 */
103 @Override
104 void showComponents( final int n ) {
105 this.cont.show( n );
106 }
107
108 }