From b5d8ac77697ce4030bd25b0c6562e852dd25fc11 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Wed, 3 May 2023 11:24:38 -0700 Subject: [PATCH] Read `1.` as a float/decimal. Fixes #23 --- lib/sxp/reader/basic.rb | 2 +- spec/reader_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/sxp/reader/basic.rb b/lib/sxp/reader/basic.rb index e23baf6..21ef63a 100644 --- a/lib/sxp/reader/basic.rb +++ b/lib/sxp/reader/basic.rb @@ -26,7 +26,7 @@ def read_atom case buffer = read_literal when '.' then buffer.to_sym when RATIONAL then Rational($1.to_i, $2.to_i) - when DECIMAL then Float(buffer) # FIXME? + when DECIMAL then Float(buffer.end_with?('.') ? "#{buffer}0" : buffer) when INTEGER then Integer(buffer) else buffer.to_sym end diff --git a/spec/reader_spec.rb b/spec/reader_spec.rb index b2212ad..971116f 100644 --- a/spec/reader_spec.rb +++ b/spec/reader_spec.rb @@ -20,6 +20,25 @@ end end + context "when reading numbers" do + { + "1" => Integer("1"), + "+1" => Integer("+1"), + "-1" => Integer("-1"), + "1." => Float("1.0"), + "1.1" => Float("1.1"), + "+1.1" => Float("1.1"), + "-1.1" => Float("-1.1"), + "1/2" => Rational(1, 2), + "+1/2" => Rational(1, 2), + "-1/2" => Rational(-1, 2), + }.each do |c, result| + it "reads #{c.inspect} as #{result.inspect}" do + expect(read(c)).to eql result + end + end + end + context "when reading lists" do it "reads '()' as an empty list" do expect(read('()')).to eq []