Skip to content

Commit 5605563

Browse files
authored
Merge pull request #121 from zephir-lang/#120-mixed-type
#120 - Add `mixed` type
2 parents 3fee124 + 02173de commit 5605563

File tree

10 files changed

+254
-1
lines changed

10 files changed

+254
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Added
9+
- Added support for `mixed` type [#120](https://github.com/phalcon/php-zephir-parser/issues/120)
810

911
## [1.3.8] - 2021-09-08
1012
### Changed

parser/base.c

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ void xx_parse_program(zval *return_value, char *program, size_t program_length,
444444
case XX_T_TYPE_RESOURCE:
445445
xx_(xx_parser, XX_TYPE_RESOURCE, NULL, parser_status);
446446
break;
447+
case XX_T_TYPE_MIXED:
448+
xx_(xx_parser, XX_TYPE_MIXED, NULL, parser_status);
449+
break;
447450
case XX_T_TYPE_CALLABLE:
448451
xx_(xx_parser, XX_TYPE_CALLABLE, NULL, parser_status);
449452
break;

parser/parser.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ static void xx_ret_return_type_item(zval *ret, zval *type, zval *cast, int manda
534534
static void xx_ret_type(zval *ret, int type)
535535
{
536536
switch (type) {
537-
538537
case XX_TYPE_INTEGER:
539538
parser_get_string(ret, "int");
540539
return;
@@ -587,6 +586,10 @@ static void xx_ret_type(zval *ret, int type)
587586
parser_get_string(ret, "object");
588587
return;
589588

589+
case XX_TYPE_MIXED:
590+
parser_get_string(ret, "mixed");
591+
return;
592+
590593
case XX_T_TYPE_NULL:
591594
parser_get_string(ret, "null");
592595
return;
@@ -1040,6 +1043,10 @@ static void xx_ret_declare_statement(zval *ret, int type, zval *variables, xx_sc
10401043
parser_add_str(ret, "data-type", "resource");
10411044
break;
10421045

1046+
case XX_T_TYPE_MIXED:
1047+
parser_add_str(ret, "data-type", "mixed");
1048+
break;
1049+
10431050
case XX_T_TYPE_OBJECT:
10441051
parser_add_str(ret, "data-type", "object");
10451052
break;

parser/scanner.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define XX_T_TYPE_RESOURCE 333
4747
#define XX_T_TYPE_NULL 334
4848
#define XX_T_TYPE_THIS 335
49+
#define XX_T_TYPE_MIXED 336
4950

5051
#define XX_T_NAMESPACE 350
5152
#define XX_T_CLASS 351

parser/scanner.re

+6
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ int xx_get_token(xx_scanner_state *s, xx_scanner_token *token) {
297297
return 0;
298298
}
299299
300+
'mixed' {
301+
s->active_char += sizeof("mixed")-1;
302+
token->opcode = XX_T_TYPE_MIXED;
303+
return 0;
304+
}
305+
300306
'if' {
301307
s->active_char += sizeof("if")-1;
302308
token->opcode = XX_T_IF;

parser/zephir.lemon

+8
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@ xx_parameter_type(R) ::= TYPE_RESOURCE . {
946946
xx_ret_type(&R, XX_TYPE_RESOURCE);
947947
}
948948

949+
xx_parameter_type(R) ::= TYPE_MIXED . {
950+
xx_ret_type(&R, XX_TYPE_MIXED);
951+
}
952+
949953
xx_parameter_type(R) ::= TYPE_OBJECT . {
950954
xx_ret_type(&R, XX_TYPE_OBJECT);
951955
}
@@ -1596,6 +1600,10 @@ xx_declare_statement(R) ::= TYPE_ARRAY xx_declare_variable_list(L) DOTCOMMA . {
15961600
xx_ret_declare_statement(&R, XX_T_TYPE_ARRAY, &L, status->scanner_state);
15971601
}
15981602

1603+
xx_declare_statement(R) ::= TYPE_MIXED xx_declare_variable_list(L) DOTCOMMA . {
1604+
xx_ret_declare_statement(&R, XX_T_TYPE_MIXED, &L, status->scanner_state);
1605+
}
1606+
15991607
xx_declare_variable_list(R) ::= xx_declare_variable_list(L) COMMA xx_declare_variable(V) . {
16001608
xx_ret_list(&R, &L, &V, status->scanner_state);
16011609
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Parameter type 'int'
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test(int value) { }
9+
ZEP;
10+
11+
$ir = zephir_parse_file($code, '(eval code)');
12+
var_dump($ir);
13+
?>
14+
--EXPECT--
15+
array(1) {
16+
[0]=>
17+
array(6) {
18+
["type"]=>
19+
string(8) "function"
20+
["name"]=>
21+
string(4) "test"
22+
["parameters"]=>
23+
array(1) {
24+
[0]=>
25+
array(9) {
26+
["type"]=>
27+
string(9) "parameter"
28+
["name"]=>
29+
string(5) "value"
30+
["const"]=>
31+
int(0)
32+
["data-type"]=>
33+
string(3) "int"
34+
["mandatory"]=>
35+
int(0)
36+
["reference"]=>
37+
int(0)
38+
["file"]=>
39+
string(11) "(eval code)"
40+
["line"]=>
41+
int(1)
42+
["char"]=>
43+
int(25)
44+
}
45+
}
46+
["file"]=>
47+
string(11) "(eval code)"
48+
["line"]=>
49+
int(1)
50+
["char"]=>
51+
int(9)
52+
}
53+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Parameter type 'mixed'
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test(mixed value) { }
9+
ZEP;
10+
11+
$ir = zephir_parse_file($code, '(eval code)');
12+
var_dump($ir);
13+
?>
14+
--EXPECT--
15+
array(1) {
16+
[0]=>
17+
array(6) {
18+
["type"]=>
19+
string(8) "function"
20+
["name"]=>
21+
string(4) "test"
22+
["parameters"]=>
23+
array(1) {
24+
[0]=>
25+
array(9) {
26+
["type"]=>
27+
string(9) "parameter"
28+
["name"]=>
29+
string(5) "value"
30+
["const"]=>
31+
int(0)
32+
["data-type"]=>
33+
string(5) "mixed"
34+
["mandatory"]=>
35+
int(0)
36+
["reference"]=>
37+
int(0)
38+
["file"]=>
39+
string(11) "(eval code)"
40+
["line"]=>
41+
int(1)
42+
["char"]=>
43+
int(27)
44+
}
45+
}
46+
["file"]=>
47+
string(11) "(eval code)"
48+
["line"]=>
49+
int(1)
50+
["char"]=>
51+
int(9)
52+
}
53+
}

tests/functions/return-types/int.phpt

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Function definition with mandatory return type
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() -> int { }
9+
ZEP;
10+
11+
$ir = zephir_parse_file($code, '(eval code)');
12+
var_dump($ir);
13+
?>
14+
--EXPECT--
15+
array(1) {
16+
[0]=>
17+
array(6) {
18+
["type"]=>
19+
string(8) "function"
20+
["name"]=>
21+
string(4) "test"
22+
["return-type"]=>
23+
array(6) {
24+
["type"]=>
25+
string(11) "return-type"
26+
["list"]=>
27+
array(1) {
28+
[0]=>
29+
array(6) {
30+
["type"]=>
31+
string(21) "return-type-parameter"
32+
["data-type"]=>
33+
string(3) "int"
34+
["mandatory"]=>
35+
int(0)
36+
["file"]=>
37+
string(11) "(eval code)"
38+
["line"]=>
39+
int(1)
40+
["char"]=>
41+
int(25)
42+
}
43+
}
44+
["void"]=>
45+
int(0)
46+
["file"]=>
47+
string(11) "(eval code)"
48+
["line"]=>
49+
int(1)
50+
["char"]=>
51+
int(25)
52+
}
53+
["file"]=>
54+
string(11) "(eval code)"
55+
["line"]=>
56+
int(1)
57+
["char"]=>
58+
int(9)
59+
}
60+
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Function definition with void
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../../skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$code =<<<ZEP
8+
function test() -> mixed { }
9+
ZEP;
10+
11+
$ir = zephir_parse_file($code, '(eval code)');
12+
var_dump($ir);
13+
?>
14+
--EXPECT--
15+
array(1) {
16+
[0]=>
17+
array(6) {
18+
["type"]=>
19+
string(8) "function"
20+
["name"]=>
21+
string(4) "test"
22+
["return-type"]=>
23+
array(6) {
24+
["type"]=>
25+
string(11) "return-type"
26+
["list"]=>
27+
array(1) {
28+
[0]=>
29+
array(6) {
30+
["type"]=>
31+
string(21) "return-type-parameter"
32+
["data-type"]=>
33+
string(5) "mixed"
34+
["mandatory"]=>
35+
int(0)
36+
["file"]=>
37+
string(11) "(eval code)"
38+
["line"]=>
39+
int(1)
40+
["char"]=>
41+
int(27)
42+
}
43+
}
44+
["void"]=>
45+
int(0)
46+
["file"]=>
47+
string(11) "(eval code)"
48+
["line"]=>
49+
int(1)
50+
["char"]=>
51+
int(27)
52+
}
53+
["file"]=>
54+
string(11) "(eval code)"
55+
["line"]=>
56+
int(1)
57+
["char"]=>
58+
int(9)
59+
}
60+
}

0 commit comments

Comments
 (0)