pull/1/head
韩天峰 3 years ago
parent 166d6faa9e
commit bbe98369bd
  1. 22
      cases/test3.php
  2. 15
      cases/test3.py
  3. 9
      cases/unsupported/gen5.py
  4. 23
      conv.php

@ -0,0 +1,22 @@
<?php
$operator = PyCore::import("operator");
$builtins = PyCore::import("builtins");
function invert_dictionary($obj) {
return (function() {
$___ = [];
$___iter = $obj->items();
foreach($___iter as $___i => [$key, $value]) {
$___[] = [$key, $value];
}
return $___;
})();
}
$ages = new PyDict([
"Peter" => 10,
"Isabel" => 11,
"Anna" => 9,
]);
invert_dictionary($ages);

@ -1,10 +1,9 @@
def invert_dictionary(obj): from math import sqrt
return { value: key for key, value in obj.items() }
ages = { def is_prime(n):
'Peter': 10, if n <= 1 or (n % 2 == 0 and n > 2):
'Isabel': 11, return False
'Anna': 9, return all(n % i for i in range(3, int(sqrt(n)) + 1, 2))
}
invert_dictionary(ages) # { 10: 'Peter', 11: 'Isabel', 9: 'Anna' } is_prime(11) # True

@ -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

@ -259,6 +259,8 @@ class Translator
return $this->parseTuple($value); return $this->parseTuple($value);
case 'ListComp': case 'ListComp':
return $this->parseListComp($value); return $this->parseListComp($value);
case 'DictComp':
return $this->parseDictComp($value);
case 'BinOp': case 'BinOp':
return $this->parseBinOp($value); return $this->parseBinOp($value);
case 'JoinedStr': case 'JoinedStr':
@ -279,6 +281,8 @@ class Translator
return $this->parseYield($value); return $this->parseYield($value);
case 'Lambda': case 'Lambda':
return $this->parseLambda($value); return $this->parseLambda($value);
case 'GeneratorExp':
return $this->parseGeneratorExp($value);
default: default:
debug($value); debug($value);
break; break;
@ -963,6 +967,25 @@ class Translator
return $expr; 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); error_reporting(E_ERROR);

Loading…
Cancel
Save