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

End of function interpreter

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.58 ms | 1419 KiB | 20 Q