Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //-----------------------------------------------------------------------------------------
- function Interpret($name, &$context, $chain)
- {
- if (!array_key_exists($name, $context)) $context[$name] = array();
- $con = $context[$name];
- if (!array_key_exists("start", $con)) $con["start"] = 0;
- $start = $con["start"];
- $res = true;
- for($i = $start; $i<count($chain); $i++)
- {
- if (!$chain[$i]($context))
- {
- $con["start"] = $i;
- $res = false;
- break;
- }
- }
- $context[$name] = $con;
- return $res;
- }
- //-----------------------------------------------------------------------------------------
- session_start();
- if (!array_key_exists("context", $_SESSION))
- {
- $_SESSION["context"] = array();
- $_SESSION["context"]["required_values"] = array();
- $_SESSION["context"]["entered_values"] = array();
- $_SESSION["names"] = 0;
- }
- $_SESSION["context"]["required_values"] = array();
- $_SESSION["context"]["entered_values"] = $_GET;
- $prog = array();
- //-----------------------------------------------------------------------------------------
- $prog[] = function(&$context)
- {
- $context["qwe"] = 5;
- return true;
- };
- $prog[] = function(&$context)
- {
- $context["qwe"] *= 2;
- return true;
- };
- $prog[] = function(&$context)
- {
- $prog = array();
- if (!array_key_exists("qwe10", $context)) $context["qwe10"] = ($context["qwe"] > 10);
- if ($context["qwe10"])
- $prog[] = function(&$context)
- {
- $context["qwe"] /= 4;
- return true;
- };
- else
- $prog[] = function(&$context)
- {
- if (array_key_exists("increment", $context["entered_values"]))
- {
- $context["qwe"] += is_numeric($context["entered_values"]["increment"]) ? $context["entered_values"]["increment"] : 0;
- return true;
- }
- else
- {
- $context["required_values"][] = "increment";
- return false;
- }
- };
- return Interpret("qwe10node", $_SESSION["context"], $prog);
- };
- //-----------------------------------------------------------------------------------------
- $res = Interpret("main", $_SESSION["context"], $prog);
- echo "Full state: ".json_encode($_SESSION["context"]);
- echo "<br />";
- if ($res)
- {
- echo "Result: ".$_SESSION["context"]["qwe"];
- echo "<br />";
- echo "Done";
- $_SESSION = array();
- }
- else
- {
- if (count($_SESSION["context"]["required_values"]) > 0)
- {
- echo '<form action="main.php">';
- foreach ($_SESSION["context"]["required_values"] as $name)
- {
- echo "Enter $name: <input type='text' name='$name'><br>";
- }
- echo '<input type="submit"><br></form><br>';
- }
- }
- //-----------------------------------------------------------------------------------------
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement