diff --git a/cases/test3.php b/cases/test3.php new file mode 100644 index 00000000..7e5d6424 --- /dev/null +++ b/cases/test3.php @@ -0,0 +1,22 @@ +items(); + foreach($___iter as $___i => [$key, $value]) { + $___[] = [$key, $value]; + } + return $___; + })(); +} + + +$ages = new PyDict([ + "Peter" => 10, + "Isabel" => 11, + "Anna" => 9, +]); +invert_dictionary($ages); diff --git a/cases/test3.py b/cases/test3.py index 1f9768eb..385f924b 100644 --- a/cases/test3.py +++ b/cases/test3.py @@ -1,10 +1,9 @@ -def invert_dictionary(obj): - return { value: key for key, value in obj.items() } +from math import sqrt -ages = { - 'Peter': 10, - 'Isabel': 11, - 'Anna': 9, -} -invert_dictionary(ages) # { 10: 'Peter', 11: 'Isabel', 9: 'Anna' } +def is_prime(n): + if n <= 1 or (n % 2 == 0 and n > 2): + return False + return all(n % i for i in range(3, int(sqrt(n)) + 1, 2)) + +is_prime(11) # True diff --git a/cases/unsupported/gen5.py b/cases/unsupported/gen5.py new file mode 100644 index 00000000..385f924b --- /dev/null +++ b/cases/unsupported/gen5.py @@ -0,0 +1,9 @@ + +from math import sqrt + +def is_prime(n): + if n <= 1 or (n % 2 == 0 and n > 2): + return False + return all(n % i for i in range(3, int(sqrt(n)) + 1, 2)) + +is_prime(11) # True diff --git a/conv.php b/conv.php index 5e8dae87..eac48ad3 100644 --- a/conv.php +++ b/conv.php @@ -259,6 +259,8 @@ class Translator return $this->parseTuple($value); case 'ListComp': return $this->parseListComp($value); + case 'DictComp': + return $this->parseDictComp($value); case 'BinOp': return $this->parseBinOp($value); case 'JoinedStr': @@ -279,6 +281,8 @@ class Translator return $this->parseYield($value); case 'Lambda': return $this->parseLambda($value); + case 'GeneratorExp': + return $this->parseGeneratorExp($value); default: debug($value); break; @@ -963,6 +967,25 @@ class Translator return $expr; } } + + private function parseDictComp($node) + { + $code = ''; + $this->indentLevel++; + $code .= $this->getIndent() . '$___ = [];' . PHP_EOL; + $recipient = '$___'; + $generators = $this->parseGenerators($node->generators, $recipient, $captures); + $code .= $this->getIndent() . $generators; + $code .= $this->getIndent() . 'return $___;' . PHP_EOL; + $this->indentLevel--; + $code .= $this->getIndent() . '})(' . implode(',', $captures) . ')'; + return '(function(' . implode(',', $captures) . ') {' . PHP_EOL . $code; + } + + private function parseGeneratorExp($value) + { + debug($value); + } } error_reporting(E_ERROR);