Categories
Microservice

write a simple calculator part2

The Tokens

tokens are the basic blocks of an expression. In this part will show you How to group the different symbols to the tokens.

token types

  • Left parenthesis
  • Right parenthesis
  • Number
  • Plus(+)
  • Minus(-)
  • Star(*)
  • Slash(/)
type tokenType int

const (
	leftParent  tokenType = iota // (
	rightParent                  // )
	minus                        // -
	plus                         // +
	slash                        // "/"
	star                         // '*'
	number                       // 123, 9, 10
	eof                          // end token type
)

token scanner

we will define a scanner struct to scan and collect the tokens.

type token struct {
	TkType  tokenType
	Lexeme  string
	Literal interface{}
}

type scanner struct {
	exp     string
	runes   []rune // the chars in the expression
	tokens  []*token // the tokens collected from expression
	start   int // scan start from
	current int // current scan index
}

How the scan works

Assume we have an expression: (1 + 2) * (9 / 3) - 5

First, we split the expression string into runes.

func newScanner(exp string) *scanner {
	return &scanner{
		exp:    exp,
		runes:  []rune(exp),
		tokens: make([]*token, 0, 10),
	}
}

Runes

index0123456789101112
rune(1+2)*(9/3)5

Then we scan through the rune slice, util end of the slice.

Leave a Reply

Your email address will not be published. Required fields are marked *