摘要:大家對解釋器的吸引,絕對沒有自己動手寫一個腳本語言更有吸引力。前幾篇文章已經(jīng)實現(xiàn)一個簡易的加減法計算器,想必看了文章之后可以很快的寫出來一個乘除法計算器。
好吧!我承認(rèn)我想標(biāo)題黨了。大家對解釋器的吸引,絕對沒有自己動手寫一個腳本語言更有吸引力。不過如果看到標(biāo)題過來的,可能也是
我承認(rèn),之前收藏的減肥視頻,我都是這樣對待他們的。
不過我還是相信很多程序猿or程序媛不僅僅希望可以做出一個牛逼的產(chǎn)品,更想做出來一個牛逼閃閃的編程語言。而這里就是朝著開發(fā)一個腳本語言的方向去努力
恩!我就是xxoo語言之父。
前幾篇文章已經(jīng)實現(xiàn)一個簡易的加減法計算器,想必看了文章之后可以很快的寫出來一個乘除法計算器。那么如果加減乘除混合運算呢?
這節(jié)將實現(xiàn)類似6-1*2+4/2這樣的運算方式,大家看到這個式子之后應(yīng)該已經(jīng)了解難點在哪里了。好了下面開始,首先展示完整的代碼。
type=$type; $this->value=$value; } /** 通過該方法來獲取類的私有屬性 */ public function __get($name) { return $this->{$name}; } /** 用于調(diào)試 */ public function __toString() { return "type:".$this->type." value:".$this->value; } } class Lexer{ private $current_char ; private $current_token ; private $text; private $pos=0; /*** $text 需要進(jìn)行解釋的字符串 */ public function __construct($text){ //去除前后可能存在的空格 這些空格是無效的 $this->text=trim($text); //初始化 獲取第一個字符 $this->current_char = $this->text[$this->pos]; } public function error() { throw new Exception("Lexer eroor"); } /* 步進(jìn)方法,每操作一個字符后前進(jìn)一位 */ 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(); } } /* 如果要支持多位的整數(shù),則需要將每位數(shù)字存儲起來 */ public function integers() { $result="";//用于存儲數(shù)字 while($this->current_char!=null&&is_numeric($this->current_char)){//只要當(dāng)前字符是數(shù)字就一直循環(huán)并將數(shù)字存儲于$result $result.=$this->current_char; $this->advance();//步進(jìn)方法,每操作一個字符后前進(jìn)一位 } return intval($result);//將數(shù)字字符串轉(zhuǎn)成整數(shù) } //獲取當(dāng)前字符的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,"-"); } if ($this->current_char=="*"){ $this->advance(); return new Token(MUL,"*"); } if ($this->current_char=="/"){ $this->advance(); return new Token(DIV,"/"); } return new Token("EOF", null); } } } //解釋器 class Interpreter{ private $current_token ; private $lexer ; public function __construct($lexer){ //去除前后可能存在的空格 這些空格是無效的 $this->lexer=$lexer; //初始化 獲取第一個字符 $this->current_token=$this->lexer->get_next_token(); } //如果字符類型和判斷的類型一致,則繼續(xù),否則輸入錯誤 public function eat($token_type) { if ($this->current_token->type==$token_type){ $this->current_token=$this->lexer->get_next_token(); }else{ $this->error(); } } public function error() { throw new Exception("eroor"); } public function factor() { $token=$this->current_token; $this->eat(ISINTEGER); return $token->value; } public function term() { $result=$this->factor(); while(in_array($this->current_token->type,[MUL,DIV])){ $token=$this->current_token; if ($token->type==MUL){ $this->eat(MUL); $result=$result*$this->factor(); } else if ($token->type==DIV){ $this->eat(DIV); $result=$result/$this->factor(); } } return $result; } //解釋方法 public function expr() { $result=$this->term(); while(in_array($this->current_token->type,[PLUS,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(new Lexer($input)); echo $Interpreter->expr(); unset($Interpreter); }while(true);
看過前面幾篇文章的已經(jīng)了解過,這里的代碼結(jié)構(gòu)發(fā)生了變化。
首先,分離出來詞法分析類,該類的作用:將字符串進(jìn)行類似分詞處理,并將每個詞的含義返回
class Lexer{ private $current_char ; private $current_token ; private $text; private $pos=0; /*** $text 需要進(jìn)行解釋的字符串 */ public function __construct($text){ //去除前后可能存在的空格 這些空格是無效的 $this->text=trim($text); //初始化 獲取第一個字符 $this->current_char = $this->text[$this->pos]; } public function error() { throw new Exception("Lexer eroor"); } /* 步進(jìn)方法,每操作一個字符后前進(jìn)一位 */ 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(); } } /* 如果要支持多位的整數(shù),則需要將每位數(shù)字存儲起來 */ public function integers() { $result="";//用于存儲數(shù)字 while($this->current_char!=null&&is_numeric($this->current_char)){//只要當(dāng)前字符是數(shù)字就一直循環(huán)并將數(shù)字存儲于$result $result.=$this->current_char; $this->advance();//步進(jìn)方法,每操作一個字符后前進(jìn)一位 } return intval($result);//將數(shù)字字符串轉(zhuǎn)成整數(shù) } //獲取當(dāng)前字符的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,"-"); } if ($this->current_char=="*"){ $this->advance(); return new Token(MUL,"*"); } if ($this->current_char=="/"){ $this->advance(); return new Token(DIV,"/"); } return new Token("EOF", null); } } }
其實四則運算最為復(fù)雜的部分就是如果解決先乘除后加減。這里將其分成了兩個部分。term函數(shù)就是在進(jìn)行乘除法部分,expr則進(jìn)行加法
public function term() { $result=$this->factor(); while(in_array($this->current_token->type,[MUL,DIV])){ $token=$this->current_token; if ($token->type==MUL){ $this->eat(MUL); $result=$result*$this->factor(); } else if ($token->type==DIV){ $this->eat(DIV); $result=$result/$this->factor(); } } return $result; } //解釋方法 public function expr() { $result=$this->term(); while(in_array($this->current_token->type,[PLUS,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; }
圖很丑,不過還是希望你可以看懂,也就是4-2*3-1 ,expr中乘除都是一個整體,其會被在term中進(jìn)行運算返回,這樣這個式子就分成了,4-積-1.而積是在term中進(jìn)行運算的。
文中很多描述存在很多不當(dāng),我也在積極的學(xué)習(xí)如何將知識講的通俗易懂。如果您有好的辦法,可以一同探討
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/29037.html
摘要:經(jīng)過幾天的努力,用已經(jīng)實現(xiàn)了一個完整的基礎(chǔ)計算器,如下圖上代碼定義整數(shù)類型描述定義操作符號類型描述加法定義操作符號類型描述減法定義操作符號類型描述乘法定義操作符號類型描述除法定義操作符號類型描述定義操作符號類型描述定義空格用來存儲輸入字符的 showImg(https://segmentfault.com/img/bVbdNO5?w=900&h=377); 經(jīng)過幾天的努力,用PHP已經(jīng)...
摘要:偶然間在朋友圈發(fā)現(xiàn)有人在看一本兩周自制腳本語言,覺得寫個腳本語言挺不錯的,方便自己對語言本身進(jìn)一步了解。,不過同樣,該教程采用的也不是。在這里寫出代碼方便自己查找,同時也希望一些對解釋器感興趣的朋友一同學(xué)習(xí)。 偶然間在朋友圈發(fā)現(xiàn)有人在看一本《兩周自制腳本語言》,覺得寫個腳本語言挺不錯的,方便自己對語言本身進(jìn)一步了解。于是乎,買了下來看了看,寫的挺通俗易懂,但是不便的是,采用的語言是Ja...
摘要:入門,第一個這是一門很新的語言,年前后正式公布,算起來是比較年輕的編程語言了,更重要的是它是面向程序員的函數(shù)式編程語言,它的代碼運行在之上。它通過編輯類工具,帶來了先進(jìn)的編輯體驗,增強(qiáng)了語言服務(wù)。 showImg(https://segmentfault.com/img/bV1xdq?w=900&h=385); 新的一年不知不覺已經(jīng)到來了,總結(jié)過去的 2017,相信小伙們一定有很多收獲...
閱讀 1690·2021-11-15 11:37
閱讀 3424·2021-09-28 09:44
閱讀 1678·2021-09-07 10:15
閱讀 2802·2021-09-03 10:39
閱讀 2698·2019-08-29 13:20
閱讀 1306·2019-08-29 12:51
閱讀 2215·2019-08-26 13:44
閱讀 2137·2019-08-23 18:02