// Parse a statement. // // stmt -> block-stmt // | declaration-stmt // | expression-stmt Stmt* Parser::stmt() { switch (lookahead()) { case semicolon_tok: return empty_stmt(); case lbrace_tok: return block_stmt(); case return_kw: return return_stmt(); case if_kw: return if_stmt(); case while_kw: return while_stmt(); case break_kw: return break_stmt(); case continue_kw: return continue_stmt(); case var_kw: case def_kw: case foreign_kw: return declaration_stmt(); default: return expression_stmt(); } }
// Parse a method declaration. // // // method-decl -> 'def' identifier parameter-clause return-type function-definition // // Note that methods must be declared inside // the class. // // TODO: Support out-of-class definitions? // // TODO: Support specifiers to modify the "this" // parameter. Maybe before the return type? Maybe // as part of the specifiers? // // TODO: // // struct R { // const def f() -> void { } // Why not... Decl* Parser::method_decl(Specifier spec) { require(def_kw); Token n = match(identifier_tok); //check for a this_kw //do stuff //return on_ctor <- reference on_method // parameter-clause Decl_seq parms; match(lparen_tok); while (lookahead() != rparen_tok) { Decl* p = parameter_decl(); parms.push_back(p); if (match_if(comma_tok)) continue; else break; } match(rparen_tok); // return-type match(arrow_tok); Type const* t = type(); // function-definition. Stmt* s = block_stmt(); return on_method(spec, n, parms, t, s); }
// Parse a function declaration. // // function-decl -> 'def' identifier parameter-clause return-type ';' // | 'def' identifier parameter-clause return-type function-definition // parameter-clause -> '(' [parameter-list] ')' // // parameter-list -> parameter-decl | parameter-decl ',' parameter-list // // return-type -> '->' type // // function-definition -> block-stmt // // A function declaration may not have a definition. Decl* Parser::function_decl(Specifier spec) { require(def_kw); Token n = match(identifier_tok); // parameter-clause Decl_seq parms; match(lparen_tok); while (lookahead() != rparen_tok) { Decl* p = parameter_decl(); parms.push_back(p); if (match_if(comma_tok)) continue; else break; } match(rparen_tok); // return-type match(arrow_tok); Type const* t = type(); // function declaration if (match_if(semicolon_tok)) return on_function(spec, n, parms, t); // function-definition. Stmt* s = block_stmt(); return on_function(spec, n, parms, t, s); }
// NOTE ADD LAMDBA PARSER HERE Expr* Parser::lambda_expr() { require(bslash_tok); //Match the identifier inserted earlier Token n = match(identifier_tok); // parameter-clause Decl_seq parms; match(lparen_tok); while (lookahead() != rparen_tok) { Decl* p = parameter_decl(); parms.push_back(p); if (match_if(comma_tok)) { continue; } else break; } match(rparen_tok); // return-type match(arrow_tok); Type const* t = type(); // must be function-definition Stmt* s = block_stmt(); //return a lambda expression return on_lambda(n, parms, t, s); }