赋值操作作为右值,修复 try/catch 没有 finally 无法跳出循环的问题

pull/1/head
韩天峰 8 months ago
parent b0d261576f
commit 5824e859e6
  1. 61
      examples/bench/test1.php
  2. 4
      src/Php/Translator.php

@ -10,8 +10,9 @@
modified by anon modified by anon
*/ */
function write_mandelbrot_to_stream($w, $h, $stream, $bitmap) { function write_mandelbrot_to_stream($w, $h, $stream, $bitmap)
if($bitmap) {
if ($bitmap)
fprintf($stream, "P4\n%d %d\n", $w, $h); fprintf($stream, "P4\n%d %d\n", $w, $h);
$bit_num = 128; $bit_num = 128;
@ -24,22 +25,32 @@ function write_mandelbrot_to_stream($w, $h, $stream, $bitmap) {
$ochars = ' .:-;!/>)|&IH%*#'; $ochars = ' .:-;!/>)|&IH%*#';
$pack_format = 'c*'; $pack_format = 'c*';
for ($y = 0 ; $y < $h ; ++$y) { for ($y = 0; $y < $h; ++$y) {
$Ci = $y * $yfac - 1.0; $Ci = $y * $yfac - 1.0;
for ($x = 0 ; $x < $w ; ++$x) { for ($x = 0; $x < $w; ++$x) {
$Zr = 0; $Zi = 0; $Tr = 0; $Ti = 0.0; $Zr = 0.0;
$Zi = 0.0;
$Tr = 0.0;
$Ti = 0.0;
$Cr = $x * $xfac - 1.5; $Cr = $x * $xfac - 1.5;
try {
do { do {
for ($i = 0; $i < $iter; ++$i) { for ($i = 0; $i < $iter; ++$i) {
$Zi = 2.0 * $Zr * $Zi + $Ci; $Zi = 2.0 * $Zr * $Zi + $Ci;
$Zr = $Tr - $Ti + $Cr; $Zr = $Tr - $Ti + $Cr;
$Tr = $Zr * $Zr; $Tr = $Zr * $Zr;
if (($Tr+($Ti = $Zi * $Zi)) > 4.0) break 2;
if (($Tr + ($Ti = $Zi * $Zi)) > 4.0) {
throw new Exception("break 2");
}
} }
$byte_acc += $bit_num; $byte_acc += $bit_num;
} while (false); } while (false);
} catch (Exception $e) {
}
if($bitmap) { if ($bitmap) {
if ($bit_num === 1) { if ($bit_num === 1) {
fwrite($stream, pack($pack_format, $byte_acc)); fwrite($stream, pack($pack_format, $byte_acc));
$bit_num = 128; $bit_num = 128;
@ -48,14 +59,14 @@ function write_mandelbrot_to_stream($w, $h, $stream, $bitmap) {
$bit_num >>= 1; $bit_num >>= 1;
} }
} else { } else {
if($i == $iter) { if ($i == $iter) {
fwrite($stream, $ochars[0]); fwrite($stream, $ochars[0]);
} else { } else {
fwrite($stream, $ochars[($i+1) & 15]); fwrite($stream, $ochars[($i + 1) & 15]);
} }
} }
} }
if($bitmap) { if ($bitmap) {
if ($bit_num !== 128) { if ($bit_num !== 128) {
fwrite($stream, pack($pack_format, $byte_acc)); fwrite($stream, pack($pack_format, $byte_acc));
$bit_num = 128; $bit_num = 128;
@ -68,34 +79,44 @@ function write_mandelbrot_to_stream($w, $h, $stream, $bitmap) {
return true; return true;
} }
function treffynnon_mandelbrot_to_file($filename, $w, $h, $binary_output) { function treffynnon_mandelbrot_to_file($filename, $w, $h, $binary_output)
{
$file_open_type = 'w'; $file_open_type = 'w';
if($binary_output) if ($binary_output)
$file_open_type = 'wb'; $file_open_type = 'wb';
$stream = fopen((string) $filename, $file_open_type); $stream = fopen((string)$filename, $file_open_type);
if(false === $stream) if (false === $stream)
return false; return false;
write_mandelbrot_to_stream((int) $w, (int) $h, $stream, (bool) $binary_output); write_mandelbrot_to_stream((int)$w, (int)$h, $stream, (bool)$binary_output);
fclose($stream); fclose($stream);
return true; return true;
} }
function treffynnon_mandelbrot_to_mem($w, $h, $binary_output) { function treffynnon_mandelbrot_to_mem($w, $h, $binary_output)
{
$file_open_type = 'w+'; $file_open_type = 'w+';
if($binary_output) if ($binary_output)
$file_open_type = 'w+b'; $file_open_type = 'w+b';
$stream = fopen('php://memory', $file_open_type); $stream = fopen('php://memory', $file_open_type);
if(false === $stream) if (false === $stream)
return ''; return '';
write_mandelbrot_to_stream((int) $w, (int) $h, $stream, (bool) $binary_output); write_mandelbrot_to_stream((int)$w, (int)$h, $stream, (bool)$binary_output);
rewind($stream); rewind($stream);
$ret = stream_get_contents($stream); $ret = stream_get_contents($stream);
fclose($stream); fclose($stream);
return $ret; return $ret;
} }
echo treffynnon_mandelbrot_to_mem((int) $argv[1], (int) $argv[1], false); function main()
{
global $argv;
if (count($argv) < 3) {
echo "Usage: php mandelbrot.php <width> <height> [<output_file>]\n";
exit(1);
}
echo treffynnon_mandelbrot_to_mem((int)$argv[2], (int)$argv[2], false);
}

@ -891,7 +891,7 @@ class Translator extends \PhpAot\Core\Translator
$leftExpr = $this->convertExprType($leftExpr, $leftType, self::TYPE_INT); $leftExpr = $this->convertExprType($leftExpr, $leftType, self::TYPE_INT);
} }
return '(' . $leftExpr . ' ' . $op . ' ' . $rightExpr . ')'; return '((' . $leftExpr . ') ' . $op . ' (' . $rightExpr . '))';
} }
private function parseBinaryOpPlus(mixed $expr): string private function parseBinaryOpPlus(mixed $expr): string
@ -2477,8 +2477,8 @@ class Translator extends \PhpAot\Core\Translator
if ($finally) { if ($finally) {
$code .= $this->parseStmts($finally->stmts); $code .= $this->parseStmts($finally->stmts);
$code .= PHP_EOL; $code .= PHP_EOL;
$code .= 'if (' . $exVar . ') {' . PHP_EOL . $this->getIndent() . 'php::throwException(' . $exVar . ');' . PHP_EOL . $this->getIndent() . '}';
} }
$code .= 'if (' . $exVar . ') {' . PHP_EOL . $this->getIndent() . 'php::throwException(' . $exVar . ');' . PHP_EOL . $this->getIndent() . '}';
return $code; return $code;
} }

Loading…
Cancel
Save