3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $source = <<<"SOURCEND" This is a test {% block hello %} This is a test This is a {{ $test }} {% end block %} {{ block('hello') }} {% for $post in $posts %} {% end %} SOURCEND; function microtwig_compile($source){ function microtwig_parsecmd($cmd){ static $stack = array(); $fragments = preg_split('/\s+/',$cmd); $cmd = array_shift($fragments); switch($cmd){ case 'block': return 'function block_'.$fragments[0].'(){'; break; case 'if': $stack[] = 'if'; return 'if('.$fragments[2].' as '.$fragments[0].'):'; break; case 'for': $stack[] = 'foreach'; return 'foreach('.$fragments[2].' as '.$fragments[0].'):'; break; case 'end': switch(isset($fragments[0])?$fragments[0]:''){ case 'block': return '}'; break; default: switch($peek = array_pop($stack)){ case 'if': return 'endif;'; break; case 'foreach': return 'endforeach;'; break; } break; } break; default: break; } } $cnt=0; $fragments = array(); $source = str_replace(array('{{','}}'),array('<?=','?>'),$source); foreach(explode('{%',$source) as $tmp) { foreach(explode('%}',$tmp) as $item) { $fragments[] = $cnt++%2?'<?php '.microtwig_parsecmd(trim($item)).'?>':$item; } } return implode('',$fragments); } echo microtwig_compile($source);
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/PifBP
function name:  (null)
number of ops:  14
compiled vars:  !0 = $source, !1 = $test, !2 = $post, !3 = $posts
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   8     0  E >   ROPE_INIT                                     7  ~5      'This+is+a+test%0A%7B%25+block+hello+%25%7D%0A%0A++++This+is+a+test%0A++++This+is+a+%7B%7B+'
         1        ROPE_ADD                                      1  ~5      ~5, !1
  14     2        ROPE_ADD                                      2  ~5      ~5, '+%7D%7D%0A%0A%7B%25+end+block+%25%7D%0A%0A%7B%7B+block%28%27hello%27%29+%7D%7D%0A%0A%7B%25+for+'
         3        ROPE_ADD                                      3  ~5      ~5, !2
         4        ROPE_ADD                                      4  ~5      ~5, '+in+'
         5        ROPE_ADD                                      5  ~5      ~5, !3
  16     6        ROPE_END                                      6  ~4      ~5, '+%25%7D%0A%7B%25+end+%25%7D%0A'
         7        ASSIGN                                                   !0, ~4
  20     8        NOP                                                      
  60     9        INIT_FCALL                                               'microtwig_compile'
        10        SEND_VAR                                                 !0
        11        DO_FCALL                                      0  $10     
        12        ECHO                                                     $10
        13      > RETURN                                                   1

