Module: Furnace::AVM2::Tokens::TokenWithTraits

Included in:
ClassToken, ScriptToken
Defined in:
lib/furnace-avm2/source/declaration_tokens/token_with_traits.rb

Instance Method Summary collapse

Instance Method Details

#transform_trait(trait, options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/furnace-avm2/source/declaration_tokens/token_with_traits.rb', line 32

def transform_trait(trait, options)
  case trait.kind
  when :Method
    MethodToken.new(trait, options)
  when :Getter
    MethodToken.new(trait, options.merge(type: :getter))
  when :Setter
    MethodToken.new(trait, options.merge(type: :setter))
  when :Slot
    SlotToken.new(trait, options.merge(const: false))
  when :Const
    SlotToken.new(trait, options.merge(const: true))
  when :Class
    # nothing
  else
    CommentToken.new(trait, "%%#{trait.kind}", options)
  end
end

#transform_traits(origin, options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/furnace-avm2/source/declaration_tokens/token_with_traits.rb', line 3

def transform_traits(origin, options)
  tokens = []

  vars, methods = origin.traits.partition do |trait|
    [:Class, :Slot, :Const].include? trait.kind
  end

  tokens += vars.map { |trait| transform_trait trait, options }

  if tokens.any?
    tokens << Furnace::Code::NewlineToken.new(origin, options)
  end

  if options[:environment] == :class
    if options[:static]
      tokens << CommentToken.new(origin, "Static initializer", options)
      tokens << CommentToken.new(origin,
        ConstructorToken.new(origin, options.merge(commented: true)),
      options)
    else
      tokens << ConstructorToken.new(origin, options)
    end
  end

  tokens += methods.map { |trait| transform_trait trait, options }

  tokens
end