Class: Command::While

Inherits:
Object
  • Object
show all
Defined in:
lib/karel/command/while.rb

Instance Method Summary collapse

Constructor Details

#initialize(condition_statement) ⇒ While

Returns a new instance of While.



5
6
7
8
# File 'lib/karel/command/while.rb', line 5

def initialize(condition_statement)
  @condition_statement = condition_statement
  @statements = []
end

Instance Method Details

#add_statement(statement) ⇒ Object



10
11
12
# File 'lib/karel/command/while.rb', line 10

def add_statement(statement)
  @statements << statement
end

#execute(compass, location, tokens) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/karel/command/while.rb', line 14

def execute(compass, location, tokens)
  response = @condition_statement.execute(compass, location, tokens)
  operations_count = response.operations_count
  while response.return_value
    response = Batch.new(@statements).execute(
      response.compass,
      response.location,
      response.tokens
    )
    operations_count += response.operations_count
    response = @condition_statement.execute(
      response.compass, response.location, response.tokens
    )
    operations_count += response.operations_count
  end
  Response.new(
    compass: response.compass,
    location: response.location,
    tokens: response.tokens,
    operations_count: operations_count
  )
end