Add node_handle alias suffix _h

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-26 21:29:50 +02:00
committed by CHatingPython
parent f130553e20
commit 86988ffea5
9 changed files with 74 additions and 58 deletions
+25 -19
View File
@@ -27,22 +27,11 @@ protected:
bool equal(const node& rhs) const override;
};
struct function_body {
location begin, end;
std::vector<node_handle<statement_node>> statements;
using declaration_node_h = node_handle<declaration_node>;
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<ast::function_body>;
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<function_declaration_node>;
struct function_body {
location begin, end;
std::vector<node_handle<statement_node>> 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<ast::function_body>;
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<function_definition_node>;
} // namespace ast
} // namespace furc
+6
View File
@@ -24,6 +24,8 @@ protected:
bool equal(const node& rhs) const override;
};
using literal_node_h = node_handle<literal_node>;
class string_literal_node : public literal_node {
public:
string_literal_node(handle<std::string_view>&& value)
@@ -40,6 +42,8 @@ private:
handle<std::string_view> m_value;
};
using string_literal_node_h = node_handle<string_literal_node>;
class integer_literal_node : public literal_node {
public:
integer_literal_node(handle<front::integer_token>&& value)
@@ -58,6 +62,8 @@ private:
handle<front::integer_token> m_value;
};
using integer_literal_node_h = node_handle<integer_literal_node>;
} // namespace ast
} // namespace furc
+2
View File
@@ -26,6 +26,8 @@ private:
std::vector<node_handle<declaration_node>> m_declarations;
};
using program_node_h = node_handle<program_node>;
} // namespace ast
} // namespace furc
+4
View File
@@ -21,6 +21,8 @@ protected:
bool equal(const node& rhs) const override;
};
using statement_node_h = node_handle<statement_node>;
class expression_node;
class return_statement_node : public statement_node {
public:
@@ -40,6 +42,8 @@ private:
node_handle<expression_node> m_value;
};
using return_statement_node_h = node_handle<return_statement_node>;
} // namespace ast
} // namespace furc
+6 -7
View File
@@ -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<ast::program_node> parse() &;
ast::program_node_h parse() &;
private:
ast::node_handle<ast::declaration_node> parse_declaration();
ast::node_handle<ast::statement_node> parse_statement();
ast::node_handle<ast::expression_node> parse_expression();
ast::node_handle<ast::literal_node> 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();
+5 -5
View File
@@ -57,16 +57,16 @@ bool declaration_node::equal(const node& rhs) const {
return declaration_type() == reinterpret_cast<const declaration_node&>(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<const function_declarartion_node&>(rhs).m_name;
bool function_declaration_node::equal(const node& rhs) const {
return declaration_node::equal(rhs) && m_name == reinterpret_cast<const function_declaration_node&>(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<const function_definition_node&>(rhs).m_body;
}
+2
View File
@@ -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();
}
+13 -16
View File
@@ -26,7 +26,7 @@ parser::parser(std::string_view filename)
m_lexer = { filename, m_content };
}
ast::node_handle<ast::program_node> parser::parse() & {
ast::program_node_h parser::parse() & {
auto program = m_arena.allocate_shared<ast::program_node>();
while (peek_token()->type != token_t::None && !m_lexer.empty()) {
@@ -36,7 +36,7 @@ ast::node_handle<ast::program_node> parser::parse() & {
return { location{ m_filename, 0, 0 }, program };
}
ast::node_handle<ast::declaration_node> 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<ast::declaration_node> 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<ast::function_definition_node>{ 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<ast::function_declarartion_node>{ 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<ast::declaration_node> parser::parse_declaration() {
}
}
ast::node_handle<ast::statement_node> 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<ast::statement_node> parser::parse_statement() {
auto tok = next_token();
if (peek_token()->type == token_t::Semicolon) {
next_token();
return ast::node_handle<ast::return_statement_node>{ 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<ast::return_statement_node>{ 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<ast::statement_node> parser::parse_statement() {
return { token.location(), "unexpected token "s + token->type + ", expected statement, declaration or expression" };
}
ast::node_handle<ast::expression_node> parser::parse_expression() {
ast::expression_node_h parser::parse_expression() {
return parse_expression_rhs(parse_expression_primary(), 16);
}
ast::node_handle<ast::literal_node> 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<ast::string_literal_node>{ tok.location(),
return ast::string_literal_node_h{ tok.location(),
m_arena,
handle<std::string_view>{ tok.location(), (*tok)->string } };
}
case token_t::Integer: {
auto tok = next_token();
return ast::node_handle<ast::integer_literal_node>{ tok.location(),
return ast::integer_literal_node_h{ tok.location(),
m_arena,
handle<integer_token>{ 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);
+11 -11
View File
@@ -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<function_definition_node> 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<function_declarartion_node> 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<function_definition_node> funcDef = test1;
function_definition_node_h funcDef = test1;
EXPECT_EQ(funcDef->name()->string, "test1");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
node_handle<return_statement_node> ret = funcDef->body()->statements[0];
return_statement_node_h ret = funcDef->body()->statements[0];
EXPECT_EQ(ret->value(), integer_literal_node({ furc::location{ "<TEMP>", 1, 26 }, 67 }));
}
{
auto test2 = program->declarations()[1];
EXPECT_TRUE(test2.present());
EXPECT_EQ(test2->declaration_type(), declaration_node_t::FunctionDefinition);
node_handle<function_definition_node> 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_node> literal = (expr); \
literal_node_h literal = (expr); \
EXPECT_EQ(literal->literal_type(), literal_node_t::Integer); \
node_handle<integer_literal_node> 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<function_definition_node> funcDef = func;
function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
node_handle<return_statement_node> 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<function_definition_node> funcDef = func;
function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
node_handle<return_statement_node> ret = funcDef->body()->statements[0];
return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value();
EXPECT_TRUE(retVal.present());