1. 程式人生 > >《Let's Build A Simple Interpreter》之 Golang 版

《Let's Build A Simple Interpreter》之 Golang 版

OS advance esp n) indicate javascrip value 龍書 use

  一直以來對編譯器/解釋器等都較有興趣。我非科班出身,當初還在大學時,只是馬馬虎虎看完了《編譯原理》之類教材,上機非常少,對龍書之類聖經也只是淺嘗輒止而已。工作至今,基本已將編譯原理相關知識忘記得差不多了,可能也就還對譬如預處理詞法分析語法分析 AST 生成等基礎性的概念還有點印象罷。

  約 1 年多前,我也有想法搞一套基於簡化的 Pascal 語法的帶類型的腳本語言“編譯器”(PaxCompiler 之類可能太復雜了),並將此腳本語言編寫的腳本與 Golang 交互起來。當然這只是我個人的業余興趣而已,至於是否會付諸行動、能搞成怎樣都是未知。而選擇 Pascal 作為參考改編語言的原因,其一我比較喜歡它的語言設計,其二它曾是我某段時間內的工作語言所以感情成分使然,其三較之諸如 Python、Lua 我更喜歡帶類型的腳本語言(TypeScript?我不太喜歡 JavaScript 的語法...),當然,Pascal 的語法形式也確實比較方便為之開發編譯器/解釋器。

  而短期內,個人恐怕沒有太多精力去啃龍書之類,於是索性,看點基礎資料且按此系列教程之類慢慢溫習並從 tokenizer 開始一步步實現自己的 EcoPascal——即便最終,它只是個玩具腳本語言而已。

  近 2 天趁有空,粗略看了前文所述教程的前兩章,並以 Golang 重寫了這兩章裏的解釋程序(代碼寫得有些粗放)。

  第一章:

package interpreter

import (
	"fmt"
)

// Token types
//
// EOF (end-of-file) token is used to indicate that
// there is no more input left for lexical analysis
type TokenType int

const (
	cTokenTypeOfNone TokenType = iota
	cTokenTypeOfInteger
	cTokenTypeOfPlusSign
	cTokenTypeOfEOF
)

type token struct {
	t TokenType   // token type: INTEGER, PLUS, or EOF
	v interface{} // token value: 0, 1, 2. 3, 4, 5, 6, 7, 8, 9, ‘+‘, or None
}

func newToken(t TokenType, v interface{}) token {
	return token{
		t: t,
		v: v,
	}
}

type Interpreter struct {
	text      []rune // client string input, e.g. "3+5"
	pos       int    // an index into text
	currToken token  // current token instance
}

func New() *Interpreter {
	return &Interpreter{
		text:      []rune(""),
		pos:       0,
		currToken: newToken(cTokenTypeOfNone, nil),
	}
}

func convToDigit(c rune) (int, bool) {
	if ‘0‘ <= c && c <= ‘9‘ {
		return int(c - ‘0‘), true
	}
	return 0, false
}

// Lexical analyzer (also known as scanner or tokenizer)
//
// This method is responsible for breaking a sentence apart into tokens.
// One token at a time.
func (self *Interpreter) getNextToken() token {
	text := self.text

	// is self.pos index past the end of the self.text ?
	// if so, then return EOF token because there is no more
	// input left to convert into tokens
	if self.pos > len(text)-1 {
		return newToken(cTokenTypeOfEOF, nil)
	}

	// get a character at the position self.pos and decide
	// what token to create based on the single character
	// var currChar interface{} = text[self.pos]
	currChar := text[self.pos]

	// if the character is a digit then convert it to
	// integer, create an INTEGER token, increment self.pos
	// index to point to the next character after the digit,
	// and return the INTEGER token
	if v, ok := convToDigit(text[self.pos]); ok {
		self.pos += 1
		return newToken(cTokenTypeOfInteger, v)
	}

	if currChar == ‘+‘ {
		self.pos += 1
		return newToken(cTokenTypeOfPlusSign, ‘+‘)
	}

	panic(fmt.Sprintf("Error parsing input1: %s", string(self.text)))
}

// compare the current token type with the passed token type
// and if they match then "eat" the current token
// and assign the next token to the self.currToken,
// otherwise raise an exception.
func (self *Interpreter) eat(tokenType TokenType) {
	if self.currToken.t == tokenType {
		self.currToken = self.getNextToken()
		return
	}

	panic(fmt.Sprintf("Error parsing input: %s", self.text))
}

// parse "INTEGER PLUS INTEGER"
func (self *Interpreter) Parse(s string) int {
	self.text = []rune(s)
	self.pos = 0

	// set current token to the first token taken from the input
	self.currToken = self.getNextToken()

	// we expect the current token to be a single-digit integer
	left := self.currToken
	self.eat(cTokenTypeOfInteger)

	// we expect the current token to be a ‘+‘ token
	// op := self.currToken
	self.eat(cTokenTypeOfPlusSign)

	// we expect the current token to be a single-digit integer
	right := self.currToken
	self.eat(cTokenTypeOfInteger)

	// after the above call the self.current_token is set to EOF token.
	// at this point INTEGER PLUS INTEGER sequence of tokens
	// has been successfully found and the method can just
	// return the result of adding two integers, thus
	// effectively interpreting client input
	return left.v.(int) + right.v.(int)
}

  第二章:

