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

End of function lex

Class Trie: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.61 ms | 1411 KiB | 18 Q