Class: Fable::CallStack

Inherits:
Object
  • Object
show all
Defined in:
lib/fable/call_stack.rb

Defined Under Namespace

Classes: Element, Thread

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(story_context_or_call_stack) ⇒ CallStack

Returns a new instance of CallStack.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fable/call_stack.rb', line 5

def initialize(story_context_or_call_stack)
  if story_context_or_call_stack.is_a?(Story)
    start_of_root = Pointer.start_of(story_context_or_call_stack.root_content_container)
    reset!
  elsif story_context_or_call_stack.is_a?(CallStack)
    call_stack_to_copy = story_context_or_call_stack
    self.threads = []

    call_stack_to_copy.threads.each do |thread|
      self.threads << thread.copy
    end

    self.thread_container = call_stack_to_copy.thread_counter
    self.start_of_root = call_stack_to_copy.start_of_root
  end
end

Instance Attribute Details

#start_of_rootObject

Returns the value of attribute start_of_root.



3
4
5
# File 'lib/fable/call_stack.rb', line 3

def start_of_root
  @start_of_root
end

#thread_counterObject

Returns the value of attribute thread_counter.



3
4
5
# File 'lib/fable/call_stack.rb', line 3

def thread_counter
  @thread_counter
end

#threadsObject

Returns the value of attribute threads.



3
4
5
# File 'lib/fable/call_stack.rb', line 3

def threads
  @threads
end

Instance Method Details

#call_stackObject



141
142
143
# File 'lib/fable/call_stack.rb', line 141

def call_stack
  current_thread.call_stack
end

#call_stack_traceObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fable/call_stack.rb', line 190

def call_stack_trace
  result = StringIO.new

  self.threads.each_with_index do |thread, i|
    is_current_thread = thread == current_thread

    result << "=== THREAD #{i}/#{threads.count} #{'(current)' if is_current_thread }\n"

    thread.call_stack.each do |element|
      case element.type
      when :function
        result << "  [FUNCTION] "
      when :tunnel
        result << "  [TUNNEL] "
      end

      pointer = element.current_pointer
      if !pointer.null_pointer?
        result << "<SOMEWHERE IN #{pointer.container.path.to_s}>\n"
      end
    end
  end

  result.rewind
  result.read
end

#can_pop?(type = nil) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/fable/call_stack.rb', line 79

def can_pop?(type = nil)
  return false if call_stack.size <= 1
  return true if type.nil?
  return current_element.type == type
end

#can_pop_thread?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/fable/call_stack.rb', line 59

def can_pop_thread?
  threads.size > 1 && !element_is_evaluate_from_game?
end

#context_for_variable_named(name) ⇒ Object

Find the most appropriate context for this variable. Are we referencing a temporary or global variable? Note that the compiler will have warned us about possible conflicts, so anything that happens here should be safe



126
127
128
129
130
131
132
133
134
135
# File 'lib/fable/call_stack.rb', line 126

def context_for_variable_named(name)
  # Current temporary context?
  # (Shouldn't attempt to access contexts higher in the callstack)
  if current_element.temporary_variables.has_key?(name)
    return current_element_index + 1
  else
    # Global
    return 0
  end
end

#current_elementObject



37
38
39
40
# File 'lib/fable/call_stack.rb', line 37

def current_element
  thread = threads.last
  thread.call_stack.last
end

#current_element_indexObject



42
43
44
# File 'lib/fable/call_stack.rb', line 42

def current_element_index
  call_stack.size - 1
end

#current_threadObject



46
47
48
# File 'lib/fable/call_stack.rb', line 46

def current_thread
  threads.last
end

#current_thread=(value) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/fable/call_stack.rb', line 50

def current_thread=(value)
  if threads.size != 1
    raise StoryError, "Shouldn't be directly setting the current thread when we have a stack of them"
  end

  threads.clear
  threads << value
end

#depthObject



33
34
35
# File 'lib/fable/call_stack.rb', line 33

def depth
  elements.size
end

#element_is_evaluate_from_game?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/fable/call_stack.rb', line 63

def element_is_evaluate_from_game?
  current_element.type == PushPopType::TYPES[:function_evaluation_from_game]
