better handling of comments

This commit is contained in:
Vaclav Tvrdik 2021-03-07 18:40:26 +01:00
parent 1c7dea823b
commit 3d54ed9fb3
4 changed files with 22 additions and 16 deletions

View File

@ -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

30
ml.cpp
View File

@ -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 == "") {

4
ml.h
View File

@ -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

View File

@ -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;