about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-02 15:50:59 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-02 15:50:59 -0700
commita316f1e4168a98ba8c1bbe7b091f8a7e7b3f03e6 (patch)
treecd8835427171818f1c6c6587bd77987e45d7fd46 /cpp
parent518e6c5e571077ecc29d28f620f7312cf18bd5d0 (diff)
downloadmu-a316f1e4168a98ba8c1bbe7b091f8a7e7b3f03e6.tar.gz
1241 - bugfix: comments inside run [ ... ]
Diffstat (limited to 'cpp')
-rw-r--r--cpp/.traces/run_handles_comments29
-rw-r--r--cpp/011load.cc43
-rw-r--r--cpp/014types.cc10
-rw-r--r--cpp/041name.cc2
-rw-r--r--cpp/049scenario_helpers.cc10
5 files changed, 73 insertions, 21 deletions
diff --git a/cpp/.traces/run_handles_comments b/cpp/.traces/run_handles_comments
new file mode 100644
index 00000000..7043fa31
--- /dev/null
+++ b/cpp/.traces/run_handles_comments
@@ -0,0 +1,29 @@
+parse/0: instruction: run
+parse/0:   ingredient: {name: "
+    # comment
+    1:integer <- copy 13:literal
+  ", value: 0, type: 0, properties: ["
+    # comment
+    1:integer <- copy 13:literal
+  ": "literal-string"]}
+after-brace/0: recipe main
+after-brace/0: run ...
+new/0: routine allocated memory from 1000 to 101000
+schedule/0: main
+run/0: instruction main/0
+run/0: run {name: "
+    # comment
+    1:integer <- copy 13:literal
+  ", value: 0, type: 0, properties: ["
+    # comment
+    1:integer <- copy 13:literal
+  ": "literal-string"]}
+parse/0: instruction: copy
+parse/0:   ingredient: {name: "13", value: 0, type: 0, properties: ["13": "literal"]}
+parse/0:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}
+after-brace/0: recipe tmp0
+after-brace/0: copy ...
+run/0: instruction tmp0/0
+run/0: {name: "1", value: 1, type: 1, properties: ["1": "integer"]} <- copy {name: "13", value: 13, type: 0, properties: ["13": "literal"]}
+run/0: ingredient 0 is 13
+mem/0: storing 13 in location 1
diff --git a/cpp/011load.cc b/cpp/011load.cc
index 35a9bf23..3308863e 100644
--- a/cpp/011load.cc
+++ b/cpp/011load.cc
@@ -19,7 +19,7 @@ vector<recipe_number> load(string form) {
 vector<recipe_number> load(istream& in) {
   vector<recipe_number> result;
   while (!in.eof()) {
-    skip_comments_and_newlines(in);
+    skip_whitespace_and_comments(in);
     if (in.eof()) break;
     string command = next_word(in);
     // Command Handlers
@@ -35,7 +35,7 @@ vector<recipe_number> load(istream& in) {
 }
 
 recipe_number add_recipe(istream& in) {
-  skip_comments_and_newlines(in);
+  skip_whitespace_and_comments(in);
   string recipe_name = next_word(in);
 //?   cout << "recipe name: ^" << recipe_name << "$\n"; //? 3
   if (recipe_name.empty())
@@ -56,7 +56,9 @@ recipe_number add_recipe(istream& in) {
   if (in.get() != '[')
     raise << "recipe body must begin with '['\n";
 
-  skip_comments_and_newlines(in);
+//?   show_rest_of_stream(in); //? 1
+  skip_whitespace_and_comments(in);
+//?   show_rest_of_stream(in); //? 1
 
   instruction curr;
   while (next_instruction(in, &curr)) {
@@ -73,17 +75,21 @@ recipe_number add_recipe(istream& in) {
 bool next_instruction(istream& in, instruction* curr) {
   curr->clear();
   if (in.eof()) return false;
+//?   show_rest_of_stream(in); //? 1
   skip_whitespace(in);  if (in.eof()) return false;
-  skip_comments_and_newlines(in);  if (in.eof()) return false;
+//?   show_rest_of_stream(in); //? 1
+  skip_whitespace_and_comments(in);  if (in.eof()) return false;
 
   vector<string> words;
+//?   show_rest_of_stream(in); //? 1
   while (in.peek() != '\n') {
     skip_whitespace(in);  if (in.eof()) return false;
+//?     show_rest_of_stream(in); //? 1
     string word = next_word(in);  if (in.eof()) return false;
     words.push_back(word);
     skip_whitespace(in);  if (in.eof()) return false;
   }
-  skip_comments_and_newlines(in);  if (in.eof()) return false;
+  skip_whitespace_and_comments(in);  if (in.eof()) return false;
 
 //?   if (words.size() == 1) cout << words[0] << ' ' << words[0].size() << '\n'; //? 1
   if (words.size() == 1 && words[0] == "]") {
@@ -166,13 +172,11 @@ void skip_whitespace(istream& in) {
   }
 }
 
-void skip_comments_and_newlines(istream& in) {
-  while (in.peek() == '\n' || in.peek() == '#') {
-    if (in.peek() == '#') {
-      in.get();
-      while (in.peek() != '\n') in.get();
-    }
-    in.get();
+void skip_whitespace_and_comments(istream& in) {
+  while (true) {
+    if (isspace(in.peek())) in.get();
+    else if (in.peek() == '#') skip_comment(in);
+    else break;
   }
 }
 
@@ -189,6 +193,21 @@ void skip_comma(istream& in) {
   skip_whitespace(in);
 }
 
+// for debugging
+:(before "End Globals")
+bool Show_rest_of_stream = false;
+:(code)
+void show_rest_of_stream(istream& in) {
+  if (!Show_rest_of_stream) return;
+  cerr << '^';
+  char c;
+  while (in >> c) {
+    cerr << c;
+  }
+  cerr << "$\n";
+  exit(0);
+}
+
 //: Have tests clean up any recipes they added.
 :(before "End Globals")
 vector<recipe_number> recently_added_recipes;
diff --git a/cpp/014types.cc b/cpp/014types.cc
index d005bcbf..ffbd8b63 100644
--- a/cpp/014types.cc
+++ b/cpp/014types.cc
@@ -90,15 +90,7 @@ Next_type_number = 1000;
 
 :(code)
 void skip_bracket(istream& in, string message) {
-  skip_whitespace(in);  skip_comments_and_newlines(in);  skip_whitespace(in);
+  skip_whitespace_and_comments(in);
   if (in.get() != '[')
     raise << message << '\n';
 }
-
-void skip_whitespace_and_comments(istream& in) {
-  while (true) {
-    if (isspace(in.peek())) in.get();
-    else if (in.peek() == '#') skip_comment(in);
-    else break;
-  }
-}
diff --git a/cpp/041name.cc b/cpp/041name.cc
index 9336ae04..ac7c751c 100644
--- a/cpp/041name.cc
+++ b/cpp/041name.cc
@@ -45,6 +45,8 @@ void transform_names(const recipe_number r) {
 //?       cout << "ingredient " << inst.ingredients[in].to_string() << '\n'; //? 1
       if (inst.ingredients[in].name == "default-space")
         inst.ingredients[in].initialized = true;
+      if (inst.ingredients[in].types.empty())
+        raise << "missing type in " << inst.to_string() << '\n';
       assert(!inst.ingredients[in].types.empty());
       if (inst.ingredients[in].types[0]  // not a literal
           && !inst.ingredients[in].initialized
diff --git a/cpp/049scenario_helpers.cc b/cpp/049scenario_helpers.cc
index d1d71c99..fa584565 100644
--- a/cpp/049scenario_helpers.cc
+++ b/cpp/049scenario_helpers.cc
@@ -12,6 +12,15 @@ recipe main [
 ]
 +mem: storing 13 in location 1
 
+:(scenario run_handles_comments)
+recipe main [
+  run [
+    # comment
+    1:integer <- copy 13:literal
+  ]
+]
++mem: storing 13 in location 1
+
 :(before "End Globals")
 size_t Num_temporary_recipes = 0;
 :(before "End Setup")
@@ -26,6 +35,7 @@ case RUN: {
 //?   cout << "recipe " << current_instruction().ingredients[0].name << '\n'; //? 1
   ostringstream tmp;
   tmp << "recipe tmp" << Num_temporary_recipes++ << " [ " << current_instruction().ingredients[0].name << " ]";
+//?   Show_rest_of_stream = true; //? 1
   vector<recipe_number> tmp_recipe = load(tmp.str());
   transform_all();
 //?   cout << tmp_recipe[0] << ' ' << Recipe_number["main"] << '\n'; //? 1