asm/main now tokenises and prints the tokens of a given file
With error checking!
This commit is contained in:
60
asm/main.cpp
60
asm/main.cpp
@@ -15,6 +15,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <tuple>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <lib/inst.h>
|
#include <lib/inst.h>
|
||||||
|
|
||||||
@@ -53,4 +55,62 @@ int main(int argc, const char *argv[])
|
|||||||
{
|
{
|
||||||
usage(argv[1], stdout);
|
usage(argv[1], stdout);
|
||||||
return 0;
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user