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.NoneAction;
24 import org.millscript.millscript.vm.CompilerState;
25
26 /**
27 * This class implements a <code>none</code> expression. This expression will
28 * never return any results.
29 *
30 * @see org.millscript.millscript.syntax.NoneSyntax
31 * @see NoneAction
32 */
33 public final class NoneExpr extends Expr< NoneAction > implements ZeroResults {
34
35 /**
36 * Returns the specified expression ensuring it will return zero results.
37 * This method provides some compile-time optimisation, as it avoids
38 * creating additional unnecessary objects in the expression tree. If the
39 * specified expression is already returns zero results, we simply return
40 * it, otherwise we proceed to wrap it in a new none expression.
41 *
42 * @param x the expression for which to discard results
43 * @return the specified expression if it returns zero results, or a new
44 * {@link NoneExpr} for the specified expression
45 */
46 public static Expr< ? > make( final Expr< ? > x ) {
47 return x.arity() == 0 ? x : new NoneExpr( x );
48 }
49
50 /**
51 * The expression to evaluate, but dispose of results.
52 */
53 private final Expr< ? > cont;
54
55 /**
56 * Creates a new <code>none</code> expression for the specified expression.
57 *
58 * @param x the expression to evaluate
59 */
60 public NoneExpr( final Expr< ? > x ) {
61 cont = x;
62 }
63
64 /**
65 * @see org.millscript.millscript.expr.Expr#compileIt()
66 */
67 @Override
68 public NoneAction compileIt() {
69 return new NoneAction( cont.compile() );
70 }
71
72 /**
73 * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
74 */
75 @Override
76 public void resolve( final CompilerState state ) {
77 this.cont.resolve( state );
78 }
79
80 /**
81 * @see org.millscript.millscript.expr.Expr#showComponents(int)
82 */
83 @Override
84 void showComponents( final int n ) {
85 this.cont.show( n );
86 }
87
88 }