From 3f3e431d6450d96e6a73ccfc9a9e91e06acb85b5 Mon Sep 17 00:00:00 2001 From: StarCells Date: Thu, 5 Nov 2026 22:43:13 +0800 Subject: [PATCH] r8asm: say what is wrong instead of dying or lying A typo used to end in one of three ways: std::out_of_range aborting the process (unknown macro, unknown directive), a silent 0 (unknown name used as an address, which quietly mis-assembles), or a silent empty tape (a file or %include that could not be opened). All four now go through ast::fail(), print a line on stderr, set the error flag and make the CLI exit with 1. Names that are reported repeatedly are only printed once. While here: import_file set the cell size *after* parsing, so the %include handler read main_tree.CELL_SIZE before it had ever been set and handed 0 to included files. It worked by accident because the top-level call ran again at the end. Moved it before the parse. r8asm_test.cc came from the working tree (argv instead of a hard-coded path); it now also checks the arguments and the error flag. --- include/r8asm/r8asm.hh | 21 ++++++++++++++++++++- tests/r8_test.cc | 17 +++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) 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 +}