Class: Estreet::SwitchStatement

Inherits:
Statement show all
Defined in:
lib/estreet/switch_statement.rb

Instance Attribute Summary

Attributes inherited from Node

#source_location

Instance Method Summary collapse

Methods inherited from Statement

#<<, #to_statement

Methods inherited from Node

#as_json, #loc, #type

Constructor Details

#initialize(discriminant, kases) ⇒ SwitchStatement

kases is an array of 2-element arrays, or 1 for the default case



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/estreet/switch_statement.rb', line 4

def initialize(discriminant, kases)
  @kases = kases.map do |kase|
    raise TypeError, "Invalid case: #{kase}" unless kase.is_a? Array

    case kase.length
    when 2 then SwitchCase.new(*kase)
    when 1 then SwitchCase.default(kase.first)
    else raise TypeError, "Invalid case: #{kase}"
    end
  end
  
  @discriminant = discriminant.to_expression
end

Instance Method Details

#attributesObject



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

def attributes
  super.merge(
    discriminant: @discriminant,
    cases: @kases,
    lexical: false, # TODO: an ES6 thing?
  )
end