end

#elementsObject



29
30
31
# File 'lib/fable/call_stack.rb', line 29

def elements
  call_stack
end

#fork_thread!Object



151
152
153
154
155
156
157
# File 'lib/fable/call_stack.rb', line 151

def fork_thread!
  forked_thread = current_thread.copy
  self.thread_counter += 1
  forked_thread.thread_index = self.thread_counter

  return forked_thread
end

#from_hash!(hash_to_use, story_context) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/fable/call_stack.rb', line 167

def from_hash!(hash_to_use, story_context)
  self.threads = []

  hash_to_use["threads"].each do |thread_object|
    self.threads << Thread.new(thread_object, story_context)
  end

  self.thread_counter = hash_to_use["threadCounter"]
  self.start_of_root = Pointer.start_of(story_context.root_content_container)
  self
end

#get_temporary_variable_with_name(name, context_index = -1)) ⇒ Object

Get variable value, dereferencing a variable pointer if necessary



94
95
96
97
98
99
100
101
102
# File 'lib/fable/call_stack.rb', line 94

def get_temporary_variable_with_name(name, context_index = -1)
  if context_index == -1
    context_index = current_element_index + 1
  end

  context_element = call_stack[context_index - 1]

  return context_element.temporary_variables[name]
end

#pop!(type = nil) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/fable/call_stack.rb', line 85

def pop!(type=nil)
  if can_pop?(type)
    call_stack.pop
  else
    raise Error, "Mismatched push/pop in Callstack"
  end
end

#pop_thread!Object



159
160
161
162
163
164
165
# File 'lib/fable/call_stack.rb', line 159

def pop_thread!
  if can_pop_thread?
    threads.delete(current_thread)
  else
    raise Error, "Can't pop thread"
  end
end

#push(type, options = {external_evaluation_stack_height: 0, output_stream_length_when_pushed: 0}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fable/call_stack.rb', line 67

def push(type, options = {external_evaluation_stack_height: 0, output_stream_length_when_pushed: 0})
  external_evaluation_stack_height = options[:external_evaluation_stack_height] || 0
  output_stream_length_when_pushed = options[:output_stream_length_when_pushed] || 0
  # When pushing to callstack, maintain the current content path, but jump
  # out of expressions by default
  element = Element.new(type, current_element.current_pointer, in_expression_evaluation: false)
  element.evaluation_stack_height_when_pushed = external_evaluation_stack_height
  element.function_start_in_output_stream = output_stream_length_when_pushed

  self.call_stack << element
end

#push_thread!Object



145
146
147
148
149
# File 'lib/fable/call_stack.rb', line 145

def push_thread!
  new_thread = current_thread.copy
  self.thread_counter += 1
  self.threads << new_thread
end

#reset!Object



22
23
24
25
26
27
# File 'lib/fable/call_stack.rb', line 22

def reset!
  new_thread = Thread.new
  new_thread.call_stack << Element.new(:tunnel, self.start_of_root)
  self.threads = [new_thread]
  self.thread_counter = 0
end

#set_temporary_variable(name, value, declare_new, context_index = -1)) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fable/call_stack.rb', line 104

def set_temporary_variable(name, value, declare_new, context_index = -1)
  if context_index == -1
    context_index = current_element_index + 1
  end

  context_element = call_stack[context_index - 1]

  if !declare_new && !context_element.temporary_variables.has_key?(name)
    raise Error, "Could not find temporary variable to set: #{name}"
  end

  if context_element.temporary_variables.has_key?(name)
    old_value = context_element.temporary_variables[name]
    ListValue.retain_list_origins_for_assignment(old_value, value)
  end

  context_element.temporary_variables[name] = value
end

#thread_with_index(index) ⇒ Object



137
138
139
# File 'lib/fable/call_stack.rb', line 137

def thread_with_index(index)
  threads.find{|thread| thread.thread_index == index}
end

#to_hashObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/fable/call_stack.rb', line 179

def to_hash
  export = {}
  export["threads"] = []
  self.threads.each do |thread|
    export["threads"] << thread.to_hash
  end

  export["threadCounter"] = self.thread_counter
  export
end