From fc5b5e3704c43f55ac19646e51d78d1b8141850b Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sat, 30 Jun 2018 23:28:35 -0700 Subject: 4302 - a more elaborate pass-through phase Starting to work out the skeleton every phase needs to have. --- subx/010core.cc | 1 + subx/022transform_immediate.cc | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) (limited to 'subx') 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'); } -- cgit 1.4.1-2-gfad0