Skip to content

Commit

Permalink
Support Common Lisp vectors (#(foo)) using the Ruby Vector class.
Browse files Browse the repository at this point in the history
For #16.
  • Loading branch information
gkellogg committed Dec 17, 2021
1 parent 57861f5 commit adfe085
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
21 changes: 15 additions & 6 deletions lib/sxp/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*-
require 'bigdecimal'
require 'matrix'
require 'time'

##
Expand Down Expand Up @@ -146,12 +147,6 @@ class Array
##
# Returns the SXP representation of this object.
#
# If array is of the form `[:base, uri, ..]`, the base_uri is taken from the second value
#
# If array is of the form `[:prefix, [..], ..]`, prefixes are taken from the second value
#
# Prefixes always are terminated by a ':'
#
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
Expand All @@ -160,6 +155,20 @@ def to_sxp(prefixes: nil, base_uri: nil)
end
end

##
# Extensions for Ruby's `Vector` class.
class Vector
##
# Returns the SXP representation of this object.
#
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
'#(' << to_a.map { |x| x.to_sxp(prefixes: prefixes, base_uri: base_uri) }.join(' ') << ')'
end
end

##
# Extensions for Ruby's `Hash` class.
class Hash
Expand Down
5 changes: 4 additions & 1 deletion lib/sxp/reader/common_lisp.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- encoding: utf-8 -*-
require 'matrix'

module SXP; class Reader
##
# A Common Lisp S-expressions parser.
Expand Down Expand Up @@ -88,7 +90,8 @@ def read_symbol(delimiter = nil)
#
# @return [Array]
def read_vector
raise NotImplementedError, "#{self.class}#read_vector" # TODO
list = read_list(')')
Vector.[](*list)
end

##
Expand Down
10 changes: 7 additions & 3 deletions spec/common_lisp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@
end
end

context "when reading vectors", pending: "Support for vectors" do
context "when reading vectors" do
it "reads `#()` as an empty vector" do
expect(read(%q(#()))).to eq []
expect(read(%q(#()))).to eq Vector[]
end

it "reads `#(1 2 3)` as a vector" do
expect(read(%q(#(1 2 3)))).to eq [1, 2, 3]
expect(read(%q(#(1 2 3)))).to eq Vector[1, 2, 3]
end

it "reads `#(hello \"world\")` as a vector" do
expect(read(%q(#(hello "world")))).to eq Vector[:hello, "world"]
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
end
end

describe "Vector" do
{
Vector[] => %q(#()),
Vector[1, 2, 3] => %q(#(1 2 3)),
Vector[:hello, "world"] => %q(#(hello "world"))
}.each do |value, result|
it "returns #{result.inspect} for #{value.inspect}" do
expect(value.to_sxp).to eq result
end
end
end

describe "RDF::Node#to_sxp" do
specify { expect(RDF::Node.new("a").to_sxp).to eq %q(_:a)}
end
Expand Down

0 comments on commit adfe085

Please sign in to comment.