Add ast::node and handle<T*, Error> comparison

And parser test for literals fingerscrossed
This commit is good, innit? But there will also be second one, and dessert

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-26 14:11:28 +02:00
committed by CHatingPython
parent 4a58e97812
commit 48593a4350
12 changed files with 148 additions and 17 deletions
+2 -1
View File
@@ -27,7 +27,8 @@ Checks: >
-cppcoreguidelines-pro-bounds-constant-array-index, -cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-macro-usage, -cppcoreguidelines-macro-usage,
-cppcoreguidelines-owning-memory, -cppcoreguidelines-owning-memory,
-cppcoreguidelines-non-private-member-variables-in-classes -cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access
WarningsAsErrors: "*" WarningsAsErrors: "*"
+12
View File
@@ -23,11 +23,19 @@ public:
statement_node_t statement_type() const override { return statement_node_t::Declaration; } statement_node_t statement_type() const override { return statement_node_t::Declaration; }
virtual declaration_node_t declaration_type() const = 0; virtual declaration_node_t declaration_type() const = 0;
protected:
bool equal(const node& rhs) const override;
}; };
struct function_body { struct function_body {
location begin, end; location begin, end;
std::vector<node_handle<statement_node>> statements; 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_handle = handle<ast::function_body>; using function_body_handle = handle<ast::function_body>;
@@ -42,6 +50,8 @@ public:
front::token name() const { return m_name; } front::token name() const { return m_name; }
public: public:
std::ostream& print(std::ostream& os) const override; std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
protected: protected:
front::token m_name; front::token m_name;
}; };
@@ -56,6 +66,8 @@ public:
const function_body_handle& body() const { return m_body; } const function_body_handle& body() const { return m_body; }
public: public:
std::ostream& print(std::ostream& os) const override; std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private: private:
function_body_handle m_body; function_body_handle m_body;
}; };
+2
View File
@@ -18,6 +18,8 @@ public:
statement_node_t statement_type() const override { return statement_node_t::Expression; } statement_node_t statement_type() const override { return statement_node_t::Expression; }
virtual expression_node_t expression_type() const = 0; virtual expression_node_t expression_type() const = 0;
protected:
bool equal(const node& rhs) const override;
}; };
} // namespace ast } // namespace ast
+7 -4
View File
@@ -20,6 +20,8 @@ public:
expression_node_t expression_type() const override { return expression_node_t::Literal; } expression_node_t expression_type() const override { return expression_node_t::Literal; }
virtual literal_node_t literal_type() const = 0; virtual literal_node_t literal_type() const = 0;
protected:
bool equal(const node& rhs) const override;
}; };
class string_literal_node : public literal_node { class string_literal_node : public literal_node {
@@ -31,10 +33,9 @@ public:
const handle<std::string_view>& value() const { return m_value; } const handle<std::string_view>& value() const { return m_value; }
public: public:
std::ostream& print(std::ostream& os) const override { std::ostream& print(std::ostream& os) const override;
if (m_value.has_error()) return os << m_value.error(); protected:
return os << "string literal (" << *m_value << ")"; bool equal(const node& rhs) const override;
}
private: private:
handle<std::string_view> m_value; handle<std::string_view> m_value;
}; };
@@ -49,6 +50,8 @@ public:
const handle<front::integer_token>& value() const { return m_value; } const handle<front::integer_token>& value() const { return m_value; }
public: public:
std::ostream& print(std::ostream& os) const override; std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private: private:
handle<front::integer_token> m_value; handle<front::integer_token> m_value;
}; };
+6 -1
View File
@@ -37,10 +37,15 @@ public:
node& operator=(const node&) = delete; node& operator=(const node&) = delete;
public: public:
virtual node_t category() const = 0; virtual node_t category() const = 0;
public:
bool operator==(const node& rhs) const { return category() == rhs.category() && equal(rhs); }
bool operator!=(const node& rhs) const { return !this->operator==(rhs); }
public:
virtual std::ostream& print(std::ostream& os) const = 0; virtual std::ostream& print(std::ostream& os) const = 0;
friend std::ostream& operator<<(std::ostream& os, const node& node) { return node.print(os); } friend std::ostream& operator<<(std::ostream& os, const node& node) { return node.print(os); }
protected:
virtual bool equal(const node& rhs) const = 0;
}; };
template <typename T, typename Error = std::string> template <typename T, typename Error = std::string>
+2
View File
@@ -20,6 +20,8 @@ public:
const std::vector<node_handle<declaration_node>>& declarations() const { return m_declarations; } const std::vector<node_handle<declaration_node>>& declarations() const { return m_declarations; }
public: public:
std::ostream& print(std::ostream& os) const override; std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private: private:
std::vector<node_handle<declaration_node>> m_declarations; std::vector<node_handle<declaration_node>> m_declarations;
}; };
+6
View File
@@ -17,6 +17,8 @@ public:
node_t category() const override { return node_t::Statement; } node_t category() const override { return node_t::Statement; }
virtual statement_node_t statement_type() const = 0; virtual statement_node_t statement_type() const = 0;
protected:
bool equal(const node& rhs) const override;
}; };
class expression_node; class expression_node;
@@ -26,10 +28,14 @@ public:
return_statement_node(node_handle<expression_node>&& value) return_statement_node(node_handle<expression_node>&& value)
: m_value(std::move(value)) {} : m_value(std::move(value)) {}
public:
node_handle<expression_node> value() const { return m_value; }
public: public:
statement_node_t statement_type() const override { return statement_node_t::Return; } statement_node_t statement_type() const override { return statement_node_t::Return; }
std::ostream& print(std::ostream& os) const override; std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private: private:
node_handle<expression_node> m_value; node_handle<expression_node> m_value;
}; };
+6
View File
@@ -10,6 +10,12 @@ struct location {
std::string_view filename; std::string_view filename;
std::size_t line = 0; std::size_t line = 0;
std::size_t column = 0; std::size_t column = 0;
bool operator==(const location& rhs) const {
return filename == rhs.filename && line == rhs.line && column == rhs.column;
}
bool operator!=(const location& rhs) const { return !this->operator==(rhs); }
}; };
static inline std::ostream& operator<<(std::ostream& os, const location& location) { static inline std::ostream& operator<<(std::ostream& os, const location& location) {
+31 -10
View File
@@ -80,7 +80,16 @@ public:
pointer operator->() { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access) pointer operator->() { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
const_pointer operator->() const { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access) const_pointer operator->() const { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
public: public:
bool operator==(const T& rhs) const { return m_value.has_value() && *m_value == rhs; } bool operator==(const handle<T, Error>& rhs) const {
if (present() != rhs.present() || error() != rhs.error()) return false;
if (m_value.has_value() && m_value.value() != rhs.m_value.value()) // NOLINT(bugprone-unchecked-optional-access)
return false;
return true;
}
bool operator==(const T& rhs) const {
return m_value.has_value() && *m_value == rhs; // NOLINT(bugprone-unchecked-optional-access)
}
public: public:
friend std::ostream& operator<<(std::ostream& os, const handle& result) { friend std::ostream& operator<<(std::ostream& os, const handle& result) {
os << result.m_location << ": "; os << result.m_location << ": ";
@@ -167,13 +176,23 @@ public:
bool present() const { return m_data.has_value() && m_data->value != nullptr; } bool present() const { return m_data.has_value() && m_data->value != nullptr; }
bool has_error() const { return m_data.has_value() && m_data->value == nullptr; } bool has_error() const { return m_data.has_value() && m_data->value == nullptr; }
std::shared_ptr<value_type> shared() const { return m_data->value; } std::shared_ptr<value_type> shared() const { return m_data->value; } // NOLINT(bugprone-unchecked-optional-access)
Error error() const { return m_data->error; } Error error() const { return m_data->error; } // NOLINT(bugprone-unchecked-optional-access)
reference operator*() { return *m_data->value; } reference operator*() { return *m_data->value; } // NOLINT(bugprone-unchecked-optional-access)
const_reference operator*() const { return *m_data->value; } const_reference operator*() const { return *m_data->value; } // NOLINT(bugprone-unchecked-optional-access)
pointer operator->() { return m_data->value.get(); } pointer operator->() { return m_data->value.get(); } // NOLINT(bugprone-unchecked-optional-access)
const_pointer operator->() const { return m_data->value.get(); } const_pointer operator->() const { return m_data->value.get(); } // NOLINT(bugprone-unchecked-optional-access)
public:
bool operator==(const handle<T*, Error>& rhs) const {
if (present() != rhs.present() || error() != rhs.error()) return false;
if (present() && m_data->value != rhs.m_data->value) return false; // NOLINT(bugprone-unchecked-optional-access)
return true;
}
bool operator==(const value_type& rhs) const {
return present() && *m_data->value == rhs; // NOLINT(bugprone-unchecked-optional-access)
}
public: public:
friend std::ostream& operator<<(std::ostream& os, const handle& result) { friend std::ostream& operator<<(std::ostream& os, const handle& result) {
if (!result.m_data.has_value()) return os << "handle empty"; if (!result.m_data.has_value()) return os << "handle empty";
@@ -211,11 +230,13 @@ private:
template <typename U, template <typename U,
typename = std::enable_if_t<std::is_base_of_v<value_type, U> || std::is_base_of_v<U, value_type>>> typename = std::enable_if_t<std::is_base_of_v<value_type, U> || std::is_base_of_v<U, value_type>>>
data(const handle<U*, Error>& other) data(const handle<U*, Error>& other)
: location(other.m_data->location) { : location(other.m_data->location) { // NOLINT(bugprone-unchecked-optional-access)
if constexpr (std::is_base_of_v<value_type, U>) { if constexpr (std::is_base_of_v<value_type, U>) {
value = std::static_pointer_cast<value_type>(std::move(other.m_data->value)); value = std::static_pointer_cast<value_type>(
std::move(other.m_data->value)); // NOLINT(bugprone-unchecked-optional-access)
} else { } else {
value = std::dynamic_pointer_cast<value_type>(std::move(other.m_data->value)); value = std::dynamic_pointer_cast<value_type>(
std::move(other.m_data->value)); // NOLINT(bugprone-unchecked-optional-access)
} }
} }
+46
View File
@@ -8,15 +8,44 @@
namespace furc::ast { namespace furc::ast {
bool literal_node::equal(const node& rhs) const {
return literal_type() == reinterpret_cast<const literal_node&>(rhs).literal_type();
}
std::ostream& integer_literal_node::print(std::ostream& os) const { std::ostream& integer_literal_node::print(std::ostream& os) const {
if (m_value.has_error()) return os << m_value.error(); if (m_value.has_error()) return os << m_value.error();
return os << "integer literal (" << *m_value << ")"; return os << "integer literal (" << *m_value << ")";
} }
bool integer_literal_node::equal(const node& rhs) const {
return literal_node::equal(rhs) && m_value == reinterpret_cast<const integer_literal_node&>(rhs).m_value;
}
std::ostream& string_literal_node::print(std::ostream& os) const {
if (m_value.has_error()) return os << m_value.error();
return os << "string literal (" << *m_value << ")";
}
bool string_literal_node::equal(const node& rhs) const {
return literal_node::equal(rhs) && m_value == reinterpret_cast<const string_literal_node&>(rhs).m_value;
}
bool expression_node::equal(const node& rhs) const {
return expression_type() == reinterpret_cast<const expression_node&>(rhs).expression_type();
}
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_declarartion_node::print(std::ostream& os) const {
return os << "function " << m_name->string << " declaration"; 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;
}
std::ostream& function_definition_node::print(std::ostream& os) const { std::ostream& function_definition_node::print(std::ostream& os) const {
function_declarartion_node::print(os); function_declarartion_node::print(os);
os << ':'; os << ':';
@@ -28,12 +57,29 @@ std::ostream& function_definition_node::print(std::ostream& os) const {
return os << m_body.error(); // error return os << m_body.error(); // error
} }
bool function_definition_node::equal(const node& rhs) const {
return function_declarartion_node::equal(rhs) &&
m_body == reinterpret_cast<const function_definition_node&>(rhs).m_body;
}
bool statement_node::equal(const node& rhs) const {
return statement_type() == reinterpret_cast<const statement_node&>(rhs).statement_type();
}
std::ostream& return_statement_node::print(std::ostream& os) const { std::ostream& return_statement_node::print(std::ostream& os) const {
os << "return statement"; os << "return statement";
if (m_value.present()) return os << " (" << *m_value << ')'; if (m_value.present()) return os << " (" << *m_value << ')';
return os; return os;
} }
bool return_statement_node::equal(const node& rhs) const {
return statement_node::equal(rhs) && m_value == reinterpret_cast<const return_statement_node&>(rhs).m_value;
}
bool program_node::equal(const node& rhs) const {
return m_declarations == reinterpret_cast<const program_node&>(rhs).m_declarations;
}
std::ostream& program_node::print(std::ostream& os) const { std::ostream& program_node::print(std::ostream& os) const {
os << "program:"; os << "program:";
for (const auto& handle : m_declarations) { for (const auto& handle : m_declarations) {
+1 -1
View File
@@ -28,7 +28,7 @@ parser::parser(std::string_view filename)
ast::node_handle<ast::program_node> parser::parse() & { ast::node_handle<ast::program_node> parser::parse() & {
auto program = m_arena.allocate_shared<ast::program_node>(); auto program = m_arena.allocate_shared<ast::program_node>();
while (!m_lexer.empty()) { while (peek_token()->type != token_t::None && !m_lexer.empty()) {
program->push(std::move(parse_declaration())); program->push(std::move(parse_declaration()));
} }
+27
View File
@@ -30,4 +30,31 @@ TEST(Parser, EmptyFunctions) {
} }
} }
TEST(Parser, Literals) {
parser parser("<TEMP>", R"(
func test1() { return 67; }
func test2() { return "uwu"; }
)");
auto program = parser.parse();
EXPECT_TRUE(program.present());
EXPECT_EQ(program->declarations().size(), 2);
{
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;
EXPECT_EQ(funcDef->name()->string, "test1");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
node_handle<return_statement_node> 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;
EXPECT_EQ(funcDecl->name()->string, "test2");
}
}
} // namespace } // namespace