解释器模式
发布时间:2020-12-14 06:15:16 所属栏目:百科 来源:网络整理
导读:问题: 解释器模式就是用“迷你语言”来表现程序要解决的问题,以“迷你语言”写成“迷你程序”来表现具体的问题,例如正则表达式就是一个很好的例子。所以,通常当有一个语言需要解释执行,并且可以将该语言中的句子表示为一个抽象语法树时,就可以使用解释
问题: 概念: 实现:
2. 代码示例:(实现一个迷你语言解释器) //表达文本类
class InterpreterContext
{
private $expressionstore = [];
public function replace(Expression $exp,$value)
{
$this->expressionstore[$exp->getKey()] = $value;
}
public function lookup(Expression $exp)
{
return $this->expressionstore[$exp->getKey()];
}
}
//表达式基类
abstract class Expression
{
private static $keycount = 0;
private $key;
abstract public function interpret(InterpreterContext $context);
public function getKey()
{
if(!isset($this->key)) {
self::$keycount ++;
$this->key = self::$keycount;
}
return $this->key;
}
}
//字符串表达式类
class LiteralExpression extends Expression
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function interpret(InterpreterContext $context)
{
$context->replace($this,$this->value);
}
}
//变量表达式类
class VariableExpression extends Expression
{
private $name;
private $val;
public function __construct($name,$val = null)
{
$this->name = $name;
$this->val = $val;
}
public function interpret(InterpreterContext $context)
{
if(!is_null($this->val)) {
$context->replace($this,$this->val);
$this->val = null;
}
}
public function setValue($value)
{
$this->val = $value;
}
public function getKey()
{
return $this->name;
}
}
//操作符表达式基类
abstract class OperatorExpression extends Expression
{
protected $l_op;
protected $r_op;
public function __construct(Expression $l_op,Expression $r_op)
{
$this->l_op = $l_op;
$this->r_op = $r_op;
}
public function interpret(InterpreterContext $context)
{
$this->l_op->interpret($context);
$this->r_op->interpret($context);
$result_l = $context->lookup($this->l_op);
$result_r = $context->lookup($this->r_op);
$this->doInterpret($context,$result_l,$result_r);
}
abstract protected function doInterpret(InterpreterContext $context,$result_r);
}
//相等表达式类
class EqualsExpression extends OperatorExpression
{
protected function doInterpret(InterpreterContext $context,$result_r)
{
$context->replace($this,$result_l == $result_r);
}
}
//布尔或表达式类
class BooleanOrExpression extends OperatorExpression
{
protected function doInterpret(InterpreterContext $context,$result_l || $result_r);
}
}
//布尔与表达式类
class BooleanAndExpression extends OperatorExpression
{
protected function doInterpret(InterpreterContext $context,$result_l && $result_r);
}
}
//应用:$input equals "4" or $input equals "four"
$context = new InterpreterContext();
$input = new VariableExpression(‘input‘);
$statement = new BooleanOrExpression(
new EqualsExpression($input,new LiteralExpression(‘four‘)),new EqualsExpression($input,new LiteralExpression(‘4‘))
);
foreach([‘four‘,‘4‘,‘52‘] as $val) {
$input->setValue($val);
echo $val,‘:‘;
$statement->interpret($context);
if($context->lookup($statement)) {
echo ‘Yes<br>‘;
} else {
echo ‘No<br>‘;
}
}
注:示例代码中同时也使用了模板方法模式和组合模式。 效果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- xml file too big to import to wordpress websi
- node.js – 无法连接到Xcode中的本地服务器
- 总结Cocos2d-x 3.x版本的一些变化
- c# – 通过NetworkStream的BinaryReader和Binary
- 使用DispatchQueue.concurrentPerform(迭代:)的S
- 将PostgreSQL数据库复制到另一个服务器
- Swift – BTLE – 如何添加CBCentralManagerDele
- iphone – 在Interface Builder中无法正确加载XI
- 按照xml文件写Schema文件样例——orders.xml
- .Net Remoting 入门教程-原理&&Hello
热点阅读