格式化代码

pull/1/head
韩天峰 1 year ago
parent d17aed873f
commit eca4e28346
  1. 4
      src/Core/Translator.php
  2. 67
      src/Python/Translator.php

@ -9,12 +9,12 @@ abstract class Translator
public string $mode = 'cli'; public string $mode = 'cli';
protected string $lang; protected string $lang;
function setMode($mode): void public function setMode($mode): void
{ {
$this->mode = $mode; $this->mode = $mode;
} }
function setIndent(string $indent): void public function setIndent(string $indent): void
{ {
$this->indentStr = $indent; $this->indentStr = $indent;
} }

@ -42,13 +42,13 @@ class Translator extends \PhpAot\Core\Translator
'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError' 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError'
]; ];
function __construct() public function __construct()
{ {
$this->keywordsMap = array_flip($this->keywords); $this->keywordsMap = array_flip($this->keywords);
$this->builtinTypes = array_flip($this->builtinTypes); $this->builtinTypes = array_flip($this->builtinTypes);
} }
function prepare($root) public function prepare($root)
{ {
foreach ($root->body as $body) { foreach ($root->body as $body) {
if ($body->_type == 'FunctionDef') { if ($body->_type == 'FunctionDef') {
@ -57,17 +57,17 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function getLine($node): int public function getLine($node): int
{ {
return $node->lineno; return $node->lineno;
} }
function getType($node): string public function getType($node): string
{ {
return $node->_type; return $node->_type;
} }
function parseAttribute($attr) public function parseAttribute($attr)
{ {
switch ($attr->_type) { switch ($attr->_type) {
case 'Name': case 'Name':
@ -88,7 +88,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseFunc($fn) public function parseFunc($fn)
{ {
switch ($fn->_type) { switch ($fn->_type) {
case 'Name': case 'Name':
@ -102,6 +102,7 @@ class Translator extends \PhpAot\Core\Translator
} else { } else {
return '$' . $id; return '$' . $id;
} }
// no break
case 'Attribute': case 'Attribute':
return $this->parseAttribute($fn->value) . '->' . $fn->attr; return $this->parseAttribute($fn->value) . '->' . $fn->attr;
default: default:
@ -109,7 +110,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseCall($call): string public function parseCall($call): string
{ {
$fn = $this->parseFunc($call->func); $fn = $this->parseFunc($call->func);
if_empty_debug($fn, $call); if_empty_debug($fn, $call);
@ -134,13 +135,14 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
static function valueToRepr($v, $python = false) public static function valueToRepr($v, $python = false)
{ {
if (is_string($v)) { if (is_string($v)) {
$v = str_replace( $v = str_replace(
["\\", "\n", "\r", "\t", "\v", "\x00", "\""], ["\\", "\n", "\r", "\t", "\v", "\x00", "\""],
["\\\\", "\\n", "\\r", "\\t", "\\v", "\\x00", "\\\""], ["\\\\", "\\n", "\\r", "\\t", "\\v", "\\x00", "\\\""],
$v); $v
);
return "\"$v\""; return "\"$v\"";
} elseif ($v === []) { } elseif ($v === []) {
return '[]'; return '[]';
@ -164,12 +166,12 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseConstant($value): string public function parseConstant($value): string
{ {
return self::valueToRepr($value->value); return self::valueToRepr($value->value);
} }
function parseTuple($tuple) public function parseTuple($tuple)
{ {
$ids = []; $ids = [];
foreach ($tuple->dims as $dim) { foreach ($tuple->dims as $dim) {
@ -178,7 +180,7 @@ class Translator extends \PhpAot\Core\Translator
return 'PyCore::tuple([' . implode(', ', $ids) . '])'; return 'PyCore::tuple([' . implode(', ', $ids) . '])';
} }
function parseListComp($listComp) public function parseListComp($listComp)
{ {
$code = ''; $code = '';
$this->indentLevel++; $this->indentLevel++;
@ -192,7 +194,7 @@ class Translator extends \PhpAot\Core\Translator
return '(function(' . implode(',', $captures) . ') {' . PHP_EOL . $code; return '(function(' . implode(',', $captures) . ') {' . PHP_EOL . $code;
} }
function parseValue($value) public function parseValue($value)
{ {
switch ($value->_type) { switch ($value->_type) {
case 'Call': case 'Call':
@ -243,7 +245,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseSlice($slice): string public function parseSlice($slice): string
{ {
if ($slice->_type == 'Constant') { if ($slice->_type == 'Constant') {
return $this->parseConstant($slice); return $this->parseConstant($slice);
@ -259,7 +261,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseImportFrom($node): string public function parseImportFrom($node): string
{ {
$module = $node->module; $module = $node->module;
$imports = []; $imports = [];
@ -272,7 +274,7 @@ class Translator extends \PhpAot\Core\Translator
return implode(';' . PHP_EOL, $imports); return implode(';' . PHP_EOL, $imports);
} }
function parseImport($node): string public function parseImport($node): string
{ {
$out = ''; $out = '';
foreach ($node->names as $name) { foreach ($node->names as $name) {
@ -283,7 +285,7 @@ class Translator extends \PhpAot\Core\Translator
return $out; return $out;
} }
function parseTarget($target) public function parseTarget($target)
{ {
switch ($target->_type) { switch ($target->_type) {
case 'Name': case 'Name':
@ -332,7 +334,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseIfExp($exp) public function parseIfExp($exp)
{ {
$test = $this->parseTest($exp->test); $test = $this->parseTest($exp->test);
$orelse = $this->parseValue($exp->orelse); $orelse = $this->parseValue($exp->orelse);
@ -340,7 +342,7 @@ class Translator extends \PhpAot\Core\Translator
return $test . ' ? ' . $body . ' : ' . $orelse; return $test . ' ? ' . $body . ' : ' . $orelse;
} }
function parseExpr($node) public function parseExpr($node)
{ {
switch ($node->_type) { switch ($node->_type) {
case 'Expr': case 'Expr':
@ -349,12 +351,13 @@ class Translator extends \PhpAot\Core\Translator
} else { } else {
return $this->parseValue($node->value) . ';'; return $this->parseValue($node->value) . ';';
} }
// no break
default: default:
return $this->parseValue($node->value) . ';'; return $this->parseValue($node->value) . ';';
} }
} }
function parseArguments($args): string public function parseArguments($args): string
{ {
$defaults = $args->defaults ?? []; $defaults = $args->defaults ?? [];
$names = []; $names = [];
@ -371,12 +374,12 @@ class Translator extends \PhpAot\Core\Translator
return implode(', ', $names); return implode(', ', $names);
} }
function parseReturn($node) public function parseReturn($node)
{ {
return 'return ' . $this->parseValue($node->value); return 'return ' . $this->parseValue($node->value);
} }
function parseTest($test) public function parseTest($test)
{ {
$comparator = null; $comparator = null;
$left = $test->left ? $this->parseValue($test->left) : ''; $left = $test->left ? $this->parseValue($test->left) : '';
@ -423,7 +426,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseIf($node) public function parseIf($node)
{ {
$expr = $this->parseTest($node->test); $expr = $this->parseTest($node->test);
$this->indentLevel++; $this->indentLevel++;
@ -437,7 +440,7 @@ class Translator extends \PhpAot\Core\Translator
PHP_EOL; PHP_EOL;
} }
function parseIter($iter) public function parseIter($iter)
{ {
switch ($iter->_type) { switch ($iter->_type) {
case 'Call': case 'Call':
@ -459,7 +462,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseSubscript($node) public function parseSubscript($node)
{ {
if ($node->slice) { if ($node->slice) {
$op = $node->ctx->_type; $op = $node->ctx->_type;
@ -476,7 +479,7 @@ class Translator extends \PhpAot\Core\Translator
} }
} }
function parseFor($node) public function parseFor($node)
{ {
$target = $this->parseTarget($node->target); $target = $this->parseTarget($node->target);
$iter = $this->parseIter($node->iter); $iter = $this->parseIter($node->iter);
@ -492,13 +495,14 @@ class Translator extends \PhpAot\Core\Translator
return $code . $this->getIndent() . PHP_EOL . $this->getIndent() . '}'; return $code . $this->getIndent() . PHP_EOL . $this->getIndent() . '}';
} }
function parseFunctionDef($node) public function parseFunctionDef($node)
{ {
$name = $node->name; $name = $node->name;
$args = $this->parseArguments($node->args); $args = $this->parseArguments($node->args);
$fn = PHP_EOL . $this->getIndent() . 'function ' . $name . '(' . $args . ') {' . PHP_EOL; $fn = PHP_EOL . $this->getIndent() . 'function ' . $name . '(' . $args . ') {' . PHP_EOL;
$this->indentLevel++; $this->indentLevel++;
$fn .= $this->parseBody($node->body) . PHP_EOL;; $fn .= $this->parseBody($node->body) . PHP_EOL;
;
$this->indentLevel--; $this->indentLevel--;
$fn .= $this->getIndent() . '}' . PHP_EOL . PHP_EOL; $fn .= $this->getIndent() . '}' . PHP_EOL . PHP_EOL;
@ -606,7 +610,7 @@ class Translator extends \PhpAot\Core\Translator
$this->addLine($line, $lines); $this->addLine($line, $lines);
} }
function parseBody($tree): string public function parseBody($tree): string
{ {
$lines = []; $lines = [];
foreach ($tree as $node) { foreach ($tree as $node) {
@ -618,7 +622,7 @@ class Translator extends \PhpAot\Core\Translator
return implode(PHP_EOL, $lines); return implode(PHP_EOL, $lines);
} }
function convert($tree): void public function convert($tree): void
{ {
$this->prepare($tree); $this->prepare($tree);
$output = '<?php' . PHP_EOL; $output = '<?php' . PHP_EOL;
@ -733,7 +737,8 @@ class Translator extends \PhpAot\Core\Translator
$parseItem = function ($call, $target) use (&$code, &$finally_code) { $parseItem = function ($call, $target) use (&$code, &$finally_code) {
$call = $this->parseCall($call); $call = $this->parseCall($call);
$target = empty($target) ? '$__' : $this->parseTarget($target); $target = empty($target) ? '$__' : $this->parseTarget($target);
$code .= $target . '__object = ' . $call . ';' . PHP_EOL;; $code .= $target . '__object = ' . $call . ';' . PHP_EOL;
;
$code .= $target . ' = ' . $target . '__object->__enter__();' . PHP_EOL; $code .= $target . ' = ' . $target . '__object->__enter__();' . PHP_EOL;
$this->indentLevel++; $this->indentLevel++;
$finally_code .= $this->getIndent() . $target . '__object->__exit__(null, null, null);' . PHP_EOL; $finally_code .= $this->getIndent() . $target . '__object->__exit__(null, null, null);' . PHP_EOL;

Loading…
Cancel
Save