From fdd732fb4d7196b3b0ca094193d837243975ca9f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 16 Feb 2015 16:33:32 -0800 Subject: 766 - can recognize first word 'recipe' --- cpp/002main.cc | 30 ++++++++++++++++++++++++++++++ cpp/002main.test.cc | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cpp/002main.cc b/cpp/002main.cc index c2baea50..cb0ed411 100644 --- a/cpp/002main.cc +++ b/cpp/002main.cc @@ -142,6 +142,36 @@ void setup_types() { } void compile(string form) { + istringstream in(form); + in >> std::noskipws; + if (next_word(in) != "recipe") + RAISE << "top-level forms must be of the form 'recipe _name_ [ _instruction_ ... ]'"; +} + +string next_word(istream& in) { + ostringstream out; + skip_whitespace(in); + slurp_word(in, out); +//? cout << out.str() << '\n'; //? 1 + return out.str(); +} + +void slurp_word(istream& in, ostream& out) { + char c; + while (in >> c) { +//? cout << c << '\n'; //? 1 + if (isspace(c)) { +//? cout << " space\n"; //? 1 + in.putback(c); + break; + } + out << c; + } +} + +void skip_whitespace(istream& in) { + while (isspace(in.peek()) && in.peek() != '\n') + in.get(); } diff --git a/cpp/002main.test.cc b/cpp/002main.test.cc index b622828b..1f77ab39 100644 --- a/cpp/002main.test.cc +++ b/cpp/002main.test.cc @@ -1,5 +1,5 @@ void test_literal() { - compile("function main [\n" + compile("recipe main [\n" " 1:integer <- copy 23:literal\n" "]\n"); run("main"); -- cgit 1.4.1-2-gfad0