Function %00microtwig_parsecmd%2Fin%2FPifBP0x7f13d02e7505:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 14, Position 2 = 21
Branch analysis from position: 14
Jump found. Position 1 = 16, Position 2 = 26
Branch analysis from position: 16
Jump found. Position 1 = 18, Position 2 = 36
Branch analysis from position: 18
Jump found. Position 1 = 20, Position 2 = 46
Branch analysis from position: 20
Jump found. Position 1 = 79
Branch analysis from position: 79
Jump found. Position 1 = 80
Branch analysis from position: 80
Jump found. Position 1 = -2
Branch analysis from position: 46
Jump found. Position 1 = 48, Position 2 = 51
Branch analysis from position: 48
Jump found. Position 1 = 52
Branch analysis from position: 52
Jump found. Position 1 = 54, Position 2 = 55
Branch analysis from position: 54
Jump found. Position 1 = 58
Branch analysis from position: 58
Jump found. Position 1 = 64, Position 2 = 67
Branch analysis from position: 64
Jump found. Position 1 = 66, Position 2 = 71
Branch analysis from position: 66
Jump found. Position 1 = 75
Branch analysis from position: 75
Jump found. Position 1 = 77
Branch analysis from position: 77
Jump found. Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 71
Jump found. Position 1 = -2
Branch analysis from position: 67
Jump found. Position 1 = -2
Branch analysis from position: 55
Jump found. Position 1 = -2
Branch analysis from position: 51
Jump found. Position 1 = 54, Position 2 = 55
Branch analysis from position: 54
Branch analysis from position: 55
Branch analysis from position: 36
Jump found. Position 1 = -2
Branch analysis from position: 26
Jump found. Position 1 = -2
Branch analysis from position: 21
Jump found. Position 1 = -2
filename:       /in/PifBP
function name:  microtwig_parsecmd
number of ops:  81
compiled vars:  !0 = $cmd, !1 = $stack, !2 = $fragments, !3 = $peek
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  21     0  E >   RECV                                             !0      
  22     1        FETCH_W                      static              $4      'stack'
         2        ASSIGN_REF                                               !1, $4
  23     3        INIT_FCALL                                               'preg_split'
         4        SEND_VAL                                                 '%2F%5Cs%2B%2F'
         5        SEND_VAR                                                 !0
         6        DO_ICALL                                         $5      
         7        ASSIGN                                                   !2, $5
  24     8        INIT_FCALL                                               'array_shift'
         9        SEND_REF                                                 !2
        10        DO_ICALL                                         $7      
        11        ASSIGN                                                   !0, $7
  26    12        CASE                                             ~9      !0, 'block'
        13      > JMPNZ                                                    ~9, ->21
  27    14    >   CASE                                             ~9      !0, 'if'
        15      > JMPNZ                                                    ~9, ->26
  30    16    >   CASE                                             ~9      !0, 'for'
        17      > JMPNZ                                                    ~9, ->36
  33    18    >   CASE                                             ~9      !0, 'end'
        19      > JMPNZ                                                    ~9, ->46
        20    > > JMP                                                      ->79
  26    21    >   FETCH_DIM_R                                      $10     !2, 0
        22        CONCAT                                           ~11     'function+block_', $10
        23        CONCAT                                           ~12     ~11, '%28%29%7B'
        24      > RETURN                                                   ~12
        25*       JMP                                                      ->80
  28    26    >   ASSIGN_DIM                                               !1
        27        OP_DATA                                                  'if'
  29    28        FETCH_DIM_R                                      $14     !2, 2
        29        CONCAT                                           ~15     'if%28', $14
        30        CONCAT                                           ~16     ~15, '+as+'
        31        FETCH_DIM_R                                      $17     !2, 0
        32        CONCAT                                           ~18     ~16, $17
        33        CONCAT                                           ~19     ~18, '%29%3A'
        34      > RETURN                                                   ~19
        35*       JMP                                                      ->80
  31    36    >   ASSIGN_DIM                                               !1
        37        OP_DATA                                                  'foreach'
  32    38        FETCH_DIM_R                                      $21     !2, 2
        39        CONCAT                                           ~22     'foreach%28', $21
        40        CONCAT                                           ~23     ~22, '+as+'
        41        FETCH_DIM_R                                      $24     !2, 0
        42        CONCAT                                           ~25     ~23, $24
        43        CONCAT                                           ~26     ~25, '%29%3A'
        44      > RETURN                                                   ~26
        45*       JMP                                                      ->80
  34    46    >   ISSET_ISEMPTY_DIM_OBJ                       33554432  ~27     !2, 0
        47      > JMPZ                                                     ~27, ->51
        48    >   FETCH_DIM_R                                      $28     !2, 0
        49        QM_ASSIGN                                        ~29     $28
        50      > JMP                                                      ->52
        51    >   QM_ASSIGN                                        ~29     ''
  35    52    >   CASE                                             ~30     ~29, 'block'
        53      > JMPNZ                                                    ~30, ->55
        54    > > JMP                                                      ->58
        55    >   FREE                                                     ~29
        56      > RETURN                                                   '%7D'
        57*       JMP                                                      ->77
  37    58    >   INIT_FCALL                                               'array_pop'
        59        SEND_REF                                                 !1
        60        DO_ICALL                                         $31     
        61        ASSIGN                                           $32     !3, $31
  38    62        CASE                                             ~33     $32, 'if'
        63      > JMPNZ                                                    ~33, ->67
  39    64    >   CASE                                             ~33     $32, 'foreach'
        65      > JMPNZ                                                    ~33, ->71
        66    > > JMP                                                      ->75
  38    67    >   FREE                                                     $32
        68        FREE                                                     ~29
        69      > RETURN                                                   'endif%3B'
        70*       JMP                                                      ->75
  39    71    >   FREE                                                     $32
        72        FREE                                                     ~29
        73      > RETURN                                                   'endforeach%3B'
        74*       JMP                                                      ->75
        75    >   FREE                                                     $32
  41    76      > JMP                                                      ->77
        77    >   FREE                                                     ~29
  43    78      > JMP                                                      ->80
  44    79    > > JMP                                                      ->80
  46    80    > > RETURN                                                   null

