Ameba::AST::Variable - ameba 1.7.0

# class Ameba::AST::Variable

- Ameba::AST::Variable
- Reference
- Object

##
Overview

Represents the existence of the local variable. Holds the var node and variable assignments.

##
Direct Known Subclasses

- Ameba::AST::Reference

##
Defined in:

ameba/ast/variabling/variable.cr

##
Constructors

- .new(node : Crystal::Var, scope : Ameba::AST::Scope)

  Creates a new variable(in the scope).

##
Instance Method Summary

- #assign(node, scope)

  Assigns the variable (creates a new assignment).

- #assignments : Array(Ameba::AST::Assignment)

  List of the assignments of this variable.

- #captured_by_block?(scope = @scope)

  Returns `true` if the current var is referenced in the block.

- #end_location(*args, **options)
- #end_location(*args, **options, &)
- #eql?(node)

  Returns `true` if the `#node` represents exactly the same Crystal node as `@node`.

- #ignored?

  Returns `true` if the name starts with '_', `false` if not.

- #location(*args, **options)
- #location(*args, **options, &)
- #name(*args, **options)
- #name(*args, **options, &)
- #node : Crystal::Var

  The actual var node.

- #reference(node : Crystal::Var, scope : Scope)

  Creates a reference to this variable in some scope.

- #reference(scope : Scope)

  Creates a reference to this variable in some scope.

- #referenced?

  Returns `true` if variable has any reference.

- #references : Array(Ameba::AST::Reference)

  List of the references of this variable.

- #scope : Scope

  Scope of this variable.

- #special?

  Returns `true` if it is a special variable, i.e `$?`.

- #target_of?(assign)

  Returns `true` if the variable is a target (on the left) of the assignment, `false` otherwise.

- #to_s(*args, **options)
- #to_s(*args, **options, &)
- #used_in_macro?(scope = @scope)

  Returns `true` if current variable potentially referenced in a macro, `false` if not.

##
Constructor Detail

def self.new(node : Crystal::Var, scope : Ameba::AST::Scope) #

Creates a new variable(in the scope).

    Variable.new(node, scope)

##
Instance Method Detail

def assign(node, scope) #

Assigns the variable (creates a new assignment). Variable may have multiple assignments.

    variable = Variable.new(node, scope)
    variable.assign(node1)
    variable.assign(node2)
    variable.assignments.size # => 2

def assignments : Array(Ameba::AST::Assignment) #

List of the assignments of this variable.

def captured_by_block?(scope = @scope) #

Returns `true` if the current var is referenced in the block. For example this variable is captured by block:

    a = 1
    3.times { |i| a = a + i }

And this variable is not captured by block.

    i = 1
    3.times { |i| i + 1 }

def end_location(*args, **options) #
def end_location(*args, **options, &) #
def eql?(node) #

Returns `true` if the `#node` represents exactly the same Crystal node as `@node`.

def ignored? #

Returns `true` if the name starts with '_', `false` if not.

def location(*args, **options) #
def location(*args, **options, &) #
def name(*args, **options) #
def name(*args, **options, &) #
def node : Crystal::Var #

The actual var node.

def reference(node : Crystal::Var, scope : Scope) #

Creates a reference to this variable in some scope.

    variable = Variable.new(node, scope)
    variable.reference(var_node, some_scope)

def reference(scope : Scope) #

Creates a reference to this variable in some scope.

    variable = Variable.new(node, scope)
    variable.reference(var_node, some_scope)

def referenced? #

Returns `true` if variable has any reference.

    variable = Variable.new(node, scope)
    variable.reference(var_node, some_scope)
    variable.referenced? # => true

def references : Array(Ameba::AST::Reference) #

List of the references of this variable.

def scope : Scope #

Scope of this variable.

def special? #

Returns `true` if it is a special variable, i.e `$?`.

def target_of?(assign) #

Returns `true` if the variable is a target (on the left) of the assignment, `false` otherwise.

def to_s(*args, **options) #
def to_s(*args, **options, &) #
def used_in_macro?(scope = @scope) #

Returns `true` if current variable potentially referenced in a macro, `false` if not.