@@ -42,7 +42,7 @@ public ASTStmtListNode parseStmtlist() throws Exception {
4242 Token curToken = m_lexer .lookAhead ();
4343 final List <ASTStmtNode > stmtList = new ArrayList <>();
4444
45- while (curToken .m_type != com .compiler .TokenIntf .Type .RBRACE ) {
45+ while (curToken .m_type != com .compiler .TokenIntf .Type .RBRACE && curToken . m_type != Type . CASE ) { // RBrace and Case in Follow set of Statementlist
4646 stmtList .add (parseStmt ());
4747 curToken = m_lexer .lookAhead ();
4848 }
@@ -81,6 +81,10 @@ public ASTStmtNode parseStmt() throws Exception {
8181 return parseNumericIfStmt ();
8282 }
8383
84+ if (type == Type .SWITCH ) {
85+ return parseSwitchStmt ();
86+ }
87+
8488 m_lexer .throwCompilerException ("Invalid begin of statement" , "DECLARE or IDENTIFIER or PRINT or NUMERIC_IF" );
8589 return null ; // unreachable
8690 }
@@ -230,4 +234,39 @@ ASTStmtNode parseExecuteNTimesStmt() throws Exception {
230234
231235 return new ASTExecuteNTimesNode (count , stmtlistNode );
232236 }
237+
238+ private ASTStmtNode parseSwitchStmt () throws Exception {
239+ // switch_statement -> SWITCH LPAREN expression RPAREN LBRACE case_list RBRACE
240+ m_lexer .expect (Type .SWITCH );
241+ m_lexer .expect (Type .LPAREN );
242+ ASTExprNode expression = m_exprParser .getQuestionMarkExpr ();
243+ m_lexer .expect (Type .RPAREN );
244+ m_lexer .expect (Type .LBRACE );
245+ ASTCaseListNode caseList = parseCaseList ();
246+ m_lexer .expect (Type .RBRACE );
247+ return new ASTSwitchStmtNode (expression , caseList );
248+ }
249+
250+ private ASTCaseListNode parseCaseList () throws Exception {
251+ // case_list -> case_item case_list | epsilon
252+ Token curToken = m_lexer .lookAhead ();
253+ final List <ASTCaseNode > caseList = new ArrayList <>();
254+
255+ while (curToken .m_type == Type .CASE ) {
256+ caseList .add (parseCaseStmt ());
257+ curToken = m_lexer .lookAhead ();
258+ }
259+ return new ASTCaseListNode (caseList );
260+ }
261+
262+ private ASTCaseNode parseCaseStmt () throws Exception {
263+ // case_item -> CASE LITERAL COLON statement_list
264+ m_lexer .expect (Type .CASE );
265+ Token curToken = m_lexer .lookAhead ();
266+ m_lexer .expect (Type .INTEGER );
267+ ASTIntegerLiteralNode value = new ASTIntegerLiteralNode (curToken .m_value ); //Integer
268+ m_lexer .expect (Type .DOUBLECOLON ); // should be renamed to COLON as double colon is "::"
269+ ASTStmtListNode stmtList = parseStmtlist ();
270+ return new ASTCaseNode (value , stmtList );
271+ }
233272}
0 commit comments