End of function %00microtwig_parsecmd%2Fin%2FPifBP0x7f13d02e7505

Function microtwig_compile:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 15, Position 2 = 41
Branch analysis from position: 15
Jump found. Position 1 = 16, Position 2 = 41
Branch analysis from position: 16
Jump found. Position 1 = 21, Position 2 = 39
Branch analysis from position: 21
Jump found. Position 1 = 22, Position 2 = 39
Branch analysis from position: 22
Jump found. Position 1 = 25, Position 2 = 35
Branch analysis from position: 25
Jump found. Position 1 = 36
Branch analysis from position: 36
Jump found. Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 35
Jump found. Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 39
Jump found. Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 39
Branch analysis from position: 41
Jump found. Position 1 = -2
Branch analysis from position: 41
filename:       /in/PifBP
function name:  microtwig_compile
number of ops:  48
compiled vars:  !0 = $source, !1 = $cnt, !2 = $fragments, !3 = $tmp, !4 = $item
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  20     0  E >   RECV                                             !0      
  21     1        DECLARE_FUNCTION                                         '%00microtwig_parsecmd%2Fin%2FPifBP0x7f13d02e7505', 'microtwig_parsecmd'
  48     2        ASSIGN                                                   !1, 0
  49     3        ASSIGN                                                   !2, <array>
  50     4        INIT_FCALL                                               'str_replace'
         5        SEND_VAL                                                 <array>
         6        SEND_VAL                                                 <array>
         7        SEND_VAR                                                 !0
         8        DO_ICALL                                         $7      
         9        ASSIGN                                                   !0, $7
  51    10        INIT_FCALL                                               'explode'
        11        SEND_VAL                                                 '%7B%25'
        12        SEND_VAR                                                 !0
        13        DO_ICALL                                         $9      
        14      > FE_RESET_R                                       $10     $9, ->41
        15    > > FE_FETCH_R                                               $10, !3, ->41
  52    16    >   INIT_FCALL                                               'explode'
        17        SEND_VAL                                                 '%25%7D'
        18        SEND_VAR                                                 !3
        19        DO_ICALL                                         $11     
        20      > FE_RESET_R                                       $12     $11, ->39
        21    > > FE_FETCH_R                                               $12, !4, ->39
  53    22    >   POST_INC                                         ~14     !1
        23        MOD                                              ~15     ~14, 2
        24      > JMPZ                                                     ~15, ->35
        25    >   INIT_FCALL_BY_NAME                                       'microtwig_parsecmd'
        26        INIT_FCALL                                               'trim'
        27        SEND_VAR                                                 !4
        28        DO_ICALL                                         $16     
        29        SEND_VAR_NO_REF                               4          $16
        30        DO_FCALL                                      0  $17     
        31        CONCAT                                           ~18     '%3C%3Fphp+', $17
        32        CONCAT                                           ~19     ~18, '%3F%3E'
        33        QM_ASSIGN                                        ~20     ~19
        34      > JMP                                                      ->36
        35    >   QM_ASSIGN                                        ~20     !4
        36    >   ASSIGN_DIM                                               !2
        37        OP_DATA                                                  ~20
        38      > JMP                                                      ->21
        39    >   FE_FREE                                                  $12
        40      > JMP                                                      ->15
        41    >   FE_FREE                                                  $10
  57    42        INIT_FCALL                                               'implode'
        43        SEND_VAL                                                 ''
        44        SEND_VAR                                                 !2
        45        DO_ICALL                                         $21     
        46      > RETURN                                                   $21
  58    47*     > RETURN                                                   null

End of function microtwig_compile

Generated using Vulcan Logic Dumper, using php 7.3.0