Class: Y2R::AST::YCP::Variable

Inherits:
Node
  • Object
show all
Defined in:
lib/y2r/ast/ycp.rb

Instance Method Summary collapse

Methods inherited from Node

#always_returns?, #compile_statements, #compile_statements_inside_block, #compile_statements_with_whitespace, #creates_local_scope?, #never_nil?, #optimize_last_statement, #optimize_next, #optimize_return, #remove_duplicate_imports, transfers_comments

Instance Method Details

#compile(context) ⇒ Object



1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
# File 'lib/y2r/ast/ycp.rb', line 1984

def compile(context)
  case category
    when :variable, :reference
      RubyVar.for(ns, name, context, :in_code)
    when :function
      getter = if !ns && context.locals.include?(name)
        RubyVar.for(nil, name, context, :in_code)
      else
        # In the XML, all global module function references are
        # qualified (e.g. "M::i"). This includes references to functions
        # defined in this module. The problem is that in generated Ruby
        # code, the module namespace may not exist yet (e.g. when the
        # function is refrenced at module toplvel in YCP), so we have to
        # omit it (which is OK, because then the |method| call will be
        # invoked on |self|, whish is always our module).
        real_ns = ns == context.module_name ? nil : ns

        Ruby::MethodCall.new(
          :receiver => real_ns ? Ruby::Variable.new(:name => real_ns) : nil,
          :name     => "method",
          :args     => [
            Ruby::Literal.new(:value => name.to_sym)
          ],
          :block    => nil,
          :parens   => true
        )
      end

      Ruby::MethodCall.new(
        :receiver => nil,
        :name     => "fun_ref",
        :args     => [getter, Ruby::Literal.new(:value => type.to_s)],
        :block    => nil,
        :parens   => true
      )
    else
      raise "Unknown variable category: #{category.inspect}."
  end
end

#compile_as_copy_if_needed(context) ⇒ Object



1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
# File 'lib/y2r/ast/ycp.rb', line 1968

def compile_as_copy_if_needed(context)
  node = compile(context)

  if needs_copy?
    Ruby::MethodCall.new(
      :receiver => nil,
      :name     => "deep_copy",
      :args     => [node],
      :block    => nil,
      :parens   => true
    )
  else
    node
  end
end

#needs_copy?Boolean

Returns:

  • (Boolean)


1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
# File 'lib/y2r/ast/ycp.rb', line 1957

def needs_copy?
  case category
    when :variable, :reference
      type.needs_copy?
    when :function
      false
    else
      raise "Unknown variable category: #{category.inspect}."
  end
end