Skip to content

#137 - Add support of false return type #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*.lo
*.la
*.profraw
*.dep

.deps
.libs
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added - xxxx-xx-xx
- Added support for `false` return type [#137](https://github.com/phalcon/php-zephir-parser/issues/137)

## [1.4.2] - 2021-12-11
### Added
Expand Down
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
</dir>

<dir name="return-types">
<file name="false.phpt" role="test"/>
<file name="float.phpt" role="test"/>
<file name="int.phpt" role="test"/>
<file name="mixed.phpt" role="test"/>
</dir>
Expand Down
4 changes: 4 additions & 0 deletions parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ static void xx_ret_type(zval *ret, int type)
parser_get_string(ret, "this");
return;

case XX_T_TYPE_FALSE:
parser_get_string(ret, "false");
return;

default:
fprintf(stderr, "unknown type?\n");
}
Expand Down
1 change: 1 addition & 0 deletions parser/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#define XX_T_TYPE_NULL 334
#define XX_T_TYPE_THIS 335
#define XX_T_TYPE_MIXED 336
#define XX_T_TYPE_FALSE 337

#define XX_T_NAMESPACE 350
#define XX_T_CLASS 351
Expand Down
12 changes: 12 additions & 0 deletions parser/zephir.lemon
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,14 @@ xx_method_return_type_item(R) ::= THIS . {
}
}

xx_method_return_type_item(R) ::= FALSE . {
{
zval type;
xx_ret_type(&type, XX_T_TYPE_FALSE);
xx_ret_return_type_item(&R, &type, NULL, 0, 0, status->scanner_state);
}
}

xx_method_return_type_item(R) ::= xx_parameter_type(T) NOT . {
xx_ret_return_type_item(&R, &T, NULL, 1, 0, status->scanner_state);
}
Expand Down Expand Up @@ -950,6 +958,10 @@ xx_parameter_type(R) ::= TYPE_MIXED . {
xx_ret_type(&R, XX_TYPE_MIXED);
}

xx_parameter_type(R) ::= TYPE_FALSE . {
xx_ret_type(&R, XX_TYPE_FALSE);
}

xx_parameter_type(R) ::= TYPE_OBJECT . {
xx_ret_type(&R, XX_TYPE_OBJECT);
}
Expand Down
175 changes: 175 additions & 0 deletions tests/functions/return-types/false.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
--TEST--
Function definition with `false` return type
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
<?php
$code =<<<ZEP
function singleReturn() -> false { return false; }

function unionReturn() -> int | false { return 1; }
ZEP;

$ir = zephir_parse_file($code, '(eval code)');
var_dump($ir);
?>
--EXPECT--
array(2) {
[0]=>
array(7) {
["type"]=>
string(8) "function"
["name"]=>
string(12) "singleReturn"
["statements"]=>
array(1) {
[0]=>
array(5) {
["type"]=>
string(6) "return"
["expr"]=>
array(5) {
["type"]=>
string(4) "bool"
["value"]=>
string(5) "false"
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(49)
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(51)
}
}
["return-type"]=>
array(6) {
["type"]=>
string(11) "return-type"
["list"]=>
array(1) {
[0]=>
array(6) {
["type"]=>
string(21) "return-type-parameter"
["data-type"]=>
string(5) "false"
["mandatory"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(35)
}
}
["void"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(35)
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(3)
["char"]=>
int(8)
}
[1]=>
array(7) {
["type"]=>
string(8) "function"
["name"]=>
string(11) "unionReturn"
["statements"]=>
array(1) {
[0]=>
array(5) {
["type"]=>
string(6) "return"
["expr"]=>
array(5) {
["type"]=>
string(3) "int"
["value"]=>
string(1) "1"
["file"]=>
string(11) "(eval code)"
["line"]=>
int(3)
["char"]=>
int(49)
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(3)
["char"]=>
int(51)
}
}
["return-type"]=>
array(6) {
["type"]=>
string(11) "return-type"
["list"]=>
array(2) {
[0]=>
array(6) {
["type"]=>
string(21) "return-type-parameter"
["data-type"]=>
string(3) "int"
["mandatory"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(3)
["char"]=>
int(31)
}
[1]=>
array(6) {
["type"]=>
string(21) "return-type-parameter"
["data-type"]=>
string(5) "false"
["mandatory"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(3)
["char"]=>
int(39)
}
}
["void"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(3)
["char"]=>
int(39)
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(3)
["char"]=>
int(8)
}
}
60 changes: 60 additions & 0 deletions tests/functions/return-types/float.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
Function definition with `float` return type
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
<?php
$code =<<<ZEP
function test() -> float { }
ZEP;

$ir = zephir_parse_file($code, '(eval code)');
var_dump($ir);
?>
--EXPECT--
array(1) {
[0]=>
array(6) {
["type"]=>
string(8) "function"
["name"]=>
string(4) "test"
["return-type"]=>
array(6) {
["type"]=>
string(11) "return-type"
["list"]=>
array(1) {
[0]=>
array(6) {
["type"]=>
string(21) "return-type-parameter"
["data-type"]=>
string(6) "double"
["mandatory"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(27)
}
}
["void"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(27)
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(9)
}
}
2 changes: 1 addition & 1 deletion tests/functions/return-types/int.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Function definition with mandatory return type
Function definition with `int` return type
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
Expand Down
2 changes: 1 addition & 1 deletion tests/functions/return-types/mixed.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Function definition with void
Function definition with `mixed` return type
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
Expand Down