From 4a9deff8da89fa1cb4d7a61c7c73aa3154741d1a Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Tue, 15 Jul 2025 01:18:00 +0200 Subject: [PATCH] extracted the parsing of the function parameters into it's own function (will be useful for the parsing of the parameters in the function calls) --- src/parser/parser.cpp | 43 ++++++++++++++++++++++--------------------- src/parser/parser.hpp | 1 + 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index 53bb282..b0a831e 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -248,6 +248,27 @@ namespace parser { ); }; + std::vector parser::parse_function_parameters() { + std::vector ret; + while (next.type != token::type::RPAREN + && expect_next(token::type::IDENTIFIER)) { + ret.push_back(parse_identifier()); + if (next.type == token::type::RPAREN) + break; + if (!expect_next(token::type::COMMA)) + return {}; + } + if (current.type == token::type::COMMA + && next.type == token::type::RPAREN) { + next_error(token::type::IDENTIFIER); + return {}; + } + if (!expect_next(token::type::RPAREN)) + return {}; + + return ret; + } + ast::function_literal* parser::parse_function() { TRACE_FUNCTION; ast::function_literal* ret = new ast::function_literal(current); @@ -256,27 +277,7 @@ namespace parser { return nullptr; } - while (next.type != token::type::RPAREN - && expect_next(token::type::IDENTIFIER)) { - ret->parameters.push_back(parse_identifier()); - if (next.type == token::type::RPAREN) - break; - if (!expect_next(token::type::COMMA)) { - delete ret; - return nullptr; - } - } - if (current.type == token::type::COMMA - && next.type == token::type::RPAREN) { - next_error(token::type::IDENTIFIER); - delete ret; - return nullptr; - } - - if (!expect_next(token::type::RPAREN)) { - delete ret; - return nullptr; - } + ret->parameters = parse_function_parameters(); if (!expect_next(token::type::LBRACE)) { delete ret; diff --git a/src/parser/parser.hpp b/src/parser/parser.hpp index 6c35852..fb520e1 100644 --- a/src/parser/parser.hpp +++ b/src/parser/parser.hpp @@ -60,6 +60,7 @@ namespace parser { ast::identifier* parse_identifier(); ast::integer_literal* parse_integer(); ast::boolean_literal* parse_boolean(); + std::vector parse_function_parameters(); ast::function_literal* parse_function(); ast::prefix_expr* parse_prefix_expr(); ast::expression* parse_grouped_expr();