From e18cf2f52e927c05de1b55525f4e26fecc8fa9b0 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Mon, 16 Jan 2023 13:02:12 +0100 Subject: [PATCH] Based Object Notation for Gigachags - BONG --- src/bong.cpp | 47 ++++++++++++++++++++++++- src/bong.hpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++- src/meson.build | 1 + src/result.hpp | 41 +++++++++------------- 4 files changed, 153 insertions(+), 27 deletions(-) diff --git a/src/bong.cpp b/src/bong.cpp index c5e0e45..f06f3a1 100644 --- a/src/bong.cpp +++ b/src/bong.cpp @@ -1,3 +1,48 @@ #include "bong.hpp" +#include "utils.hpp" +#include -namespace bong { } \ No newline at end of file +namespace bong { + +auto Lexer::make_token() noexcept -> Result +{ + auto c = current(); + if (std::isspace(c)) + return make_whitespace(); + else if (std::isdigit(c)) + return make_number(); + else if (std::isalpha(c)) + return make_name(); + else + return make_static(); +} + +auto Lexer::make_name() noexcept -> Result +{ + auto begin_index = index; + auto begin = location; + while (!done() + and (std ::isalpha(current()) or std::isdigit(current()) + or current() == '_' or current() == '-')) { + step(); + } + return Token { Tokens::Name, begin_index, index - begin_index, begin }; +} + +auto Lexer::make_number() noexcept -> Result; + +auto Lexer::make_static() noexcept -> Result; + +auto Lexer::make_whitespace() noexcept -> Result; + +auto Lexer::make_singleline_comment() noexcept -> Result; + +auto Lexer::make_multiline_comment() noexcept -> Result; + +auto Lexer::make_string() noexcept -> Result; + +auto Lexer::make_id() noexcept -> Result; + +auto Lexer::make_class() noexcept -> Result; + +} diff --git a/src/bong.hpp b/src/bong.hpp index 294215d..07efc1a 100644 --- a/src/bong.hpp +++ b/src/bong.hpp @@ -1,3 +1,92 @@ #pragma once -namespace bong { } +#include "utils.hpp" +#include +#include + +namespace bong { + +enum class Tokens { + Eof, + Whitespace, + SingleLineComment, + MultiLineComment, + + Name, + Id, + Class, + + Int, + Float, + String, + Bool, + + LBrace, + RBrace, + LBracket, + RBracket, + + Equal, + Colon, + SemiColon, + Comma, +}; + +struct Location { + int line, col; +}; + +struct Token { + Tokens type; + size_t index, length; + Location location; +}; + +class Lexer { +public: + struct Error { + std::string message; + size_t index; + Location location; + }; + + Lexer(std::string_view text) + : text { text } + { + (void)next(); + } + + auto next() noexcept -> Result + { + return current_token = make_token(); + } + [[nodiscard]] auto peek() const noexcept -> Result + { + return current_token; + } + +private: + auto make_token() noexcept -> Result; + auto make_name() noexcept -> Result; + auto make_number() noexcept -> Result; + auto make_static() noexcept -> Result; + auto make_whitespace() noexcept -> Result; + auto make_singleline_comment() noexcept -> Result; + auto make_multiline_comment() noexcept -> Result; + auto make_string() noexcept -> Result; + auto make_id() noexcept -> Result; + auto make_class() noexcept -> Result; + + auto current() const noexcept -> char { return text.at(index); } + auto done() const noexcept -> bool { return index >= text.size(); } + auto step() noexcept -> void { index++; } + + Result current_token { + Error { "next() not called first", index, location }, + }; + std::string_view text; + size_t index { 0 }; + Location location { 1, 1 }; +}; + +} diff --git a/src/meson.build b/src/meson.build index ede1bcf..e9bb8e5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,4 +1,5 @@ sources = files( 'main.cpp', + 'bong.cpp', ) diff --git a/src/result.hpp b/src/result.hpp index ef1585a..36f40a9 100644 --- a/src/result.hpp +++ b/src/result.hpp @@ -209,28 +209,28 @@ public: return is_ok() ? if_ok(unwrap()) : if_error(unwrap_error()); } - [[nodiscard]] constexpr auto flatten() noexcept - requires std::same_as::Error> + [[nodiscard]] constexpr auto flatten() noexcept requires + std::same_as::Error> { using InnerValue = typename Extracter::Value; return is_ok() ? unwrap() : Result(unwrap_error()); } - [[nodiscard]] constexpr auto flatten() const noexcept - requires std::same_as::Error> + [[nodiscard]] constexpr auto flatten() const noexcept requires + std::same_as::Error> { using InnerValue = typename Extracter::Value; return is_ok() ? unwrap() : Result(unwrap_error()); } - [[nodiscard]] constexpr auto flatten_error() noexcept - requires std::same_as::Value> + [[nodiscard]] constexpr auto flatten_error() noexcept requires + std::same_as::Value> { using InnerError = typename Extracter::Error; return is_error() ? unwrap_error() : Result(unwrap()); } - [[nodiscard]] constexpr auto flatten_error() const noexcept - requires std::same_as::Value> + [[nodiscard]] constexpr auto flatten_error() const noexcept requires + std::same_as::Value> { using InnerError = typename Extracter::Error; return is_error() ? unwrap_error() @@ -385,14 +385,14 @@ public: return is_ok() ? if_ok(unwrap()) : if_error(); } - [[nodiscard]] constexpr auto flatten() noexcept - requires std::same_as::Error> + [[nodiscard]] constexpr auto flatten() noexcept requires + std::same_as::Error> { using InnerValue = typename Extracter::Value; return is_ok() ? unwrap() : Result(); } - [[nodiscard]] constexpr auto flatten() const noexcept - requires std::same_as::Error> + [[nodiscard]] constexpr auto flatten() const noexcept requires + std::same_as::Error> { using InnerValue = typename Extracter::Value; return is_ok() ? unwrap() : Result(); @@ -528,14 +528,14 @@ public: return is_ok() ? if_ok() : if_error(unwrap_error()); } - [[nodiscard]] constexpr auto flatten_error() noexcept - requires std::same_as::Value> + [[nodiscard]] constexpr auto flatten_error() noexcept requires + std::same_as::Value> { using InnerError = typename Extracter::Error; return is_error() ? unwrap_error() : Result(); } - [[nodiscard]] constexpr auto flatten_error() const noexcept - requires std::same_as::Value> + [[nodiscard]] constexpr auto flatten_error() const noexcept requires + std::same_as::Value> { using InnerError = typename Extracter::Error; return is_error() ? unwrap_error() : Result(); @@ -656,12 +656,3 @@ private: }; } - -// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -// #define TRY(expr) \ -// ({ \ -// auto result = (expr); \ -// if (result.is_error()) \ -// return { std::move(result.unwrap_error()) }; \ -// std::move(result.unwrap()); \ -// })