Skip to content

Commit

Permalink
Merge pull request #121 from zephir-lang/#120-mixed-type
Browse files Browse the repository at this point in the history
#120 - Add `mixed` type
  • Loading branch information
Jeckerson authored Sep 15, 2021
2 parents 3fee124 + 02173de commit 5605563
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 1 deletion.
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
- Added support for `mixed` type [#120](https://github.com/phalcon/php-zephir-parser/issues/120)

## [1.3.8] - 2021-09-08
### Changed
Expand Down
3 changes: 3 additions & 0 deletions parser/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ void xx_parse_program(zval *return_value, char *program, size_t program_length,
case XX_T_TYPE_RESOURCE:
xx_(xx_parser, XX_TYPE_RESOURCE, NULL, parser_status);
break;
case XX_T_TYPE_MIXED:
xx_(xx_parser, XX_TYPE_MIXED, NULL, parser_status);
break;
case XX_T_TYPE_CALLABLE:
xx_(xx_parser, XX_TYPE_CALLABLE, NULL, parser_status);
break;
Expand Down
9 changes: 8 additions & 1 deletion parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ static void xx_ret_return_type_item(zval *ret, zval *type, zval *cast, int manda
static void xx_ret_type(zval *ret, int type)
{
switch (type) {

case XX_TYPE_INTEGER:
parser_get_string(ret, "int");
return;
Expand Down Expand Up @@ -587,6 +586,10 @@ static void xx_ret_type(zval *ret, int type)
parser_get_string(ret, "object");
return;

case XX_TYPE_MIXED:
parser_get_string(ret, "mixed");
return;

case XX_T_TYPE_NULL:
parser_get_string(ret, "null");
return;
Expand Down Expand Up @@ -1040,6 +1043,10 @@ static void xx_ret_declare_statement(zval *ret, int type, zval *variables, xx_sc
parser_add_str(ret, "data-type", "resource");
break;

case XX_T_TYPE_MIXED:
parser_add_str(ret, "data-type", "mixed");
break;

case XX_T_TYPE_OBJECT:
parser_add_str(ret, "data-type", "object");
break;
Expand Down
1 change: 1 addition & 0 deletions parser/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define XX_T_TYPE_RESOURCE 333
#define XX_T_TYPE_NULL 334
#define XX_T_TYPE_THIS 335
#define XX_T_TYPE_MIXED 336

#define XX_T_NAMESPACE 350
#define XX_T_CLASS 351
Expand Down
6 changes: 6 additions & 0 deletions parser/scanner.re
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ int xx_get_token(xx_scanner_state *s, xx_scanner_token *token) {
return 0;
}
'mixed' {
s->active_char += sizeof("mixed")-1;
token->opcode = XX_T_TYPE_MIXED;
return 0;
}
'if' {
s->active_char += sizeof("if")-1;
token->opcode = XX_T_IF;
Expand Down
8 changes: 8 additions & 0 deletions parser/zephir.lemon
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,10 @@ xx_parameter_type(R) ::= TYPE_RESOURCE . {
xx_ret_type(&R, XX_TYPE_RESOURCE);
}

xx_parameter_type(R) ::= TYPE_MIXED . {
xx_ret_type(&R, XX_TYPE_MIXED);
}

xx_parameter_type(R) ::= TYPE_OBJECT . {
xx_ret_type(&R, XX_TYPE_OBJECT);
}
Expand Down Expand Up @@ -1596,6 +1600,10 @@ xx_declare_statement(R) ::= TYPE_ARRAY xx_declare_variable_list(L) DOTCOMMA . {
xx_ret_declare_statement(&R, XX_T_TYPE_ARRAY, &L, status->scanner_state);
}

xx_declare_statement(R) ::= TYPE_MIXED xx_declare_variable_list(L) DOTCOMMA . {
xx_ret_declare_statement(&R, XX_T_TYPE_MIXED, &L, status->scanner_state);
}

xx_declare_variable_list(R) ::= xx_declare_variable_list(L) COMMA xx_declare_variable(V) . {
xx_ret_list(&R, &L, &V, status->scanner_state);
}
Expand Down
53 changes: 53 additions & 0 deletions tests/functions/parameter-types/int.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
Parameter type 'int'
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
<?php
$code =<<<ZEP
function test(int value) { }
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"
["parameters"]=>
array(1) {
[0]=>
array(9) {
["type"]=>
string(9) "parameter"
["name"]=>
string(5) "value"
["const"]=>
int(0)
["data-type"]=>
string(3) "int"
["mandatory"]=>
int(0)
["reference"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(25)
}
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(9)
}
}
53 changes: 53 additions & 0 deletions tests/functions/parameter-types/mixed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
Parameter type 'mixed'
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
<?php
$code =<<<ZEP
function test(mixed value) { }
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"
["parameters"]=>
array(1) {
[0]=>
array(9) {
["type"]=>
string(9) "parameter"
["name"]=>
string(5) "value"
["const"]=>
int(0)
["data-type"]=>
string(5) "mixed"
["mandatory"]=>
int(0)
["reference"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(27)
}
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(9)
}
}
60 changes: 60 additions & 0 deletions tests/functions/return-types/int.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
Function definition with mandatory return type
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
<?php
$code =<<<ZEP
function test() -> int { }
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(3) "int"
["mandatory"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(25)
}
}
["void"]=>
int(0)
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(25)
}
["file"]=>
string(11) "(eval code)"
["line"]=>
int(1)
["char"]=>
int(9)
}
}
60 changes: 60 additions & 0 deletions tests/functions/return-types/mixed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
Function definition with void
--SKIPIF--
<?php include(__DIR__ . '/../../skipif.inc'); ?>
--FILE--
<?php
$code =<<<ZEP
function test() -> mixed { }
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(5) "mixed"
["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)
}
}

0 comments on commit 5605563

Please sign in to comment.