Rumah > pembangunan bahagian belakang > tutorial php > PHP解释器模式的一个简单示例分享

PHP解释器模式的一个简单示例分享

黄舟
Lepaskan: 2023-03-06 16:32:01
asal
2124 orang telah melayarinya

PHP解释器模式的一个简单示例分享

<?php
// 解释器模式

abstract class Expression
{
	private static $keyCount = 0;
	private $key = NULL;

	abstract function interpret(InterpreterContext $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(Expression $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;

	public 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;
	private $val;

	public function construct($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&#39;s result
	 * @param $result_r  Expression $r&#39;s result
	 */
	protected abstract function doInterpret(InterpreterContext $ctx, 
		$result_l, $result_r);

	public 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;
	
	public function construct(Expression $e) {
		$this->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(&#39;four&#39;);
$literarl->interpret($ctx);
// echo $ctx->lookup($literarl);

$variable = new VariableExpression(&#39;input&#39;, &#39;444&#39;);
$variable->interpret($ctx);
// echo $ctx->lookup($variable);

$answer = new VariableExpression(&#39;input&#39;);
$answer->interpret($ctx);
echo $ctx->lookup($answer);
*/

// $input equas four or $input equals 4
$ctx = new InterpreterContext();
$input = new VariableExpression(&#39;input&#39;);
$statement = new BooleanOrExpression(
	new EqualsExpression($input, new LiteralExpression(&#39;four&#39;)),
	new EqualsExpression($input, new LiteralExpression(4))
);

$input->setValue(&#39;four&#39;);
$statement->interpret($ctx);
var_dump($ctx->lookup($statement)); // true

$input->setValue(4);
$statement->interpret($ctx);
var_dump($ctx->lookup($statement)); // true

$input->setValue(42);
$statement->interpret($ctx);
var_dump($ctx->lookup($statement)); // false

$not = new BooleanNotExpression($statement); // true
$not->interpret($ctx);
var_dump( $ctx->lookup($not) );
Salin selepas log masuk

Atas ialah kandungan terperinci PHP解释器模式的一个简单示例分享. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan