From 86988ffea5f9f282b24f9e25b9906362b66a6f9a Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Tue, 26 May 2026 21:29:50 +0200 Subject: [PATCH] Add node_handle alias suffix _h Signed-off-by: CHatingPython --- furc/include/furc/ast/declaration.hpp | 44 +++++++++++++++------------ furc/include/furc/ast/literal.hpp | 6 ++++ furc/include/furc/ast/program.hpp | 2 ++ furc/include/furc/ast/statement.hpp | 4 +++ furc/include/furc/front/parser.hpp | 13 ++++---- furc/src/ast.cpp | 10 +++--- furc/src/front/lexer.cpp | 2 ++ furc/src/front/parser.cpp | 29 ++++++++---------- furc/test/parser.cpp | 22 +++++++------- 9 files changed, 74 insertions(+), 58 deletions(-) diff --git a/furc/include/furc/ast/declaration.hpp b/furc/include/furc/ast/declaration.hpp index 2e758ce..85b9ef3 100644 --- a/furc/include/furc/ast/declaration.hpp +++ b/furc/include/furc/ast/declaration.hpp @@ -27,22 +27,11 @@ protected: bool equal(const node& rhs) const override; }; -struct function_body { - location begin, end; - std::vector> statements; +using declaration_node_h = node_handle; - bool operator==(const function_body& rhs) const { - return begin == rhs.begin && end == rhs.end && statements == rhs.statements; - } - - bool operator!=(const function_body& rhs) const { return !this->operator==(rhs); } -}; - -using function_body_handle = handle; - -class function_declarartion_node : public declaration_node { +class function_declaration_node : public declaration_node { public: - function_declarartion_node(front::token name) + function_declaration_node(front::token name) : m_name(name) {} public: declaration_node_t declaration_type() const override { return declaration_node_t::FunctionDeclaration; } @@ -56,22 +45,39 @@ protected: front::token m_name; }; -class function_definition_node : public function_declarartion_node { +using function_declaration_node_h = node_handle; + +struct function_body { + location begin, end; + std::vector> statements; + + bool operator==(const function_body& rhs) const { + return begin == rhs.begin && end == rhs.end && statements == rhs.statements; + } + + bool operator!=(const function_body& rhs) const { return !this->operator==(rhs); } +}; + +using function_body_h = handle; + +class function_definition_node : public function_declaration_node { public: - function_definition_node(front::token name, function_body_handle&& body) - : function_declarartion_node(name), m_body(std::move(body)) {} + function_definition_node(front::token name, function_body_h&& body) + : function_declaration_node(name), m_body(std::move(body)) {} public: declaration_node_t declaration_type() const override { return declaration_node_t::FunctionDefinition; } - const function_body_handle& body() const { return m_body; } + const function_body_h& body() const { return m_body; } public: std::ostream& print(std::ostream& os) const override; protected: bool equal(const node& rhs) const override; private: - function_body_handle m_body; + function_body_h m_body; }; +using function_definition_node_h = node_handle; + } // namespace ast } // namespace furc diff --git a/furc/include/furc/ast/literal.hpp b/furc/include/furc/ast/literal.hpp index 1938ee7..244ee52 100644 --- a/furc/include/furc/ast/literal.hpp +++ b/furc/include/furc/ast/literal.hpp @@ -24,6 +24,8 @@ protected: bool equal(const node& rhs) const override; }; +using literal_node_h = node_handle; + class string_literal_node : public literal_node { public: string_literal_node(handle&& value) @@ -40,6 +42,8 @@ private: handle m_value; }; +using string_literal_node_h = node_handle; + class integer_literal_node : public literal_node { public: integer_literal_node(handle&& value) @@ -58,6 +62,8 @@ private: handle m_value; }; +using integer_literal_node_h = node_handle; + } // namespace ast } // namespace furc diff --git a/furc/include/furc/ast/program.hpp b/furc/include/furc/ast/program.hpp index 1f5b137..82aad36 100644 --- a/furc/include/furc/ast/program.hpp +++ b/furc/include/furc/ast/program.hpp @@ -26,6 +26,8 @@ private: std::vector> m_declarations; }; +using program_node_h = node_handle; + } // namespace ast } // namespace furc diff --git a/furc/include/furc/ast/statement.hpp b/furc/include/furc/ast/statement.hpp index 160b385..eb6b1d1 100644 --- a/furc/include/furc/ast/statement.hpp +++ b/furc/include/furc/ast/statement.hpp @@ -21,6 +21,8 @@ protected: bool equal(const node& rhs) const override; }; +using statement_node_h = node_handle; + class expression_node; class return_statement_node : public statement_node { public: @@ -40,6 +42,8 @@ private: node_handle m_value; }; +using return_statement_node_h = node_handle; + } // namespace ast } // namespace furc diff --git a/furc/include/furc/front/parser.hpp b/furc/include/furc/front/parser.hpp index f7f9e9b..c2645d1 100644 --- a/furc/include/furc/front/parser.hpp +++ b/furc/include/furc/front/parser.hpp @@ -4,7 +4,6 @@ #include "furc/ast/declaration.hpp" #include "furc/ast/expression.hpp" #include "furc/ast/literal.hpp" -#include "furc/ast/node.hpp" #include "furc/ast/program.hpp" #include "furc/front/lexer.hpp" #include "furlang/arena.hpp" @@ -26,17 +25,17 @@ public: parser& operator=(const parser&) = delete; public: // parser owns the arena :3c - ast::node_handle parse() &; + ast::program_node_h parse() &; private: - ast::node_handle parse_declaration(); - ast::node_handle parse_statement(); - ast::node_handle parse_expression(); - ast::node_handle parse_literal(); + ast::declaration_node_h parse_declaration(); + ast::statement_node_h parse_statement(); + ast::expression_node_h parse_expression(); + ast::literal_node_h parse_literal(); ast::expression_node_h parse_expression_primary(); ast::expression_node_h parse_expression_rhs(const ast::expression_node_h& init, std::uint32_t precedence); - ast::function_body_handle parse_body(); + ast::function_body_h parse_body(); private: token_handle<> next_token(); const token_handle<>& peek_token(); diff --git a/furc/src/ast.cpp b/furc/src/ast.cpp index 3fb956c..e11e19a 100644 --- a/furc/src/ast.cpp +++ b/furc/src/ast.cpp @@ -57,16 +57,16 @@ bool declaration_node::equal(const node& rhs) const { return declaration_type() == reinterpret_cast(rhs).declaration_type(); } -std::ostream& function_declarartion_node::print(std::ostream& os) const { +std::ostream& function_declaration_node::print(std::ostream& os) const { return os << "function " << m_name->string << " declaration"; } -bool function_declarartion_node::equal(const node& rhs) const { - return declaration_node::equal(rhs) && m_name == reinterpret_cast(rhs).m_name; +bool function_declaration_node::equal(const node& rhs) const { + return declaration_node::equal(rhs) && m_name == reinterpret_cast(rhs).m_name; } std::ostream& function_definition_node::print(std::ostream& os) const { - function_declarartion_node::print(os); + function_declaration_node::print(os); os << ':'; if (m_body.present()) { for (const auto& entry : m_body->statements) @@ -77,7 +77,7 @@ std::ostream& function_definition_node::print(std::ostream& os) const { } bool function_definition_node::equal(const node& rhs) const { - return function_declarartion_node::equal(rhs) && + return function_declaration_node::equal(rhs) && m_body == reinterpret_cast(rhs).m_body; } diff --git a/furc/src/front/lexer.cpp b/furc/src/front/lexer.cpp index 7ba5e7e..847187c 100644 --- a/furc/src/front/lexer.cpp +++ b/furc/src/front/lexer.cpp @@ -37,6 +37,8 @@ token_handle<> lexer::next_token() { return { current_location(), "unexpected end of file before enclosing `*/`" }; } m_cursor += 2; + } else { + break; } skip_spaces(); } diff --git a/furc/src/front/parser.cpp b/furc/src/front/parser.cpp index 7e2b261..f0d080b 100644 --- a/furc/src/front/parser.cpp +++ b/furc/src/front/parser.cpp @@ -26,7 +26,7 @@ parser::parser(std::string_view filename) m_lexer = { filename, m_content }; } -ast::node_handle parser::parse() & { +ast::program_node_h parser::parse() & { auto program = m_arena.allocate_shared(); while (peek_token()->type != token_t::None && !m_lexer.empty()) { @@ -36,7 +36,7 @@ ast::node_handle parser::parse() & { return { location{ m_filename, 0, 0 }, program }; } -ast::node_handle parser::parse_declaration() { +ast::declaration_node_h parser::parse_declaration() { const auto& first = peek_token(); switch (first->type) { case token_t::Keyword: { @@ -55,16 +55,13 @@ ast::node_handle parser::parse_declaration() { if (peek.has_error()) return peek; switch (peek->type) { case token_t::Lbrace: { - ast::function_body_handle body = parse_body(); + ast::function_body_h body = parse_body(); if (body.has_error()) return body; - return ast::node_handle{ first.location(), - m_arena, - *name, - std::move(body) }; + return ast::function_definition_node_h{ first.location(), m_arena, *name, std::move(body) }; } case token_t::Semicolon: { m_peekBuffer.clear(); - return ast::node_handle{ first.location(), m_arena, *name }; + return ast::function_declaration_node_h{ first.location(), m_arena, *name }; } default: return { tok.location(), "unexpected token "s + tok->type }; } @@ -89,7 +86,7 @@ ast::node_handle parser::parse_declaration() { } } -ast::node_handle parser::parse_statement() { +ast::statement_node_h parser::parse_statement() { const auto& tok = peek_token(); if (tok.has_error()) return tok; switch (tok->type) { @@ -97,13 +94,13 @@ ast::node_handle parser::parse_statement() { auto tok = next_token(); if (peek_token()->type == token_t::Semicolon) { next_token(); - return ast::node_handle{ tok.location(), m_arena }; + return ast::return_statement_node_h{ tok.location(), m_arena }; } auto value = parse_expression(); auto err = eat_token(token_t::Semicolon); if (err.has_error()) return err; - return ast::node_handle{ tok.location(), m_arena, std::move(value) }; + return ast::return_statement_node_h{ tok.location(), m_arena, std::move(value) }; } default: break; } @@ -121,22 +118,22 @@ ast::node_handle parser::parse_statement() { return { token.location(), "unexpected token "s + token->type + ", expected statement, declaration or expression" }; } -ast::node_handle parser::parse_expression() { +ast::expression_node_h parser::parse_expression() { return parse_expression_rhs(parse_expression_primary(), 16); } -ast::node_handle parser::parse_literal() { +ast::literal_node_h parser::parse_literal() { const auto& tok = peek_token(); switch (tok->type) { case token_t::String: { auto tok = next_token(); - return ast::node_handle{ tok.location(), + return ast::string_literal_node_h{ tok.location(), m_arena, handle{ tok.location(), (*tok)->string } }; } case token_t::Integer: { auto tok = next_token(); - return ast::node_handle{ tok.location(), + return ast::integer_literal_node_h{ tok.location(), m_arena, handle{ tok.location(), (*tok)->integer } }; } @@ -204,7 +201,7 @@ ast::expression_node_h parser::parse_expression_rhs(const ast::expression_node_h } } -ast::function_body_handle parser::parse_body() { +ast::function_body_h parser::parse_body() { ast::function_body body; token_handle<> begin = eat_token(token_t::Lbrace); diff --git a/furc/test/parser.cpp b/furc/test/parser.cpp index 172f93a..310cdb6 100644 --- a/furc/test/parser.cpp +++ b/furc/test/parser.cpp @@ -17,7 +17,7 @@ TEST(Parser, EmptyFunctions) { auto first = program->declarations()[0]; EXPECT_TRUE(first.present()); EXPECT_EQ(first->declaration_type(), declaration_node_t::FunctionDefinition); - node_handle funcDef = first; + function_definition_node_h funcDef = first; EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->body()->statements.size(), 0); } @@ -25,7 +25,7 @@ TEST(Parser, EmptyFunctions) { auto second = program->declarations()[1]; EXPECT_TRUE(second.present()); EXPECT_EQ(second->declaration_type(), declaration_node_t::FunctionDeclaration); - node_handle funcDecl = second; + function_declaration_node_h funcDecl = second; EXPECT_EQ(funcDecl->name()->string, "foo"); } } @@ -42,17 +42,17 @@ TEST(Parser, Literals) { auto test1 = program->declarations()[0]; EXPECT_TRUE(test1.present()); EXPECT_EQ(test1->declaration_type(), declaration_node_t::FunctionDefinition); - node_handle funcDef = test1; + function_definition_node_h funcDef = test1; EXPECT_EQ(funcDef->name()->string, "test1"); EXPECT_EQ(funcDef->body()->statements.size(), 1); - node_handle ret = funcDef->body()->statements[0]; + return_statement_node_h ret = funcDef->body()->statements[0]; EXPECT_EQ(ret->value(), integer_literal_node({ furc::location{ "", 1, 26 }, 67 })); } { auto test2 = program->declarations()[1]; EXPECT_TRUE(test2.present()); EXPECT_EQ(test2->declaration_type(), declaration_node_t::FunctionDefinition); - node_handle funcDecl = test2; + function_definition_node_h funcDecl = test2; EXPECT_EQ(funcDecl->name()->string, "test2"); } } @@ -60,9 +60,9 @@ TEST(Parser, Literals) { #define EXPECT_INTLIT(expr, integer) \ do { \ EXPECT_EQ((expr)->expression_type(), expression_node_t::Literal); \ - node_handle literal = (expr); \ + literal_node_h literal = (expr); \ EXPECT_EQ(literal->literal_type(), literal_node_t::Integer); \ - node_handle intLit = literal; \ + integer_literal_node_h intLit = literal; \ EXPECT_EQ(*intLit, (integer)); \ } while (0); @@ -75,10 +75,10 @@ TEST(Parser, OperatorPrecedence_AddMul) { auto func = program->declarations()[0]; EXPECT_TRUE(func.present()); EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); - node_handle funcDef = func; + function_definition_node_h funcDef = func; EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->body()->statements.size(), 1); - node_handle ret = funcDef->body()->statements[0]; + return_statement_node_h ret = funcDef->body()->statements[0]; auto retVal = ret->value(); EXPECT_TRUE(retVal.present()); @@ -103,10 +103,10 @@ TEST(Parser, OperatorPrecedence_Complex) { auto func = program->declarations()[0]; EXPECT_TRUE(func.present()); EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition); - node_handle funcDef = func; + function_definition_node_h funcDef = func; EXPECT_EQ(funcDef->name()->string, "main"); EXPECT_EQ(funcDef->body()->statements.size(), 1); - node_handle ret = funcDef->body()->statements[0]; + return_statement_node_h ret = funcDef->body()->statements[0]; auto retVal = ret->value(); EXPECT_TRUE(retVal.present());