1 # coding: utf-8
2 # frozen_string_literal: true
3
4
5
6 module Umu
7
8 module Environment
9
10 module Tracer
11
12 module Stack
13
14 class Abstract < Abstraction::Collection
15 def each
16 return self.to_enum unless block_given?
17
18 elem = self
19
20 until elem.kind_of? Empty
21 yield elem.event
22
23 elem = elem.old_cons
24 end
25
26 nil
27 end
28
29
30 def push(event)
31 ASSERT.kind_of event, Tracer::Event
32
33 Cons.new(event, self).freeze
34 end
35
36
37 def pop
38 raise X::InternalSubclassResponsibility
39 end
40
41
42 def print
43 self.each do |event|
44 STDERR.puts event.to_s
45 end
46 end
47 end
48
49
50
51 class Empty < Abstract
52 def pop
53 ASSERT.abort "Empty Stack"
54 end
55 end
56
57 EMPTY = Empty.new.freeze
58
59
60 class Cons < Abstract
61 attr_reader :event
62 attr_reader :old_cons
63
64
65 def initialize(event, old_cons)
66 ASSERT.kind_of event, Tracer::Event
67 ASSERT.kind_of old_cons, Abstract
68
69 @event = event
70 @old_cons = old_cons
71 end
72
73
74 def pop
75 self.old_cons
76 end
77 end
78
79
80
81 module_function
82
83 def empty
84 EMPTY
85 end
86 end # Umu::Environment::Tracer::Stack
87
88 end # Umu::Environment::Tracer
89
90 end # Umu::Environment
91
92 end # Umu