feat(furc): implement a basic parser

This commit is contained in:
2026-08-06 10:54:54 +02:00
parent 854343a5a3
commit db485af7f6
4 changed files with 343 additions and 14 deletions
+152
View File
@@ -0,0 +1,152 @@
#ifndef FURC_FRONT_AST_HPP
#define FURC_FRONT_AST_HPP
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
namespace furc {
struct ast_type {
enum type_e {
Void = 0,
S8,
U8,
S16,
U16,
S32,
U32,
S64,
U64,
} type = Void;
};
class ast_node {
public:
enum category_e {
Statement,
Declaration,
Expression,
Literal,
};
public:
ast_node() = default;
virtual ~ast_node() = default;
ast_node(ast_node&&) noexcept = default;
ast_node& operator=(ast_node&&) noexcept = default;
ast_node(const ast_node&) = delete;
ast_node& operator=(const ast_node&) = delete;
public:
virtual category_e category() const = 0;
};
using ast_node_cat = ast_node::category_e;
class stmt_node : public ast_node {
public:
enum stmt_type_e {
Declaration = 0,
Expression,
Compound,
};
public:
category_e category() const override { return ast_node_cat::Statement; }
virtual stmt_type_e stmt_type() const = 0;
};
struct comp_stmt_node final : public stmt_node {
stmt_type_e stmt_type() const override { return Compound; }
std::vector<stmt_node*> stmts;
};
class decl_node : public stmt_node {
public:
enum decl_type_e {
Variable,
Function
};
public:
category_e category() const override { return ast_node_cat::Declaration; }
stmt_type_e stmt_type() const override { return Declaration; }
virtual decl_type_e decl_type() const = 0;
};
class expr_node;
struct var_decl_node final : public decl_node {
decl_type_e decl_type() const override { return Variable; }
std::string name;
ast_type type;
expr_node* init = nullptr;
};
struct func_decl_node final : public decl_node {
decl_type_e decl_type() const override { return Function; }
std::string name;
ast_type type;
std::vector<var_decl_node> params;
std::optional<comp_stmt_node> body;
};
class expr_node : public stmt_node {
public:
enum expr_type_e {
Literal,
};
public:
category_e category() const override { return ast_node_cat::Expression; }
stmt_type_e stmt_type() const override { return Expression; }
virtual expr_type_e expr_type() const = 0;
};
class lit_node : public expr_node {
public:
enum lit_type_e {
Integer,
Char,
};
public:
category_e category() const override { return ast_node_cat::Literal; }
expr_type_e expr_type() const override { return Literal; }
virtual lit_type_e lit_type() const = 0;
};
struct int_lit_node final : public lit_node {
int_lit_node(std::uint64_t value)
: value(value) {}
lit_type_e lit_type() const override { return Integer; }
std::uint64_t value;
};
struct char_lit_node final : public lit_node {
char_lit_node(char value)
: value(value) {}
lit_type_e lit_type() const override { return Char; }
char value;
};
struct ast {
std::vector<decl_node*> decls;
};
} // namespace furc
#endif // FURC_FRONT_AST_HPP