From 5824e859e6c0dfaf2644840a321f39571d54bd74 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 7 Jan 2026 12:09:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=8B=E5=80=BC=E6=93=8D=E4=BD=9C=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=8F=B3=E5=80=BC=EF=BC=8C=E4=BF=AE=E5=A4=8D=20try/ca?= =?UTF-8?q?tch=20=E6=B2=A1=E6=9C=89=20finally=20=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=B7=B3=E5=87=BA=E5=BE=AA=E7=8E=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/bench/test1.php | 125 +++++++++++++++++++++++---------------- src/Php/Translator.php | 4 +- 2 files changed, 75 insertions(+), 54 deletions(-) diff --git a/examples/bench/test1.php b/examples/bench/test1.php index 1e85a75d..34b89593 100644 --- a/examples/bench/test1.php +++ b/examples/bench/test1.php @@ -10,8 +10,9 @@ modified by anon */ -function write_mandelbrot_to_stream($w, $h, $stream, $bitmap) { - if($bitmap) +function write_mandelbrot_to_stream($w, $h, $stream, $bitmap) +{ + if ($bitmap) fprintf($stream, "P4\n%d %d\n", $w, $h); $bit_num = 128; @@ -24,78 +25,98 @@ function write_mandelbrot_to_stream($w, $h, $stream, $bitmap) { $ochars = ' .:-;!/>)|&IH%*#'; $pack_format = 'c*'; - for ($y = 0 ; $y < $h ; ++$y) { - $Ci = $y * $yfac - 1.0; - for ($x = 0 ; $x < $w ; ++$x) { - $Zr = 0; $Zi = 0; $Tr = 0; $Ti = 0.0; - $Cr = $x * $xfac - 1.5; - do { - for ($i = 0; $i < $iter; ++$i) { - $Zi = 2.0 * $Zr * $Zi + $Ci; - $Zr = $Tr - $Ti + $Cr; - $Tr = $Zr * $Zr; - if (($Tr+($Ti = $Zi * $Zi)) > 4.0) break 2; - } - $byte_acc += $bit_num; - } while (false); - - if($bitmap) { - if ($bit_num === 1) { - fwrite($stream, pack($pack_format, $byte_acc)); - $bit_num = 128; - $byte_acc = 0; - } else { - $bit_num >>= 1; - } - } else { - if($i == $iter) { - fwrite($stream, $ochars[0]); - } else { - fwrite($stream, $ochars[($i+1) & 15]); - } - } - } - if($bitmap) { - if ($bit_num !== 128) { - fwrite($stream, pack($pack_format, $byte_acc)); - $bit_num = 128; - $byte_acc = 0; - } - } else { - fwrite($stream, "\n"); - } + for ($y = 0; $y < $h; ++$y) { + $Ci = $y * $yfac - 1.0; + for ($x = 0; $x < $w; ++$x) { + $Zr = 0.0; + $Zi = 0.0; + $Tr = 0.0; + $Ti = 0.0; + $Cr = $x * $xfac - 1.5; + + try { + do { + for ($i = 0; $i < $iter; ++$i) { + $Zi = 2.0 * $Zr * $Zi + $Ci; + $Zr = $Tr - $Ti + $Cr; + $Tr = $Zr * $Zr; + + if (($Tr + ($Ti = $Zi * $Zi)) > 4.0) { + throw new Exception("break 2"); + } + } + $byte_acc += $bit_num; + } while (false); + } catch (Exception $e) { + } + + if ($bitmap) { + if ($bit_num === 1) { + fwrite($stream, pack($pack_format, $byte_acc)); + $bit_num = 128; + $byte_acc = 0; + } else { + $bit_num >>= 1; + } + } else { + if ($i == $iter) { + fwrite($stream, $ochars[0]); + } else { + fwrite($stream, $ochars[($i + 1) & 15]); + } + } + } + if ($bitmap) { + if ($bit_num !== 128) { + fwrite($stream, pack($pack_format, $byte_acc)); + $bit_num = 128; + $byte_acc = 0; + } + } else { + fwrite($stream, "\n"); + } } 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'; - if($binary_output) + if ($binary_output) $file_open_type = 'wb'; - $stream = fopen((string) $filename, $file_open_type); - if(false === $stream) + $stream = fopen((string)$filename, $file_open_type); + if (false === $stream) 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); return true; } -function treffynnon_mandelbrot_to_mem($w, $h, $binary_output) { +function treffynnon_mandelbrot_to_mem($w, $h, $binary_output) +{ $file_open_type = 'w+'; - if($binary_output) + if ($binary_output) $file_open_type = 'w+b'; $stream = fopen('php://memory', $file_open_type); - if(false === $stream) + if (false === $stream) 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); $ret = stream_get_contents($stream); fclose($stream); return $ret; } -echo treffynnon_mandelbrot_to_mem((int) $argv[1], (int) $argv[1], false); \ No newline at end of file +function main() +{ + global $argv; + if (count($argv) < 3) { + echo "Usage: php mandelbrot.php []\n"; + exit(1); + } + echo treffynnon_mandelbrot_to_mem((int)$argv[2], (int)$argv[2], false); +} diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 4d785149..5f14c671 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -891,7 +891,7 @@ class Translator extends \PhpAot\Core\Translator $leftExpr = $this->convertExprType($leftExpr, $leftType, self::TYPE_INT); } - return '(' . $leftExpr . ' ' . $op . ' ' . $rightExpr . ')'; + return '((' . $leftExpr . ') ' . $op . ' (' . $rightExpr . '))'; } private function parseBinaryOpPlus(mixed $expr): string @@ -2477,8 +2477,8 @@ class Translator extends \PhpAot\Core\Translator if ($finally) { $code .= $this->parseStmts($finally->stmts); $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; }