3v4l.org

run code in 300+ PHP versions simultaneously
<?PHP class InvalidStateException extends RuntimeException { } class Compiler { private $ptr; private $arr; private $code; private $commands = array('>', '<', '+', '-', '.', ',', '[', ']'); private $call_stack = array(); private $cellsize = 8; public function parseString($code, $throwException = true) { $this->code = $code; for ($i=0;$i<strlen($this->code);$i++) { $c = substr($this->code, $i, 1); if (!in_array($c, $this->commands)) { continue; } } } public function execute($code) { $this->arr = array(0 => 0); $this->ptr = 0; $this->code = $code; for ($this->i=0;$this->i<strlen($this->code);$this->i++) { $c = $this->c = substr($this->code, $this->i, 1); if (!in_array($c, $this->commands)) { continue; } elseif ($c == '>') { $this->increment_pointer(); } elseif ($c == '<') { $this->decrement_pointer(); } elseif ($c == '+') { $this->increment_val(); } elseif ($c == '-') { $this->decrement_val(); } elseif ($c == '.') { $this->output_val(); } elseif ($c == ',') { $this->store_val($this->input_val()); } elseif ($c == '[') { $this->loop($this->i); } } } private function stack($func) { if (is_null($func)) { if (count($this->call_stack) == 0) { throw new InvalidStateException("Trying to pop empty call stack."); return false; } array_pop($this->call_stack); return true; } else { $info = array('command' => $this->c, 'pos' => $this->i, 'internal_function' => $func); array_push($this->call_stack, $info); return true; } } public function minimum_cellsize() { return (-1*(pow(2, $this->cellsize-1))); } public function maximum_cellsize() { return pow(2, $this->cellsize-1)-1; } private function increment_pointer() { $this->stack(__FUNCTION__); $this->ptr++; if (!isset($this->arr[$this->ptr])) $this->arr[$this->ptr] = 0; $this->stack(); return true; } private function decrement_pointer() { $this->stack(__FUNCTION__); $this->ptr--; if ($this->ptr < 0) { throw new IllegalStateException("The data pointer is less than 0."); return false; } $this->stack(); return true; } private function increment_val() { $x = $this->arr[$this->ptr]++; if ($x < $this->minimum_cellsize()) { $this->arr[$this->ptr] = $this->maximum_cellsize(); } elseif ($x > $this->maximum_cellsize()) { $this->arr[$this->ptr] = $this->minimum_cellsize(); } return true; } public function getCallStack() { return $this->call_stack; } } $c = new Compiler(); $bf = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.'; $c->execute($bf); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H7OrI
function name:  (null)
number of ops:  8
compiled vars:  !0 = $c, !1 = $bf
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  116     0  E >   NEW                                              $2      'Compiler'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
  117     3        ASSIGN                                                   !1, '%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%5B%3E%2B%2B%2B%2B%2B%2B%2B%3E%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%3E%2B%2B%2B%3E%2B%3C%3C%3C%3C-%5D%3E%2B%2B.%3E%2B.%2B%2B%2B%2B%2B%2B%2B..%2B%2B%2B.%3E%2B%2B.%3C%3C%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B.%3E.%2B%2B%2B.------.--------.%3E%2B.%3E.'
  118     4        INIT_METHOD_CALL                                         !0, 'execute'
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
  120     7      > RETURN                                                   1

