Add integers to lexer
Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
@@ -42,7 +42,9 @@ public:
|
||||
|
||||
front::token name() const { return m_name; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override { return os << "function " << m_name.value << " declaration"; }
|
||||
std::ostream& print(std::ostream& os) const override {
|
||||
return os << "function " << m_name->string << " declaration";
|
||||
}
|
||||
protected:
|
||||
front::token m_name;
|
||||
};
|
||||
@@ -73,13 +75,14 @@ public:
|
||||
const function_body_handle& body() const { return m_body; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override {
|
||||
os << "function " << m_name.value << " definition:";
|
||||
function_declarartion_node::print(os);
|
||||
os << ':';
|
||||
if (m_body.present()) {
|
||||
for (const auto& entry : m_body->statements)
|
||||
os << '\n' << entry;
|
||||
return os << '\n' << m_body->end << ": " << m_name.value << " end";
|
||||
return os << '\n' << m_body->end << ": " << m_name->string << " end";
|
||||
}
|
||||
return os << (std::string)m_body; // error
|
||||
return os << m_body.error(); // error
|
||||
}
|
||||
private:
|
||||
function_body_handle m_body;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "furc/ast/expression.hpp"
|
||||
#include "furc/ast/node.hpp"
|
||||
#include "furc/front/token.hpp"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
@@ -14,13 +15,6 @@ enum class literal_node_t {
|
||||
Integer
|
||||
};
|
||||
|
||||
static inline std::ostream& operator<<(std::ostream& os, literal_node_t type) {
|
||||
switch (type) {
|
||||
case literal_node_t::String: return os << "string";
|
||||
case literal_node_t::Integer: return os << "integer";
|
||||
}
|
||||
}
|
||||
|
||||
class literal_node : public expression_node {
|
||||
public:
|
||||
node_t category() const override { return node_t::Literal; }
|
||||
@@ -40,8 +34,8 @@ public:
|
||||
const handle<std::string_view>& value() const { return m_value; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override {
|
||||
if (m_value.error()) return os << (std::string)m_value;
|
||||
return os << '"' << *m_value << '"';
|
||||
if (m_value.has_error()) return os << (std::string)m_value;
|
||||
return os << "string literal (" << *m_value << ")";
|
||||
}
|
||||
private:
|
||||
handle<std::string_view> m_value;
|
||||
@@ -49,14 +43,19 @@ private:
|
||||
|
||||
class integer_literal_node : public literal_node {
|
||||
public:
|
||||
integer_literal_node(handle<std::uint64_t>&& value)
|
||||
integer_literal_node(handle<front::integer_token>&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
public:
|
||||
literal_node_t literal_type() const override { return literal_node_t::Integer; }
|
||||
|
||||
const handle<std::uint64_t>& value() const { return m_value; }
|
||||
const handle<front::integer_token>& value() const { return m_value; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override {
|
||||
if (m_value.has_error()) return os << m_value.error();
|
||||
return os << "integer literal (" << *m_value << ")";
|
||||
}
|
||||
private:
|
||||
handle<std::uint64_t> m_value;
|
||||
handle<front::integer_token> m_value;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -13,8 +13,8 @@ namespace front {
|
||||
enum class token_t {
|
||||
None,
|
||||
Identifier,
|
||||
Keyword,
|
||||
String,
|
||||
Keyword,
|
||||
Integer,
|
||||
Lparen,
|
||||
Rparen,
|
||||
@@ -32,8 +32,8 @@ static inline std::ostream& operator<<(std::ostream& os, token_t type) {
|
||||
switch (type) {
|
||||
case token_t::None: return os << "none";
|
||||
case token_t::Identifier: return os << "identifier";
|
||||
case token_t::Keyword: return os << "keyword";
|
||||
case token_t::String: return os << "string";
|
||||
case token_t::Keyword: return os << "keyword";
|
||||
case token_t::Integer: return os << "integer";
|
||||
case token_t::Lparen: return os << "'('";
|
||||
case token_t::Rparen: return os << "')'";
|
||||
@@ -52,8 +52,8 @@ static inline std::string operator+(const std::string& str, token_t type) {
|
||||
switch (type) {
|
||||
case token_t::None: return str + "none";
|
||||
case token_t::Identifier: return str + "identifier";
|
||||
case token_t::Keyword: return str + "keyword";
|
||||
case token_t::String: return str + "string";
|
||||
case token_t::Keyword: return str + "keyword";
|
||||
case token_t::Integer: return str + "integer";
|
||||
case token_t::Lparen: return str + "'('";
|
||||
case token_t::Rparen: return str + "')'";
|
||||
@@ -90,10 +90,28 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
|
||||
}
|
||||
}
|
||||
|
||||
using integer_token = std::uint64_t;
|
||||
|
||||
struct token {
|
||||
token_t type = token_t::None;
|
||||
keyword_token keyword = keyword_token::None;
|
||||
std::string_view value;
|
||||
token_t type = token_t::None;
|
||||
union value {
|
||||
std::nullptr_t null;
|
||||
std::string_view string;
|
||||
keyword_token keyword;
|
||||
integer_token integer;
|
||||
|
||||
value(std::nullptr_t value = nullptr)
|
||||
: null(value) {}
|
||||
|
||||
value(std::string_view value)
|
||||
: string(value) {}
|
||||
|
||||
value(keyword_token value)
|
||||
: keyword(value) {}
|
||||
|
||||
value(integer_token value)
|
||||
: integer(value) {}
|
||||
} value;
|
||||
|
||||
token() = default;
|
||||
|
||||
@@ -101,15 +119,21 @@ struct token {
|
||||
: type(type), value(value) {}
|
||||
|
||||
token(keyword_token keyword, std::string_view value = {})
|
||||
: type(token_t::Keyword), keyword(keyword), value(value) {}
|
||||
: type(token_t::Keyword), value(keyword) {}
|
||||
|
||||
token(integer_token integer)
|
||||
: type(token_t::Integer), value(integer) {}
|
||||
|
||||
union value* operator->() { return &value; }
|
||||
const union value* operator->() const { return &value; }
|
||||
|
||||
bool operator==(const token& rhs) const {
|
||||
if (type != rhs.type) return false;
|
||||
switch (type) {
|
||||
case token_t::Identifier:
|
||||
case token_t::String:
|
||||
case token_t::Integer: return value == rhs.value;
|
||||
case token_t::Keyword: return keyword == rhs.keyword;
|
||||
case token_t::String: return value.string == rhs.value.string;
|
||||
case token_t::Keyword: return value.keyword == rhs.value.keyword;
|
||||
case token_t::Integer: return value.integer == rhs.value.integer;
|
||||
case token_t::None:
|
||||
case token_t::Lparen:
|
||||
case token_t::Rparen:
|
||||
@@ -128,9 +152,9 @@ struct token {
|
||||
static inline std::ostream& operator<<(std::ostream& os, const token& token) {
|
||||
switch (token.type) {
|
||||
case token_t::Identifier:
|
||||
case token_t::String:
|
||||
case token_t::Integer: return os << token.value;
|
||||
case token_t::Keyword: return os << token.keyword;
|
||||
case token_t::String: return os << token.value.string;
|
||||
case token_t::Keyword: return os << token.value.keyword;
|
||||
case token_t::Integer: return os << token.value.integer;
|
||||
default: return os << token.type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public:
|
||||
|
||||
template <typename U>
|
||||
handle(const handle<U, Error>& error)
|
||||
: m_location(error.location()), m_error(error) {}
|
||||
: m_location(error.location()), m_error(error.error()) {}
|
||||
|
||||
~handle() = default;
|
||||
|
||||
@@ -65,17 +65,18 @@ public:
|
||||
location location() const { return m_location; }
|
||||
|
||||
bool present() const { return m_value.has_value(); }
|
||||
bool error() const { return !m_value.has_value(); }
|
||||
bool has_error() const { return !m_value.has_value(); }
|
||||
|
||||
value_type move() {
|
||||
value_type value = *m_value;
|
||||
value_type value = std::move(*m_value);
|
||||
m_value.reset();
|
||||
return value;
|
||||
}
|
||||
|
||||
Error error() const { return m_error; }
|
||||
|
||||
operator reference() { return *m_value; }
|
||||
operator const_reference() const { return *m_value; }
|
||||
operator Error() const { return m_error; }
|
||||
|
||||
reference operator*() { return *m_value; }
|
||||
const_reference operator*() const { return *m_value; }
|
||||
@@ -133,7 +134,7 @@ public:
|
||||
|
||||
template <typename U>
|
||||
handle(const handle<U, Error>& error)
|
||||
: m_location(error.location()), m_error(error) {}
|
||||
: m_location(error.location()), m_error(error.error()) {}
|
||||
|
||||
~handle() = default;
|
||||
|
||||
@@ -172,13 +173,13 @@ public:
|
||||
location location() const { return m_location; }
|
||||
|
||||
bool present() const { return m_value != nullptr; }
|
||||
bool error() const { return m_value == nullptr; }
|
||||
bool has_error() const { return m_value == nullptr; }
|
||||
|
||||
std::shared_ptr<value_type> shared() { return m_value; }
|
||||
Error error() const { return m_error; }
|
||||
|
||||
operator reference() { return *m_value; }
|
||||
operator const_reference() const { return *m_value; }
|
||||
operator Error() const { return m_error; }
|
||||
|
||||
reference operator*() { return *m_value; }
|
||||
const_reference operator*() const { return *m_value; }
|
||||
|
||||
Reference in New Issue
Block a user