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

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