php代码解释器,用PHP写一个最简单的解释器Part1

偶然间在朋友圈发现有人在看一本《两周自制脚本语言》,觉得写个脚本语言挺不错的,方便自己对语言本身进一步了解。于是乎,买了下来看了看,写的挺通俗易懂,但是不便的是,采用的语言是Java,PHP才是最好的语言么!为什么要采用Java。

这几日,我也在网上搜索了一些资料,发现这个不错。https://github.com/rspivak/ls...,不过同样,该教程采用的也不是PHP。正如作者所言,选什么语言由你,解释器并不依赖语言特性。

于是乎,我用PHP重写了part1的部分,并在以后几日,将会采用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);

97a710fc993cae5623007ebabb251220.png

目前仅支持个位整数相加