class Mulang::Ruby::AstProcessor

Public Instance Methods

_() click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 46
def _
  Object.new.tap { |it| it.define_singleton_method(:==) { |_| true } }
end
handle_send_with_args(node, extra_args=[]) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 279
def handle_send_with_args(node, extra_args=[])
  receptor, message, *args = *node
  receptor ||= s(:self)

  ms :Send, process(receptor), message_reference(message), (process_all(args) + extra_args)
end
handler_missing(*args) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 275
def handler_missing(*args)
  ms :Other, args.to_s, nil
end
message_reference(message) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 286
def message_reference(message)
  case message
    when :==     then primitive :Equal
    when :!=     then primitive :NotEqual
    when :!      then primitive :Negation
    when :'&&'   then primitive :And
    when :'||'   then primitive :Or
    when :hash   then primitive :Hash
    when :>=     then primitive :GreatherOrEqualThan
    when :>      then primitive :GreatherThan
    when :<=     then primitive :LessOrEqualThan
    when :<      then primitive :LessThan
    when :+      then primitive :Plus
    when :-      then primitive :Minus
    when :*      then primitive :Multiply
    when :/      then primitive :Divide
    when :length then primitive :Size
    when :size   then primitive :Size
    else ms :Reference, message
  end
end
on_and(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 104
def on_and(node)
  value, other = *node

  simple_send process(value), '&&', [process(other)]
end
on_and_asgn(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 249
def on_and_asgn(node)
  assignee, value = *node
  on_op_asgn s :op_asgn, assignee, '&&', value
end
on_arg(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 153
def on_arg(node)
  name, _ = *node
  if name.is_a? Parser::AST::Node
    process name
  else
    ms :VariablePattern, name
  end
end
Also aliased as: on_restarg, on_procarg0
on_array(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 270
def on_array(node)
  elements = *node
  {tag: :MuList, contents: process_all(elements)}
end
on_begin(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 27
def on_begin(node)
  if node.children.size == 1 && node.children[0].nil?
    none # ruby < 2.6 only
  else
    sequence(*process_all(node))
  end
end
on_block(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 135
def on_block(node)
  send, parameters, body = *node
  lambda = ms(:Lambda, process_all(parameters), process(body) || none)
  handle_send_with_args send, [lambda]
end
on_casgn(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 209
def on_casgn(node)
  _ns, id, value = *node
  ms :Assignment, id, process(value)
end
on_class(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 10
def on_class(node)
  name, superclass, body = *node

  _, class_name = *name
  _, superclass_name = *superclass

  ms :Class, class_name, superclass_name, process(body)
end
on_const(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 257
def on_const(node)
  _ns, value = *node
  ms :Reference, value
end
on_def(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 122
def on_def(node)
  id, args, body = *node

  case id
  when :equal?, :eql?, :==
    primitive_method :Equal, process_all(args), process(body)
  when :hash
    primitive_method :Hash, process_all(args), process(body)
  else
    simple_method id, process_all(args), process(body)
  end
end
on_defs(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 116
def on_defs(node)
  _target, id, args, body = *node

  simple_method id, process_all(args), process(body)
end
on_dstr(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 93
def on_dstr(node)
  parts = *node

  simple_send ms(:MuList, process_all(parts)), :join, []
end
on_ensure(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 77
def on_ensure(node)
  catch, finally = *node
  try, catches = on_rescue(catch)[:contents]
  ms :Try, try, catches, process(finally)
end
on_false(_) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 266
def on_false(_)
  ms :MuBool, false
end
on_float(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 186
def on_float(node)
  value, _ = *node
  ms :MuNumber, value
end
Also aliased as: on_int
on_for(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 162
def on_for(node)
  variable, list, block = *node

  pattern = ms(:VariablePattern, variable.children.first)
  ms(:For, [ms(:Generator, pattern, process(list))], process(block) || none)
end
on_if(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 193
def on_if(node)
  condition, if_true, if_false = *node

  ms :If, process(condition), process(if_true), process(if_false)
end
on_int(node)
Alias for: on_float
on_irange(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 83
def on_irange(node)
  ms :Other, node.to_s, nil
end
on_ivar(node)
Alias for: on_lvar
on_ivasgn(node)
Alias for: on_lvasgn
on_kwbegin(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 73
def on_kwbegin(node)
  process node.to_a.first
end
on_lvar(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 199
def on_lvar(node)
  value = *node
  ms :Reference, value.first
end
Also aliased as: on_ivar
on_lvasgn(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 204
def on_lvasgn(node)
  id, value = *node
  ms :Assignment, id, process(value)
end
Also aliased as: on_ivasgn
on_module(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 19
def on_module(node)
  name, body = *node

  _, module_name = *name

  ms :Object, module_name, process(body)
end
on_nil(_) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 145
def on_nil(_)
  mnil
end
on_op_asgn(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 214
def on_op_asgn(node)
  assignee, message, value = *node

  if assignee.type == :send
    property_assignment assignee, message, value
  else
    var_assignment assignee, message, value
  end
end
on_optarg(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 169
def on_optarg(node)
  ms :OtherPattern, node.to_s, nil
end
on_or(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 99
def on_or(node)
  value, other = *node
  simple_send process(value), '||', [process(other)]
end
on_or_asgn(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 244
def on_or_asgn(node)
  assignee, value = *node
  on_op_asgn s :op_asgn, assignee, '||', value
end
on_procarg0(node)
Alias for: on_arg
on_regexp(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 87
def on_regexp(node)
  value, _ops = *node

  simple_send ms(:Reference, :Regexp), :new, [process(value)]
end
on_resbody(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 40
def on_resbody(node)
  patterns, variable, block = *node

  [to_mulang_pattern(patterns, variable), process(block) || none]
end
on_rescue(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 35
def on_rescue(node)
  try, *catch, _ = *node
  ms :Try, process(try), process_all(catch), none
end
on_restarg(node)
Alias for: on_arg
on_return(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 110
def on_return(node)
  value = *node

  ms(:Return, process(value.first))
end
on_self(_) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 149
def on_self(_)
  ms :Self
end
on_send(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 141
def on_send(node)
  handle_send_with_args(node)
end
on_str(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 176
def on_str(node)
  value, _ = *node
  ms :MuString, value
end
on_sym(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 181
def on_sym(node)
  value, _ = *node
  ms :MuSymbol, value.to_s
end
on_true(_) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 262
def on_true(_)
  ms :MuBool, true
end
process(node) click to toggle source
Calls superclass method
# File lib/mulang/ruby/ast_processor.rb, line 6
def process(node)
  node.nil? ? none : super
end
property_assignment(assignee, message, value) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 229
def property_assignment(assignee, message, value)
  receiver, accessor, *accessor_args = *assignee

  reasign accessor, process_all(accessor_args), process(receiver), message, process(value)
end
reasign(accessor, args, id, message, value) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 235
def reasign(accessor, args, id, message, value)
  simple_send id,
              "#{accessor}=".to_sym,
              args + [ms(:Send,
                        simple_send(id, accessor, args),
                        message_reference(message),
                        [value])]
end
to_mulang_pattern(patterns, variable) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 50
def to_mulang_pattern(patterns, variable)
  case [patterns, variable]
    when [nil, nil]
      ms :WildcardPattern
    when [nil, _]
      ms :VariablePattern, variable.to_a.first
    when [_, nil]
      to_single_pattern patterns
    else
      ms(:AsPattern, variable.to_a.first, to_single_pattern(patterns))
  end
end
to_single_pattern(patterns) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 63
def to_single_pattern(patterns)
  mu_patterns = patterns.to_a.map { |it| to_type_pattern it }
  mu_patterns.size == 1 ? mu_patterns.first : ms(:UnionPattern, mu_patterns)
end
to_type_pattern(node) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 68
def to_type_pattern(node)
  _, type = *node
  ms :TypePattern, type
end
var_assignment(assignee, message, value) click to toggle source
# File lib/mulang/ruby/ast_processor.rb, line 224
def var_assignment(assignee, message, value)
  id = assignee.to_a.first
  ms :Assignment, id, ms(:Send, ms(:Reference, id), message_reference(message), [process(value)])
end