3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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); $test = "fuck"; 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]); $lexed[1] = str_replace('is_bool', is_bool($test), $lexed[1]); $lexedList[$key] = $lexed[1]; } //var_dump($lexedList); $eh = implode("", $lexedList); var_dump($eh); $result = eval($eh); var_dump($result);
based on Mu5KG
Output for 7.0.0 - 7.2.0
string(9) "(1)&(1|!)" Parse error: syntax error, unexpected ')' in /in/OtWoU(99) : eval()'d code on line 1
Process exited with code 255.
Output for 5.4.11 - 5.6.28
string(9) "(1)&(1|!)" Parse error: syntax error, unexpected ')' in /in/OtWoU(99) : eval()'d code on line 1 bool(false)
Output for 5.4.2 - 5.4.10
string(9) "(1)&(1|!)" Parse error: syntax error, unexpected ')' in /in/OtWoU(99) : eval()'d code on line 1 bool(false)
Process exited with code 255.
Output for 5.3.18 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/OtWoU on line 3
Process exited with code 255.