Advertisement
Transformator

Parser

May 17th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <form action="" method="post">
  2.     <textarea name="parse"></textarea><br>
  3.     <input type="submit" value="run your code!">
  4. </form>
  5.  
  6. <br><br><br><br>
  7.  
  8. <?php
  9.  
  10. ini_set('display_errors',1);
  11. ini_set('display_startup_errors',1);
  12. error_reporting(-1);
  13.  
  14. if(isset($_POST) && isset($_POST["parse"]))
  15.     echo parse($_POST["parse"]);
  16. else
  17. echo parse("If 10 == 10, then here will appear a <<IF [10] == [10] FI>>, but there is also the possibility to get a <<IF [30] < [10] FI>>,<br>
  18.    therefor the IF clause I wrote there have to be wrong, but that is not the case.<br> But it can also happen a error like this: <<IF[30]=[30]FI>><br>
  19.    soon there will be the possibility to add variables, they will be used like this:<br>
  20.    define: <<[HalloWelt]=[13]>><br>
  21.    and output: <<ECHO[HalloWelt]ECHO>><br>
  22.    Now a litte test: <<IF[<<IF[10]==[10]FI>>]==[0]FI>>");
  23.  
  24. function parse($toParse) {
  25.     $variables = [];
  26.  
  27.     if(preg_match_all("/<<+(.*?)>>/", $toParse, $matches)) {
  28.     foreach($matches[1] as $match) {
  29.             $save = $match;
  30.  
  31.             $match = str_replace(" ", "", $match);
  32.  
  33.             if(preg_match_all("/if+(.*?)fi/i", $match, $ifs)) {
  34.                 foreach($ifs[1] as $if) {
  35.                     $if = parse($if);
  36.  
  37.                     $if = str_replace(">", "", $if, $gt);
  38.                     $if = str_replace("<", "", $if, $lt);
  39.                     $if = str_replace("==", "", $if, $eq);
  40.  
  41.                     preg_match_all("/\[+(.*?)\]/", $if, $params);
  42.                     if(count($params[1]) > 2 || count($params[1]) < 2) die ("Syntax Error!");
  43.  
  44.                     if($gt == 1 && $lt != 1 && $eq != 1) {
  45.                         if($params[1][0] > $params[1][1])
  46.                             $toParse = str_replace("<<".$save.">>","1", $toParse);
  47.                         else
  48.                             $toParse = str_replace("<<".$save.">>","0", $toParse);
  49.                     } else if($gt != 1 && $lt == 1 && $eq != 1) {
  50.                         if($params[1][0] < $params[1][1])
  51.                             $toParse = str_replace("<<".$save.">>","1", $toParse);
  52.                         else
  53.                             $toParse = str_replace("<<".$save.">>","0", $toParse);
  54.                     } else if($gt != 1 && $lt != 1 && $eq == 1) {
  55.                         if($params[1][0] == $params[1][1])
  56.                             $toParse = str_replace("<<".$save.">>","1", $toParse);
  57.                         else
  58.                             $toParse = str_replace("<<".$save.">>","0", $toParse);
  59.                     } else {
  60.                         $toParse = str_replace("<<".$save.">>","Syntax Error!", $toParse);
  61.                     }
  62.                 }
  63.             }
  64.             else if(preg_match_all("/echo+(.*?)echo/i", $match, $echos)) {
  65.                 foreach($echos[1] as $echo) {
  66.                     preg_match_all("/\[+(.*?)\]/", $echo, $params);
  67.                     if(count($params[1]) == 1)
  68.                         $toParse = str_replace("<<".$save.">>",$variables[strtolower($params[1][0])], $toParse);
  69.                     else
  70.                         $toParse = str_replace("<<".$save.">>","Syntax Error!", $toParse);
  71.                 }
  72.             } else {
  73.                 $match = str_replace("=", "", $match, $ct);
  74.                 if($ct < 1 || $ct > 1)
  75.                     $toParse = str_replace("<<".$save.">>","Syntax Error!", $toParse);
  76.                 else {
  77.                     preg_match_all("/\[+(.*?)\]/", $match, $params);
  78.  
  79.                     if(count($params[1]) == 2) {
  80.                         $variables[strtolower($params[1][0])] = $params[1][1];
  81.                         $toParse = str_replace("<<".$save.">>","", $toParse);
  82.                     } else
  83.                         $toParse = str_replace("<<".$save.">>","Syntax Error!", $toParse);
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     return $toParse;
  89. }
  90.  
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement