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.
This commit is contained in:
1 parent
f68d68d497
commit
3f3e431d64
2 files changed
+33
-5
No files matched your search
+20
-1
@@ -3,6 +3,7 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -54,12 +55,18 @@ private:
|
|||||||
|
|
||||||
token_stream(const std::string &filename) {
|
token_stream(const std::string &filename) {
|
||||||
this->filename = 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;
|
std::stringstream buf;
|
||||||
buf << file.rdbuf();
|
buf << file.rdbuf();
|
||||||
lines = lex(buf.str());
|
lines = lex(buf.str());
|
||||||
}
|
}
|
||||||
std::vector<token_line> lex(const std::string &char_stream);
|
std::vector<token_line> lex(const std::string &char_stream);
|
||||||
|
|
||||||
|
bool ok = true; // false when the file could not be read
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class ast_node_t {
|
enum class ast_node_t {
|
||||||
@@ -87,10 +94,20 @@ private:
|
|||||||
// I know template class is the most elegant solution of CELL_SIZE
|
// I know template class is the most elegant solution of CELL_SIZE
|
||||||
// but I can't be bother to add that.
|
// 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:
|
private:
|
||||||
addr dataptr = 0;
|
addr dataptr = 0;
|
||||||
std::vector<std::map<std::string, general_data>> scopes;
|
std::vector<std::map<std::string, general_data>> scopes;
|
||||||
std::map<std::string, ast_node> macros;
|
std::map<std::string, ast_node> macros;
|
||||||
|
std::set<std::string> reported; // keep a typo from being printed n times
|
||||||
|
|
||||||
enum class builtin_t {
|
enum class builtin_t {
|
||||||
ANCHOR,
|
ANCHOR,
|
||||||
@@ -179,4 +196,6 @@ private:
|
|||||||
public:
|
public:
|
||||||
std::string export_rot8();
|
std::string export_rot8();
|
||||||
bool import_file(const std::string &filename, size_t cell_size);
|
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; }
|
||||||
};
|
};
|
||||||
+13
-4
@@ -1,9 +1,18 @@
|
|||||||
|
|
||||||
#include "r8asm/r8asm.hh"
|
#include "r8asm/r8asm.hh"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
int main() {
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
if (argc < 2) {
|
||||||
|
std::cerr << "usage: r8asm_test <file.r8asm>" << std::endl;
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
r8asm a;
|
r8asm a;
|
||||||
a.import_file("asm/test.r8asm", 8);
|
if (!a.import_file(argv[1], 8))
|
||||||
std::cout << a.export_rot8();
|
return (1);
|
||||||
|
std::string tape = a.export_rot8();
|
||||||
|
if (a.failed())
|
||||||
|
return (1);
|
||||||
|
std::cout << tape;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user