A simple example of PHP interpreter pattern sharing

Source: Internet
Author: User
A simple example of PHP interpreter pattern sharing

<?php//Interpreter mode abstract class Expression{private static $keyCount = 0;private $key = null;abstract function Interpret (Int Erpretercontext $ctx);/** * as array key * @return Auto increment value */public function GetKey () {if (!isset ($this->key ) {self:: $keyCount + +; $this->key = self:: $keyCount;} return $this->key;}} /** * Context */class interpretercontext{private $expressionstore = Array ()/** * Store value */public function replace (Ex Pression $exp, $value) {$this->expressionstore[$exp->getkey ()] = $value;} /** * Find value */public function lookup (Expression $exp) {return $this->expressionstore[$exp->getkey ()];}} /** * Literal expression */class literalexpression extends expression{private $value;p ublic function construct ($value) {$ This->value = $value;} Public function interpret (Interpretercontext $ctx) {$ctx->replace ($this, $this->value);}} /** * Var=value expression */class variableexpression extends Expression{private $name;p rivate $val;p ublic function const Ruct ($name, $val =null) {$this->name = $name; $this->val = $val;} Public function interpret (Interpretercontext $ctx) {if (!is_null ($this->val)) {$ctx->replace ($this, $this- val); $this->val = null;}} Public Function SetValue ($value) {$this->val = $value;} /** * @override */public function GetKey () {return $this->name;}} Abstract class Operatorexpression extends expression{protected $l _op;protected $r _op;public function construct ( Expression $l, expression $r) {$this->l_op = $l; $this->r_op = $r;} /** * @param $ctx interpretercontext * @param $result _l expression $l ' s result * @param $result _r expression $r ' s Resul T */protected abstract function Dointerpret (Interpretercontext $ctx, $result _l, $result _r);p ublic function interpret ( Interpretercontext $ctx) {$this->l_op->interpret ($ctx); $this->r_op->interpret ($CTX); $result _l = $ctx- >lookup ($this->l_op); $result _r = $ctx->lookup ($this->r_op); $this->dointerpret ($ctx, $result _l, $ Result_r);}} /** * equals * *Class Equalsexpression extends operatorexpression{protected function Dointerpret (interpretercontext $ctx, $result _l, $ Result_r) {$ctx->replace ($this, $result _l = = $result _r);}} /** * or */class booleanorexpression extends operatorexpression{protected function Dointerpret (Interpretercontext $ctx, $result _l, $result _r) {$ctx->replace ($this, $result _l | | $result _r);}} /** * and */class booleanandexpression extends operatorexpression{protected function Dointerpret (Interpretercontext $ CTX, $result _l, $result _r) {$ctx->replace ($this, $result _l && $result _r);}} /** * Not */class booleannotexpression extends expression{private $expr;p ublic function construct (Expression $e) {$this-& gt;expr = $e;} Public function interpret (Interpretercontext $ctx) {$this->expr->interpret ($ctx); $ctx->replace ($this,!$ Ctx->lookup ($this->expr));}} Test code/* $ctx = new Interpretercontext () $literarl = new Literalexpression (' four '); $literarl->interpret ($CTX) ;//Echo $ctx->lookup ($litErarl); $variable = new VariableExpression (' Input ', ' 444 '); $variable->interpret ($CTX);//Echo $ctx->lookup ($ variable); $answer = new VariableExpression (' input '); $answer->interpret ($CTX); Echo $ctx->lookup ($answer); */// $input Equas four or $input equals 4$ctx = new Interpretercontext (); $input = new VariableExpression (' input '); $statement = New Booleanorexpression (New Equalsexpression ($input, New Literalexpression (' Four ')), new Equalsexpression ($input, new Literalexpression (4)); $input->setvalue (' four '); $statement->interpret ($CTX); Var_dump ($ctx->lookup ($ statement)); True$input->setvalue (4); $statement->interpret ($CTX); Var_dump ($ctx->lookup ($statement)); True$input->setvalue, $statement->interpret ($ctx), Var_dump ($ctx->lookup ($statement)); False$not = new Booleannotexpression ($statement); True$not->interpret ($CTX); Var_dump ($ctx->lookup ($not));
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.