Skip to content

Instantly share code, notes, and snippets.

@mattsches
Created December 13, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsches/4d3e2cd2d766ab42071f to your computer and use it in GitHub Desktop.
Save mattsches/4d3e2cd2d766ab42071f to your computer and use it in GitHub Desktop.
Create a simple, human-readable AST dump using the `php-ast` extension and its `util.php` helper file. Requires PHP 7.
<?php
require 'path/to/php-ast/util.php';
$code = <<<'EOC'
<?php
$random = random_int(0,1);
if ($random === 1) {
echo 'EINS';
} else {
echo 'NICHT EINS';
}
EOC;
echo ast_dump(ast\parse_code($code, 30)), "\n";
// Output:
AST_STMT_LIST
0: AST_ASSIGN
var: AST_VAR
name: "random"
expr: AST_CALL
expr: AST_NAME
flags: NAME_NOT_FQ (1)
name: "random_int"
args: AST_ARG_LIST
0: 0
1: 1
1: AST_IF
0: AST_IF_ELEM
cond: AST_BINARY_OP
flags: BINARY_IS_IDENTICAL (15)
left: AST_VAR
name: "random"
right: 1
stmts: AST_STMT_LIST
0: AST_ECHO
expr: "EINS"
1: AST_IF_ELEM
cond: null
stmts: AST_STMT_LIST
0: AST_ECHO
expr: "NICHT EINS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment