Class: Peruby::Op::List

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

Overview

Perl comma list with context-dependent evaluation.

Instance Method Summary collapse

Methods inherited from Base

#local_slot, #run_subroutine, #to_callable, #warning_directive?

Constructor Details

#initialize(items) ⇒ List

Returns a new instance of List.



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

def initialize(items)
  @items = items
end

Instance Method Details

#argument_callableObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/peruby/op/list.rb', line 26

def argument_callable
  return ->(_env, _prototype = nil) { [] } if @items.empty?

  operations = @items.map(&:to_callable)
  lambda do |env, prototype = nil|
    next prototype_cells(env, prototype) if prototype

    operations.flat_map { |operation| operation.call(env, :list) }
  end
end

#argument_cells(env, prototype = nil) ⇒ Object



19
20
21
22
23
24
# File 'lib/peruby/op/list.rb', line 19

def argument_cells(env, prototype = nil)
  return prototype_cells(env, prototype) unless prototype.nil?
  return @items.first.argument_cells(env) if @items.length == 1

  @items.flat_map { |item| item.argument_cells(env) }
end

#empty?Boolean

Returns:

  • (Boolean)


37
# File 'lib/peruby/op/list.rb', line 37

def empty? = @items.empty?

#lvalue(env, _context) ⇒ Object



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

def lvalue(env, _context)
  @items.flat_map do |item|
    value = item.lvalue(env, :scalar)
    value.is_a?(Array) ? value : [value]
  end
end

#run(env, context) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/peruby/op/list.rb', line 11

def run(env, context)
  return @items.flat_map { |item| item.run(env, :list) } if context == :list
  return nil if @items.empty?

  @items[0...-1].each { |item| item.run(env, :void) }
  @items.last.run(env, context)
end