class AST

Public Class Methods

new(a_name, a_subname, a_val) click to toggle source
# File lib/ast.rb, line 2
def initialize(a_name, a_subname, a_val)
  @name = a_name
  @subname = a_subname
  @val = a_val
end

Public Instance Methods

eval() click to toggle source
# File lib/ast.rb, line 71
def eval
  to_s.to_f
end
find_all(options={}) click to toggle source
# File lib/ast.rb, line 20
def find_all(options={})
  ret = []
  ret << self if match options
  sub_options = options.merge top: true
  @val.each do |v|
    if v.is_a? AST
      ret.concat (v.find_all sub_options)
    end
  end
  ret
end
find_left(options={}) click to toggle source
# File lib/ast.rb, line 32
def find_left(options={})
  return self if match options
  sub_options = options.merge top: true
  @val.each do |v|
    if v.is_a? AST
      ret = v.find_left sub_options
      return ret if ret
    end
  end
  nil
end
find_top(options={}) click to toggle source
# File lib/ast.rb, line 44
def find_top(options={})
  return self if match options
  sub_options = options.merge top: true
  @val.each do |v|
    if v.is_a? AST
      return v if v.match sub_options
    end
  end
  nil
end
inspect() click to toggle source
# File lib/ast.rb, line 95
def inspect
  "<#{@name}: #{@val}>"
end
match(options) click to toggle source
# File lib/ast.rb, line 12
def match(options)
  (
    {top: true}.merge(options)[:top] &&
    @name == options[:name] &&
    (options[:subname].nil? || @subname == options[:subname])
  )
end
name() click to toggle source
# File lib/ast.rb, line 55
def name
  @name
end
norm_name() click to toggle source
# File lib/ast.rb, line 83
def norm_name
  s_all = to_s.strip
  node = find_left(name: :r_tbl_name) || find_left(name: :ident)
  s = node.to_s.strip
  raise 'Internal Error: trying to normalize not-a-name' if s_all != s
  if s.start_with? '`'
    s[1..-2] # strip ` from both beginning and end
  else
    s
  end
end
subname() click to toggle source
# File lib/ast.rb, line 59
def subname
  @subname
end
to_list() click to toggle source
# File lib/ast.rb, line 75
def to_list
  to_list_helper.flatten
end
to_s() click to toggle source
# File lib/ast.rb, line 79
def to_s
  @val.map { |v| v.to_s }.join
end
update(a_name, a_subname, a_val) click to toggle source
# File lib/ast.rb, line 8
def update(a_name, a_subname, a_val)
  initialize(a_name, a_subname, a_val)
end
val() click to toggle source
# File lib/ast.rb, line 63
def val
  @val
end
val=(a_val) click to toggle source
# File lib/ast.rb, line 67
def val=(a_val)
  @val = a_val
end

Protected Instance Methods

to_list_helper() click to toggle source
# File lib/ast.rb, line 101
def to_list_helper
  @val.map { |v|
    if v.is_a?(AST) && v.name == @name
      v.to_list_helper
    else
      [v]
    end
  }
end