Class: Opal::Nodes::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
opal/lib/opal/nodes/base.rb

Constant Summary

Constants included from Helpers

Helpers::BASIC_IDENTIFIER_RULES, Helpers::ES3_RESERVED_WORD_EXCLUSIVE, Helpers::ES51_RESERVED_WORD, Helpers::IMMUTABLE_PROPS, Helpers::PROTO_SPECIAL_METHODS, Helpers::PROTO_SPECIAL_PROPS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#current_indent, #empty_line, #indent, #ivar, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #valid_ivar_name?, #valid_name?, #variable

Constructor Details

#initialize(sexp, level, compiler) ⇒ Base

Returns a new instance of Base



32
33
34
35
36
37
# File 'opal/lib/opal/nodes/base.rb', line 32

def initialize(sexp, level, compiler)
  @sexp = sexp
  @type = sexp.type
  @level = level
  @compiler = compiler
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler



30
31
32
# File 'opal/lib/opal/nodes/base.rb', line 30

def compiler
  @compiler
end

#typeObject (readonly)

Returns the value of attribute type



30
31
32
# File 'opal/lib/opal/nodes/base.rb', line 30

def type
  @type
end

Class Method Details

.children(*names) ⇒ Object



18
19
20
21
22
23
24
# File 'opal/lib/opal/nodes/base.rb', line 18

def self.children(*names)
  names.each_with_index do |name, idx|
    define_method(name) do
      @sexp[idx + 1]
    end
  end
end

.handle(*types) ⇒ Object



12
13
14
15
16
# File 'opal/lib/opal/nodes/base.rb', line 12

def self.handle(*types)
  types.each do |type|
    Base.handlers[type] = self
  end
end

.handlersObject



8
9
10
# File 'opal/lib/opal/nodes/base.rb', line 8

def self.handlers
  @handlers ||= {}
end

.truthy_optimize?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'opal/lib/opal/nodes/base.rb', line 26

def self.truthy_optimize?
  false
end

Instance Method Details

#add_gvar(name) ⇒ Object



130
131
132
# File 'opal/lib/opal/nodes/base.rb', line 130

def add_gvar(name)
  scope.add_scope_gvar name
end

#add_ivar(name) ⇒ Object



126
127
128
# File 'opal/lib/opal/nodes/base.rb', line 126

def add_ivar(name)
  scope.add_scope_ivar name
end

#add_local(name) ⇒ Object



122
123
124
# File 'opal/lib/opal/nodes/base.rb', line 122

def add_local(name)
  scope.add_scope_local name.to_sym
end

#add_temp(temp) ⇒ Object



134
135
136
# File 'opal/lib/opal/nodes/base.rb', line 134

def add_temp(temp)
  scope.add_scope_temp temp
end

#childrenObject



39
40
41
# File 'opal/lib/opal/nodes/base.rb', line 39

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

#compileObject



51
52
53
# File 'opal/lib/opal/nodes/base.rb', line 51

def compile
  raise "Not Implemented"
end

#compile_to_fragmentsObject



43
44
45
46
47
48
49
# File 'opal/lib/opal/nodes/base.rb', line 43

def compile_to_fragments
  return @fragments if defined?(@fragments)

  @fragments = []
  self.compile
  @fragments
end

#error(msg) ⇒ Object



78
79
80
# File 'opal/lib/opal/nodes/base.rb', line 78

def error(msg)
  @compiler.error msg
end

#expr(sexp) ⇒ Object



106
107
108
# File 'opal/lib/opal/nodes/base.rb', line 106

def expr(sexp)
  @compiler.process sexp, :expr
end

#expr?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'opal/lib/opal/nodes/base.rb', line 90

def expr?
  @level == :expr
end

#expr_or_nil(sexp) ⇒ Object



118
119
120
# File 'opal/lib/opal/nodes/base.rb', line 118

def expr_or_nil(sexp)
  sexp ? expr(sexp) : "nil"
end

#fragment(str) ⇒ Object



74
75
76
# File 'opal/lib/opal/nodes/base.rb', line 74

def fragment(str)
  Opal::Fragment.new str, @sexp
end

#helper(name) ⇒ Object



138
139
140
# File 'opal/lib/opal/nodes/base.rb', line 138

def helper(name)
  @compiler.helper name
end

#in_while?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'opal/lib/opal/nodes/base.rb', line 146

def in_while?
  @compiler.in_while?
end

#process(sexp, level = :expr) ⇒ Object



102
103
104
# File 'opal/lib/opal/nodes/base.rb', line 102

def process(sexp, level = :expr)
  @compiler.process sexp, level
end

#push(*strs) ⇒ Object



55
56
57
58
59
60
# File 'opal/lib/opal/nodes/base.rb', line 55

def push(*strs)
  strs.each do |str|
    str = fragment(str) if str.is_a?(String)
    @fragments << str
  end
end

#recv(sexp) ⇒ Object



110
111
112
# File 'opal/lib/opal/nodes/base.rb', line 110

def recv(sexp)
  @compiler.process sexp, :recv
end

#recv?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'opal/lib/opal/nodes/base.rb', line 94

def recv?
  @level == :recv
end

#s(*args) ⇒ Object



86
87
88
# File 'opal/lib/opal/nodes/base.rb', line 86

def s(*args)
  @compiler.s(*args)
end

#scopeObject



82
83
84
# File 'opal/lib/opal/nodes/base.rb', line 82

def scope
  @compiler.scope
end

#stmt(sexp) ⇒ Object



114
115
116
# File 'opal/lib/opal/nodes/base.rb', line 114

def stmt(sexp)
  @compiler.process sexp, :stmt
end

#stmt?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'opal/lib/opal/nodes/base.rb', line 98

def stmt?
  @level == :stmt
end

#unshift(*strs) ⇒ Object



62
63
64
65
66
67
# File 'opal/lib/opal/nodes/base.rb', line 62

def unshift(*strs)
  strs.reverse.each do |str|
    str = fragment(str) if str.is_a?(String)
    @fragments.unshift str
  end
end

#while_loopObject



150
151
152
# File 'opal/lib/opal/nodes/base.rb', line 150

def while_loop
  @compiler.instance_variable_get(:@while_loop)
end

#with_temp(&block) ⇒ Object



142
143
144
# File 'opal/lib/opal/nodes/base.rb', line 142

def with_temp(&block)
  @compiler.with_temp(&block)
end

#wrap(pre, post) ⇒ Object



69
70
71
72
# File 'opal/lib/opal/nodes/base.rb', line 69

def wrap(pre, post)
  unshift pre
  push post
end