diff options
Diffstat (limited to 'subx')
-rw-r--r-- | subx/010core.cc | 1 | ||||
-rw-r--r-- | subx/022transform_immediate.cc | 37 |
2 files changed, 35 insertions, 3 deletions
diff --git a/subx/010core.cc b/subx/010core.cc index f391a369..f8dcdf20 100644 --- a/subx/010core.cc +++ b/subx/010core.cc @@ -100,6 +100,7 @@ End_of_program = 0; // of its bytes, and run it void run(string text_bytes) { // Begin run() For Scenarios + cerr << text_bytes << '\n'; load_program(text_bytes, 1); // tests always assume a starting address of 1 EIP = 1; // preserve null pointer while (EIP < End_of_program) diff --git a/subx/022transform_immediate.cc b/subx/022transform_immediate.cc index 24a61f68..65ff95ce 100644 --- a/subx/022transform_immediate.cc +++ b/subx/022transform_immediate.cc @@ -1,8 +1,39 @@ :(before "End One-time Setup") -Transform.push_back(identity); +Transform.push_back(skip_whitespace_and_comments); :(code) -void identity(const string& input, string& output) { +void skip_whitespace_and_comments(const string& input, string& output) { cerr << "running compiler phase\n"; - output = input; + istringstream in(input); + in >> std::noskipws; + ostringstream out; + while (has_data(in)) { + string word = next_word(in); + out << word << ' '; + } + out.str().swap(output); +} + +string next_word(istream& in) { + skip_whitespace_and_comments(in); + string result; + in >> result; + return result; +} + +void skip_whitespace_and_comments(istream& in) { + while (true) { + char c = in.peek(); + if (isspace(c)) { in.get(); continue; } + else if (c == '#') skip_comment(in); + else return; + } +} + +void skip_comment(istream& in) { + assert(in.peek() == '#'); + char c = '\0'; + do { + in >> c; + } while (c != '\n'); } |