1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript.expr;
23
24 import org.millscript.commons.util.IList;
25 import org.millscript.commons.util.list.IArrayList;
26 import org.millscript.millscript.action.Action;
27 import org.millscript.millscript.alert.Alerts;
28 import org.millscript.millscript.vm.CompilerState;
29
30 /**
31 * This class implements a <code>case</code> expression. An individual case
32 * expression can match multiple patterns.
33 *
34 * @see org.millscript.millscript.syntax.SwitchSyntax
35 * @see org.millscript.millscript.action.SwitchAction
36 */
37 public final class CaseExpr extends Expr< Action > {
38
39 /**
40 * The set of patterns this case expression matches. An individual case
41 * expression can match multiple patterns.
42 */
43 private final IList< Expr< ? > > patterns;
44
45 /**
46 * The body expression for this case. This will be executed when one of the
47 * patterns is satisfied.
48 */
49 private final Expr< ? > body;
50
51 /**
52 * Creates a new <code>case</code> expression from the specified patterns
53 * and body expression.
54 *
55 * @param a a list of pattern expressions
56 * @param b the body expression
57 */
58 public CaseExpr( final IList< Expr > a, final Expr< ? > b ) {
59 final Expr< ? >[] unchecked = a.toArray( new Expr< ? >[ a.size() ] );
60
61
62 for ( int i = 0; i < unchecked.length; i++ ) {
63 unchecked[ i ] = CheckExpr.make( unchecked[ i ] );
64 }
65 this.patterns = new IArrayList< Expr< ? > >( unchecked, true );
66
67 this.body = b;
68 }
69
70 /**
71 * @see org.millscript.millscript.expr.Expr#compileIt()
72 */
73 @Override
74 public Action compileIt() {
75
76
77 throw(
78 Alerts.fault( "Trying to compile a naked CaseExpr" ).mishap()
79 );
80 }
81
82 /**
83 * Returns this case expressions body expression.
84 *
85 * @return this expressions body expression
86 */
87 public Expr< ? > getAct() {
88 return body;
89 }
90
91 /**
92 * Returns this case expressions patterns.
93 *
94 * @return an immutable list containing this expressions pattern
95 * expressions
96 */
97 public IList< Expr< ? > > getPatterns() {
98 return patterns;
99 }
100
101 /**
102 * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
103 */
104 @Override
105 public void resolve( final CompilerState state ) {
106 Expr.resolveList( state, this.patterns );
107 this.body.resolve( state );
108 }
109
110 /**
111 * @see org.millscript.millscript.expr.Expr#showComponents(int)
112 */
113 @Override
114 void showComponents( final int n ) {
115 for ( int i = 1; i <= this.patterns.size(); i++ ) {
116 this.patterns.get( i ).show( n );
117 }
118 this.body.show( n );
119 }
120
121 }