Class: Sexpistol

Inherits:
Object
  • Object
show all
Defined in:
lib/sexpistol.rb,
lib/sexpistol/parser.rb,
lib/sexpistol/version.rb,
lib/sexpistol/s_expression_array.rb

Defined Under Namespace

Classes: Parser, SExpressionArray

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.convert_ruby_keyword_literals(expression) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/sexpistol.rb', line 28

def self.convert_ruby_keyword_literals(expression)
  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



39
40
41
42
43
44
45
46
# File 'lib/sexpistol.rb', line 39

def self.convert_scheme_literals(data)
  case data
  when nil then []
  when true then :'#t'
  when false then :'#f'
  else data
  end
end

.parse(string, parse_ruby_keyword_literals: false) ⇒ Object



8
9
10
11
12
13
# File 'lib/sexpistol.rb', line 8

def self.parse(string, parse_ruby_keyword_literals: false)
  tree = Sexpistol::Parser.new(string).parse
  return convert_ruby_keyword_literals(tree) if parse_ruby_keyword_literals

  tree
end

.recursive_map(data, &block) ⇒ Object



48
49
50
51
52
# File 'lib/sexpistol.rb', line 48

def self.recursive_map(data, &block)
  return data.map { |x| recursive_map(x, &block) } if data.is_a?(Array)

  block.call(data)
end

.to_sexp(data, scheme_compatability: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sexpistol.rb', line 15

def self.to_sexp(data, scheme_compatability: false)
  data = convert_scheme_literals(data) if scheme_compatability

  return "\"#{data}\"" if data.is_a?(String)
  return data.to_s unless data.is_a?(Array)

  if data.is_a?(SExpressionArray)
    data.map { |x| to_sexp(x, scheme_compatability: scheme_compatability) }.join(' ')
  else
    "(#{data.map { |x| to_sexp(x, scheme_compatability: scheme_compatability) }.join(' ')})"
  end
end