Class: Peruby::Op::Literal

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/literal.rb

Overview

Constant scalar or quote-like value.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #warning_directive?

Constructor Details

#initialize(value) ⇒ Literal

Returns a new instance of Literal.



7
8
9
# File 'lib/peruby/op/literal.rb', line 7

def initialize(value)
  @value = value
end

Instance Method Details

#run(env, context) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/peruby/op/literal.rb', line 11

def run(env, context)
  value = if @value.is_a?(Lexer::Quote) && @value.operator == :backtick
            command = Interpolation.expand(@value, env)
            output = IO.popen(command, &:read)
            env.fetch(:scalar, '?').set($?.to_i) # rubocop:disable Style/SpecialGlobalVars
            output
          elsif @value.is_a?(Lexer::Heredoc)
            quote = Lexer::Quote.new(operator: :heredoc, parts: [@value.body], modifiers: '',
                                     interpolate: @value.interpolate)
            Interpolation.expand(quote, env)
          elsif @value.is_a?(Lexer::Quote)
            Interpolation.expand(@value, env)
          else
            @value
          end
  context == :list ? [Scalar.new(value)] : value
end

#scalar_callableObject



39
40
41
42
43
44
45
46
# File 'lib/peruby/op/literal.rb', line 39

def scalar_callable
  return ->(_env) { @value } unless @value.is_a?(Lexer::Quote) || @value.is_a?(Lexer::Heredoc)
  return if @value.is_a?(Lexer::Heredoc) || @value.operator == :backtick
  return unless @value.is_a?(Lexer::Quote) && simple_interpolation?

  callable = interpolated_callable
  ->(env) { callable.call(env, :scalar) }
end

#to_callableObject



29
30
31
32
33
34
35
36
37
# File 'lib/peruby/op/literal.rb', line 29

def to_callable
  return super if @value.is_a?(Lexer::Quote) && @value.operator == :backtick
  return super if @value.is_a?(Lexer::Heredoc)
  return interpolated_callable if @value.is_a?(Lexer::Quote) && simple_interpolation?
  return super if @value.is_a?(Lexer::Quote)

  value = @value
  ->(_env, context) { context == :list ? [Scalar.new(value)] : value }
end