Class InvalidStateException: [no user functions]
Class Compiler:
Function parsestring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 6
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 6
Branch analysis from position: 26
Branch analysis from position: 6
Branch analysis from position: 21
filename:       /in/H7OrI
function name:  parseString
number of ops:  27
compiled vars:  !0 = $code, !1 = $throwException, !2 = $i, !3 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <true>
   14     2        ASSIGN_OBJ                                               'code'
          3        OP_DATA                                                  !0
   15     4        ASSIGN                                                   !2, 0
          5      > JMP                                                      ->22
   16     6    >   INIT_FCALL                                               'substr'
          7        FETCH_OBJ_R                                      ~6      'code'
          8        SEND_VAL                                                 ~6
          9        SEND_VAR                                                 !2
         10        SEND_VAL                                                 1
         11        DO_ICALL                                         $7      
         12        ASSIGN                                                   !3, $7
   17    13        INIT_FCALL                                               'in_array'
         14        SEND_VAR                                                 !3
         15        FETCH_OBJ_R                                      ~9      'commands'
         16        SEND_VAL                                                 ~9
         17        DO_ICALL                                         $10     
         18        BOOL_NOT                                         ~11     $10
         19      > JMPZ                                                     ~11, ->21
   18    20    > > JMP                                                      ->21
   15    21    >   PRE_INC                                                  !2
         22    >   FETCH_OBJ_R                                      ~13     'code'
         23        STRLEN                                           ~14     ~13
         24        IS_SMALLER                                               !2, ~14
         25      > JMPNZ                                                    ~15, ->6
   21    26    > > RETURN                                                   null

End of function parsestring

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 10
Branch analysis from position: 75
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 29
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 10
Branch analysis from position: 75
Branch analysis from position: 10
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 39
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 44
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 49
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 54
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 62
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 69
Branch analysis from position: 64
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 10
Branch analysis from position: 75
Branch analysis from position: 10
Branch analysis from position: 69
filename:       /in/H7OrI
function name:  execute
number of ops:  76
compiled vars:  !0 = $code, !1 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   24     1        ASSIGN_OBJ                                               'arr'
          2        OP_DATA                                                  <array>
   25     3        ASSIGN_OBJ                                               'ptr'
          4        OP_DATA                                                  0
   26     5        ASSIGN_OBJ                                               'code'
          6        OP_DATA                                                  !0
   28     7        ASSIGN_OBJ                                               'i'
          8        OP_DATA                                                  0
          9      > JMP                                                      ->70
   29    10    >   INIT_FCALL                                               'substr'
         11        FETCH_OBJ_R                                      ~7      'code'
         12        SEND_VAL                                                 ~7
         13        FETCH_OBJ_R                                      ~8      'i'
         14        SEND_VAL                                                 ~8
         15        SEND_VAL                                                 1
         16        DO_ICALL                                         $9      
         17        ASSIGN_OBJ                                       ~6      'c'
         18        OP_DATA                                                  $9
         19        ASSIGN                                                   !1, ~6
   30    20        INIT_FCALL                                               'in_array'
         21        SEND_VAR                                                 !1
         22        FETCH_OBJ_R                                      ~11     'commands'
         23        SEND_VAL                                                 ~11
         24        DO_ICALL                                         $12     
         25        BOOL_NOT                                         ~13     $12
         26      > JMPZ                                                     ~13, ->29
   31    27    > > JMP                                                      ->69
         28*       JMP                                                      ->69
   33    29    >   IS_EQUAL                                                 !1, '%3E'
         30      > JMPZ                                                     ~14, ->34
   34    31    >   INIT_METHOD_CALL                                         'increment_pointer'
         32        DO_FCALL                                      0          
         33      > JMP                                                      ->69
   36    34    >   IS_EQUAL                                                 !1, '%3C'
         35      > JMPZ                                                     ~16, ->39
   37    36    >   INIT_METHOD_CALL                                         'decrement_pointer'
         37        DO_FCALL                                      0          
         38      > JMP                                                      ->69
   39    39    >   IS_EQUAL                                                 !1, '%2B'
         40      > JMPZ                                                     ~18, ->44
   40    41    >   INIT_METHOD_CALL                                         'increment_val'
         42        DO_FCALL                                      0          
         43      > JMP                                                      ->69
   42    44    >   IS_EQUAL                                                 !1, '-'
         45      > JMPZ                                                     ~20, ->49
   43    46    >   INIT_METHOD_CALL                                         'decrement_val'
         47        DO_FCALL                                      0          
         48      > JMP                                                      ->69
   45    49    >   IS_EQUAL                                                 !1, '.'
         50      > JMPZ                                                     ~22, ->54
   46    51    >   INIT_METHOD_CALL                                         'output_val'
         52        DO_FCALL                                      0          
         53      > JMP                                                      ->69
   48    54    >   IS_EQUAL                                                 !1, '%2C'
         55      > JMPZ                                                     ~24, ->62
   49    56    >   INIT_METHOD_CALL                                         'store_val'
         57        INIT_METHOD_CALL                                         'input_val'
         58        DO_FCALL                                      0  $25     
         59        SEND_VAR_NO_REF_EX                                       $25
         60        DO_FCALL                                      0          
         61      > JMP                                                      ->69
   51    62    >   IS_EQUAL                                                 !1, '%5B'
         63      > JMPZ                                                     ~27, ->69
   52    64    >   INIT_METHOD_CALL                                         'loop'
         65        CHECK_FUNC_ARG                                           
         66        FETCH_OBJ_FUNC_ARG                               $28     'i'
         67        SEND_FUNC_ARG                                            $28
         68        DO_FCALL                                      0          
   28    69    >   PRE_INC_OBJ                                              'i'
         70    >   FETCH_OBJ_R                                      ~31     'i'
         71        FETCH_OBJ_R                                      ~32     'code'
         72        STRLEN                                           ~33     ~32
         73        IS_SMALLER                                               ~31, ~33
         74      > JMPNZ                                                    ~34, ->10
   55    75    > > RETURN                                                   null

