about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-11-11 22:43:43 -0800
committerKartik K. Agaram <vc@akkartik.com>2016-11-11 23:04:01 -0800
commitaa3d29a5666afecd6bce1821ff72ccc5600b431f (patch)
tree14964a3a27004d850f5357cc365623208bb095e6
parent6d007fda037331e7761d2a9ed3a2e435131daf7e (diff)
downloadmu-aa3d29a5666afecd6bce1821ff72ccc5600b431f.tar.gz
3668 - $read a word, stopping at whitespace
Useful for programming contests like https://halite.io

Doesn't suffer from C++'s usual buffered gotchas: it'll skip leading
whitespace. Slow, though. Can be speeded up, though.

- 20 minutes later
But what's the point? Typewriter mode is actually harder to test than
'raw' console mode. Writing Mu programs in typewriter mode is just going
to encourage us all to slack off on writing tests.
-rw-r--r--038new_text.cc30
-rw-r--r--089scenario_filesystem.cc8
2 files changed, 30 insertions, 8 deletions
diff --git a/038new_text.cc b/038new_text.cc
index 0b4e4663..c449e6c9 100644
--- a/038new_text.cc
+++ b/038new_text.cc
@@ -137,3 +137,33 @@ string read_mu_text(int address) {
   }
   return tmp.str();
 }
+
+//:: 'cheating' by using the host system
+
+:(before "End Primitive Recipe Declarations")
+_READ,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "$read", _READ);
+:(before "End Primitive Recipe Checks")
+case _READ: {
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case _READ: {
+  skip_whitespace(cin);
+  string result;
+  if (has_data(cin))
+    cin >> result;
+  products.resize(1);
+  products.at(0).push_back(new_mu_text(result));
+  break;
+}
+
+:(code)
+void skip_whitespace(istream& in) {
+  while (true) {
+    if (!has_data(in)) break;
+    if (isspace(in.peek())) in.get();
+    else break;
+  }
+}
diff --git a/089scenario_filesystem.cc b/089scenario_filesystem.cc
index ba126cfe..1e64baec 100644
--- a/089scenario_filesystem.cc
+++ b/089scenario_filesystem.cc
@@ -244,11 +244,3 @@ int size_of_resources() {
   delete type;
   return result;
 }
-
-void skip_whitespace(istream& in) {
-  while (true) {
-    if (!has_data(in)) break;
-    if (isspace(in.peek())) in.get();
-    else break;
-  }
-}