Class: ToSource::Emitter

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/to_source/emitter.rb,
lib/to_source/emitter/if.rb,
lib/to_source/emitter/for.rb,
lib/to_source/emitter/case.rb,
lib/to_source/emitter/flip.rb,
lib/to_source/emitter/iter.rb,
lib/to_source/emitter/loop.rb,
lib/to_source/emitter/send.rb,
lib/to_source/emitter/util.rb,
lib/to_source/emitter/when.rb,
lib/to_source/emitter/alias.rb,
lib/to_source/emitter/begin.rb,
lib/to_source/emitter/block.rb,
lib/to_source/emitter/class.rb,
lib/to_source/emitter/match.rb,
lib/to_source/emitter/scope.rb,
lib/to_source/emitter/splat.rb,
lib/to_source/emitter/super.rb,
lib/to_source/emitter/undef.rb,
lib/to_source/emitter/yield.rb,
lib/to_source/emitter/access.rb,
lib/to_source/emitter/define.rb,
lib/to_source/emitter/ensure.rb,
lib/to_source/emitter/match2.rb,
lib/to_source/emitter/match3.rb,
lib/to_source/emitter/module.rb,
lib/to_source/emitter/rescue.rb,
lib/to_source/emitter/static.rb,
lib/to_source/emitter/unless.rb,
lib/to_source/emitter/defined.rb,
lib/to_source/emitter/literal.rb,
lib/to_source/emitter/nth_ref.rb,
lib/to_source/emitter/z_super.rb,
lib/to_source/emitter/back_ref.rb,
lib/to_source/emitter/to_array.rb,
lib/to_source/emitter/toplevel.rb,
lib/to_source/emitter/push_args.rb,
lib/to_source/emitter/to_string.rb,
lib/to_source/emitter/assignment.rb,
lib/to_source/emitter/block_pass.rb,
lib/to_source/emitter/empty_body.rb,
lib/to_source/emitter/op_assign1.rb,
lib/to_source/emitter/op_assign2.rb,
lib/to_source/emitter/scope_name.rb,
lib/to_source/emitter/splat_when.rb,
lib/to_source/emitter/ensure_body.rb,
lib/to_source/emitter/scoped_name.rb,
lib/to_source/emitter/collect_splat.rb,
lib/to_source/emitter/keyword_value.rb,
lib/to_source/emitter/literal/range.rb,
lib/to_source/emitter/block_argument.rb,
lib/to_source/emitter/execute_string.rb,
lib/to_source/emitter/literal/regexp.rb,
lib/to_source/emitter/binary_operator.rb,
lib/to_source/emitter/literal/dynamic.rb,
lib/to_source/emitter/singleton_class.rb,
lib/to_source/emitter/actual_arguments.rb,
lib/to_source/emitter/concat_arguments.rb,
lib/to_source/emitter/formal_arguments.rb,
lib/to_source/emitter/rescue_condition.rb,
lib/to_source/emitter/default_arguments.rb,
lib/to_source/emitter/element_reference.rb,
lib/to_source/emitter/pattern_arguments.rb,
lib/to_source/emitter/assignment/element.rb,
lib/to_source/emitter/assignment/constant.rb,
lib/to_source/emitter/assignment/variable.rb,
lib/to_source/emitter/assignment_operator.rb,
lib/to_source/emitter/multiple_assignment.rb,
lib/to_source/emitter/send_with_arguments.rb,
lib/to_source/emitter/assignment/attribute.rb,
lib/to_source/emitter/unary_operator_method.rb,
lib/to_source/emitter/binary_operator_method.rb,
lib/to_source/emitter/literal/dynamic/regexp.rb,
lib/to_source/emitter/literal/regexp/options.rb

Overview

Abstract base class for emitter

Defined Under Namespace

Classes: Access, ActualArguments, Alias, Assignment, AssignmentOperator, BackRef, Begin, BinaryOperator, BinaryOperatorMethod, Block, BlockArgument, BlockPass, Case, Class, CollectSplat, ConcatArguments, DefaultArguments, Define, DefineSingleton, Defined, ElementReference, EmptyBody, Ensure, EnsureBody, ExecuteString, Flip, For, For19, ForArguments, FormalArguments, If, Iter, Iter19, KeywordValue, Literal, Loop, Match, Match2, Match3, Module, MultipleAssignment, NthRef, OpAssign1, OpAssign2, PatternArguments, PushArgs, Rescue, RescueCondition, Scope, ScopeName, ScopedName, Send, SendWithArguments, SingletonClass, Splat, SplatWhen, Static, Super, ToArray, ToString, Toplevel, UnaryOperatorMethod, Undef, Unless, Util, VAlias, When, Yield, ZSuper

Constant Summary collapse

REGISTRY =

The emitter registry

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, buffer) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize object

Parameters:

  • node (Rubinius::AST::Node)
  • buffer (Array<Command>)


140
141
142
143
# File 'lib/to_source/emitter.rb', line 140

def initialize(node, buffer)
  @node, @buffer = node, buffer
  dispatch
end

Instance Attribute Details

#bufferArray<Command> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return buffer

Returns:



121
122
123
# File 'lib/to_source/emitter.rb', line 121

def buffer
  @buffer
end

#nodeRubinius::AST::Node (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return node

Returns:

  • (Rubinius::AST::Node)


129
130
131
# File 'lib/to_source/emitter.rb', line 129

def node
  @node
end

Class Method Details

.build(node, buffer = []) ⇒ Emitter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build emitter for node

Parameters:

  • node (Rubinius::AST::Node)
  • buffer (Array) (defaults to: [])

Returns:



18
19
20
21
22
23
24
# File 'lib/to_source/emitter.rb', line 18

def self.build(node, buffer = [])
  klass = node.class
  emitter = REGISTRY.fetch(klass) do
    raise RuntimeError, "No emmitter for: #{klass} at line: #{node.line}"
  end
  emitter.new(node, buffer)
end

.define_predicate(name) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define predicate

Parameters:

  • name (Symbol)

Returns:

  • (undefined)


105
106
107
108
109
110
111
# File 'lib/to_source/emitter.rb', line 105

def self.define_predicate(name)
  predicate = :"#{name}?"
  define_method(predicate) do
    !!send(name)
  end
  private(predicate)
end

.run(node) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run emitter for node

Parameters:

  • node (Rubinius::AST::Node)

Returns:

  • (String)


34
35
36
# File 'lib/to_source/emitter.rb', line 34

def self.run(node)
  build(node).source
end

Instance Method Details

#sourceString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return source

Returns:

  • (String)


151
152
153
154
155
# File 'lib/to_source/emitter.rb', line 151

def source
  buffer.each_with_object(State.new) do |command, state|
    state.execute(command)
  end.source
end