Class: Mirah::AST::LocalAssignment

Inherits:
Node
  • Object
show all
Includes:
Named, Scoped, Valued
Defined in:
lib/mirah/ast/local.rb,
lib/mirah/compiler/local.rb,
lib/mirah/jvm/source_generator/precompile.rb

Instance Attribute Summary

Attributes included from Valued

#value

Attributes included from Typed

#type

Attributes included from Named

#name

Attributes inherited from Node

#children, #inferred_type, #newline, #parent, #position

Instance Method Summary collapse

Methods included from Scoped

#containing_scope, #scope

Methods included from Named

#string_value, #validate_name

Methods inherited from Node

#<<, ===, #[], #[]=, #_dump, _load, #_set_parent, child, child_name, #child_nodes, #each, #empty?, #inferred_type!, #initialize_copy, #insert, #inspect, #inspect_children, #line_number, #log, #resolve_if, #resolved?, #simple_name, #string_value, #temp, #top_level?, #validate_child, #validate_children

Constructor Details

#initialize(parent, line_number, name, &block) ⇒ LocalAssignment

Returns a new instance of LocalAssignment.



54
55
56
57
# File 'lib/mirah/ast/local.rb', line 54

def initialize(parent, line_number, name, &block)
  super(parent, line_number, &block)
  self.name = name
end

Instance Method Details

#captured?Boolean

Returns:



59
60
61
# File 'lib/mirah/ast/local.rb', line 59

def captured?
  scope.static_scope.captured?(name)
end

#compile(compiler, expression) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/mirah/compiler/local.rb', line 32

def compile(compiler, expression)
  compiler.line(line_number)
  if captured? && scope.has_binding?
    compiler.captured_local_assign(self, expression)
  else
    compiler.local_assign(containing_scope, name, inferred_type, expression, value)
  end
rescue Exception => ex
  raise Mirah::InternalCompilerError.wrap(ex, self)
end

#expr?(compiler) ⇒ Boolean

Returns:



145
146
147
# File 'lib/mirah/jvm/source_generator/precompile.rb', line 145

def expr?(compiler)
  compiler.method.local?(name) && value.expr?(compiler)
end

#infer(typer, expression) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/mirah/ast/local.rb', line 67

def infer(typer, expression)
  resolve_if(typer) do
    scope.static_scope << name
    type = typer.infer(value, true)
    if type && type.null?
      type = typer.local_type(containing_scope, name) unless typer.last_chance
    end
    type
  end
end

#precompile(compiler) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/mirah/jvm/source_generator/precompile.rb', line 149

def precompile(compiler)
  if expr?(compiler)
    self
  else
    compile(compiler, false)
    TempValue.new(name)
  end
end

#resolved!(typer) ⇒ Object



78
79
80
81
# File 'lib/mirah/ast/local.rb', line 78

def resolved!(typer)
  typer.learn_local_type(containing_scope, name, @inferred_type)
  super
end

#to_sObject



63
64
65
# File 'lib/mirah/ast/local.rb', line 63

def to_s
  "LocalAssignment(name = #{name}, scope = #{scope}, captured = #{captured? == true})"
end