2017-09-08 12:10:22 +02:00
|
|
|
module Common
|
|
|
|
module List
|
|
|
|
|
|
|
|
# set the next instruction (also aliased as <<)
|
|
|
|
# throw an error if that is set, use insert for that use case
|
|
|
|
# return the instruction, so chaining works as one wants (not backwards)
|
2018-03-11 11:41:15 +01:00
|
|
|
def set_next( nekst )
|
2017-09-08 12:10:22 +02:00
|
|
|
raise "Next already set #{@next}" if @next
|
|
|
|
@next = nekst
|
|
|
|
nekst
|
|
|
|
end
|
|
|
|
|
|
|
|
# during translation we replace one by one
|
2018-03-11 11:41:15 +01:00
|
|
|
def replace_next( nekst )
|
2017-09-08 12:10:22 +02:00
|
|
|
old = @next
|
|
|
|
@next = nekst
|
|
|
|
@next.append old.next if old
|
|
|
|
end
|
|
|
|
|
|
|
|
# get the next instruction (without arg given )
|
|
|
|
# when given an interger, advance along the line that many time and return.
|
|
|
|
def next( amount = 1)
|
|
|
|
(amount == 1) ? @next : @next.next(amount-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
# set the give instruction as the next, while moving any existing
|
|
|
|
# instruction along to the given ones's next.
|
|
|
|
# ie insert into the linked list that the instructions form
|
2018-03-11 11:41:15 +01:00
|
|
|
def insert( instruction )
|
2017-09-08 12:10:22 +02:00
|
|
|
instruction.set_next @next
|
|
|
|
@next = instruction
|
|
|
|
end
|
|
|
|
|
|
|
|
# return last set instruction. ie follow the linked list until it stops
|
|
|
|
def last
|
|
|
|
code = self
|
|
|
|
code = code.next while( code.next )
|
|
|
|
return code
|
|
|
|
end
|
|
|
|
|
|
|
|
# set next for the last (see last)
|
|
|
|
# so append the given code to the linked list at the end
|
2018-03-11 11:41:15 +01:00
|
|
|
def append( code )
|
2017-09-08 12:10:22 +02:00
|
|
|
last.set_next code
|
2018-03-15 17:24:03 +01:00
|
|
|
self
|
2017-09-08 12:10:22 +02:00
|
|
|
end
|
2018-03-15 16:10:21 +01:00
|
|
|
alias :<< :append
|
2017-09-08 12:10:22 +02:00
|
|
|
|
2018-03-11 11:41:15 +01:00
|
|
|
def length( labels = [] )
|
2017-09-08 12:10:22 +02:00
|
|
|
ret = 1
|
|
|
|
ret += self.next.length( labels ) if self.next
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|