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; } 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 $key => $lexed) { $thing = $lexed[1]; $lexed[1] = str_replace('is_string', 'is_string($test)', $thing); $lexedList[$key] = $lexed; } //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 = 38, Position 2 = 52
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 52
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
Branch analysis from position: 31
filename:       /in/mDqgd
function name:  (null)
number of ops:  65
compiled vars:  !0 = $tokens, !1 = $root, !2 = $value, !3 = $name, !4 = $node, !5 = $i, !6 = $char, !7 = $lexedList, !8 = $lexed, !9 = $key, !10 = $thing, !11 = $test, !12 = $eh, !13 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   ASSIGN                                                   !0, <array>
   20     1        NEW                                              $15     'Trie'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $15
   21     4      > FE_RESET_R                                       $18     !0, ->31
          5    > > FE_FETCH_R                                       ~19     $18, !2, ->31
          6    >   ASSIGN                                                   !3, ~19
   22     7        ASSIGN                                                   !4, !1
   24     8        ASSIGN                                                   !5, 0
          9      > JMP                                                      ->25
   25    10    >   FETCH_DIM_R                                      ~23     !3, !5
         11        ASSIGN                                                   !6, ~23
   26    12        FETCH_OBJ_IS                                     ~25     !4, 'data'
         13        ISSET_ISEMPTY_DIM_OBJ                         0  ~26     ~25, !6
         14        BOOL_NOT                                         ~27     ~26
         15      > JMPZ                                                     ~27, ->21
   29    16    >   NEW                                              $30     'Trie'
         17        DO_FCALL                                      0          
         18        FETCH_OBJ_W                                      $28     !4, 'data'
         19        ASSIGN_DIM                                               $28, !6
         20        OP_DATA                                                  $30
   32    21    >   FETCH_OBJ_R                                      ~32     !4, 'data'
         22        FETCH_DIM_R                                      ~33     ~32, !6
         23        ASSIGN                                                   !4, ~33
   24    24        PRE_INC                                                  !5
         25    >   STRLEN                                           ~36     !3
         26        IS_SMALLER                                               !5, ~36
         27      > JMPNZ                                                    ~37, ->10
   35    28    >   ASSIGN_OBJ                                               !4, 'value'
         29        OP_DATA                                                  !2
   21    30      > JMP                                                      ->5
         31    >   FE_FREE                                                  $18
   81    32        INIT_FCALL                                               'lex'
         33        SEND_VAL                                                 '%28is_string%29%26%28is_string%7C%21is_bool%29'
         34        SEND_VAR                                                 !1
         35        DO_FCALL                                      0  $39     
         36        ASSIGN                                                   !7, $39
   85    37      > FE_RESET_R                                       $41     !7, ->52
         38    > > FE_FETCH_R                                       ~42     $41, !8, ->52
         39    >   ASSIGN                                                   !9, ~42
   86    40        FETCH_DIM_R                                      ~44     !8, 1
         41        ASSIGN                                                   !10, ~44
   87    42        INIT_FCALL                                               'str_replace'
         43        SEND_VAL                                                 'is_string'
         44        SEND_VAL                                                 'is_string%28%24test%29'
         45        SEND_VAR                                                 !10
         46        DO_ICALL                                         $47     
         47        ASSIGN_DIM                                               !8, 1
         48        OP_DATA                                                  $47
   88    49        ASSIGN_DIM                                               !7, !9
         50        OP_DATA                                                  !8
   85    51      > JMP                                                      ->38
         52    >   FE_FREE                                                  $41
   92    53        ASSIGN                                                   !11, 'fuck'
   93    54        INIT_FCALL                                               'implode'
         55        SEND_VAL                                                 ''
         56        SEND_VAR                                                 !8
         57        DO_ICALL                                         $50     
         58        ASSIGN                                                   !12, $50
   94    59        INCLUDE_OR_EVAL                                  $52     !12, EVAL
         60        ASSIGN                                                   !13, $52
   95    61        INIT_FCALL                                               'var_dump'
         62        SEND_VAR                                                 !13
         63        DO_ICALL                                                 
         64      > 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/mDqgd
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
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   39     2        STRLEN                                           ~8      !0
          3        ASSIGN                                                   !2, ~8
   40     4        ASSIGN                                                   !3, 0
   41     5        ASSIGN                                                   !4, <array>
   42     6        ASSIGN                                                   !5, !1
   43     7        ASSIGN                                                   !6, ''
   45     8      > JMP                                                      ->36
   47     9    >   FETCH_DIM_R                                      ~14     !0, !3
         10        ASSIGN                                                   !7, ~14
   48    11        FETCH_OBJ_IS                                     ~16     !5, 'data'
         12        ISSET_ISEMPTY_DIM_OBJ                         0          ~16, !7
         13      > JMPZ                                                     ~17, ->20
   50    14    >   PRE_INC                                                  !3
   52    15        ASSIGN_OP                                     8          !6, !7
   54    16        FETCH_OBJ_R                                      ~20     !5, 'data'
         17        FETCH_DIM_R                                      ~21     ~20, !7
         18        ASSIGN                                                   !5, ~21
         19      > JMP                                                      ->36
   55    20    >   FETCH_OBJ_R                                      ~23     !5, 'value'
         21      > JMPZ                                                     ~23, ->30
   58    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
   60    27        ASSIGN                                                   !6, ''
   62    28        ASSIGN                                                   !5, !1
         29      > JMP                                                      ->36
   65    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
   45    36    >   IS_SMALLER                                               !3, !2
         37      > JMPNZ                                                    ~32, ->9
   68    38    >   IS_NOT_IDENTICAL                                         !6, ''
         39      > JMPZ                                                     ~33, ->54
   70    40    >   FETCH_OBJ_R                                      ~34     !5, 'value'
         41      > JMPZ                                                     ~34, ->48
   71    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
   74    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
   77    54    > > RETURN                                                   !4
   78    55*     > RETURN                                                   null

End of function lex

Class Trie: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.69 ms | 1411 KiB | 20 Q