Class: Pione::Lang::Sequence

Inherits:
Expr
  • Object
show all
Includes:
Enumerable, Callable, Util::Positionable
Defined in:
lib/pione/lang/sequence.rb

Overview

Sequence is a base class for all expressions.

Direct Known Subclasses

KeyedSequence, OrdinalSequence

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callable

#call_pione_method

Methods included from Util::Positionable

#line_and_column, #pos, #set_source_position

Methods inherited from Expr

#eval!, pione_type, set_pione_type, #textize, #to_s

Class Method Details

.index_type(env) ⇒ Object

Set the index type.



38
39
40
# File 'lib/pione/lang/sequence.rb', line 38

def index_type(env)
  @index_type
end

.inherited(subclass) ⇒ Object



22
23
24
25
26
# File 'lib/pione/lang/sequence.rb', line 22

def inherited(subclass)
  members.each {|member_name| subclass.member(member_name, default: default_values[member_name])}
  subclass.immutable true
  subclass.set_index_type(index_type(nil))
end

.piece_class(klass) ⇒ Object

Get/set piece class.



29
30
31
# File 'lib/pione/lang/sequence.rb', line 29

def piece_class(klass)
  (@piece_classes ||= []) << klass
end

.piece_classesObject



33
34
35
# File 'lib/pione/lang/sequence.rb', line 33

def piece_classes
  @piece_classes ||= []
end

.set_index_type(type) ⇒ Object

Set the index type.



43
44
45
# File 'lib/pione/lang/sequence.rb', line 43

def set_index_type(type)
  @index_type = type
end

.voidObject

Make a void sequence.



49
50
51
# File 'lib/pione/lang/sequence.rb', line 49

def Sequence.void
  Sequence.new([])
end

Instance Method Details

#assertive?Boolean

Return true if the sequence is assertive about attributes.



69
70
71
# File 'lib/pione/lang/sequence.rb', line 69

def assertive?
  true
end

#attributeObject



73
74
75
# File 'lib/pione/lang/sequence.rb', line 73

def attribute
  members - [:pieces]
end

#concat(other) ⇒ Sequence Also known as: +

Concatenate self and another sequence. If self and another are assertive, raise +SequenceAttributeError+ exception when the attributes are different.



85
86
87
88
89
90
91
# File 'lib/pione/lang/sequence.rb', line 85

def concat(other)
  if assertive? and other.assertive?
    raise SequenceAttributeError.new(other) unless attribute == other.attribute
  end
  attr = not(other.assertive?) ? @attribute : other.attribute
  set(pieces: pieces + other.pieces)
end

#eachEnumerator

Iterate each elements.



103
104
105
106
107
108
109
# File 'lib/pione/lang/sequence.rb', line 103

def each
  if block_given?
    pieces.each {|e| yield set(pieces: [e])}
  else
    to_enum(:each)
  end
end

#eval(env) ⇒ Object



111
112
113
# File 'lib/pione/lang/sequence.rb', line 111

def eval(env)
  set(pieces: pieces.map{|piece| piece.eval(env)})
end

#push(piece) ⇒ Object

Push the piece to the sequecence.



95
96
97
# File 'lib/pione/lang/sequence.rb', line 95

def push(piece)
  set(pieces: pieces + [piece])
end

#set_annotation_type(type) ⇒ Object

Set the annotation type to the sequence.



127
128
129
# File 'lib/pione/lang/sequence.rb', line 127

def set_annotation_type(type)
  set(annotation_type: type)
end

#update_pieces(data) ⇒ Object

Update pieces with the data.



116
117
118
119
120
121
122
123
124
# File 'lib/pione/lang/sequence.rb', line 116

def update_pieces(data)
  _pieces = pieces.map do |piece|
    _data = data.inject({}) do |tbl, (key, val)|
      tbl.tap {|x| x[key] = val.kind_of?(Proc) ? val.call(piece) : val}
    end
    piece.set(_data)
  end
  set(pieces: _pieces)
end

#void?Boolean

Return true if the sequence is void.



64
65
66
# File 'lib/pione/lang/sequence.rb', line 64

def void?
  self.class == Sequence and empty?
end