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(); const 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; } } 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/Li9Kv
function name:  (null)
number of ops:  8
compiled vars:  !0 = $c, !1 = $bf
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  109     0  E >   NEW                                              $2      'Compiler'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
  110     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.'
  111     4        INIT_METHOD_CALL                                         !0, 'execute'
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
  113     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/Li9Kv
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
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <true>
   15     2        ASSIGN_OBJ                                               'code'
          3        OP_DATA                                                  !0
   16     4        ASSIGN                                                   !2, 0
          5      > JMP                                                      ->22
   17     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
   18    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
   19    20    > > JMP                                                      ->21
   16    21    >   PRE_INC                                                  !2
         22    >   FETCH_OBJ_R                                      ~13     'code'
         23        STRLEN                                           ~14     ~13
         24        IS_SMALLER                                               !2, ~14
         25      > JMPNZ                                                    ~15, ->6
   22    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/Li9Kv
function name:  execute
number of ops:  76
compiled vars:  !0 = $code, !1 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        ASSIGN_OBJ                                               'arr'
          2        OP_DATA                                                  <array>
   26     3        ASSIGN_OBJ                                               'ptr'
          4        OP_DATA                                                  0
   27     5        ASSIGN_OBJ                                               'code'
          6        OP_DATA                                                  !0
   29     7        ASSIGN_OBJ                                               'i'
          8        OP_DATA                                                  0
          9      > JMP                                                      ->70
   30    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
   31    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
   32    27    > > JMP                                                      ->69
         28*       JMP                                                      ->69
   34    29    >   IS_EQUAL                                                 !1, '%3E'
         30      > JMPZ                                                     ~14, ->34
   35    31    >   INIT_METHOD_CALL                                         'increment_pointer'
         32        DO_FCALL                                      0          
         33      > JMP                                                      ->69
   37    34    >   IS_EQUAL                                                 !1, '%3C'
         35      > JMPZ                                                     ~16, ->39
   38    36    >   INIT_METHOD_CALL                                         'decrement_pointer'
         37        DO_FCALL                                      0          
         38      > JMP                                                      ->69
   40    39    >   IS_EQUAL                                                 !1, '%2B'
         40      > JMPZ                                                     ~18, ->44
   41    41    >   INIT_METHOD_CALL                                         'increment_val'
         42        DO_FCALL                                      0          
         43      > JMP                                                      ->69
   43    44    >   IS_EQUAL                                                 !1, '-'
         45      > JMPZ                                                     ~20, ->49
   44    46    >   INIT_METHOD_CALL                                         'decrement_val'
         47        DO_FCALL                                      0          
         48      > JMP                                                      ->69
   46    49    >   IS_EQUAL                                                 !1, '.'
         50      > JMPZ                                                     ~22, ->54
   47    51    >   INIT_METHOD_CALL                                         'output_val'
         52        DO_FCALL                                      0          
         53      > JMP                                                      ->69
   49    54    >   IS_EQUAL                                                 !1, '%2C'
         55      > JMPZ                                                     ~24, ->62
   50    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
   52    62    >   IS_EQUAL                                                 !1, '%5B'
         63      > JMPZ                                                     ~27, ->69
   53    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          
   29    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
   56    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/Li9Kv
function name:  stack
number of ops:  31
compiled vars:  !0 = $func, !1 = $info
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   59     1        TYPE_CHECK                                    2          !0
          2      > JMPZ                                                     ~2, ->18
   60     3    >   FETCH_OBJ_R                                      ~3      'call_stack'
          4        COUNT                                            ~4      ~3
          5        IS_EQUAL                                                 ~4, 0
          6      > JMPZ                                                     ~5, ->12
   61     7    >   NEW                                              $6      'InvalidStateException'
          8        SEND_VAL_EX                                              'Trying+to+pop+empty+call+stack.'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $6
   62    11*       RETURN                                                   <false>
   64    12    >   INIT_FCALL                                               'array_pop'
         13        FETCH_OBJ_W                                      $8      'call_stack'
         14        SEND_REF                                                 $8
         15        DO_ICALL                                                 
   65    16      > RETURN                                                   <true>
         17*       JMP                                                      ->30
   68    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
   69    24        INIT_FCALL                                               'array_push'
         25        FETCH_OBJ_W                                      $14     'call_stack'
         26        SEND_REF                                                 $14
         27        SEND_VAR                                                 !1
         28        DO_ICALL                                                 
   70    29      > RETURN                                                   <true>
   72    30*     > RETURN                                                   null

End of function stack

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/Li9Kv
function name:  increment_pointer
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   INIT_METHOD_CALL                                         'stack'
          1        SEND_VAL                                                 'increment_pointer'
          2        DO_FCALL                                      0          
   76     3        PRE_INC_OBJ                                              'ptr'
   77     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
   78    13    >   INIT_METHOD_CALL                                         'stack'
         14        DO_FCALL                                      0          
   79    15      > RETURN                                                   <true>
   80    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/Li9Kv
function name:  decrement_pointer
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   INIT_METHOD_CALL                                         'stack'
          1        SEND_VAL                                                 'decrement_pointer'
          2        DO_FCALL                                      0          
   84     3        PRE_DEC_OBJ                                              'ptr'
   85     4        FETCH_OBJ_R                                      ~2      'ptr'
          5        IS_SMALLER                                               ~2, 0
          6      > JMPZ                                                     ~3, ->12
   86     7    >   NEW                                              $4      'IllegalStateException'
          8        SEND_VAL_EX                                              'The+data+pointer+is+less+than+0.'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $4
   87    11*       RETURN                                                   <false>
   89    12    >   INIT_METHOD_CALL                                         'stack'
         13        DO_FCALL                                      0          
   90    14      > RETURN                                                   <true>
   91    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/Li9Kv
function name:  increment_val
number of ops:  28
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     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
   95     5        INIT_METHOD_CALL                                         'minimum_cellsize'
          6        DO_FCALL                                      0  $6      
          7        IS_SMALLER                                               !0, $6
          8      > JMPZ                                                     ~7, ->16
   96     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
   98    16    >   INIT_METHOD_CALL                                         'maximum_cellsize'
         17        DO_FCALL                                      0  $12     
         18        IS_SMALLER                                               $12, !0
         19      > JMPZ                                                     ~13, ->26
   99    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
  101    26    > > RETURN                                                   <true>
  102    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/Li9Kv
function name:  getCallStack
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  105     0  E >   FETCH_OBJ_R                                      ~0      'call_stack'
          1      > RETURN                                                   ~0
  106     2*     > RETURN                                                   null

End of function getcallstack

End of class Compiler.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.4 ms | 1416 KiB | 21 Q