Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 5a13869

Browse files
committed
Implemented PHP::Loop::While and PHP::Generator#process_while.
1 parent fabe63a commit 5a13869

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/php/generator.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,18 @@ def process_for(exp)
429429
Loop::ForEach.new(process(exp.shift), process(exp.shift), process(exp.shift))
430430
end
431431

432+
##
433+
# Processes `[:while, condition, block]` expressions.
434+
#
435+
# @example
436+
# process([:while, [:true], [:call, nil, :puts, [:arglist, [:str, "looping..."]]], true])
437+
#
438+
# @param [Array(Array, Array, Boolean)] exp
439+
# @return [Loop::While]
440+
def process_while(exp)
441+
Loop::While.new(process(exp.shift), process(exp.shift))
442+
end
443+
432444
##
433445
# Processes `[:not, operand]` expressions.
434446
#

lib/php/syntax/loop.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@ module PHP
22
##
33
# @see http://php.net/manual/en/language.control-structures.php
44
class Loop < Expression
5+
##
6+
# @see http://php.net/manual/en/control-structures.while.php
7+
class While < Loop
8+
##
9+
# @return [Expression]
10+
attr_accessor :condition
11+
12+
##
13+
# @param [Expression] condition
14+
# @param [Array<Block>] body
15+
def initialize(condition, *body)
16+
@condition = condition
17+
@children = body.map { |exp| Block.for(exp) }
18+
end
19+
20+
##
21+
# Returns the PHP representation of this `while` loop.
22+
#
23+
# @return [String]
24+
def to_php
25+
body = children.map(&:to_php).join('; ')
26+
"while (#{condition}) { #{body} }" # FIXME
27+
end
28+
end
29+
530
##
631
# @see http://php.net/manual/en/control-structures.foreach.php
732
class ForEach < Loop

0 commit comments

Comments
 (0)