Class: Platform::IEL::StdLibList

Inherits:
Object
  • Object
show all
Defined in:
lib/introhive_expression_language/iel/std_lib_list.rb

Class Method Summary collapse

Class Method Details

.declare(defining_context) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/introhive_expression_language/iel/std_lib_list.rb', line 6

def declare(defining_context)
  declare_list_elem(defining_context)
  declare_list_join(defining_context)
  declare_list_append(defining_context)
  declare_list_prepend(defining_context)
  declare_list_len(defining_context)
  declare_list_any(defining_context)
end

.list_elem(list_node, index_node) ⇒ Object

Return an element from the list at its ordinal position. List indexes are zero based. If a negative index is provided then it specifies items from the end of the list.

e.g. 0 is the first item in the list
    -1 is the last item in the list
    -2 is the second-to-last item in the list

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/introhive_expression_language/iel/std_lib_list.rb', line 20

def list_elem(list_node, index_node)
  raise EvaluationError.new("list argument to function 'list-elem' must be of type list but it is #{list_node.kind}", list_node) unless list_node.kind == :list
  raise EvaluationError.new("index argument to function 'list-elem' must be of type numeric_literal but it is #{index_node.kind}", index_node) unless index_node.kind == :numeric_literal
  index = index_node.value
  if index >= list_node.value.size
    SexpParser::Node.nil
  elsif index < 0
    index_from_end = list_node.value.size + index
    if index_from_end < 0
      SexpParser::Node.nil
    else
      list_node.value[index_from_end]
    end
  else
    list_node.value[index]
  end
end