3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Nil {} class Leaf { public function __construct(public int $value) {} } class Node { public function __construct(public Tree $left, public Tree $right) {} } class Tree { public function __construct(public Nil|Leaf|Node $val) {} } function depth(Tree $tree): int { $val = $tree->val; return match ($val::class) { Nil::class => 0, Leaf::class => 1, Node::class => 1 + max(depth($val->left), depth($val->right)), }; } $treeExample = new Tree(new Node( new Tree(new Leaf(5)), new Tree(new Node( new Tree(new Node( new Tree(new Nil()), new Tree(new Leaf(2)), )), new Tree(new Nil()), )) )); print depth($treeExample);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KPDAY
function name:  (null)
number of ops:  49
compiled vars:  !0 = $treeExample
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   NEW                                              $1      'Tree'
          1        NEW                                              $2      'Node'
   23     2        NEW                                              $3      'Tree'
          3        NEW                                              $4      'Leaf'
          4        SEND_VAL_EX                                              5
          5        DO_FCALL                                      0          
          6        SEND_VAR_NO_REF_EX                                       $4
          7        DO_FCALL                                      0          
          8        SEND_VAR_NO_REF_EX                                       $3
   24     9        NEW                                              $7      'Tree'
         10        NEW                                              $8      'Node'
   25    11        NEW                                              $9      'Tree'
         12        NEW                                              $10     'Node'
   26    13        NEW                                              $11     'Tree'
         14        NEW                                              $12     'Nil'
         15        DO_FCALL                                      0          
         16        SEND_VAR_NO_REF_EX                                       $12
         17        DO_FCALL                                      0          
         18        SEND_VAR_NO_REF_EX                                       $11
   27    19        NEW                                              $15     'Tree'
         20        NEW                                              $16     'Leaf'
         21        SEND_VAL_EX                                              2
         22        DO_FCALL                                      0          
         23        SEND_VAR_NO_REF_EX                                       $16
         24        DO_FCALL                                      0          
         25        SEND_VAR_NO_REF_EX                                       $15
         26        DO_FCALL                                      0          
         27        SEND_VAR_NO_REF_EX                                       $10
         28        DO_FCALL                                      0          
         29        SEND_VAR_NO_REF_EX                                       $9
   29    30        NEW                                              $21     'Tree'
         31        NEW                                              $22     'Nil'
         32        DO_FCALL                                      0          
         33        SEND_VAR_NO_REF_EX                                       $22
         34        DO_FCALL                                      0          
         35        SEND_VAR_NO_REF_EX                                       $21
         36        DO_FCALL                                      0          
         37        SEND_VAR_NO_REF_EX                                       $8
         38        DO_FCALL                                      0          
         39        SEND_VAR_NO_REF_EX                                       $7
         40        DO_FCALL                                      0          
         41        SEND_VAR_NO_REF_EX                                       $2
         42        DO_FCALL                                      0          
   22    43        ASSIGN                                                   !0, $1
   33    44        INIT_FCALL                                               'depth'
         45        SEND_VAR                                                 !0
         46        DO_FCALL                                      0  $30     
         47        ECHO                                                     $30
         48      > RETURN                                                   1

Function depth:
Finding entry points
Branch analysis from position: 0
4 jumps found. (Code = 195) Position 1 = 6, Position 2 = 8, Position 3 = 10, Position 4 = 5
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 5
1 jumps found. (Code = 197) Position 1 = -2
filename:       /in/KPDAY
function name:  depth
number of ops:  32
compiled vars:  !0 = $tree, !1 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        FETCH_OBJ_R                                      ~2      !0, 'val'
          2        ASSIGN                                                   !1, ~2
   15     3        FETCH_CLASS_NAME                                 ~4      !1
          4      > MATCH                                                    ~4, [ 'Nil':->6, 'Leaf':->8, 'Node':->10, ]
          5    > > MATCH_ERROR                                              ~4
   16     6    >   QM_ASSIGN                                        ~6      0
          7      > JMP                                                      ->27
   17     8    >   QM_ASSIGN                                        ~6      1
          9      > JMP                                                      ->27
   18    10    >   INIT_FCALL                                               'max'
         11        INIT_FCALL_BY_NAME                                       'depth'
         12        CHECK_FUNC_ARG                                           
         13        FETCH_OBJ_FUNC_ARG                               $7      !1, 'left'
         14        SEND_FUNC_ARG                                            $7
         15        DO_FCALL                                      0  $8      
         16        SEND_VAR                                                 $8
         17        INIT_FCALL_BY_NAME                                       'depth'
         18        CHECK_FUNC_ARG                                           
         19        FETCH_OBJ_FUNC_ARG                               $9      !1, 'right'
         20        SEND_FUNC_ARG                                            $9
         21        DO_FCALL                                      0  $10     
         22        SEND_VAR                                                 $10
         23        DO_ICALL                                         $11     
         24        ADD                                              ~12     1, $11
         25        QM_ASSIGN                                        ~6      ~12
         26      > JMP                                                      ->27
         27    >   FREE                                                     ~4
         28        VERIFY_RETURN_TYPE                                       ~6
         29      > RETURN                                                   ~6
   20    30*       VERIFY_RETURN_TYPE                                       
         31*     > RETURN                                                   null

End of function depth

Class Nil: [no user functions]
Class Leaf:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KPDAY
function name:  __construct
number of ops:  4
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'value'
          2        OP_DATA                                                  !0
          3      > RETURN                                                   null

End of function __construct

End of class Leaf.

Class Node:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KPDAY
function name:  __construct
number of ops:  7
compiled vars:  !0 = $left, !1 = $right
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        ASSIGN_OBJ                                               'left'
          3        OP_DATA                                                  !0
          4        ASSIGN_OBJ                                               'right'
          5        OP_DATA                                                  !1
          6      > RETURN                                                   null

End of function __construct

End of class Node.

Class Tree:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KPDAY
function name:  __construct
number of ops:  4
compiled vars:  !0 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'val'
          2        OP_DATA                                                  !0
          3      > RETURN                                                   null

End of function __construct

End of class Tree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.89 ms | 1411 KiB | 16 Q