From a44b88f570eabf38d100b4b38026a5144b181910 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 21 Oct 2015 14:07:29 +0300 Subject: [PATCH] trigger on state change also make states symbols --- lib/interpreter/interpreter.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/interpreter/interpreter.rb b/lib/interpreter/interpreter.rb index 602bc6ad..bd32d8ed 100644 --- a/lib/interpreter/interpreter.rb +++ b/lib/interpreter/interpreter.rb @@ -18,7 +18,7 @@ module Interpreter attr_reader :flags # somewhat like the lags on a cpu, hash sym => bool (zero .. . ) def initialize - @state = "runnnig" + @state = :stopped @stdout = "" @registers = {} @flags = { :zero => false , :positive => false , @@ -27,12 +27,21 @@ module Interpreter (0...12).each do |r| set_register "r#{r}".to_sym , "r#{r}:unknown" end + @block = nil end def start bl + set_state(:running) set_block bl end + def set_state state + old = @state + return if state == old + @state = state + trigger(:state_changed , old , state ) + end + def set_block bl return if @block == bl raise "Error, nil block" unless bl @@ -43,11 +52,11 @@ module Interpreter end def set_instruction i - @state = "exited" unless i return if @instruction == i old = @instruction @instruction = i trigger(:instruction_changed, old , i) + set_state( :exited) unless i end def get_register( reg ) @@ -62,8 +71,7 @@ module Interpreter @flags[:zero] = (val == 0) @flags[:positive] = (val > 0) @flags[:negative] = (val < 0) - #puts "Set_flags #{val} :zero=#{@flags[:zero]}" - + #puts "Set_flags #{val} :#{@flags.inspect}" end return if old === val reg = reg.symbol if reg.is_a? Register::RegisterValue @@ -210,7 +218,7 @@ module Interpreter else raise "unimplemented '#{@instruction.operator}' #{@instruction}" end - #puts "#{@instruction} == #{result} (#{left}|#{right})" + puts "#{@instruction} == #{result} (#{left}|#{right})" right = set_register(@instruction.left , result) true end