3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Trie { public $data = []; public $value = false; } $tokens = [ "apple" => "T_APPLE", "ape" => "T_APE", "cape" => "T_CAPE", "(" => "T_L_PARENTH", ")" => "T_R_PARENTH", "is_string" => "T_IS_STRING", "is_bool" => "T_IS_BOOL", "!is_bool" => "T_NOT_IS_BOOL", "&" => "T_AND", "|" => "T_OR", ]; $root = new Trie; foreach ($tokens as $name => $value) { $node = $root; // For each character in the string for ($i = 0; $i < strlen($name); $i++) { $char = $name[$i]; if (!isset($node->data[$char])) { // If we don't have a child with that char // Create it $node->data[$char] = new Trie; } // Reset the node for the next character $node = $node->data[$char]; } // Finally, set the value on the final node $node->value = $value; } // "ape" is a valid token, so we expect T_APE // var_dump($root->data['a']->data['p']->data['n']->value); // T_APE var_dump( $root ->data['a'] ->data['p'] ->data['e'] ->value ); // T_APE var_dump( $root ->data['('] ->value ); // T_L_PARENTH var_dump( $root ->data['i'] ->data['s'] ->data['_'] ->data['s'] ->data['t'] ->data['r'] ->data['i'] ->data['n'] ->data['g'] ->value ); // T_APE function lex($string, Trie $root) { $length = strlen($string); $i = 0; $tokens = []; $node = $root; $buffer = ''; // We want to iterate over the entire string. while ($i < $length) { // Get the current character $char = $string[$i]; if (isset($node->data[$char])) { // We have a valid next character $i++; // Save the character in the buffer $buffer .= $char; // Move to the next state $node = $node->data[$char]; } elseif ($node->value) { // We have a value and no valid next character // Emit the token $tokens[] = [$node->value, $buffer]; // Clear the buffer $buffer = ''; // Reset back to the root for the next token $node = $root; } else { // We can't continue parsing this node throw new Exception("Syntax error at offset $i"); } } if ($buffer !== '') { // We finished without flushing one token if ($node->value) { $tokens[] = [$node->value, $buffer]; } else { // Not a valid complete token throw new Exception("Syntax error at offset $i"); } } return $tokens; } // var_dump(lex('(is_string)', $root)); $lexedList = lex('(is_string)&(is_string|!is_bool)', $root); // get stuff between each parenth, put them in things, then use the & / | to compare the two var_dump($lexedList); foreach ($lexedList as $lexed) { $thing = $lexed[1]; $lexed[1] = str_replace('is_string', 'is_string($test)', $thing); } var_dump($lexedList); $test = "fuck"; $eh = implode("", $lexed); $result = eval($eh); var_dump($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 31
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 31
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 10
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 10
Branch analysis from position: 28
Branch analysis from position: 10
Branch analysis from position: 21
Branch analysis from position: 31
2 jumps found. (Code = 77) Position 1 = 79, Position 2 = 90
Branch analysis from position: 79
2 jumps found. (Code = 78) Position 1 = 80, Position 2 = 90
Branch analysis from position: 80
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
Branch analysis from position: 90
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 90
Branch analysis from position: 31
filename:       /in/t8Hll
function name:  (null)
number of ops:  106
compiled vars:  !0 = $tokens, !1 = $root, !2 = $value, !3 = $name, !4 = $node, !5 = $i, !6 = $char, !7 = $lexedList, !8 = $lexed, !9 = $thing, !10 = $test, !11 = $eh, !12 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   ASSIGN                                                   !0, <array>
   22     1        NEW                                              $14     'Trie'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $14
   23     4      > FE_RESET_R                                       $17     !0, ->31
          5    > > FE_FETCH_R                                       ~18     $17, !2, ->31
          6    >   ASSIGN                                                   !3, ~18
   24     7        ASSIGN                                                   !4, !1
   26     8        ASSIGN                                                   !5, 0
          9      > JMP                                                      ->25
   27    10    >   FETCH_DIM_R                                      ~22     !3, !5
         11        ASSIGN                                                   !6, ~22
   28    12        FETCH_OBJ_IS                                     ~24     !4, 'data'
         13        ISSET_ISEMPTY_DIM_OBJ                         0  ~25     ~24, !6
         14        BOOL_NOT                                         ~26     ~25
         15      > JMPZ                                                     ~26, ->21
   31    16    >   NEW                                              $29     'Trie'
         17        DO_FCALL                                      0          
         18        FETCH_OBJ_W                                      $27     !4, 'data'
         19        ASSIGN_DIM                                               $27, !6
         20        OP_DATA                                                  $29
   34    21    >   FETCH_OBJ_R                                      ~31     !4, 'data'
         22        FETCH_DIM_R                                      ~32     ~31, !6
         23        ASSIGN                                                   !4, ~32
   26    24        PRE_INC                                                  !5
         25    >   STRLEN                                           ~35     !3
         26        IS_SMALLER                                               !5, ~35
         27      > JMPNZ                                                    ~36, ->10
   37    28    >   ASSIGN_OBJ                                               !4, 'value'
         29        OP_DATA                                                  !2
   23    30      > JMP                                                      ->5
         31    >   FE_FREE                                                  $17
   42    32        INIT_FCALL                                               'var_dump'
   44    33        FETCH_OBJ_R                                      ~38     !1, 'data'
         34        FETCH_DIM_R                                      ~39     ~38, 'a'
   45    35        FETCH_OBJ_R                                      ~40     ~39, 'data'
         36        FETCH_DIM_R                                      ~41     ~40, 'p'
   46    37        FETCH_OBJ_R                                      ~42     ~41, 'data'
         38        FETCH_DIM_R                                      ~43     ~42, 'e'
   47    39        FETCH_OBJ_R                                      ~44     ~43, 'value'
         40        SEND_VAL                                                 ~44
         41        DO_ICALL                                                 
   51    42        INIT_FCALL                                               'var_dump'
   53    43        FETCH_OBJ_R                                      ~46     !1, 'data'
         44        FETCH_DIM_R                                      ~47     ~46, '%28'
   54    45        FETCH_OBJ_R                                      ~48     ~47, 'value'
         46        SEND_VAL                                                 ~48
         47        DO_ICALL                                                 
   57    48        INIT_FCALL                                               'var_dump'
   59    49        FETCH_OBJ_R                                      ~50     !1, 'data'
         50        FETCH_DIM_R                                      ~51     ~50, 'i'
   60    51        FETCH_OBJ_R                                      ~52     ~51, 'data'
         52        FETCH_DIM_R                                      ~53     ~52, 's'
   61    53        FETCH_OBJ_R                                      ~54     ~53, 'data'
         54        FETCH_DIM_R                                      ~55     ~54, '_'
   62    55        FETCH_OBJ_R                                      ~56     ~55, 'data'
         56        FETCH_DIM_R                                      ~57     ~56, 's'
   63    57        FETCH_OBJ_R                                      ~58     ~57, 'data'
         58        FETCH_DIM_R                                      ~59     ~58, 't'
   64    59        FETCH_OBJ_R                                      ~60     ~59, 'data'
         60        FETCH_DIM_R                                      ~61     ~60, 'r'
   65    61        FETCH_OBJ_R                                      ~62     ~61, 'data'
         62        FETCH_DIM_R                                      ~63     ~62, 'i'
   66    63        FETCH_OBJ_R                                      ~64     ~63, 'data'
         64        FETCH_DIM_R                                      ~65     ~64, 'n'
   67    65        FETCH_OBJ_R                                      ~66     ~65, 'data'
         66        FETCH_DIM_R                                      ~67     ~66, 'g'
   68    67        FETCH_OBJ_R                                      ~68     ~67, 'value'
         68        SEND_VAL                                                 ~68
         69        DO_ICALL                                                 
  114    70        INIT_FCALL                                               'lex'
         71        SEND_VAL                                                 '%28is_string%29%26%28is_string%7C%21is_bool%29'
         72        SEND_VAR                                                 !1
         73        DO_FCALL                                      0  $70     
         74        ASSIGN                                                   !7, $70
  116    75        INIT_FCALL                                               'var_dump'
         76        SEND_VAR                                                 !7
         77        DO_ICALL                                                 
  118    78      > FE_RESET_R                                       $73     !7, ->90
         79    > > FE_FETCH_R                                               $73, !8, ->90
  119    80    >   FETCH_DIM_R                                      ~74     !8, 1
         81        ASSIGN                                                   !9, ~74
  120    82        INIT_FCALL                                               'str_replace'
         83        SEND_VAL                                                 'is_string'
         84        SEND_VAL                                                 'is_string%28%24test%29'
         85        SEND_VAR                                                 !9
         86        DO_ICALL                                         $77     
         87        ASSIGN_DIM                                               !8, 1
         88        OP_DATA                                                  $77
  118    89      > JMP                                                      ->79
         90    >   FE_FREE                                                  $73
  122    91        INIT_FCALL                                               'var_dump'
         92        SEND_VAR                                                 !7
         93        DO_ICALL                                                 
  124    94        ASSIGN                                                   !10, 'fuck'
  125    95        INIT_FCALL                                               'implode'
         96        SEND_VAL                                                 ''
         97        SEND_VAR                                                 !8
         98        DO_ICALL                                         $80     
         99        ASSIGN                                                   !11, $80
  126   100        INCLUDE_OR_EVAL                                  $82     !11, EVAL
        101        ASSIGN                                                   !12, $82
  127   102        INIT_FCALL                                               'var_dump'
        103        SEND_VAR                                                 !12
        104        DO_ICALL                                                 
        105      > RETURN                                                   1

Function lex:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 9
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 54
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 48
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 54
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 30
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 30
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/t8Hll
function name:  lex
number of ops:  56
compiled vars:  !0 = $string, !1 = $root, !2 = $length, !3 = $i, !4 = $tokens, !5 = $node, !6 = $buffer, !7 = $char
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   72     2        STRLEN                                           ~8      !0
          3        ASSIGN                                                   !2, ~8
   73     4        ASSIGN                                                   !3, 0
   74     5        ASSIGN                                                   !4, <array>
   75     6        ASSIGN                                                   !5, !1
   76     7        ASSIGN                                                   !6, ''
   78     8      > JMP                                                      ->36
   80     9    >   FETCH_DIM_R                                      ~14     !0, !3
         10        ASSIGN                                                   !7, ~14
   81    11        FETCH_OBJ_IS                                     ~16     !5, 'data'
         12        ISSET_ISEMPTY_DIM_OBJ                         0          ~16, !7
         13      > JMPZ                                                     ~17, ->20
   83    14    >   PRE_INC                                                  !3
   85    15        ASSIGN_OP                                     8          !6, !7
   87    16        FETCH_OBJ_R                                      ~20     !5, 'data'
         17        FETCH_DIM_R                                      ~21     ~20, !7
         18        ASSIGN                                                   !5, ~21
         19      > JMP                                                      ->36
   88    20    >   FETCH_OBJ_R                                      ~23     !5, 'value'
         21      > JMPZ                                                     ~23, ->30
   91    22    >   FETCH_OBJ_R                                      ~25     !5, 'value'
         23        INIT_ARRAY                                       ~26     ~25
         24        ADD_ARRAY_ELEMENT                                ~26     !6
         25        ASSIGN_DIM                                               !4
         26        OP_DATA                                                  ~26
   93    27        ASSIGN                                                   !6, ''
   95    28        ASSIGN                                                   !5, !1
         29      > JMP                                                      ->36
   98    30    >   NEW                                              $29     'Exception'
         31        NOP                                                      
         32        FAST_CONCAT                                      ~30     'Syntax+error+at+offset+', !3
         33        SEND_VAL_EX                                              ~30
         34        DO_FCALL                                      0          
         35      > THROW                                         0          $29
   78    36    >   IS_SMALLER                                               !3, !2
         37      > JMPNZ                                                    ~32, ->9
  101    38    >   IS_NOT_IDENTICAL                                         !6, ''
         39      > JMPZ                                                     ~33, ->54
  103    40    >   FETCH_OBJ_R                                      ~34     !5, 'value'
         41      > JMPZ                                                     ~34, ->48
  104    42    >   FETCH_OBJ_R                                      ~36     !5, 'value'
         43        INIT_ARRAY                                       ~37     ~36
         44        ADD_ARRAY_ELEMENT                                ~37     !6
         45        ASSIGN_DIM                                               !4
         46        OP_DATA                                                  ~37
         47      > JMP                                                      ->54
  107    48    >   NEW                                              $38     'Exception'
         49        NOP                                                      
         50        FAST_CONCAT                                      ~39     'Syntax+error+at+offset+', !3
         51        SEND_VAL_EX                                              ~39
         52        DO_FCALL                                      0          
         53      > THROW                                         0          $38
  110    54    > > RETURN                                                   !4
  111    55*     > RETURN                                                   null

End of function lex

Class Trie: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.02 ms | 1415 KiB | 20 Q