Skip to content

Use short form for type casts #102

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 1 commit into from
Dec 26, 2017
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
4 changes: 2 additions & 2 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ module.exports = {
return this.node('cast')('int', this.next().read_expr());

case this.tok.T_DOUBLE_CAST:
return this.node('cast')('double', this.next().read_expr());
return this.node('cast')('float', this.next().read_expr());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure in PHP but in principle double and float have not the same precision, I'll check this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 got it from https://github.com/glayzzle/php-parser/blob/master/src/lexer.js#L111, actually there is only one token to these 3 casts types

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly - they are just aliases in PHP 😉


case this.tok.T_STRING_CAST:
return this.node('cast')('string', this.next().read_expr());
Expand All @@ -284,7 +284,7 @@ module.exports = {
return this.node('cast')('object', this.next().read_expr());

case this.tok.T_BOOL_CAST:
return this.node('cast')('boolean', this.next().read_expr());
return this.node('cast')('bool', this.next().read_expr());

case this.tok.T_UNSET_CAST:
return this.node('unset')(
Expand Down
33 changes: 20 additions & 13 deletions test/exprTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,35 @@ describe('Test expressions', function() {
});

it('test cast', function() {
var typesWithAlias = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

int: ['int', 'integer'],
bool: ['bool', 'boolean'],
float: ['float', 'double', 'real']
}
Object.keys(typesWithAlias).forEach(function(shortName) {
var aliases = typesWithAlias[shortName];
var ast = parser.parseEval(aliases.map(function(alias) {
return '(' + alias + ')$var;';
}).join('\n'));

aliases.forEach(function(alias, index) {
ast.children[index].kind.should.be.exactly('cast');
ast.children[index].type.should.be.exactly(shortName);
});
});
var ast = parser.parseEval([
'(int)$var;',
'(double)$var;',
'(string)$var;',
'(array)$var;',
'(object)$var;',
'(boolean)$var;',
'(unset)$var;'
].join('\n'));
ast.children[0].kind.should.be.exactly('cast');
ast.children[0].type.should.be.exactly('int');
ast.children[0].type.should.be.exactly('string');
ast.children[1].kind.should.be.exactly('cast');
ast.children[1].type.should.be.exactly('double');
ast.children[1].type.should.be.exactly('array');
ast.children[2].kind.should.be.exactly('cast');
ast.children[2].type.should.be.exactly('string');
ast.children[3].kind.should.be.exactly('cast');
ast.children[3].type.should.be.exactly('array');
ast.children[4].kind.should.be.exactly('cast');
ast.children[4].type.should.be.exactly('object');
ast.children[5].kind.should.be.exactly('cast');
ast.children[5].type.should.be.exactly('boolean');
ast.children[6].kind.should.be.exactly('unset');
ast.children[2].type.should.be.exactly('object');
ast.children[3].kind.should.be.exactly('unset');
});

it('test exit', function() {
Expand Down