about summary refs log tree commit diff stats
path: root/cpp/002main.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-02-16 16:33:32 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-02-16 16:33:32 -0800
commitfdd732fb4d7196b3b0ca094193d837243975ca9f (patch)
tree72f865bff8b816a0d349fcb7e84eff91f3eb5c5d /cpp/002main.cc
parent61c46db282f0539aba78375788e38848c3bbd8d1 (diff)
downloadmu-fdd732fb4d7196b3b0ca094193d837243975ca9f.tar.gz
766 - can recognize first word 'recipe'
Diffstat (limited to 'cpp/002main.cc')
-rw-r--r--cpp/002main.cc30
1 files changed, 30 insertions, 0 deletions
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();
 }