Skip to content

Commit

Permalink
Read 1. as a float/decimal.
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
gkellogg committed May 3, 2023
1 parent d48e6f3 commit b5d8ac7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sxp/reader/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down

0 comments on commit b5d8ac7

Please sign in to comment.