支持 empty 语法

pull/1/head
韩天峰 8 months ago
parent 73f84217db
commit 730e0e45f1
  1. 7
      src/Php/Translator.php
  2. 14
      tests/core/lang/empty_variation.phpt
  3. 76
      tests/core/lang/foreachLoop.004.phpt
  4. 44
      tests/core/lang/string_decimals_001.phpt
  5. 13
      tests/core/lang/throw_variation_001.phpt
  6. 13
      tests/core/lang/zend_throw_exception_001.phpt

@ -598,6 +598,8 @@ class Translator extends \PhpAot\Core\Translator
switch ($type) {
case 'Expr_Isset':
return $this->parseIsset($expr);
case 'Expr_Empty':
return $this->parseEmpty($expr);
case 'Expr_Assign':
return $this->parseAssign($expr);
case 'Expr_AssignRef':
@ -2218,6 +2220,11 @@ class Translator extends \PhpAot\Core\Translator
}
}
private function parseEmpty(mixed $expr): string
{
return 'php::empty(' . $this->parseExpr($expr->expr) . ')';
}
private function parseCastArray(mixed $expr): string
{
return $this->convertArrayExpr($this->parseIdentifier($expr->expr));

@ -0,0 +1,14 @@
--TEST--
empty() on array elements
--FILE--
<?php
$a=array('0' => '0', 'empty' => '0');
var_dump(empty($a['empty']));
var_dump(empty($a[0]));
$b='0';
var_dump(empty($b));
?>
--EXPECT--
bool(true)
bool(true)
bool(true)

@ -0,0 +1,76 @@
--TEST--
Foreach loop tests - using an array element as the $value
--FILE--
<?php
$a=array("a", "b", "c");
$v=array();
foreach($a as $v[0]) {
var_dump($v);
}
var_dump($a);
var_dump($v);
echo "\n";
$a=array("a", "b", "c");
$v=array();
foreach($a as $k=>$v[0]) {
var_dump($k, $v);
}
var_dump($a);
var_dump($k, $v);
?>
--EXPECT--
array(1) {
[0]=>
string(1) "a"
}
array(1) {
[0]=>
string(1) "b"
}
array(1) {
[0]=>
string(1) "c"
}
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
array(1) {
[0]=>
string(1) "c"
}
int(0)
array(1) {
[0]=>
string(1) "a"
}
int(1)
array(1) {
[0]=>
string(1) "b"
}
int(2)
array(1) {
[0]=>
string(1) "c"
}
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
int(2)
array(1) {
[0]=>
string(1) "c"
}

@ -0,0 +1,44 @@
--TEST--
String conversion with multiple decimal points
--FILE--
<?php
function test($str) {
echo "\n--> Testing $str:\n";
var_dump((int)$str);
var_dump((float)$str);
var_dump($str > 0);
}
function main() {
test("..9");
test(".9.");
test("9..");
test("9.9.");
test("9.9.9");
}
?>
--EXPECT--
--> Testing ..9:
int(0)
float(0)
bool(false)
--> Testing .9.:
int(0)
float(0.9)
bool(false)
--> Testing 9..:
int(9)
float(9)
bool(true)
--> Testing 9.9.:
int(9)
float(9.9)
bool(true)
--> Testing 9.9.9:
int(9)
float(9.9)
bool(true)

@ -0,0 +1,13 @@
--TEST--
Catching an exception thrown from an included file
--FILE--
<?php
try {
include __DIR__ . "/inc_throw.inc";
} catch (Exception $e) {
echo "caught exception\n";
}
?>
--EXPECT--
caught exception

@ -0,0 +1,13 @@
--TEST--
zend_throw_exception with NULL message
--FILE--
<?php
try {
$assert = 'assert';
$assert(false);
} catch (AssertionError $assertionError) {
echo 'Done' . PHP_EOL;
}
?>
--EXPECT--
Done
Loading…
Cancel
Save