From 0b9d7d9c335d182f221d1e6f1569b1c93fc8cd7d Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Mon, 7 Jul 2025 18:33:39 +0200 Subject: [PATCH] added return parsing --- src/ast/statements/return.cpp | 15 +++++++++++++++ src/ast/statements/return.hpp | 16 ++++++++++++++++ src/parser/parser.cpp | 11 +++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/ast/statements/return.cpp create mode 100644 src/ast/statements/return.hpp diff --git a/src/ast/statements/return.cpp b/src/ast/statements/return.cpp new file mode 100644 index 0000000..16a3cc4 --- /dev/null +++ b/src/ast/statements/return.cpp @@ -0,0 +1,15 @@ +#include "return.hpp" + +namespace ast { + return_stmt::return_stmt(token::token token) + : token(std::move(token)), + value(nullptr) {} + + std::string return_stmt::token_literal() const { + return token.literal; + } + + return_stmt::~return_stmt() { + delete value; + }; +} // namespace ast diff --git a/src/ast/statements/return.hpp b/src/ast/statements/return.hpp new file mode 100644 index 0000000..58747a5 --- /dev/null +++ b/src/ast/statements/return.hpp @@ -0,0 +1,16 @@ + +#include "ast/ast.hpp" +#include "token/token.hpp" + +namespace ast { + struct return_stmt : statement { + return_stmt(token::token token); + + token::token token; + expression* value; + + std::string token_literal() const override; + + ~return_stmt(); + }; +} // namespace ast diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index 69c35dc..03a1ed5 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -51,6 +51,17 @@ namespace parser { return false; } + ast::return_stmt* parser::parse_return() { + ast::return_stmt* stmt = new ast::return_stmt(current); + next_token(); + + // TODO: we are currently skipping expressions until we encounter a + // semicolon + for (; current.type != token::type::SEMICOLON; next_token()) {} + + return stmt; + } + ast::let_stmt* parser::parse_let() { ast::let_stmt* stmt = new ast::let_stmt(current);