diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 2a20b77..c0306bc 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -54,12 +55,18 @@ private: token_stream(const std::string &filename) { this->filename = filename; - std::ifstream file(filename); // TODO: when open failed + std::ifstream file(filename); + if (!file.is_open()) { + ok = false; + return; + } std::stringstream buf; buf << file.rdbuf(); lines = lex(buf.str()); } std::vector lex(const std::string &char_stream); + + bool ok = true; // false when the file could not be read }; enum class ast_node_t { @@ -87,10 +94,20 @@ private: // I know template class is the most elegant solution of CELL_SIZE // but I can't be bother to add that. + // The assembler has no exceptions: anything that cannot be assembled + // sets this flag and says why on stderr. + bool error = false; + + void fail(const std::string &message) { + std::cerr << "r8asm: " << message << std::endl; + error = true; + } + private: addr dataptr = 0; std::vector> scopes; std::map macros; + std::set reported; // keep a typo from being printed n times enum class builtin_t { ANCHOR, @@ -179,4 +196,6 @@ private: public: std::string export_rot8(); bool import_file(const std::string &filename, size_t cell_size); + // True once anything (a missing file, an unknown name, ...) has failed. + bool failed() const { return main_tree.error; } }; \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 1dcbde5..5e5f345 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,9 +1,18 @@ #include "r8asm/r8asm.hh" #include -int main() { + +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "usage: r8asm_test " << std::endl; + return (2); + } r8asm a; - a.import_file("asm/test.r8asm", 8); - std::cout << a.export_rot8(); + if (!a.import_file(argv[1], 8)) + return (1); + std::string tape = a.export_rot8(); + if (a.failed()) + return (1); + std::cout << tape; return (0); -} \ No newline at end of file +}