End of function execute

Function stack:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 18
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H7OrI
function name:  stack
number of ops:  31
compiled vars:  !0 = $func, !1 = $info
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   58     1        TYPE_CHECK                                    2          !0
          2      > JMPZ                                                     ~2, ->18
   59     3    >   FETCH_OBJ_R                                      ~3      'call_stack'
          4        COUNT                                            ~4      ~3
          5        IS_EQUAL                                                 ~4, 0
          6      > JMPZ                                                     ~5, ->12
   60     7    >   NEW                                              $6      'InvalidStateException'
          8        SEND_VAL_EX                                              'Trying+to+pop+empty+call+stack.'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $6
   61    11*       RETURN                                                   <false>
   63    12    >   INIT_FCALL                                               'array_pop'
         13        FETCH_OBJ_W                                      $8      'call_stack'
         14        SEND_REF                                                 $8
         15        DO_ICALL                                                 
   64    16      > RETURN                                                   <true>
         17*       JMP                                                      ->30
   67    18    >   FETCH_OBJ_R                                      ~10     'c'
         19        INIT_ARRAY                                       ~11     ~10, 'command'
         20        FETCH_OBJ_R                                      ~12     'i'
         21        ADD_ARRAY_ELEMENT                                ~11     ~12, 'pos'
         22        ADD_ARRAY_ELEMENT                                ~11     !0, 'internal_function'
         23        ASSIGN                                                   !1, ~11
   68    24        INIT_FCALL                                               'array_push'
         25        FETCH_OBJ_W                                      $14     'call_stack'
         26        SEND_REF                                                 $14
         27        SEND_VAR                                                 !1
         28        DO_ICALL                                                 
   69    29      > RETURN                                                   <true>
   71    30*     > RETURN                                                   null

End of function stack

Function minimum_cellsize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H7OrI
function name:  minimum_cellsize
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   INIT_FCALL                                               'pow'
          1        SEND_VAL                                                 2
          2        FETCH_OBJ_R                                      ~0      'cellsize'
          3        SUB                                              ~1      ~0, 1
          4        SEND_VAL                                                 ~1
          5        DO_ICALL                                         $2      
          6        MUL                                              ~3      $2, -1
          7      > RETURN                                                   ~3
   75     8*     > RETURN                                                   null

End of function minimum_cellsize

Function maximum_cellsize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H7OrI
function name:  maximum_cellsize
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   INIT_FCALL                                               'pow'
          1        SEND_VAL                                                 2
          2        FETCH_OBJ_R                                      ~0      'cellsize'
          3        SUB                                              ~1      ~0, 1
          4        SEND_VAL                                                 ~1
          5        DO_ICALL                                         $2      
          6        SUB                                              ~3      $2, 1
          7      > RETURN                                                   ~3
   79     8*     > RETURN                                                   null

