better handling of comments
This commit is contained in:
parent
1c7dea823b
commit
3d54ed9fb3
|
|
@ -1,6 +1,5 @@
|
||||||
|
|
||||||
### BUGS
|
### BUGS
|
||||||
- see malformed_program - wrong parsing of comments - probably comment with "
|
|
||||||
|
|
||||||
### TODO
|
### TODO
|
||||||
- test whether AIG,,,10 csv row is parsed
|
- test whether AIG,,,10 csv row is parsed
|
||||||
|
|
@ -40,6 +39,7 @@
|
||||||
- format (printf)
|
- format (printf)
|
||||||
- setq
|
- setq
|
||||||
- mapcar (funcall, apply)
|
- mapcar (funcall, apply)
|
||||||
|
- delete UNIT (replace it by nil)
|
||||||
|
|
||||||
#### Performance
|
#### Performance
|
||||||
- push_back - repeatedly without reserving size
|
- push_back - repeatedly without reserving size
|
||||||
|
|
|
||||||
30
ml.cpp
30
ml.cpp
|
|
@ -736,8 +736,22 @@ MlValue MlValue::eval(MlEnvironment &env) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void skip_whitespace(const std::string &s, int &ptr) {
|
void skip_whitespace(std::string &s, int &ptr);
|
||||||
while (isspace(s[ptr])) { 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
|
// 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) {
|
MlValue parse(std::string &s, int &ptr) {
|
||||||
skip_whitespace(s, ptr);
|
skip_whitespace(s, ptr);
|
||||||
|
|
||||||
while (s[ptr] == ';') {
|
if (s[ptr] == ';')
|
||||||
// If this is a comment
|
throw std::runtime_error("this should never happen!");
|
||||||
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 == "") {
|
if (s == "") {
|
||||||
|
|
|
||||||
4
ml.h
4
ml.h
|
|
@ -16,7 +16,7 @@ class MlValue;
|
||||||
class MlEnvironment {
|
class MlEnvironment {
|
||||||
public:
|
public:
|
||||||
// Default constructor
|
// Default constructor
|
||||||
MlEnvironment() : parent_scope(NULL) {}
|
MlEnvironment() : parent_scope(nullptr) {}
|
||||||
|
|
||||||
// Does this environment, or its parent environment,
|
// Does this environment, or its parent environment,
|
||||||
// have this atom in scope?
|
// have this atom in scope?
|
||||||
|
|
@ -120,7 +120,7 @@ public:
|
||||||
|
|
||||||
bool is_number() const;
|
bool is_number() const;
|
||||||
|
|
||||||
// Get the "truthy" boolean value of this value.
|
// Get the boolean value of this value.
|
||||||
bool as_bool() const;
|
bool as_bool() const;
|
||||||
|
|
||||||
// Get this item's integer value
|
// Get this item's integer value
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ std::string read_file_contents(const std::string &filename) {
|
||||||
std::ifstream f;
|
std::ifstream f;
|
||||||
f.open(filename.c_str());
|
f.open(filename.c_str());
|
||||||
if (!f)
|
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);
|
f.seekg(0, std::ios::end);
|
||||||
std::string contents;
|
std::string contents;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue