From 81efc9006d9ebcb0587d1d4d5ee7a1b9d2996026 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Mon, 15 Apr 2024 04:46:11 +0630 Subject: Implement printing of pp_err_t Another great thing for C++: the ability to tell it how to print structures the way I want. In C it's either: 1) Write a function to print the structure out (preferably to a file pointer) 2) Write a function to return a string (allocated on the heap) which represents it Both are not fun to write, whereas it's much easier to write this. --- asm/preprocesser.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'asm') diff --git a/asm/preprocesser.cpp b/asm/preprocesser.cpp index e87a505..31cf6a7 100644 --- a/asm/preprocesser.cpp +++ b/asm/preprocesser.cpp @@ -63,6 +63,31 @@ preprocess_use_blocks(vector tokens) return VAL(new_tokens); } + +std::ostream &operator<<(std::ostream &os, pp_err_t &err) +{ + os << "PREPROCESSING_"; + switch (err.type) + { + case OK: + return os << "OK"; + case EXPECTED_NAME: + return os << "EXPECTED_NAME"; + case EXPECTED_STRING: + return os << "EXPECTED_STRING"; + case EXPECTED_END: + return os << "EXPECTED_END"; + case FILE_NONEXISTENT: + return os << "FILE_NONEXISTENT"; + case FILE_PARSE_ERROR: + return os << "FILE_PARSE_ERROR -> \n\t[" << err.reference->content + << "]: " << lerr_as_cstr(err.lerr); + case UNKNOWN_NAME: + return os << "UNKNOWN_NAME"; + } + return os; +} + pp_err_t::pp_err_t(pp_err_type_t e) : reference{nullptr}, type{e}, lerr{lerr_t::OK} {} -- cgit v1.2.3-13-gbd6f