diff options
author | Christian Theune <ct@flyingcircus.io> | 2016-01-05 00:40:40 +0100 |
---|---|---|
committer | Christian Theune <ct@flyingcircus.io> | 2016-01-05 00:40:40 +0100 |
commit | 14ebde52893263930cdcde1406cc91cc5c42556f (patch) | |
tree | 2b65c11405f9aef33eb284208716f9cb5b434599 /src/libexpr/json-to-value.cc | |
parent | b8258a4475726b56a4caa6553568c67a343a091d (diff) |
First hit at providing support for floats in the language.
Diffstat (limited to 'src/libexpr/json-to-value.cc')
-rw-r--r-- | src/libexpr/json-to-value.cc | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/libexpr/json-to-value.cc b/src/libexpr/json-to-value.cc index 0898b5609..7ef2b2c56 100644 --- a/src/libexpr/json-to-value.cc +++ b/src/libexpr/json-to-value.cc @@ -105,17 +105,22 @@ static void parseJSON(EvalState & state, const char * & s, Value & v) mkString(v, parseJSONString(s)); } - else if (isdigit(*s) || *s == '-') { - bool neg = false; - if (*s == '-') { - neg = true; - if (!*++s) throw JSONParseError("unexpected end of JSON number"); + else if (isdigit(*s) || *s == '-' || *s == '.' ) { + // Buffer into a string first, then use built-in C++ conversions + std::string tmp_number; + ValueType number_type = tInt; + + while (isdigit(*s) || *s == '-' || *s == '.' || *s == 'e' || *s == 'E') { + if (*s == '.' || *s == 'e' || *s == 'E') + number_type = tFloat; + tmp_number.append(*s++, 1); + } + + if (number_type == tFloat) { + mkFloat(v, stod(tmp_number)); + } else { + mkInt(v, stoi(tmp_number)); } - NixInt n = 0; - // FIXME: detect overflow - while (isdigit(*s)) n = n * 10 + (*s++ - '0'); - if (*s == '.' || *s == 'e') throw JSONParseError("floating point JSON numbers are not supported"); - mkInt(v, neg ? -n : n); } else if (strncmp(s, "true", 4) == 0) { |