End of function maximum_cellsize

Function increment_pointer:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/H7OrI
function name:  increment_pointer
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   INIT_METHOD_CALL                                         'stack'
          1        SEND_VAL                                                 'increment_pointer'
          2        DO_FCALL                                      0          
   83     3        PRE_INC_OBJ                                              'ptr'
   84     4        FETCH_OBJ_R                                      ~3      'ptr'
          5        FETCH_OBJ_IS                                     ~2      'arr'
          6        ISSET_ISEMPTY_DIM_OBJ                         0  ~4      ~2, ~3
          7        BOOL_NOT                                         ~5      ~4
          8      > JMPZ                                                     ~5, ->13
          9    >   FETCH_OBJ_R                                      ~7      'ptr'
         10        FETCH_OBJ_W                                      $6      'arr'
         11        ASSIGN_DIM                                               $6, ~7
         12        OP_DATA                                                  0
   85    13    >   INIT_METHOD_CALL                                         'stack'
         14        DO_FCALL                                      0          
   86    15      > RETURN                                                   <true>
   87    16*     > RETURN                                                   null

End of function increment_pointer

Function decrement_pointer:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H7OrI
function name:  decrement_pointer
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   INIT_METHOD_CALL                                         'stack'
          1        SEND_VAL                                                 'decrement_pointer'
          2        DO_FCALL                                      0          
   91     3        PRE_DEC_OBJ                                              'ptr'
   92     4        FETCH_OBJ_R                                      ~2      'ptr'
          5        IS_SMALLER                                               ~2, 0
          6      > JMPZ                                                     ~3, ->12
   93     7    >   NEW                                              $4      'IllegalStateException'
          8        SEND_VAL_EX                                              'The+data+pointer+is+less+than+0.'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $4
   94    11*       RETURN                                                   <false>
   96    12    >   INIT_METHOD_CALL                                         'stack'
         13        DO_FCALL                                      0          
   97    14      > RETURN                                                   <true>
   98    15*     > RETURN                                                   null

End of function decrement_pointer

Function increment_val:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/H7OrI
function name:  increment_val
number of ops:  28
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   FETCH_OBJ_R                                      ~2      'ptr'
          1        FETCH_OBJ_RW                                     $1      'arr'
          2        FETCH_DIM_RW                                     $3      $1, ~2
          3        POST_INC                                         ~4      $3
          4        ASSIGN                                                   !0, ~4
  102     5        INIT_METHOD_CALL                                         'minimum_cellsize'
          6        DO_FCALL                                      0  $6      
          7        IS_SMALLER                                               !0, $6
          8      > JMPZ                                                     ~7, ->16
  103     9    >   FETCH_OBJ_R                                      ~9      'ptr'
         10        INIT_METHOD_CALL                                         'maximum_cellsize'
         11        DO_FCALL                                      0  $11     
         12        FETCH_OBJ_W                                      $8      'arr'
         13        ASSIGN_DIM                                               $8, ~9
         14        OP_DATA                                                  $11
         15      > JMP                                                      ->26
  105    16    >   INIT_METHOD_CALL                                         'maximum_cellsize'
         17        DO_FCALL                                      0  $12     
         18        IS_SMALLER                                               $12, !0
         19      > JMPZ                                                     ~13, ->26
  106    20    >   FETCH_OBJ_R                                      ~15     'ptr'
         21        INIT_METHOD_CALL                                         'minimum_cellsize'
         22        DO_FCALL                                      0  $17     
         23        FETCH_OBJ_W                                      $14     'arr'
         24        ASSIGN_DIM                                               $14, ~15
         25        OP_DATA                                                  $17
  108    26    > > RETURN                                                   <true>
  109    27*     > RETURN                                                   null

End of function increment_val

Function getcallstack:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/H7OrI
function name:  getCallStack
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  112     0  E >   FETCH_OBJ_R                                      ~0      'call_stack'
          1      > RETURN                                                   ~0
  113     2*     > RETURN                                                   null

End of function getcallstack

End of class Compiler.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.88 ms | 1412 KiB | 23 Q