From aa3d29a5666afecd6bce1821ff72ccc5600b431f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 11 Nov 2016 22:43:43 -0800 Subject: 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. --- 038new_text.cc | 30 ++++++++++++++++++++++++++++++ 089scenario_filesystem.cc | 8 -------- 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; - } -} -- cgit 1.4.1-2-gfad0