3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ExpressionCalculator { private $operators = array(); public function __construct() { $operators['+'] = function ($leftOperand, $rightOperand) { return $leftOperand + $rightOperand; }; $operators['-'] = function ($leftOperand, $rightOperand) { return $leftOperand - $rightOperand; }; $operators['*'] = function ($leftOperand, $rightOperand) { return $leftOperand * $rightOperand; }; $operators['/'] = function ($leftOperand, $rightOperand) { return $leftOperand / $rightOperand; }; } public function calc($expression) { $operands = array(); while (($token = strtok($expression, ' ')) !== false) { if ($this->isOperator($token)) { $rightOperand = array_pop($operands); $leftOperand = array_pop($operands); $result = $this->execute($token, $leftOperand, $rightOperand); array_push($operands, $result); } else if (is_numeric($token)) { array_push($operands, $token); } else { throw new InvalidArgumentException('Expression has invalid token: ' + $token); } } return $result; } private function isOperator($token) { return isset($operators[$token]); } private function execute($operator, $leftOperand, $rightOperand) { return call_user_func($operators[$operator], $leftOperand, $rightOperand); } } $calc = new ExpressionCalculator(); echo $calc->calc('5 1 2 + 4 * + 3 -'); // 14
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  (null)
number of ops:  8
compiled vars:  !0 = $calc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   NEW                                              $1      'ExpressionCalculator'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   50     3        INIT_METHOD_CALL                                         !0, 'calc'
          4        SEND_VAL_EX                                              '5+1+2+%2B+4+%2A+%2B+3+-'
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FcAHiS%3A7%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        ADD                                              ~2      !0, !1
          3      > RETURN                                                   ~2
    9     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcAHiS%3A7%240

Function %00%7Bclosure%7D%2Fin%2FcAHiS%3A10%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        SUB                                              ~2      !0, !1
          3      > RETURN                                                   ~2
   12     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcAHiS%3A10%241

Function %00%7Bclosure%7D%2Fin%2FcAHiS%3A13%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        MUL                                              ~2      !0, !1
          3      > RETURN                                                   ~2
   15     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcAHiS%3A13%242

Function %00%7Bclosure%7D%2Fin%2FcAHiS%3A16%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   17     2        DIV                                              ~2      !0, !1
          3      > RETURN                                                   ~2
   18     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcAHiS%3A16%243

Class ExpressionCalculator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  __construct
number of ops:  13
compiled vars:  !0 = $operators
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcAHiS%3A7%240'
          1        ASSIGN_DIM                                               !0, '%2B'
    9     2        OP_DATA                                                  ~2
   10     3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcAHiS%3A10%241'
          4        ASSIGN_DIM                                               !0, '-'
   12     5        OP_DATA                                                  ~4
   13     6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcAHiS%3A13%242'
          7        ASSIGN_DIM                                               !0, '%2A'
   15     8        OP_DATA                                                  ~6
   16     9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcAHiS%3A16%243'
         10        ASSIGN_DIM                                               !0, '%2F'
   18    11        OP_DATA                                                  ~8
   19    12      > RETURN                                                   null

End of function __construct

Function calc:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 3
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 26
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 35
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 35
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/cAHiS
function name:  calc
number of ops:  49
compiled vars:  !0 = $expression, !1 = $operands, !2 = $token, !3 = $rightOperand, !4 = $leftOperand, !5 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        ASSIGN                                                   !1, <array>
   24     2      > JMP                                                      ->40
   25     3    >   INIT_METHOD_CALL                                         'isOperator'
          4        SEND_VAR_EX                                              !2
          5        DO_FCALL                                      0  $7      
          6      > JMPZ                                                     $7, ->26
   26     7    >   INIT_FCALL                                               'array_pop'
          8        SEND_REF                                                 !1
          9        DO_ICALL                                         $8      
         10        ASSIGN                                                   !3, $8
   27    11        INIT_FCALL                                               'array_pop'
         12        SEND_REF                                                 !1
         13        DO_ICALL                                         $10     
         14        ASSIGN                                                   !4, $10
   28    15        INIT_METHOD_CALL                                         'execute'
         16        SEND_VAR_EX                                              !2
         17        SEND_VAR_EX                                              !4
         18        SEND_VAR_EX                                              !3
         19        DO_FCALL                                      0  $12     
         20        ASSIGN                                                   !5, $12
   29    21        INIT_FCALL                                               'array_push'
         22        SEND_REF                                                 !1
         23        SEND_VAR                                                 !5
         24        DO_ICALL                                                 
         25      > JMP                                                      ->40
   30    26    >   INIT_FCALL                                               'is_numeric'
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                         $15     
         29      > JMPZ                                                     $15, ->35
   31    30    >   INIT_FCALL                                               'array_push'
         31        SEND_REF                                                 !1
         32        SEND_VAR                                                 !2
         33        DO_ICALL                                                 
         34      > JMP                                                      ->40
   33    35    >   NEW                                              $17     'InvalidArgumentException'
         36        ADD                                              ~18     'Expression+has+invalid+token%3A+', !2
         37        SEND_VAL_EX                                              ~18
         38        DO_FCALL                                      0          
         39      > THROW                                         0          $17
   24    40    >   INIT_FCALL                                               'strtok'
         41        SEND_VAR                                                 !0
         42        SEND_VAL                                                 '+'
         43        DO_ICALL                                         $20     
         44        ASSIGN                                           ~21     !2, $20
         45        TYPE_CHECK                                  1018          ~21
         46      > JMPNZ                                                    ~22, ->3
   37    47    > > RETURN                                                   !5
   38    48*     > RETURN                                                   null

End of function calc

Function isoperator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  isOperator
number of ops:  4
compiled vars:  !0 = $token, !1 = $operators
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      !1, !0
          2      > RETURN                                                   ~2
   42     3*     > RETURN                                                   null

End of function isoperator

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cAHiS
function name:  execute
number of ops:  10
compiled vars:  !0 = $operator, !1 = $leftOperand, !2 = $rightOperand, !3 = $operators
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   45     3        FETCH_DIM_R                                      ~4      !3, !0
          4        INIT_USER_CALL                                2          'call_user_func', ~4
          5        SEND_USER                                                !1
          6        SEND_USER                                                !2
          7        DO_FCALL                                      0  $5      
          8      > RETURN                                                   $5
   46     9*     > RETURN                                                   null

End of function execute

End of class ExpressionCalculator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.74 ms | 1408 KiB | 21 Q