From 3d54ed9fb3a7e3b4e11db0894321ba7757e106fc Mon Sep 17 00:00:00 2001 From: Vaclav Tvrdik Date: Sun, 7 Mar 2021 18:40:26 +0100 Subject: [PATCH] better handling of comments --- Readme.md | 2 +- ml.cpp | 30 ++++++++++++++++++------------ ml.h | 4 ++-- ml_io.cpp | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Readme.md b/Readme.md index e6357ea..9fcf0e2 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,5 @@ ### BUGS -- see malformed_program - wrong parsing of comments - probably comment with " ### TODO - test whether AIG,,,10 csv row is parsed @@ -40,6 +39,7 @@ - format (printf) - setq - mapcar (funcall, apply) +- delete UNIT (replace it by nil) #### Performance - push_back - repeatedly without reserving size diff --git a/ml.cpp b/ml.cpp index fe9ba00..7216b9c 100644 --- a/ml.cpp +++ b/ml.cpp @@ -736,8 +736,22 @@ MlValue MlValue::eval(MlEnvironment &env) { } } -void skip_whitespace(const std::string &s, int &ptr) { - while (isspace(s[ptr])) { ptr++; } +void skip_whitespace(std::string &s, int &ptr); + +void erase_comments(std::string &s, int &ptr) { + while (s[ptr] == ';') { + int save_ptr = ptr; + while (s[save_ptr] != '\n' && save_ptr < int(s.length())) { save_ptr++; } + s.erase(ptr, save_ptr - ptr); + + skip_whitespace(s, ptr); + } +} + +void skip_whitespace(std::string &s, int &ptr) { + while (isspace(s[ptr])) { ptr++; } + + erase_comments(s, ptr); } // Parse a single value and increment the pointer @@ -745,16 +759,8 @@ void skip_whitespace(const std::string &s, int &ptr) { MlValue parse(std::string &s, int &ptr) { skip_whitespace(s, ptr); - while (s[ptr] == ';') { - // If this is a comment - int save_ptr = ptr; - while (s[save_ptr] != '\n' && save_ptr < int(s.length())) { save_ptr++; } - s.erase(ptr, save_ptr - ptr); - skip_whitespace(s, ptr); - - if (s.substr(ptr, s.length() - ptr - 1) == "") - return MlValue(); - } + if (s[ptr] == ';') + throw std::runtime_error("this should never happen!"); if (s == "") { diff --git a/ml.h b/ml.h index 9152f37..39b1973 100644 --- a/ml.h +++ b/ml.h @@ -16,7 +16,7 @@ class MlValue; class MlEnvironment { public: // Default constructor - MlEnvironment() : parent_scope(NULL) {} + MlEnvironment() : parent_scope(nullptr) {} // Does this environment, or its parent environment, // have this atom in scope? @@ -120,7 +120,7 @@ public: bool is_number() const; - // Get the "truthy" boolean value of this value. + // Get the boolean value of this value. bool as_bool() const; // Get this item's integer value diff --git a/ml_io.cpp b/ml_io.cpp index 6abc509..4bb161f 100644 --- a/ml_io.cpp +++ b/ml_io.cpp @@ -10,7 +10,7 @@ std::string read_file_contents(const std::string &filename) { std::ifstream f; f.open(filename.c_str()); if (!f) - throw std::runtime_error("could not open file"); + throw std::runtime_error("could not open file (" + filename + ")"); f.seekg(0, std::ios::end); std::string contents;