Class: Sexp

Inherits:
Array
  • Object
show all
Defined in:
lib/linguify/sexp.rb

Overview

Copyright © 2011-2012 Patrick Hanevold.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inline_keyword_inlined!(code) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/linguify/sexp.rb', line 170

def self.inline_keyword_inlined! code
  inlines = Sexp.from_array(code).find(:call).select{ |call| call[1] == nil && call[2] == :inline }
  inlines.each do |inline|
    raise "Im lost, dont know what this is (yet)" unless inline[3].sexp_type == :arglist
    raise "Im lost, dont know what this is (yet)" unless inline[3].size == 2
    raise "Im lost, dont know what this is (yet)" unless inline[3][1].sexp_type == :lvar
    var = inline[3][1][1]

    assignments = []
    code.each_with_index do |expression,n|
      if expression.sexp_type == :lasgn and expression[1] == var
        assignments << n
      end
    end
    raise "this should never happen (yes, really!)" unless assignments.size == 1

    code_block = code[assignments.first]
    code.delete_at(assignments.first)

    # now inject code
    inline.clear
    inline[0] = code_block[0]
    inline[1] = code_block[1]
    inline[2] = code_block[2]
  end
end

.lambda_envelope(code) ⇒ Object

Envelope a Sexp in a lambda

Parameters:

  • code (Sexp)

    The Sexp to envelope



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/linguify/sexp.rb', line 94

def self.lambda_envelope code
  # and wrap it up in a lambda envelope for the sexp representation
  Sexp.new(:block,
    Sexp.new(:lasgn,:code, Sexp.new(:iter,
      Sexp.new(:call, nil, :lambda, Sexp.new(:arglist)), nil,
        Sexp.new(:block,
          *code
        )
      )
    )
  )
end

Instance Method Details

#find(needle) ⇒ Object

currently not in use 1.9.2 p180 doesn’t give us a backtrace from the other side

Envelope a Sexp in the debug envelope which basicly dispatch exceptions into our unitverse

def self.debug_envelope code

Sexp.new(:block,
  Sexp.new(:lasgn,:code, Sexp.new(:iter,
    Sexp.new(:call, nil, :lambda, Sexp.new(:arglist)), nil,
      Sexp.new(:rescue,
        Sexp.new(:block,*code),
        Sexp.new(:resbody,
          Sexp.new(:array, Sexp.new(:const, :Exception), Sexp.new(:lasgn, :e, Sexp.new(:gvar, :$!))),
          Sexp.new(:call, Sexp.new(:const, :Linguify), :exception, Sexp.new(:arglist, Sexp.new(:lvar, :e))))))))

end

Parameters:

  • code (Sexp)

    The Sexp to envelope



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/linguify/sexp.rb', line 148

def find needle
  res = []
  res << self if sexp_type == needle
  case sexp_type
  when :lasgn
    self[1] == needle
  when :lvar
    self[1] == needle
  when :call
    self[2] == needle
  when :lvar
    self[1] == needle
  else
    self[1..-1].each do |h|
      if h && h.kind_of?(Sexp)
        res += h.find(needle)
      end
    end
  end
  res
end

#replace_variable_references!(params) ⇒ Object

Recurcively replace all references in a code section

Parameters:

  • replacement (Sexp, Symbol)

    The replacement code. Either a Sexp (containing code to inline) or a symbol

  • needle (Sexp)

    The search needle

  • named_args (Hash)

    The arguments of the code block



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/linguify/sexp.rb', line 32

def replace_variable_references! params

  replacement = params[:replacement]
  needle      = params[:needle]
  named_args  = params[:named_args] || ''

  case sexp_type
  when :lasgn
    self[1]=replacement.sexp if self[1] == needle
  when :lvar
    if self[1] == needle
      unless replacement.inline?
        if replacement.sexp[0] == :str
          self[0] = :str
          self[1]=replacement.sexp[1]
        else
          self[1]=replacement.sexp
        end
      end
    end
  when :call
    self[2]=replacement.sexp if self[2] == needle
  when :lvar
    self[1]=replacement.sexp if self[1] == needle
  end
  # inlining requires complex code:
  if replacement.inline? && [:iter, :block].include?(sexp_type)
    # we have a inline and a block, replace any references with the sexp
    self[1..-1].each_with_index do |h,i|
      if h && h.kind_of?(Sexp) && h == Sexp.new(:lvar, needle)
        # inline references like s(:lvar, :needle)
        # indicates a call to the needle, thus code wants to inline
        h[0] = replacement.sexp[0]
        h[1] = replacement.sexp[1]
      elsif h && h.kind_of?(Sexp) && named_args.has_key?(needle) &&
          Linguify::Reduction.parse(named_args[needle]).named_args.select{ |k,v|
            h == Sexp.new(:call, Sexp.new(:lvar, needle), :[], Sexp.new(:arglist, Sexp.new(:lit, k)))
          }.size == 1
        # code is asking for a injection of one of the argument's with:
        #  s(:call, s(:lvar, :needle), :[], s(:arglist, s(:lit, :argumen)))
        # which in ruby looks like:
        #  needle[:argument]
        # which again is the way we support calling arguments of the neede
        arg = h[3][1][1]
        sexy = Marshal.load(Marshal.dump(Linguify::Reduction.parse(Linguify::Reduction.parse(named_args[needle]).named_args[arg]).sexp)) # sexp handling is not clean cut
        self[i+1] = sexy[3]
      else
        h.replace_variable_references!(:replacement => replacement, :needle => needle, :named_args => named_args) if h && h.kind_of?(Sexp)
      end
    end
  else
    self[1..-1].each do |h|
      h.replace_variable_references!(:replacement => replacement, :needle => needle, :named_args => named_args) if h && h.kind_of?(Sexp)
    end
  end
end

#variable_exists?(needle) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/linguify/sexp.rb', line 107

def variable_exists? needle
  case sexp_type
  when :lasgn
    self[1] == needle
  when :lvar
    self[1] == needle
  when :call
    self[2] == needle
  when :lvar
    self[1] == needle
  else
    self[1..-1].each do |h|
      if h && h.kind_of?(Sexp)
        return true if h.variable_exists?(needle)
      end
    end
    false
  end
end