lib/interpreter/interpreter.rb in nudge-0.1.3 vs lib/interpreter/interpreter.rb in nudge-0.2.0
- old
+ new
@@ -15,18 +15,22 @@
class Interpreter
attr_accessor :program, :step_limit, :steps
attr_accessor :stacks, :instructions_library, :variables, :names, :types
attr_accessor :last_name, :evaluate_references
attr_accessor :sensors
+ attr_accessor :code_char_limit
+ attr_accessor :start_time, :time_limit
# A program to be interpreted can be passed in as an optional parameter
def initialize(program = nil, params = {})
initialProgram = program
@program = initialProgram
@types = params[:types] || NudgeType.all_types
@step_limit = params[:step_limit] || 3000
+ @time_limit = params[:time_limit] || 60.0 # seconds
+ @code_char_limit = params[:code_char_limit] || 2000
@sensors = Hash.new
instructions = params[:instructions] || Instruction.all_instructions
@instructions_library = Hash.new {|hash, key| raise InstructionPoint::InstructionNotFoundError,
"#{key} is not an active instruction in this context"}
@@ -59,10 +63,11 @@
self.reset_sensors
if !program.nil?
@stacks[:exec].push(NudgeProgram.new(program).linked_code)
end
@steps = 0
+ @start_time = Time.now
@evaluate_references = true
end
def clear_stacks
@@ -105,11 +110,13 @@
# Checks to see if either stopping condition applies:
# 1. Is the <b>:exec</b> stack empty?
# 2. Are the number of steps greater than self.step_limit?
def notDone?
- @stacks[:exec].depth > 0 && @steps < @step_limit
+ @stacks[:exec].depth > 0 &&
+ @steps < @step_limit &&
+ (Time.now-@start_time)<@time_limit
end
# Execute one cycle of the Push3 interpreter rule:
# 1. check termination conditions with self.notDone()?
@@ -130,9 +137,10 @@
end
# invoke self.step() until a termination condition is true
def run
+ @start_time = Time.now
while notDone?
self.step
end
fire_all_sensors
end
\ No newline at end of file