1515 */
1616package org .teavm .ast ;
1717
18+ import java .util .Arrays ;
19+ import java .util .Collection ;
1820import java .util .Collections ;
1921import java .util .List ;
22+ import java .util .function .Function ;
2023
2124public abstract class Statement {
2225 public abstract void acceptVisitor (StatementVisitor visitor );
@@ -26,26 +29,61 @@ public static Statement empty() {
2629 }
2730
2831 public static AssignmentStatement assign (Expr left , Expr right ) {
29- AssignmentStatement stmt = new AssignmentStatement ();
32+ var stmt = new AssignmentStatement ();
3033 stmt .setLeftValue (left );
3134 stmt .setRightValue (right );
3235 return stmt ;
3336 }
3437
38+ public static AssignmentStatement statementExpr (Expr expr ) {
39+ var stmt = new AssignmentStatement ();
40+ stmt .setRightValue (expr );
41+ return stmt ;
42+ }
43+
44+ public static ReturnStatement exitFunction () {
45+ return exitFunction (null );
46+ }
47+
3548 public static ReturnStatement exitFunction (Expr result ) {
36- ReturnStatement stmt = new ReturnStatement ();
49+ var stmt = new ReturnStatement ();
3750 stmt .setResult (result );
3851 return stmt ;
3952 }
4053
54+ public static SequentialStatement sequence (Statement ... statements ) {
55+ var seq = new SequentialStatement ();
56+ seq .getSequence ().addAll (Arrays .asList (statements ));
57+ return seq ;
58+ }
59+
60+ public static BlockStatement block (Function <BlockStatement , Collection <Statement >> body ) {
61+ var statement = new BlockStatement ();
62+ statement .getBody ().addAll (body .apply (statement ));
63+ return statement ;
64+ }
65+
66+ public static BreakStatement exitBlock (IdentifiedStatement identifiedStatement ) {
67+ var statement = new BreakStatement ();
68+ statement .setTarget (identifiedStatement );
69+ return statement ;
70+ }
71+
72+ public static WhileStatement loopWhile (Expr condition , Function <WhileStatement , Collection <Statement >> body ) {
73+ var statement = new WhileStatement ();
74+ statement .setCondition (condition );
75+ statement .getBody ().addAll (body .apply (statement ));
76+ return statement ;
77+ }
78+
4179 public static ThrowStatement raiseException (Expr exception ) {
42- ThrowStatement stmt = new ThrowStatement ();
80+ var stmt = new ThrowStatement ();
4381 stmt .setException (exception );
4482 return stmt ;
4583 }
4684
4785 public static Statement cond (Expr predicate , List <Statement > consequent , List <Statement > alternative ) {
48- ConditionalStatement statement = new ConditionalStatement ();
86+ var statement = new ConditionalStatement ();
4987 statement .setCondition (predicate );
5088 statement .getConsequent ().addAll (consequent );
5189 statement .getAlternative ().addAll (alternative );
@@ -57,8 +95,61 @@ public static Statement cond(Expr predicate, List<Statement> consequent) {
5795 }
5896
5997 public static InitClassStatement initClass (String className ) {
60- InitClassStatement stmt = new InitClassStatement ();
98+ var stmt = new InitClassStatement ();
6199 stmt .setClassName (className );
62100 return stmt ;
63101 }
102+
103+ public static SwitchStatement switchStatement (Expr condition , List <Statement > defaultClause ,
104+ SwitchClause ... clauses ) {
105+ var statement = new SwitchStatement ();
106+ statement .setValue (condition );
107+ statement .getDefaultClause ().addAll (defaultClause );
108+ statement .getClauses ().addAll (List .of (clauses ));
109+ return statement ;
110+ }
111+
112+ public static SwitchClause switchClause (int [] conditions , Statement ... statements ) {
113+ var clause = new SwitchClause ();
114+ clause .setConditions (conditions );
115+ clause .getBody ().addAll (List .of (statements ));
116+ return clause ;
117+ }
118+
119+ public static SwitchClause switchClause (int condition , Statement ... statements ) {
120+ return switchClause (new int [] { condition }, statements );
121+ }
122+
123+ public static TryCatchStatementBuilder doTry (Statement ... statements ) {
124+ return new TryCatchStatementBuilder (statements );
125+ }
126+
127+ public static class TryCatchStatementBuilder {
128+ Statement [] protectedBody ;
129+
130+ TryCatchStatementBuilder (Statement [] protectedBody ) {
131+ this .protectedBody = protectedBody ;
132+ }
133+
134+ public TryCatchStatementBuilder2 doCatch (String type , Integer variable ) {
135+ var result = new TryCatchStatement ();
136+ result .getProtectedBody ().addAll (Arrays .asList (protectedBody ));
137+ result .setExceptionType (type );
138+ result .setExceptionVariable (variable );
139+ return new TryCatchStatementBuilder2 (result );
140+ }
141+ }
142+
143+ public static class TryCatchStatementBuilder2 {
144+ TryCatchStatement statement ;
145+
146+ TryCatchStatementBuilder2 (TryCatchStatement statement ) {
147+ this .statement = statement ;
148+ }
149+
150+ public TryCatchStatement with (Statement ... statements ) {
151+ statement .getHandler ().addAll (Arrays .asList (statements ));
152+ return statement ;
153+ }
154+ }
64155}
0 commit comments