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



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

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



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

def compiler
  @compiler
end

#typeObject (readonly)

Returns the value of attribute type



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

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

Instance Method Details

#add_gvar(name) ⇒ Object



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

def add_gvar(name)
  scope.add_scope_gvar name
end

#add_ivar(name) ⇒ Object



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

def add_ivar(name)
  scope.add_scope_ivar name
end

#add_local(name) ⇒ Object



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

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

#add_temp(temp) ⇒ Object



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

def add_temp(temp)
  scope.add_scope_temp temp
end

#childrenObject



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

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

#compileObject



47
48
49
# File 'opal/lib/opal/nodes/base.rb', line 47

def compile
  raise "Not Implemented"
end

#compile_to_fragmentsObject



39
40
41
42
43
44
45
# File 'opal/lib/opal/nodes/base.rb', line 39

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

  @fragments = []
  self.compile
  @fragments
end

#error(msg) ⇒ Object



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

def error(msg)
  @compiler.error msg
end

#expr(sexp) ⇒ Object



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

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

#expr?Boolean

Returns:

  • (Boolean)


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

def expr?
  @level == :expr
end

#expr_or_nil(sexp) ⇒ Object



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

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

#fragment(str) ⇒ Object



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

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

#helper(name) ⇒ Object



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

def helper(name)
  @compiler.helper name
end

#in_while?Boolean

Returns:

  • (Boolean)


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

def in_while?
  @compiler.in_while?
end

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



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

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

#push(*strs) ⇒ Object



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

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

#recv(sexp) ⇒ Object



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

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

#recv?Boolean

Returns:

  • (Boolean)


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

def recv?
  @level == :recv
end

#s(*args) ⇒ Object



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

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

#scopeObject



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

def scope
  @compiler.scope
end

#stmt(sexp) ⇒ Object



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

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

#stmt?Boolean

Returns:

  • (Boolean)


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

def stmt?
  @level == :stmt
end

#unshift(*strs) ⇒ Object



58
59
60
61
62
63
# File 'opal/lib/opal/nodes/base.rb', line 58

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

#while_loopObject



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

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

#with_temp(&block) ⇒ Object



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

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

#wrap(pre, post) ⇒ Object



65
66
67
68
# File 'opal/lib/opal/nodes/base.rb', line 65

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