Class: BlocklyInterpreter::ExecutionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/blockly_interpreter/execution_context.rb

Direct Known Subclasses

TestInterpreter::ExecutionContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interpreter) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.



6
7
8
9
10
11
12
# File 'lib/blockly_interpreter/execution_context.rb', line 6

def initialize(interpreter)
  @interpreter = interpreter
  @variables = {}
  @early_return_value = nil
  @terminated = false
  @debug_messages = []
end

Instance Attribute Details

#debug_messagesObject (readonly)

Returns the value of attribute debug_messages.



4
5
6
# File 'lib/blockly_interpreter/execution_context.rb', line 4

def debug_messages
  @debug_messages
end

#early_return_valueObject (readonly)

Returns the value of attribute early_return_value.



4
5
6
# File 'lib/blockly_interpreter/execution_context.rb', line 4

def early_return_value
  @early_return_value
end

#interpreterObject (readonly)

Returns the value of attribute interpreter.



4
5
6
# File 'lib/blockly_interpreter/execution_context.rb', line 4

def interpreter
  @interpreter
end

#terminatedObject (readonly)

Returns the value of attribute terminated.



4
5
6
# File 'lib/blockly_interpreter/execution_context.rb', line 4

def terminated
  @terminated
end

Instance Method Details

#add_debug_message(message) ⇒ Object



66
67
68
69
70
71
# File 'lib/blockly_interpreter/execution_context.rb', line 66

def add_debug_message(message)
  if message.present?
    Logger.new(STDERR).debug message
    debug_messages << message
  end
end

#early_return!(value = nil) ⇒ Object



40
41
42
43
# File 'lib/blockly_interpreter/execution_context.rb', line 40

def early_return!(value = nil)
  @terminated = true
  @early_return_value = value
end

#execute(block) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/blockly_interpreter/execution_context.rb', line 31

def execute(block)
  current_block = block

  while current_block && !terminated
    current_block.execute_statement(self)
    current_block = current_block.next_block
  end
end

#execute_procedure(name, parameters) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/blockly_interpreter/execution_context.rb', line 45

def execute_procedure(name, parameters)
  procedure_block = interpreter.program.procedures[name]

  interpreter.build_execution_context.tap do |proc_context|
    proc_context.set_variables(@variables.merge(procedure_block.arg_hash(parameters)))
    proc_context.execute(procedure_block)
    merge_state_from(proc_context)
  end
end

#get_variable(name) ⇒ Object



14
15
16
# File 'lib/blockly_interpreter/execution_context.rb', line 14

def get_variable(name)
  @variables[name.to_s.upcase]
end

#merge_state_from(context) ⇒ Object



28
29
# File 'lib/blockly_interpreter/execution_context.rb', line 28

def merge_state_from(context)
end

#set_variable(name, value) ⇒ Object



18
19
20
# File 'lib/blockly_interpreter/execution_context.rb', line 18

def set_variable(name, value)
  @variables[name.to_s.upcase] = value
end

#set_variables(hash) ⇒ Object



22
23
24
25
26
# File 'lib/blockly_interpreter/execution_context.rb', line 22

def set_variables(hash)
  hash.each do |name, value|
    set_variable(name, value)
  end
end

#value_for_procedure(name, parameters) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/blockly_interpreter/execution_context.rb', line 55

def value_for_procedure(name, parameters)
  procedure_block = interpreter.program.procedures[name]

  proc_context = interpreter.build_execution_context
  proc_context.set_variables(@variables.merge(procedure_block.arg_hash(parameters)))

  procedure_block.value(proc_context).tap do |value|
    merge_state_from(proc_context)
  end
end