From c484a6216e4640b75798fc63e8bee13bf57f1b06 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Thu, 30 Apr 2026 15:19:40 -0500 Subject: [PATCH] . --- CMakeLists.txt | 1 + src/assembler/as_exceptions.hh | 17 +++++++++++++++++ src/assembler/lexer.cc | 32 +++++++++++++++++++++++++++++--- src/assembler/lexer.hh | 10 +++++++--- 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/assembler/as_exceptions.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index ba78482..429baa6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,7 @@ add_library(lib${PROJECT_NAME} SHARED src/assembler/lexer.hh src/assembler/assembler.cc src/assembler/assembler.hh + src/assembler/as_exceptions.hh ) target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings}) diff --git a/src/assembler/as_exceptions.hh b/src/assembler/as_exceptions.hh new file mode 100644 index 0000000..0690bbd --- /dev/null +++ b/src/assembler/as_exceptions.hh @@ -0,0 +1,17 @@ +#ifndef TYCHE_VM_EXCEPTIONS_HH +#define TYCHE_VM_EXCEPTIONS_HH + +#include +#include + +namespace tyche::as { + +class AssemblyError : public std::runtime_error +{ +public: + explicit AssemblyError(std::string const& str) : std::runtime_error(str.c_str()) {} +}; + +} + +#endif //TYCHE_VM_EXCEPTIONS_HH diff --git a/src/assembler/lexer.cc b/src/assembler/lexer.cc index 2a9c5ab..4ecf7c8 100644 --- a/src/assembler/lexer.cc +++ b/src/assembler/lexer.cc @@ -1,20 +1,46 @@ #include "lexer.hh" +#include "as_exceptions.hh" + namespace tyche::as { void Lexer::reset() { - + pos_ = 0; + ingest_next_token(); } Token Lexer::peek() const { - return {}; + return look_ahead_; } Token Lexer::ingest() { - return {}; + Token t = look_ahead_; + ingest_next_token(); + return t; +} + +void Lexer::ingest_next_token() +{ + char c = source_.at(pos_); + + if (pos_ >= source_.size()) { + look_ahead_ = { TokenType::EOF_, "" }; + } else if (c == '.') { + + } else if (c == '"') { + + } else if (isdigit(c)) { + + } else if (isalpha(c)) { + + } else if (c == '\n') { + + } else if (c != ' ' && c != '\t' && c != '\r') { + throw AssemblyError(std::string("Unexpected character '") + c + "' (ascii: " + std::to_string((int) c) + ")"); + } } } // tyche diff --git a/src/assembler/lexer.hh b/src/assembler/lexer.hh index 1fc4fbd..f5584d7 100644 --- a/src/assembler/lexer.hh +++ b/src/assembler/lexer.hh @@ -6,7 +6,7 @@ namespace tyche::as { -enum class TokenType { Directive, Instruction, Number, Float, String, Enter, EOF_ }; +enum class TokenType { BOF, Directive, Instruction, Number, Float, String, Enter, EOF_ }; struct Token { TokenType type; @@ -15,14 +15,18 @@ struct Token { class Lexer { public: - explicit Lexer(std::string source) : source_(std::move(source)) {} + explicit Lexer(std::string source) : source_(std::move(source) + "\n") { reset(); } void reset(); [[nodiscard]] Token peek() const; [[nodiscard]] Token ingest(); private: - std::string source_; + const std::string source_; + size_t pos_ = 0; + Token look_ahead_; + + void ingest_next_token(); }; } // tyche