diff options
author | Kartik Agaram <vc@akkartik.com> | 2018-06-30 23:28:35 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2018-06-30 23:31:39 -0700 |
commit | fc5b5e3704c43f55ac19646e51d78d1b8141850b (patch) | |
tree | 4f39d257d1463a0e9a3da2e80a3930fa1306caae /subx | |
parent | 1a3d4e2c73e86e6a905de60b1b80576e3d9f81c4 (diff) | |
download | mu-fc5b5e3704c43f55ac19646e51d78d1b8141850b.tar.gz |
4302 - a more elaborate pass-through phase
Starting to work out the skeleton every phase needs to have.
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'); } |