package interpreter

import (
	"fmt"

	"github.com/ecofast/rtl/sysutils"
)

// Token types
//
// EOF (end-of-file) token is used to indicate that
// there is no more input left for lexical analysis
type TokenType int

const (
	cTokenTypeOfNone TokenType = iota
	cTokenTypeOfInteger
	cTokenTypeOfPlusSign
	cTokenTypeOfMinusSign
	cTokenTypeOfEOF
)

type token struct {
	t TokenType   // token type: INTEGER, PLUS, MINUS, or EOF
	v interface{} // token value: non-negative integer value, ‘+‘, ‘-‘, or None
}

func newToken(t TokenType, v interface{}) token {
	return token{
		t: t,
		v: v,
	}
}

type Interpreter struct {
	text      []rune // client string input, e.g. "3 + 5", "12 - 5", etc
	pos       int    // an index into text
	currToken token  // current token instance
	currChar  rune
}

func New() *Interpreter {
	return &Interpreter{
		text:      []rune(""),
		pos:       0,
		currToken: newToken(cTokenTypeOfNone, nil),
		currChar:  0,
	}
}

func isDigit(c rune) bool {
	if ‘0‘ <= c && c <= ‘9‘ {
		return true
	}
	return false
}

func convToDigit(c rune) (int, bool) {
	if ‘0‘ <= c && c <= ‘9‘ {
		return int(c - ‘0‘), true
	}
	return 0, false
}

func isSpace(c rune) bool {
	if ‘ ‘ == c {
		return true
	}
	return false
}

// Advance the ‘pos‘ pointer and set the ‘currChar‘ variable
func (self *Interpreter) advance() {
	self.pos += 1
	if self.pos > len(self.text)-1 {
		self.currChar = 0
	} else {
		self.currChar = self.text[self.pos]
	}
}

func (self *Interpreter) skipWhiteSpace() {
	for self.currChar != 0 && isSpace(self.currChar) {
		self.advance()
	}
}

// Return a (multidigit) integer consumed from the input
func (self *Interpreter) integer() int {
	ret := ""
	for self.currChar != 0 && isDigit(self.currChar) {
		ret += string(self.currChar)
		self.advance()
	}
	return sysutils.StrToInt(ret)
}

// Lexical analyzer (also known as scanner or tokenizer)
//
// This method is responsible for breaking a sentence apart into tokens.
func (self *Interpreter) getNextToken() token {
	for self.currChar != 0 {
		if isSpace(self.currChar) {
			self.skipWhiteSpace()
			continue
		}

		if isDigit(self.currChar) {
			return newToken(cTokenTypeOfInteger, self.integer())
		}

		if self.currChar == ‘+‘ {
			self.advance()
			return newToken(cTokenTypeOfPlusSign, ‘+‘)
		}

		if self.currChar == ‘-‘ {
			self.advance()
			return newToken(cTokenTypeOfMinusSign, ‘-‘)
		}

		panic(fmt.Sprintf("Error parsing input1: %s", string(self.text)))
	}
	return newToken(cTokenTypeOfEOF, nil)
}

// compare the current token type with the passed token type
// and if they match then "eat" the current token
// and assign the next token to the self.currToken,
// otherwise raise an exception.
func (self *Interpreter) eat(tokenType TokenType) {
	if self.currToken.t == tokenType {
		self.currToken = self.getNextToken()
		return
	}

	panic(fmt.Sprintf("Error parsing input: %s", self.text))
}

// parse "INTEGER PLUS INTEGER" or "INTEGER MINUS INTEGER"
func (self *Interpreter) Parse(s string) int {
	self.text = []rune(s)
	self.pos = 0
	self.currChar = self.text[self.pos]

	// set current token to the first token taken from the input
	self.currToken = self.getNextToken()

	// we expect the current token to be an integer
	left := self.currToken
	self.eat(cTokenTypeOfInteger)

	// we expect the current token to be either a ‘+‘ or ‘-‘
	op := self.currToken
	if op.t == cTokenTypeOfPlusSign {
		self.eat(cTokenTypeOfPlusSign)
	} else {
		self.eat(cTokenTypeOfMinusSign)
	}

	// we expect the current token to be an integer
	right := self.currToken
	self.eat(cTokenTypeOfInteger)

	// after the above call the self.current_token is set to EOF token.
	// at this point either the INTEGER PLUS INTEGER or
	// the INTEGER MINUS INTEGER sequence of tokens
	// has been successfully found and the method can just
	// return the result of adding or subtracting two integers, thus
	// effectively interpreting client input
	if op.t == cTokenTypeOfPlusSign {
		return left.v.(int) + right.v.(int)
	}
	return left.v.(int) - right.v.(int)
}

  有了“核心”解釋程序,使用起來就很簡單了:

package main

import (
	"fmt"
	"part1/interpreter"
)

func main() {
	fmt.Println("Let‘s Build A Simple Interpreter - Part 1")

	parser := interpreter.New()
	s := ""
	for {
		if n, err := fmt.Scan(&s); n == 0 || err != nil {
			return
		}
		fmt.Println(parser.Parse(s))
	}
}

  

  本興趣項目已托管至 Github,比較可能會不定期慢慢更新。

《Let's Build A Simple Interpreter》之 Golang 版