3v4l.org

run code in 300+ PHP versions simultaneously
<?php function interpreter(string $code, int $iterations, int $width, int $height): string { $dataGrid = []; $output = ''; $codeOffset = -1; $skip = 0; $commands = $code; $pointer = ['x' => 0, 'y' => 0]; $jmps = new SplStack(); for ($row = 0; $row < $width; ++$row) { for ($col = 0; $col < $height; ++$col) { $dataGrid[$col][$row] = '0'; } } while (strlen($commands) && $iterations) { //if (!$skip) //echo "{$commands} ({$pointer['x']}, {$pointer['y']} = {$dataGrid[$pointer['x']][$pointer['y']]})\n"; ++$codeOffset; $command = $commands[0]; $commands = substr($commands, 1); if ($skip) { if ($command === ']') { $jmps->pop(); var_dump("pop"); --$skip; } elseif ($command === '[') { ++$skip; } continue; } //var_dump($command); switch ($command) { case 'n': if (--$pointer['x'] === -1) { $pointer['x'] = $height - 1; } break; case 'e': if (++$pointer['y'] === $width) { $pointer['y'] = 0; } break; case 's': if (++$pointer['x'] === $height) { $pointer['x'] = 0; } break; case 'w': if (--$pointer['y'] === -1) { $pointer['y'] = $width - 1; } break; case '*': if ($dataGrid[$pointer['x']][$pointer['y']] === '0') { echo "{$pointer['x']}, {$pointer['y']} = 1\n"; $dataGrid[$pointer['x']][$pointer['y']] = '1'; } else { echo "{$pointer['x']}, {$pointer['y']} = 0\n"; $dataGrid[$pointer['x']][$pointer['y']] = '0'; } break; case '[': $jmps->push($codeOffset); var_dump("push"); if ($dataGrid[$pointer['x']][$pointer['y']] === '0') { $skip = 1; } break; case ']': $jmpOffset = $jmps->top(); var_dump("pop"); if ($dataGrid[$pointer['x']][$pointer['y']] === '1') { $commands = substr($code, $jmpOffset + 1); $codeOffset = $jmpOffset; // ++$iterations; } break; default: continue 2; } --$iterations; } $rows = []; foreach ($dataGrid as $row) { $rows[] = implode('', $row); } return implode("\r\n", $rows); } var_dump(interpreter("*[es*]", 52, 5, 6)); /* 11000 01100 00110 00011 00001 10000*/ // var_dump(interpreter("*[s[e]*]", 9, 5, 5)); /*10000 10000 00000 00000 00000*/
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s5IdF
function name:  (null)
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'interpreter'
          2        SEND_VAL                                                 '%2A%5Bes%2A%5D'
          3        SEND_VAL                                                 52
          4        SEND_VAL                                                 5
          5        SEND_VAL                                                 6
          6        DO_FCALL                                      0  $0      
          7        SEND_VAR                                                 $0
          8        DO_ICALL                                                 
  114     9      > RETURN                                                   1

Function interpreter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 15
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 163
Branch analysis from position: 163
2 jumps found. (Code = 46) Position 1 = 165, Position 2 = 166
Branch analysis from position: 165
2 jumps found. (Code = 44) Position 1 = 167, Position 2 = 27
Branch analysis from position: 167
2 jumps found. (Code = 77) Position 1 = 169, Position 2 = 177
Branch analysis from position: 169
2 jumps found. (Code = 78) Position 1 = 170, Position 2 = 177
Branch analysis from position: 170
1 jumps found. (Code = 42) Position 1 = 169
Branch analysis from position: 169
Branch analysis from position: 177
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 177
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 49
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 45
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 163
Branch analysis from position: 163
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 48
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 163
Branch analysis from position: 163
Branch analysis from position: 48
Branch analysis from position: 49
9 jumps found. (Code = 188) Position 1 = 65, Position 2 = 73, Position 3 = 80, Position 4 = 87, Position 5 = 95, Position 6 = 127, Position 7 = 141, Position 8 = 161, Position 9 = 50
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 72
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
2 jumps found. (Code = 46) Position 1 = 165, Position 2 = 166
Branch analysis from position: 165
Branch analysis from position: 166
Branch analysis from position: 72
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 79
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
Branch analysis from position: 79
Branch analysis from position: 80
2 jumps found. (Code = 43) Position 1 = 84, Position 2 = 86
Branch analysis from position: 84
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
Branch analysis from position: 86
Branch analysis from position: 87
2 jumps found. (Code = 43) Position 1 = 91, Position 2 = 94
Branch analysis from position: 91
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
Branch analysis from position: 94
Branch analysis from position: 95
2 jumps found. (Code = 43) Position 1 = 101, Position 2 = 114
Branch analysis from position: 101
1 jumps found. (Code = 42) Position 1 = 126
Branch analysis from position: 126
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
Branch analysis from position: 114
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
Branch analysis from position: 127
2 jumps found. (Code = 43) Position 1 = 139, Position 2 = 140
Branch analysis from position: 139
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
Branch analysis from position: 140
Branch analysis from position: 141
2 jumps found. (Code = 43) Position 1 = 153, Position 2 = 160
Branch analysis from position: 153
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
Branch analysis from position: 160
Branch analysis from position: 161
1 jumps found. (Code = 42) Position 1 = 163
Branch analysis from position: 163
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 65
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 73
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 80
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 87
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 60, Position 2 = 95
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 127
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 141
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 161
Branch analysis from position: 161
Branch analysis from position: 141
Branch analysis from position: 127
Branch analysis from position: 95
Branch analysis from position: 87
Branch analysis from position: 80
Branch analysis from position: 73
Branch analysis from position: 65
Branch analysis from position: 166
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 17
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 15
Branch analysis from position: 26
Branch analysis from position: 15
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 17
Branch analysis from position: 23
Branch analysis from position: 17
filename:       /in/s5IdF
function name:  interpreter
number of ops:  186
compiled vars:  !0 = $code, !1 = $iterations, !2 = $width, !3 = $height, !4 = $dataGrid, !5 = $output, !6 = $codeOffset, !7 = $skip, !8 = $commands, !9 = $pointer, !10 = $jmps, !11 = $row, !12 = $col, !13 = $command, !14 = $jmpOffset, !15 = $rows
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
    4     4        ASSIGN                                                   !4, <array>
    5     5        ASSIGN                                                   !5, ''
    6     6        ASSIGN                                                   !6, -1
    7     7        ASSIGN                                                   !7, 0
    8     8        ASSIGN                                                   !8, !0
    9     9        ASSIGN                                                   !9, <array>
   10    10        NEW                                              $22     'SplStack'
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !10, $22
   12    13        ASSIGN                                                   !11, 0
         14      > JMP                                                      ->24
   13    15    >   ASSIGN                                                   !12, 0
         16      > JMP                                                      ->21
   14    17    >   FETCH_DIM_W                                      $27     !4, !12
         18        ASSIGN_DIM                                               $27, !11
         19        OP_DATA                                                  '0'
   13    20        PRE_INC                                                  !12
         21    >   IS_SMALLER                                               !12, !3
         22      > JMPNZ                                                    ~30, ->17
   12    23    >   PRE_INC                                                  !11
         24    >   IS_SMALLER                                               !11, !2
         25      > JMPNZ                                                    ~32, ->15
   18    26    > > JMP                                                      ->163
   21    27    >   PRE_INC                                                  !6
   22    28        FETCH_DIM_R                                      ~34     !8, 0
         29        ASSIGN                                                   !13, ~34
   23    30        INIT_FCALL                                               'substr'
         31        SEND_VAR                                                 !8
         32        SEND_VAL                                                 1
         33        DO_ICALL                                         $36     
         34        ASSIGN                                                   !8, $36
   25    35      > JMPZ                                                     !7, ->49
   26    36    >   IS_IDENTICAL                                             !13, '%5D'
         37      > JMPZ                                                     ~38, ->45
   27    38    >   INIT_METHOD_CALL                                         !10, 'pop'
         39        DO_FCALL                                      0          
   28    40        INIT_FCALL                                               'var_dump'
         41        SEND_VAL                                                 'pop'
         42        DO_ICALL                                                 
   29    43        PRE_DEC                                                  !7
         44      > JMP                                                      ->48
   30    45    >   IS_IDENTICAL                                             !13, '%5B'
         46      > JMPZ                                                     ~42, ->48
   31    47    >   PRE_INC                                                  !7
   33    48    > > JMP                                                      ->163
   37    49    > > SWITCH_STRING                                            !13, [ 'n':->65, 'e':->73, 's':->80, 'w':->87, '%2A':->95, '%5B':->127, '%5D':->141, ], ->161
   38    50    >   IS_EQUAL                                                 !13, 'n'
         51      > JMPNZ                                                    ~44, ->65
   43    52    >   IS_EQUAL                                                 !13, 'e'
         53      > JMPNZ                                                    ~44, ->73
   48    54    >   IS_EQUAL                                                 !13, 's'
         55      > JMPNZ                                                    ~44, ->80
   53    56    >   IS_EQUAL                                                 !13, 'w'
         57      > JMPNZ                                                    ~44, ->87
   58    58    >   IS_EQUAL                                                 !13, '%2A'
         59      > JMPNZ                                                    ~44, ->95
   67    60    >   IS_EQUAL                                                 !13, '%5B'
         61      > JMPNZ                                                    ~44, ->127
   75    62    >   IS_EQUAL                                                 !13, '%5D'
         63      > JMPNZ                                                    ~44, ->141
         64    > > JMP                                                      ->161
   39    65    >   FETCH_DIM_RW                                     $45     !9, 'x'
         66        PRE_DEC                                          ~46     $45
         67        IS_IDENTICAL                                             ~46, -1
         68      > JMPZ                                                     ~47, ->72
   40    69    >   SUB                                              ~49     !3, 1
         70        ASSIGN_DIM                                               !9, 'x'
         71        OP_DATA                                                  ~49
   42    72    > > JMP                                                      ->162
   44    73    >   FETCH_DIM_RW                                     $50     !9, 'y'
         74        PRE_INC                                          ~51     $50
         75        IS_IDENTICAL                                             !2, ~51
         76      > JMPZ                                                     ~52, ->79
   45    77    >   ASSIGN_DIM                                               !9, 'y'
         78        OP_DATA                                                  0
   47    79    > > JMP                                                      ->162
   49    80    >   FETCH_DIM_RW                                     $54     !9, 'x'
         81        PRE_INC                                          ~55     $54
         82        IS_IDENTICAL                                             !3, ~55
         83      > JMPZ                                                     ~56, ->86
   50    84    >   ASSIGN_DIM                                               !9, 'x'
         85        OP_DATA                                                  0
   52    86    > > JMP                                                      ->162
   54    87    >   FETCH_DIM_RW                                     $58     !9, 'y'
         88        PRE_DEC                                          ~59     $58
         89        IS_IDENTICAL                                             ~59, -1
         90      > JMPZ                                                     ~60, ->94
   55    91    >   SUB                                              ~62     !2, 1
         92        ASSIGN_DIM                                               !9, 'y'
         93        OP_DATA                                                  ~62
   57    94    > > JMP                                                      ->162
   59    95    >   FETCH_DIM_R                                      ~63     !9, 'x'
         96        FETCH_DIM_R                                      ~65     !9, 'y'
         97        FETCH_DIM_R                                      ~64     !4, ~63
         98        FETCH_DIM_R                                      ~66     ~64, ~65
         99        IS_IDENTICAL                                             ~66, '0'
        100      > JMPZ                                                     ~67, ->114
   60   101    >   FETCH_DIM_R                                      ~68     !9, 'x'
        102        ROPE_INIT                                     4  ~71     ~68
        103        ROPE_ADD                                      1  ~71     ~71, '%2C+'
        104        FETCH_DIM_R                                      ~69     !9, 'y'
        105        ROPE_ADD                                      2  ~71     ~71, ~69
        106        ROPE_END                                      3  ~70     ~71, '+%3D+1%0A'
        107        ECHO                                                     ~70
   61   108        FETCH_DIM_R                                      ~73     !9, 'x'
        109        FETCH_DIM_R                                      ~75     !9, 'y'
        110        FETCH_DIM_W                                      $74     !4, ~73
        111        ASSIGN_DIM                                               $74, ~75
        112        OP_DATA                                                  '1'
        113      > JMP                                                      ->126
   63   114    >   FETCH_DIM_R                                      ~77     !9, 'x'
        115        ROPE_INIT                                     4  ~80     ~77
        116        ROPE_ADD                                      1  ~80     ~80, '%2C+'
        117        FETCH_DIM_R                                      ~78     !9, 'y'
        118        ROPE_ADD                                      2  ~80     ~80, ~78
        119        ROPE_END                                      3  ~79     ~80, '+%3D+0%0A'
        120        ECHO                                                     ~79
   64   121        FETCH_DIM_R                                      ~82     !9, 'x'
        122        FETCH_DIM_R                                      ~84     !9, 'y'
        123        FETCH_DIM_W                                      $83     !4, ~82
        124        ASSIGN_DIM                                               $83, ~84
        125        OP_DATA                                                  '0'
   66   126    > > JMP                                                      ->162
   68   127    >   INIT_METHOD_CALL                                         !10, 'push'
        128        SEND_VAR_EX                                              !6
        129        DO_FCALL                                      0          
   69   130        INIT_FCALL                                               'var_dump'
        131        SEND_VAL                                                 'push'
        132        DO_ICALL                                                 
   71   133        FETCH_DIM_R                                      ~88     !9, 'x'
        134        FETCH_DIM_R                                      ~90     !9, 'y'
        135        FETCH_DIM_R                                      ~89     !4, ~88
        136        FETCH_DIM_R                                      ~91     ~89, ~90
        137        IS_IDENTICAL                                             ~91, '0'
        138      > JMPZ                                                     ~92, ->140
   72   139    >   ASSIGN                                                   !7, 1
   74   140    > > JMP                                                      ->162
   76   141    >   INIT_METHOD_CALL                                         !10, 'top'
        142        DO_FCALL                                      0  $94     
        143        ASSIGN                                                   !14, $94
   77   144        INIT_FCALL                                               'var_dump'
        145        SEND_VAL                                                 'pop'
        146        DO_ICALL                                                 
   79   147        FETCH_DIM_R                                      ~97     !9, 'x'
        148        FETCH_DIM_R                                      ~99     !9, 'y'
        149        FETCH_DIM_R                                      ~98     !4, ~97
        150        FETCH_DIM_R                                      ~100    ~98, ~99
        151        IS_IDENTICAL                                             ~100, '1'
        152      > JMPZ                                                     ~101, ->160
   80   153    >   INIT_FCALL                                               'substr'
        154        SEND_VAR                                                 !0
        155        ADD                                              ~102    !14, 1
        156        SEND_VAL                                                 ~102
        157        DO_ICALL                                         $103    
        158        ASSIGN                                                   !8, $103
   81   159        ASSIGN                                                   !6, !14
   84   160    > > JMP                                                      ->162
   86   161    > > JMP                                                      ->163
   89   162    >   PRE_DEC                                                  !1
   18   163    >   STRLEN                                           ~107    !8
        164      > JMPZ_EX                                          ~107    ~107, ->166
        165    >   BOOL                                             ~107    !1
        166    > > JMPNZ                                                    ~107, ->27
   92   167    >   ASSIGN                                                   !15, <array>
   94   168      > FE_RESET_R                                       $109    !4, ->177
        169    > > FE_FETCH_R                                               $109, !11, ->177
   95   170    >   INIT_FCALL                                               'implode'
        171        SEND_VAL                                                 ''
        172        SEND_VAR                                                 !11
        173        DO_ICALL                                         $111    
        174        ASSIGN_DIM                                               !15
        175        OP_DATA                                                  $111
   94   176      > JMP                                                      ->169
        177    >   FE_FREE                                                  $109
   98   178        INIT_FCALL                                               'implode'
        179        SEND_VAL                                                 '%0D%0A'
        180        SEND_VAR                                                 !15
        181        DO_ICALL                                         $112    
        182        VERIFY_RETURN_TYPE                                       $112
        183      > RETURN                                                   $112
   99   184*       VERIFY_RETURN_TYPE                                       
        185*     > RETURN                                                   null

End of function interpreter

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.55 ms | 1419 KiB | 20 Q