From fb729ece6109524d0d643f26480b82f07cf781c0 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Mon, 20 Feb 2017 09:00:14 +0100 Subject: [PATCH] cmd/easm: item -> token --- cmd/easm/lexer.go | 70 +++++++++++++++++++++++------------------------ cmd/easm/main.go | 12 ++++---- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/cmd/easm/lexer.go b/cmd/easm/lexer.go index eb46f52d46..7d69da4df9 100644 --- a/cmd/easm/lexer.go +++ b/cmd/easm/lexer.go @@ -13,29 +13,29 @@ import ( // current state. type stateFn func(*Lexer) stateFn -// item is emitted when the lexer has discovered -// a new parsable item. These are delivered over -// the items channels of the lexer -type item struct { - typ itemType +// token is emitted when the lexer has discovered +// a new parsable token. These are delivered over +// the tokens channels of the lexer +type token struct { + typ tokenType lineno int text string } -// itemType are the different types the lexer +// tokenType are the different types the lexer // is able to parse and return. -type itemType int +type tokenType int const ( - eof itemType = iota // end of file - lineStart // emitted when a line starts - lineEnd // emitted when a line ends - invalidStatement // any invalid statement - element // any element during element parsing - label // label is emitted when a labal is found - labelDef // label definition is emitted when a new label is found - number // number is emitted when a number is found - stringValue // stringValue is emitted when a string has been found + eof tokenType = iota // end of file + lineStart // emitted when a line starts + lineEnd // emitted when a line ends + invalidStatement // any invalid statement + element // any element during element parsing + label // label is emitted when a labal is found + labelDef // label definition is emitted when a new label is found + number // number is emitted when a number is found + stringValue // stringValue is emitted when a string has been found Numbers = "1234567890" // characters representing any decimal number HexadecimalNumbers = Numbers + "aAbBcCdDeEfF" // characters representing any hexadecimal @@ -43,14 +43,14 @@ const ( ) // String implements stringer -func (it itemType) String() string { - if int(it) > len(stringItemTypes) { +func (it tokenType) String() string { + if int(it) > len(stringtokenTypes) { return "invalid" } - return stringItemTypes[it] + return stringtokenTypes[it] } -var stringItemTypes = []string{ +var stringtokenTypes = []string{ eof: "EOF", invalidStatement: "invalid statement", element: "element", @@ -68,8 +68,8 @@ var stringItemTypes = []string{ type Lexer struct { input string // input contains the source code of the program - items chan item // items is used to deliver tokens to the listener - state stateFn // the current state function + tokens chan token // tokens is used to deliver tokens to the listener + state stateFn // the current state function lineno int // current line number in the source file start, pos, width int // positions for lexing and returning value @@ -78,14 +78,14 @@ type Lexer struct { } // lex lexes the program by name with the given source. It returns a -// channel on which the items are delivered. -func lex(name string, source []byte, debug bool) <-chan item { - ch := make(chan item) +// channel on which the tokens are delivered. +func lex(name string, source []byte, debug bool) <-chan token { + ch := make(chan token) l := &Lexer{ - input: string(source), - items: ch, - state: lexLine, - debug: debug, + input: string(source), + tokens: ch, + state: lexLine, + debug: debug, } go func() { l.emit(lineStart) @@ -93,7 +93,7 @@ func lex(name string, source []byte, debug bool) <-chan item { l.state = l.state(l) } l.emit(eof) - close(l.items) + close(l.tokens) }() return ch @@ -164,15 +164,15 @@ func (l *Lexer) blob() string { return l.input[l.start:l.pos] } -// Emits a new item on to item channel for processing -func (l *Lexer) emit(t itemType) { - item := item{t, l.lineno, l.blob()} +// Emits a new token on to token channel for processing +func (l *Lexer) emit(t tokenType) { + token := token{t, l.lineno, l.blob()} if l.debug { - fmt.Fprintf(os.Stderr, "%04d: (%-20v) %s\n", item.lineno, item.typ, item.text) + fmt.Fprintf(os.Stderr, "%04d: (%-20v) %s\n", token.lineno, token.typ, token.text) } - l.items <- item + l.tokens <- token l.start = l.pos } diff --git a/cmd/easm/main.go b/cmd/easm/main.go index e8538d0ebc..a13039c167 100644 --- a/cmd/easm/main.go +++ b/cmd/easm/main.go @@ -82,7 +82,7 @@ func main() { // Compiler contains information about the parsed source // and holds the tokens for the program. type Compiler struct { - tokens []item + tokens []token binary []interface{} labels map[string]int @@ -109,7 +109,7 @@ func newCompiler(debug bool) *Compiler { // of the jump dests. The labels can than be used in the // second stage to push labels and determine the right // position. -func (c *Compiler) feed(ch <-chan item) { +func (c *Compiler) feed(ch <-chan token) { for i := range ch { switch i.typ { case number: @@ -167,7 +167,7 @@ func (c *Compiler) compile() (string, []error) { // next returns the next token and increments the // posititon. -func (c *Compiler) next() item { +func (c *Compiler) next() token { token := c.tokens[c.pos] c.pos++ return token @@ -205,7 +205,7 @@ func (c *Compiler) compileLine() error { } // compileNumber compiles the number to bytes -func (c *Compiler) compileNumber(element item) (int, error) { +func (c *Compiler) compileNumber(element token) (int, error) { num := common.String2Big(element.text).Bytes() if len(num) == 0 { num = []byte{0} @@ -217,7 +217,7 @@ func (c *Compiler) compileNumber(element item) (int, error) { // compileElement compiles the element (push & label or both) // to a binary representation and may error if incorrect statements // where fed. -func (c *Compiler) compileElement(element item) error { +func (c *Compiler) compileElement(element token) error { // check for a jump. jumps must be read and compiled // from right to left. if isJump(element.text) { @@ -324,7 +324,7 @@ var ( errExpElementOrLabel = errors.New("expected beginning of line") ) -func compileErr(c item, got, want string) error { +func compileErr(c token, got, want string) error { return compileError{ got: got, want: want,