Class: Opal::Sexp

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/parser/sexp.rb

Overview

[Opal::Sexp] is used to build up the syntax tree inside [Opal::Parser]. The compiler then steps through the sexp trees to generate the javascript code.

For example, an array of integers [1, 2] might be represented by:

s(:array, s(:int, 1), s(:int, 2))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Sexp


15
16
17
# File 'lib/opal/parser/sexp.rb', line 15

def initialize(args)
  @array = args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object


31
32
33
# File 'lib/opal/parser/sexp.rb', line 31

def method_missing(sym, *args, &block)
  @array.send sym, *args, &block
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array


11
12
13
# File 'lib/opal/parser/sexp.rb', line 11

def array
  @array
end

#sourceObject

Returns the value of attribute source


13
14
15
# File 'lib/opal/parser/sexp.rb', line 13

def source
  @source
end

Instance Method Details

#<<(other) ⇒ Object


35
36
37
38
# File 'lib/opal/parser/sexp.rb', line 35

def <<(other)
  @array << other
  self
end

#==(other) ⇒ Object Also known as: eql?


53
54
55
56
57
58
59
# File 'lib/opal/parser/sexp.rb', line 53

def ==(other)
  if other.is_a? Sexp
    @array == other.array
  else
    @array == other
  end
end

#childrenObject


27
28
29
# File 'lib/opal/parser/sexp.rb', line 27

def children
  @array[1..-1]
end

#columnObject


67
68
69
# File 'lib/opal/parser/sexp.rb', line 67

def column
  @source && @source[1]
end

#dupObject


49
50
51
# File 'lib/opal/parser/sexp.rb', line 49

def dup
  Sexp.new(@array.dup)
end

#inspectObject Also known as: to_s


71
72
73
# File 'lib/opal/parser/sexp.rb', line 71

def inspect
  "(#{@array.map { |e| e.inspect }.join ', '})"
end

#lineObject


63
64
65
# File 'lib/opal/parser/sexp.rb', line 63

def line
  @source && @source[0]
end

#pretty_inspectObject


75
76
77
# File 'lib/opal/parser/sexp.rb', line 75

def pretty_inspect
  "(#{line ? "#{line} " : ''}#{@array.map { |e| e.inspect }.join ', '})"
end

#push(*parts) ⇒ Object


40
41
42
43
# File 'lib/opal/parser/sexp.rb', line 40

def push(*parts)
  @array.push(*parts)
  self
end

#to_aryObject


45
46
47
# File 'lib/opal/parser/sexp.rb', line 45

def to_ary
  @array
end

#typeObject


19
20
21
# File 'lib/opal/parser/sexp.rb', line 19

def type
  @array[0]
end

#type=(type) ⇒ Object


23
24
25
# File 'lib/opal/parser/sexp.rb', line 23

def type=(type)
  @array[0] = type
end