Class: Sexpistol
- Inherits:
-
Object
- Object
- Sexpistol
- Defined in:
- lib/sexpistol/sexpistol.rb
Overview
This class contains our logic for parsing S-Expressions. They are turned into a native Ruby representation like:
[:def, :something [:lambda, [:a], [:do_something]]]
Instance Attribute Summary collapse
-
#ruby_keyword_literals ⇒ Object
Returns the value of attribute ruby_keyword_literals.
-
#scheme_compatability ⇒ Object
Returns the value of attribute scheme_compatability.
Instance Method Summary collapse
-
#convert_ruby_keyword_literals(expression) ⇒ Object
Convert symbols corresponding to Ruby’s keyword literals into their literal forms.
-
#convert_scheme_literals(data) ⇒ Object
Convert nil, true and false into (), #t and #f for compatability with Scheme.
-
#parse_string(string) ⇒ Object
Parse a string containing an S-Expression into a nested set of Ruby arrays.
-
#to_sexp(data) ⇒ Object
Convert a set of nested arrays back into an S-Expression.
Instance Attribute Details
#ruby_keyword_literals ⇒ Object
Returns the value of attribute ruby_keyword_literals.
7 8 9 |
# File 'lib/sexpistol/sexpistol.rb', line 7 def ruby_keyword_literals @ruby_keyword_literals end |
#scheme_compatability ⇒ Object
Returns the value of attribute scheme_compatability.
7 8 9 |
# File 'lib/sexpistol/sexpistol.rb', line 7 def scheme_compatability @scheme_compatability end |
Instance Method Details
#convert_ruby_keyword_literals(expression) ⇒ Object
Convert symbols corresponding to Ruby’s keyword literals into their literal forms
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sexpistol/sexpistol.rb', line 19 def convert_ruby_keyword_literals(expression) return recursive_map(expression) do |x| case x when :'nil' then nil when :'true' then true when :'false' then false else x end end end |
#convert_scheme_literals(data) ⇒ Object
Convert nil, true and false into (), #t and #f for compatability with Scheme
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sexpistol/sexpistol.rb', line 32 def convert_scheme_literals(data) return recursive_map(data) do |x| case x when nil then [] when true then :"#t" when false then :"#f" else x end end end |
#parse_string(string) ⇒ Object
Parse a string containing an S-Expression into a nested set of Ruby arrays
11 12 13 14 15 |
# File 'lib/sexpistol/sexpistol.rb', line 11 def parse_string(string) tree = SexpistolParser.new(string).parse return convert_ruby_keyword_literals(tree) if(@ruby_keyword_literals) return tree end |
#to_sexp(data) ⇒ Object
Convert a set of nested arrays back into an S-Expression
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sexpistol/sexpistol.rb', line 44 def to_sexp(data) data = convert_scheme_literals(data) if(@scheme_compatability) if( data.is_a?(Array)) mapped = data.map do |item| if( item.is_a?(Array)) to_sexp(item) elsif item.is_a?(String) "\"" + item + "\"" else item.to_s end end "(" + mapped.join(" ") + ")" else data.to_s end end |