总想成为一名写做技巧高超的做家,却一不当心成为了码农。php
不知道,你们有没有看原文做者的一些见解(传送门)。咱们为何要学习新的知识,咱们应该如何学习新的知识。看过不少书,却没有记住多少,有时候以为本身就像鱼同样,真的只有七秒的记忆。git
正如原做者所说的,学习知识最好的方法就是去实践。这样才能够将知识掌握。github
以前,看过一篇新闻,PHPPHP,不知道有没有人记得这个项目,当时他出现时,我想不少人同样说,这个无聊的项目有什么用户,后来才逐渐发现了本身的无知。虽然PHPHP并未像pypy同样发展起来,却给了不少想学习解释器的同窗一个学习和实践的途径。学习
言归正传,这节这个解释器已经能够完成计算器的不少功能,能够实现多位数连续加减运算。this
Talk is cheap ,show me the code.spa
<?php define('ISINTEGER','ISINTEGER');//定义整数类型描述 define('PLUS','PLUS');//定义操做符号类型描述 加法 define('MINUS','MINUS');//定义操做符号类型描述 减法 define('WHITESPACE',' ');//定义空格 /** Token 用来存储输入字符的类型 */ class Token{ private $type; private $value; /** $type ISINTEGER/PLUS/MINUS $value 对应的字符串 */ public function __construct($type,$value) { $this->type=$type; $this->value=$value; } /** 经过该方法来获取类的私有属性 */ public function __get($name) { return $this->{$name}; } /** 用于调试 */ public function __toString() { return 'type:'.$this->type.' value:'.$this->value; } } //解释器 class Interpreter{ private $current_char ; private $current_token ; private $text; private $pos=0; /*** $text 须要进行解释的字符串 */ public function __construct($text){ //去除先后可能存在的空格 这些空格是无效的 $this->text=trim($text); //初始化 获取第一个字符 $this->current_char = $this->text[$this->pos]; } public function error() { throw new \Exception('Lexer eroor'); } /* 步进方法,每操做一个字符后前进一位 */ public function advance() { $this->pos++; if ($this->pos>strlen($this->text)-1){ $this->current_char=null; }else{ $this->current_char=$this->text[$this->pos]; } } /* 去除空格 */ public function skip_whitespace() { if ($this->current_char!=null&&$this->current_char==WHITESPACE){ $this->advance(); } } /* 若是要支持多位的整数,则须要将每位数字存储起来 */ public function integers() { $result='';//用于存储数字 while($this->current_char!=null&&is_numeric($this->current_char)){//只要当前字符是数字就一直循环并将数字存储于$result $result.=$this->current_char; $this->advance();//步进方法,每操做一个字符后前进一位 } return intval($result);//将数字字符串转成整数 } //获取当前字符的Token public function get_next_token() { while($this->current_char!=null){ if ($this->current_char==WHITESPACE){ $this->skip_whitespace(); continue; } if (is_numeric($this->current_char)){ return new Token(ISINTEGER,$this->integers()); } if ($this->current_char=="+"){ $this->advance(); return new Token(PLUS,'+'); } if ($this->current_char=="-"){ $this->advance(); return new Token(MINUS,'-'); } return new Token('EOF', null); } } //若是字符类型和判断的类型一致,则继续,不然输入错误 public function eat($token_type) { if ($this->current_token->type==$token_type){ $this->current_token=$this->get_next_token(); }else{ $this->error(); } } public function term() { $token=$this->current_token; $this->eat(ISINTEGER); return $token->value; } //解释方法 public function expr() { $this->current_token=$this->get_next_token();//获取字符串开头部分的数字 $result=$this->term(); while($this->current_token->type==PLUS||$this->current_token->type==MINUS){ $token=$this->current_token; if ($token->type==PLUS){ $this->eat(PLUS); $result=$result+$this->term(); } else if ($token->type==MINUS){ $this->eat(MINUS); $result=$result-$this->term(); } } return $result; } } do{ fwrite(STDOUT,'xav>');; $input=fgets(STDIN); $Interpreter=new Interpreter($input); echo $Interpreter->expr(); unset($Interpreter); }while(true);