Class: Duby::AST::LocalDeclaration

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

Instance Attribute Summary

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

#to_s

Methods inherited from Node

#<<, ===, #[], #_set_parent, child, child_name, #each, #empty?, #expr?, #initialize_copy, #insert, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved?, #simple_name, #temp, #to_s

Constructor Details

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

Returns a new instance of LocalDeclaration.



9
10
11
12
# File 'lib/duby/ast/local.rb', line 9

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

Instance Method Details

#captured?Boolean

Returns:



14
15
16
# File 'lib/duby/ast/local.rb', line 14

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

#compile(compiler, expression) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/duby/compiler.rb', line 120

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
end

#infer(typer) ⇒ Object



18
19
20
21
22
23
# File 'lib/duby/ast/local.rb', line 18

def infer(typer)
  resolve_if(typer) do
    scope.static_scope << name
    typer.known_types[type] || type
  end
end

#resolved!(typer) ⇒ Object



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

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