about summary refs log tree commit diff stats
path: root/014literal_noninteger.cc
diff options
context:
space:
mode:
Diffstat (limited to '014literal_noninteger.cc')
-rw-r--r--014literal_noninteger.cc33
1 files changed, 33 insertions, 0 deletions
diff --git a/014literal_noninteger.cc b/014literal_noninteger.cc
new file mode 100644
index 00000000..94242f41
--- /dev/null
+++ b/014literal_noninteger.cc
@@ -0,0 +1,33 @@
+//: Support literal non-integers.
+//: '3.14159:literal' is ugly, so we'll just say '3.14159' for non-integers.
+
+:(scenarios load)
+:(scenario noninteger_literal)
+recipe main [
+  1:number <- copy 3.14159
+]
++parse:   ingredient: {name: "3.14159", value: 3.14159, type: 0, properties: ["3.14159": "literal-number"]}
+
+:(after "reagent::reagent(string s)")
+  if (is_noninteger(s)) {
+    name = s;
+    types.push_back(0);
+    properties.push_back(pair<string, vector<string> >(name, vector<string>()));
+    properties.back().second.push_back("literal-number");
+    set_value(to_double(s));
+    return;
+  }
+
+:(code)
+bool is_noninteger(const string& s) {
+  return s.find_first_not_of("0123456789-.") == NOT_FOUND
+      && s.find('.') != NOT_FOUND;
+}
+
+double to_double(string n) {
+  char* end = NULL;
+  // safe because string.c_str() is guaranteed to be null-terminated
+  double result = strtod(n.c_str(), &end);
+  assert(*end == '\0');
+  return result;
+}