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 (true) { $val instanceof Nil => 0, $val instanceof Leaf => 1, $val instanceof Node => 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/0OiTK
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
   25    26        DO_FCALL                                      0          
   27    27        SEND_VAR_NO_REF_EX                                       $10
   25    28        DO_FCALL                                      0          
   27    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
   24    36        DO_FCALL                                      0          
   29    37        SEND_VAR_NO_REF_EX                                       $8
   24    38        DO_FCALL                                      0          
   29    39        SEND_VAR_NO_REF_EX                                       $7
   22    40        DO_FCALL                                      0          
   29    41        SEND_VAR_NO_REF_EX                                       $2
   22    42        DO_FCALL                                      0          
         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
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 14
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 197) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
filename:       /in/0OiTK
function name:  depth
number of ops:  39
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
   16     3        INSTANCEOF                                       ~5      !1, 'Nil'
          4        IS_IDENTICAL                                             ~5, <true>
          5      > JMPNZ                                                    ~4, ->14
   17     6    >   INSTANCEOF                                       ~6      !1, 'Leaf'
          7        IS_IDENTICAL                                             ~6, <true>
          8      > JMPNZ                                                    ~4, ->16
   18     9    >   INSTANCEOF                                       ~7      !1, 'Node'
         10        IS_IDENTICAL                                             ~7, <true>
         11      > JMPNZ                                                    ~4, ->18
         12    > > JMP                                                      ->13
         13    > > MATCH_ERROR                                              <true>
   16    14    >   QM_ASSIGN                                        ~8      0
         15      > JMP                                                      ->35
   17    16    >   QM_ASSIGN                                        ~8      1
         17      > JMP                                                      ->35
   18    18    >   INIT_FCALL                                               'max'
         19        INIT_FCALL_BY_NAME                                       'depth'
         20        CHECK_FUNC_ARG                                           
         21        FETCH_OBJ_FUNC_ARG                               $9      !1, 'left'
         22        SEND_FUNC_ARG                                            $9
         23        DO_FCALL                                      0  $10     
         24        SEND_VAR                                                 $10
         25        INIT_FCALL_BY_NAME                                       'depth'
         26        CHECK_FUNC_ARG                                           
         27        FETCH_OBJ_FUNC_ARG                               $11     !1, 'right'
         28        SEND_FUNC_ARG                                            $11
         29        DO_FCALL                                      0  $12     
         30        SEND_VAR                                                 $12
         31        DO_ICALL                                         $13     
         32        ADD                                              ~14     1, $13
         33        QM_ASSIGN                                        ~8      ~14
         34      > JMP                                                      ->35
         35    >   VERIFY_RETURN_TYPE                                       ~8
         36      > RETURN                                                   ~8
   20    37*       VERIFY_RETURN_TYPE                                       
         38*     > 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/0OiTK
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/0OiTK
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/0OiTK
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:
137.22 ms | 1011 KiB | 15 Q