Class: Node2Sexp

Inherits:
Object show all
Defined in:
lib/sql/bathon-sxp.rb

Constant Summary collapse

BINARY_METHODS =
[:+, :-, :*, :/, :%, :**, :^, :<, :>, :<=, :>=, :==, :|, :&]

Instance Method Summary collapse

Constructor Details

#initialize(binding) ⇒ Node2Sexp

Returns a new instance of Node2Sexp.



16
17
18
# File 'lib/sql/bathon-sxp.rb', line 16

def initialize(binding)
  @binding = binding
end

Instance Method Details

#and_to_sexp(hash) ⇒ Object



127
128
129
# File 'lib/sql/bathon-sxp.rb', line 127

def and_to_sexp(hash)
   [:& , to_sexp(hash[:first]), to_sexp(hash[:second])]
end

#call_to_sexp(hash) ⇒ Object

:call nodes: method call with explicit receiver: nil.foo => [:call, :mid=>:foo, :recv=>[:nil, {]}] nil == nil =>

:call, [[:nil, {]]], :mid=>:==, :recv=>[:nil, {}]}


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sql/bathon-sxp.rb', line 52

def call_to_sexp(hash)
  mid=hash[:mid]
  args=process_args(hash[:args])
  if is_sql?(hash[:recv])
     return [mid, *args]
  end
  recv=to_sexp(hash[:recv])
  if BINARY_METHODS.include?(mid)
	[mid, recv, *args]
  else
     recv.send(mid,*args)
  end
end

#false_to_sexp(hash) ⇒ Object



93
# File 'lib/sql/bathon-sxp.rb', line 93

def false_to_sexp(hash) false end

#fcall_to_sexp(hash) ⇒ Object Also known as: vcall_to_sexp

:fcall nodes: function call (no explicit receiver): foo() => [:fcall, :mid=>:foo] foo(nil) => [:fcall, [[:nil, {]]], :mid=>:foo]



69
70
71
72
73
74
75
# File 'lib/sql/bathon-sxp.rb', line 69

def fcall_to_sexp(hash)
  if origself.methods.include?(hash[:mid].to_s)
     origself.send(hash[:mid],*process_args(hash[:args]))
  else
	[hash[:mid], *process_args(hash[:args])]
  end
end

#is_sql?(arg) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/sql/bathon-sxp.rb', line 37

def is_sql?(arg)
   return true if arg[0]==:vcall and arg[1][:mid]==:sql
   return true if [:lvar,:dvar].include?(arg[0]) and arg[1][:vid]==:sql
   return false
end

#lit_to_sexp(hash) ⇒ Object Also known as: str_to_sexp

:lit nodes: literals 1 => [:lit, :lit=>1] :abc => [:lit, :lit=>:abc]



84
85
86
# File 'lib/sql/bathon-sxp.rb', line 84

def lit_to_sexp(hash)
  hash[:lit]
end

#lvar_to_sexp(hash) ⇒ Object Also known as: dvar_to_sexp, ivar_to_sexp, cvar_to_sexp, const_to_sexp

:lvar nodes: local variables var => [:lvar, :vid=>:var] cnt is the index in the lvar table



99
100
101
# File 'lib/sql/bathon-sxp.rb', line 99

def lvar_to_sexp(hash)
  eval(hash[:vid].to_s, @binding)
end

#nil_to_sexp(hash) ⇒ Object



92
# File 'lib/sql/bathon-sxp.rb', line 92

def nil_to_sexp(hash) nil end

#not_to_sexp(hash) ⇒ Object

:not nodes: boolean negation not :field => [:not, {:lit=>:field]}] !:field => [:not, {:lit=>:field]}]



118
119
120
121
122
123
124
125
# File 'lib/sql/bathon-sxp.rb', line 118

def not_to_sexp(hash)
  body = to_sexp(hash[:body])
  if Array === body && body[0] == :== && body.size == 3
    [:"!=", body[1], body[2]]
  else
    [:not, body]
  end
end

#or_to_sexp(hash) ⇒ Object



131
132
133
# File 'lib/sql/bathon-sxp.rb', line 131

def or_to_sexp(hash)
   [:| , to_sexp(hash[:first]), to_sexp(hash[:second])]
end

#origselfObject

returns the self in which the block was contained



44
45
46
# File 'lib/sql/bathon-sxp.rb', line 44

def origself
   eval("self",@binding)
end

#process_args(args_node) ⇒ Object

fixed argument lists are represented as :array nodes, e.g.

:array, [argnode1, argnode2, …]


28
29
30
31
32
33
34
35
# File 'lib/sql/bathon-sxp.rb', line 28

def process_args(args_node)
  return [] unless args_node
  if args_node.first == :array
    args_node.last.map { |node| to_sexp(node) }
  else
    raise "variable arguments not allowed"
  end
end

#self_to_sexp(hash) ⇒ Object



111
112
113
# File 'lib/sql/bathon-sxp.rb', line 111

def self_to_sexp(hash)
   origself
end

#to_sexp(node) ⇒ Object

(transformed) nodes are arrays, that look like:

:type, attribute hash or array of nodes


22
23
24
# File 'lib/sql/bathon-sxp.rb', line 22

def to_sexp(node)
  node && send("#{node.first}_to_sexp", node.last)
end

#true_to_sexp(hash) ⇒ Object



94
# File 'lib/sql/bathon-sxp.rb', line 94

def true_to_sexp(hash) true end