aboutsummaryrefslogtreecommitdiff
path: root/asm
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-04-14 17:11:48 +0630
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-04-14 17:11:48 +0630
commite5ef0292e75bb80f314b512618d748873dfe2088 (patch)
tree84538fcae1d0216db4295d44877ec2340ffbaeca /asm
parentd368a49f56d2ad0717a97113307214006a6f838c (diff)
downloadovm-e5ef0292e75bb80f314b512618d748873dfe2088.tar.gz
ovm-e5ef0292e75bb80f314b512618d748873dfe2088.tar.bz2
ovm-e5ef0292e75bb80f314b512618d748873dfe2088.zip
asm/main now tokenises and prints the tokens of a given file
With error checking!
Diffstat (limited to 'asm')
-rw-r--r--asm/main.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/asm/main.cpp b/asm/main.cpp
index c1b23b7..a168af1 100644
--- a/asm/main.cpp
+++ b/asm/main.cpp
@@ -15,6 +15,8 @@
#include <iostream>
#include <optional>
#include <string>
+#include <tuple>
+#include <vector>
#include <lib/inst.h>
@@ -53,4 +55,62 @@ int main(int argc, const char *argv[])
{
usage(argv[1], stdout);
return 0;
+ if (argc == 1 || argc > 3)
+ {
+ usage(argv[0], stderr);
+ return -1;
+ }
+ int ret = 0;
+ const char *file_name = argv[1];
+ auto file_source = read_file(file_name);
+
+ string source_str;
+ string_view original;
+ string_view src;
+ vector<token_t> tokens;
+ lerr_t lerr;
+
+ if (file_source.has_value())
+ source_str = file_source.value();
+ else
+ {
+ std::cerr << "ERROR: file `" << file_name << "` does not exist!"
+ << std::endl;
+ ret = -1;
+ goto end;
+ }
+ original = string_view{source_str};
+ src = string_view{source_str};
+ lerr = tokenise_buffer(src, tokens);
+
+ if (lerr != lerr_t::OK)
+ {
+ size_t col = 0, line = 1;
+ // TODO: Fix this UB (probably a change in API)
+ auto diff = src.begin() - original.begin();
+ for (auto i = 0; i < diff; ++i)
+ {
+ if (source_str[i] == '\n')
+ {
+ col = 0;
+ ++line;
+ }
+ else
+ ++col;
+ }
+ std::cerr << file_name << ":" << line << ":" << col << ":"
+ << lerr_as_cstr(lerr) << std::endl;
+ ret = 255 - lerr;
+ goto end;
+ }
+ else
+ {
+ for (auto token : tokens)
+ {
+ std::cout << "\t" << token << std::endl;
+ }
+ }
+
+end:
+ return ret;
}