Last active
April 3, 2017 18:10
-
-
Save ravinsharma12345/503fe078e5cfcc1da87383857a4fe62b to your computer and use it in GitHub Desktop.
Parse equality string and test equality expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// does not consider &&, ||, XOR, OR, AND operator, ternary operators for now. | |
$var_a = 100; | |
$var_b = 100; | |
$var_c = 110; | |
$var_d = (double) 100; | |
$var_e = "100"; | |
$var_f = false; | |
$symbol_table = compact("var_a", "var_b", "var_c", "var_d", "var_e", "var_f"); | |
echo "'\$var_d == \$var_e' is " . (test_eq_expression('$var_d == $var_e', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'\$var_c != \$var_e' is " . (test_eq_expression('$var_c != $var_e', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'\$var_b === \$var_e' is " . (test_eq_expression('$var_b === $var_e', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'\$var_a =< \$var_e' is " . (test_eq_expression('$var_a =< $var_e', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'\$var_a == \$var_b' is " . (test_eq_expression('$var_a == $var_b', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'!\$var_f' is " . (test_eq_expression('!$var_f', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'!\$var_e' is " . (test_eq_expression('!$var_e', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'\$var_f' is " . (test_eq_expression('$var_f', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
echo "'\$var_e' is " . (test_eq_expression('$var_e', $symbol_table) == true ? "true" : "false").PHP_EOL; | |
function test_eq_expression($expression, $symbol_table = []) { | |
$tokens = token_get_all("<?php $expression ?>"); | |
if(is_array($tokens)) { | |
$fields = []; | |
foreach($tokens as $token) { | |
if(isset($token[1])) { | |
$_ = trim($token[1]); | |
} | |
else if(is_string($token)){ | |
$_ = trim($token); | |
} | |
if(!empty($_) and $_ !== "<?php" and $_ !== "?>") { | |
$fields[] = $_; | |
} | |
} | |
if(count($fields) == 1) { | |
$operator = "unary"; | |
$param1 = str_replace("\$", "", $fields[0]); | |
} | |
else if(count($fields) == 2) { | |
if($fields[0] == "!") { | |
$param1 = str_replace("\$", "", $fields[1]); | |
$operator = $fields[0]; | |
} | |
} | |
else if(count($fields) == 3) { | |
if(in_array($fields[1], ["==", "===", "!=", "!==", "<=", ">=", ">", "<"])) { | |
$param1 = str_replace("\$", "", $fields[0]); | |
$param2 = str_replace("\$", "", $fields[2]); | |
$operator = $fields[1]; | |
} | |
} | |
switch($operator): | |
case "unary": | |
return $symbol_table[$param1] ? true : false; | |
case "!": | |
return !$symbol_table[$param1]; | |
case "==": | |
return $symbol_table[$param1] == $symbol_table[$param2]; | |
break; | |
case "===": | |
return $symbol_table[$param1] === $symbol_table[$param2]; | |
break; | |
case "!=": | |
return $symbol_table[$param1] != $symbol_table[$param2]; | |
break; | |
case "!==": | |
return $symbol_table[$param1] !== $symbol_table[$param2]; | |
break; | |
case "<=": | |
return $symbol_table[$param1] <= $symbol_table[$param2]; | |
break; | |
case ">=": | |
return $symbol_table[$param1] >= $symbol_table[$param2]; | |
break; | |
case ">": | |
return $symbol_table[$param1] > $symbol_table[$param2]; | |
break; | |
case "<": | |
return $symbol_table[$param1] < $symbol_table[$param2]; | |
break; | |
endswitch; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment