From 4f8f5111687355669d6df8db7a404fd9aaecacc4 Mon Sep 17 00:00:00 2001
From: Aryadev Chavali <aryadev@aryadevchavali.com>
Date: Sun, 14 Apr 2024 16:56:58 +0630
Subject: Implemented tokenise_literal_number (tokenise_number)

---
 asm/lexer.cpp | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

(limited to 'asm')

diff --git a/asm/lexer.cpp b/asm/lexer.cpp
index 0024b05..189b330 100644
--- a/asm/lexer.cpp
+++ b/asm/lexer.cpp
@@ -16,10 +16,11 @@
 
 #include "./lexer.hpp"
 
-using std::string_view, std::pair, std::make_pair;
+using std::string, std::string_view, std::pair, std::make_pair;
 
-constexpr auto VALID_SYMBOL =
-    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.:()%#$";
+constexpr auto VALID_SYMBOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV"
+                              "WXYZ0123456789-_.:()%#$",
+               VALID_DIGIT  = "0123456789";
 
 bool is_char_in_s(char c, const char *s)
 {
@@ -213,3 +214,26 @@ pair<token_t, lerr_t> tokenise_symbol(string_view &source, size_t &column)
   column += sym.size();
   return make_pair(t, lerr_t::OK);
 }
+
+token_t tokenise_literal_number(string_view &source, size_t &column)
+{
+  bool is_negative = false;
+  if (source[0] == '-')
+  {
+    is_negative = true;
+    source.remove_prefix(1);
+  }
+
+  auto end = source.find_first_not_of(VALID_DIGIT);
+  if (end == string::npos)
+    end = source.size() - 1;
+  string digits{source.substr(0, end)};
+  source.remove_prefix(end);
+
+  token_t t{token_type_t::LITERAL_NUMBER, (is_negative ? "-" : "") + digits,
+            column};
+
+  column += digits.size() + (is_negative ? 1 : 0);
+
+  return t;
+}
-- 
cgit v1.2.3-13-gbd6f