Merge pull request 'Types for furc' (#22) from furc-types into master
Reviewed-on: #22 Closes: #15
This commit was merged in pull request #22.
This commit is contained in:
@@ -5,10 +5,25 @@
|
||||
#include "furc/ast/statement.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace furc {
|
||||
namespace ast {
|
||||
|
||||
/**
|
||||
* @brief Type class for AST declarations.
|
||||
*/
|
||||
class type {
|
||||
public:
|
||||
template <typename NameFwd, typename = std::enable_if_t<std::is_constructible_v<std::string, NameFwd>>>
|
||||
type(NameFwd&& name)
|
||||
: m_name(std::forward<NameFwd>(name)) {}
|
||||
public:
|
||||
const std::string& name() const { return m_name; }
|
||||
private:
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Declaration node type.
|
||||
*/
|
||||
|
||||
@@ -71,6 +71,10 @@ using expression_node_p = node_p<expression_node>; /**< Alias for a shared point
|
||||
|
||||
using expression_node_r = node_r<expression_node>; /**< Alias for expression_node result */
|
||||
|
||||
class type;
|
||||
|
||||
using type_r = furlang::result<type, error>; /**< Alias for AST type result */
|
||||
|
||||
class declaration_node;
|
||||
|
||||
using declaration_node_p = node_p<declaration_node>; /**< Alias for a shared pointer to declaration_node. */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FURC_FRONT_PARSER_HPP
|
||||
#define FURC_FRONT_PARSER_HPP
|
||||
|
||||
#include "furc/ast/declaration.hpp"
|
||||
#include "furc/ast/fwd.hpp"
|
||||
#include "furc/front/lexer.hpp"
|
||||
#include "furlang/arena.hpp"
|
||||
@@ -56,6 +57,8 @@ public:
|
||||
*/
|
||||
ast::program_node_r parse() &;
|
||||
private:
|
||||
ast::type_r parse_type();
|
||||
|
||||
ast::declaration_node_r parse_declaration();
|
||||
ast::statement_node_r parse_statement();
|
||||
ast::expression_node_r parse_expression(std::uint32_t precedence = 16);
|
||||
|
||||
@@ -146,6 +146,8 @@ enum class keyword_token {
|
||||
If, /**< `if` */
|
||||
Else, /**< `else` */
|
||||
While, /**< `while` */
|
||||
|
||||
Int32, /**< `int32` */
|
||||
};
|
||||
|
||||
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
|
||||
@@ -156,6 +158,7 @@ static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword)
|
||||
case keyword_token::If: return os << "if";
|
||||
case keyword_token::Else: return os << "else";
|
||||
case keyword_token::While: return os << "while";
|
||||
case keyword_token::Int32: return os << "int32";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
@@ -168,6 +171,7 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
|
||||
case keyword_token::If: return str + "if";
|
||||
case keyword_token::Else: return str + "else";
|
||||
case keyword_token::While: return str + "while";
|
||||
case keyword_token::Int32: return str + "int32";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -100,6 +100,7 @@ token_r lexer::next_token() {
|
||||
{ "if", keyword_token::If },
|
||||
{ "else", keyword_token::Else },
|
||||
{ "while", keyword_token::While },
|
||||
{ "int32", keyword_token::Int32 },
|
||||
};
|
||||
|
||||
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second };
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "furc/ast/literal.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/program.hpp" // IWYU pragma: keep
|
||||
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
||||
#include "furc/front/token.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -46,6 +47,13 @@ ast::program_node_r parser::parse() & {
|
||||
return program != nullptr ? std::move(program) : ast::program_node_r(ast::error{ location{ m_filename } });
|
||||
}
|
||||
|
||||
ast::type_r parser::parse_type() {
|
||||
auto token = eat_token(token_t::Keyword);
|
||||
if (token.has_error() || token.value()->keyword != keyword_token::Int32)
|
||||
return ast::type_r(ast::error{ token.error().location });
|
||||
return ast::type("" + token.value()->keyword);
|
||||
}
|
||||
|
||||
ast::declaration_node_r parser::parse_declaration() {
|
||||
const auto& first = peek_token();
|
||||
switch (first->type) {
|
||||
|
||||
Reference in New Issue
Block a user