mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
cmd/easm: item -> token
This commit is contained in:
parent
22457f8cd8
commit
fb729ece61
2 changed files with 41 additions and 41 deletions
|
|
@ -13,21 +13,21 @@ import (
|
||||||
// current state.
|
// current state.
|
||||||
type stateFn func(*Lexer) stateFn
|
type stateFn func(*Lexer) stateFn
|
||||||
|
|
||||||
// item is emitted when the lexer has discovered
|
// token is emitted when the lexer has discovered
|
||||||
// a new parsable item. These are delivered over
|
// a new parsable token. These are delivered over
|
||||||
// the items channels of the lexer
|
// the tokens channels of the lexer
|
||||||
type item struct {
|
type token struct {
|
||||||
typ itemType
|
typ tokenType
|
||||||
lineno int
|
lineno int
|
||||||
text string
|
text string
|
||||||
}
|
}
|
||||||
|
|
||||||
// itemType are the different types the lexer
|
// tokenType are the different types the lexer
|
||||||
// is able to parse and return.
|
// is able to parse and return.
|
||||||
type itemType int
|
type tokenType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
eof itemType = iota // end of file
|
eof tokenType = iota // end of file
|
||||||
lineStart // emitted when a line starts
|
lineStart // emitted when a line starts
|
||||||
lineEnd // emitted when a line ends
|
lineEnd // emitted when a line ends
|
||||||
invalidStatement // any invalid statement
|
invalidStatement // any invalid statement
|
||||||
|
|
@ -43,14 +43,14 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// String implements stringer
|
// String implements stringer
|
||||||
func (it itemType) String() string {
|
func (it tokenType) String() string {
|
||||||
if int(it) > len(stringItemTypes) {
|
if int(it) > len(stringtokenTypes) {
|
||||||
return "invalid"
|
return "invalid"
|
||||||
}
|
}
|
||||||
return stringItemTypes[it]
|
return stringtokenTypes[it]
|
||||||
}
|
}
|
||||||
|
|
||||||
var stringItemTypes = []string{
|
var stringtokenTypes = []string{
|
||||||
eof: "EOF",
|
eof: "EOF",
|
||||||
invalidStatement: "invalid statement",
|
invalidStatement: "invalid statement",
|
||||||
element: "element",
|
element: "element",
|
||||||
|
|
@ -68,7 +68,7 @@ var stringItemTypes = []string{
|
||||||
type Lexer struct {
|
type Lexer struct {
|
||||||
input string // input contains the source code of the program
|
input string // input contains the source code of the program
|
||||||
|
|
||||||
items chan item // items is used to deliver tokens to the listener
|
tokens chan token // tokens is used to deliver tokens to the listener
|
||||||
state stateFn // the current state function
|
state stateFn // the current state function
|
||||||
|
|
||||||
lineno int // current line number in the source file
|
lineno int // current line number in the source file
|
||||||
|
|
@ -78,12 +78,12 @@ type Lexer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// lex lexes the program by name with the given source. It returns a
|
// lex lexes the program by name with the given source. It returns a
|
||||||
// channel on which the items are delivered.
|
// channel on which the tokens are delivered.
|
||||||
func lex(name string, source []byte, debug bool) <-chan item {
|
func lex(name string, source []byte, debug bool) <-chan token {
|
||||||
ch := make(chan item)
|
ch := make(chan token)
|
||||||
l := &Lexer{
|
l := &Lexer{
|
||||||
input: string(source),
|
input: string(source),
|
||||||
items: ch,
|
tokens: ch,
|
||||||
state: lexLine,
|
state: lexLine,
|
||||||
debug: debug,
|
debug: debug,
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +93,7 @@ func lex(name string, source []byte, debug bool) <-chan item {
|
||||||
l.state = l.state(l)
|
l.state = l.state(l)
|
||||||
}
|
}
|
||||||
l.emit(eof)
|
l.emit(eof)
|
||||||
close(l.items)
|
close(l.tokens)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return ch
|
return ch
|
||||||
|
|
@ -164,15 +164,15 @@ func (l *Lexer) blob() string {
|
||||||
return l.input[l.start:l.pos]
|
return l.input[l.start:l.pos]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Emits a new item on to item channel for processing
|
// Emits a new token on to token channel for processing
|
||||||
func (l *Lexer) emit(t itemType) {
|
func (l *Lexer) emit(t tokenType) {
|
||||||
item := item{t, l.lineno, l.blob()}
|
token := token{t, l.lineno, l.blob()}
|
||||||
|
|
||||||
if l.debug {
|
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
|
l.start = l.pos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,7 @@ func main() {
|
||||||
// Compiler contains information about the parsed source
|
// Compiler contains information about the parsed source
|
||||||
// and holds the tokens for the program.
|
// and holds the tokens for the program.
|
||||||
type Compiler struct {
|
type Compiler struct {
|
||||||
tokens []item
|
tokens []token
|
||||||
binary []interface{}
|
binary []interface{}
|
||||||
|
|
||||||
labels map[string]int
|
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
|
// of the jump dests. The labels can than be used in the
|
||||||
// second stage to push labels and determine the right
|
// second stage to push labels and determine the right
|
||||||
// position.
|
// position.
|
||||||
func (c *Compiler) feed(ch <-chan item) {
|
func (c *Compiler) feed(ch <-chan token) {
|
||||||
for i := range ch {
|
for i := range ch {
|
||||||
switch i.typ {
|
switch i.typ {
|
||||||
case number:
|
case number:
|
||||||
|
|
@ -167,7 +167,7 @@ func (c *Compiler) compile() (string, []error) {
|
||||||
|
|
||||||
// next returns the next token and increments the
|
// next returns the next token and increments the
|
||||||
// posititon.
|
// posititon.
|
||||||
func (c *Compiler) next() item {
|
func (c *Compiler) next() token {
|
||||||
token := c.tokens[c.pos]
|
token := c.tokens[c.pos]
|
||||||
c.pos++
|
c.pos++
|
||||||
return token
|
return token
|
||||||
|
|
@ -205,7 +205,7 @@ func (c *Compiler) compileLine() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// compileNumber compiles the number to bytes
|
// 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()
|
num := common.String2Big(element.text).Bytes()
|
||||||
if len(num) == 0 {
|
if len(num) == 0 {
|
||||||
num = []byte{0}
|
num = []byte{0}
|
||||||
|
|
@ -217,7 +217,7 @@ func (c *Compiler) compileNumber(element item) (int, error) {
|
||||||
// compileElement compiles the element (push & label or both)
|
// compileElement compiles the element (push & label or both)
|
||||||
// to a binary representation and may error if incorrect statements
|
// to a binary representation and may error if incorrect statements
|
||||||
// where fed.
|
// 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
|
// check for a jump. jumps must be read and compiled
|
||||||
// from right to left.
|
// from right to left.
|
||||||
if isJump(element.text) {
|
if isJump(element.text) {
|
||||||
|
|
@ -324,7 +324,7 @@ var (
|
||||||
errExpElementOrLabel = errors.New("expected beginning of line")
|
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{
|
return compileError{
|
||||||
got: got,
|
got: got,
|
||||||
want: want,
|
want: want,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue