@@ -92,39 +92,52 @@ public ASTStmtNode parseStmt() throws Exception {
9292 }
9393
9494 public ASTStmtNode parseFunctionStmt () throws Exception {
95+ // functionDecl: FUNCTION IDENTIFIER RPAREN paramList RPAREN LBRACE functionBody RBRACE SEMICOLON
9596 m_lexer .expect (Type .FUNCTION );
9697 String functionName = m_lexer .m_currentToken .m_value ;
98+
9799 m_lexer .expect (TokenIntf .Type .IDENT );
98100 m_lexer .expect (Type .LPAREN );
101+
99102 List <String > parameterList = parseParameterList ();
100- parameterList .forEach (e -> m_symbolTable . createSymbol ( e ) );
101- m_lexer . expect ( TokenIntf . Type . RPAREN );
103+ parameterList .forEach (m_symbolTable :: createSymbol );
104+ m_functionTable . createFunction ( functionName , parameterList );
102105
106+ m_lexer .expect (TokenIntf .Type .RPAREN );
103107 m_lexer .expect (TokenIntf .Type .LBRACE );
108+
104109 List <ASTStmtNode > functionBody = parseFunctionBody ();
110+
105111 m_lexer .expect (TokenIntf .Type .RBRACE );
106112 m_lexer .expect (Type .SEMICOLON );
107- m_functionTable . createFunction ( functionName , parameterList );
113+
108114 return new ASTFunctionStmtNode (functionName , parameterList , functionBody );
109115 }
110116
111117 private List <ASTStmtNode > parseFunctionBody () throws Exception {
118+ // functionBody: returnStmt | stmt functionBody
112119 List <ASTStmtNode > stmtList = new ArrayList <>();
113120 while (m_lexer .m_currentToken .m_type != Type .RETURN ){
121+ if (m_lexer .m_currentToken .m_type == Type .RBRACE ){
122+ m_lexer .throwCompilerException ("Invalid end of function body" , "RETURN" );
123+ }
114124 stmtList .add (parseStmt ());
115125 }
116126 stmtList .add (parseReturnStmt ());
117127 return stmtList ;
118128 }
119129
120130 private ASTStmtNode parseReturnStmt () throws Exception {
131+ // returnStmt: RETURN expr
121132 m_lexer .expect (Type .RETURN );
122133 ASTStmtNode returnStmtNode = new ASTReturnStmtNode (m_exprParser .getQuestionMarkExpr ());
123134 m_lexer .expect (Type .SEMICOLON );
124135 return returnStmtNode ;
125136 }
126137
127138 private List <String > parseParameterList () throws Exception {
139+ // paramList: IDENTIFIER paramListPos | eps
140+ // paramListPost: eps | COMMA IDENFIER paramListPost
128141 List <String > parameterList = new ArrayList <>();
129142 if (m_lexer .m_currentToken .m_type != Type .IDENT ){
130143 return parameterList ;
@@ -143,7 +156,6 @@ private List<String> parseParameterList() throws Exception {
143156
144157 public ASTStmtNode parsePrintStmt () throws Exception {
145158 m_lexer .expect (Type .PRINT );
146-
147159 ASTPrintStmtNode astPrintStmtNode = new ASTPrintStmtNode (m_exprParser .getQuestionMarkExpr ());
148160 m_lexer .expect (Type .SEMICOLON );
149161 return astPrintStmtNode ;
0 commit comments