偶然间在朋友圈发现有人在看一本《两周自制脚本语言》,以为写个脚本语言挺不错的,方便本身对语言自己进一步了解。因而乎,买了下来看了看,写的挺通俗易懂,可是不便的是,采用的语言是Java,PHP才是最好的语言么!为何要采用Java。php
这几日,我也在网上搜索了一些资料,发现这个不错。https://github.com/rspivak/ls...,不过一样,该教程采用的也不是PHP。正如做者所言,选什么语言由你,解释器并不依赖语言特性。git
因而乎,我用PHP重写了part1的部分,并在之后几日,将会采用PHP重写全部部分。github
在这里写出代码方便本身查找,同时也但愿一些对解释器感兴趣的朋友一同窗习。学习
<?php class Token{ private $type; private $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; public function __construct($text){ $this->text=trim($text); } public function error() { throw new \Exception('Lexer eroor'); } public function get_next_token() { $text=$this->text; if ($this->pos > strlen($text)-1){ return new Token('EOF', null); } $this->current_char = $text[$this->pos]; if (is_numeric($this->current_char)){ $token=new Token('INTEGER',intval($this->current_char)); $this->pos++; return $token; } if ($this->current_char=="+"){ $token = new Token('PLUS', $this->current_char); $this->pos ++; return $token; } $this->error(); } public function eat($token_type) { if ($this->current_token->type==$token_type){ $this->current_token=$this->get_next_token(); }else{ $this->error(); } } public function expr() { $this->current_token=$this->get_next_token(); $left=$this->current_token; $this->eat('INTEGER'); $op=$this->current_token; $this->eat('PLUS'); $right=$this->current_token; $this->eat('INTEGER'); $result=$left->value+$right->value; return $result; } } do{ fwrite(STDOUT,'xav>');; $input=fgets(STDIN); $Interpreter=new Interpreter($input); echo $Interpreter->expr(); unset($Interpreter); }while(true);
目前仅支持个位整数相加this