Class: Mirah::AST::LocalDeclaration

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

Instance Attribute Summary collapse

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, #to_s, #validate_name

Methods inherited from Node

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

Constructor Details

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

Returns a new instance of LocalDeclaration.



25
26
27
28
# File 'lib/mirah/ast/local.rb', line 25

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

Instance Attribute Details

#typeObject

Returns the value of attribute type.



23
24
25
# File 'lib/mirah/ast/local.rb', line 23

def type
  @type
end

Instance Method Details

#captured?Boolean

Returns:



30
31
32
# File 'lib/mirah/ast/local.rb', line 30

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

#compile(compiler, expression) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/mirah/compiler/local.rb', line 19

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

#infer(typer, expression) ⇒ Object



34
35
36
37
38
39
# File 'lib/mirah/ast/local.rb', line 34

def infer(typer, expression)
  resolve_if(typer) do
    scope.static_scope << name
    @type = type_node.type_reference(typer)
  end
end

#resolved!(typer) ⇒ Object



41
42
43
44
# File 'lib/mirah/ast/local.rb', line 41

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