init + lexer
This commit is contained in:
commit
9bebc6e307
5 changed files with 493 additions and 0 deletions
71
tokentype.go
Normal file
71
tokentype.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
//go:generate stringer -type tokenType -linecomment -trimprefix tokenType
|
||||
type tokenType int
|
||||
|
||||
const (
|
||||
// single char tokens
|
||||
|
||||
tokenTypeLeftParen tokenType = iota
|
||||
tokenTypeRightParen
|
||||
tokenTypeLeftBrace
|
||||
tokenTypeRightBrace
|
||||
tokenTypeComma
|
||||
tokenTypeDot
|
||||
tokenTypeMinus
|
||||
tokenTypePlus
|
||||
tokenTypeSemicolon
|
||||
tokenTypeSlash
|
||||
tokenTypeStar
|
||||
|
||||
// 1-2 char token
|
||||
|
||||
tokenTypeBang
|
||||
tokenTypeBangEq
|
||||
tokenTypeEqual
|
||||
tokenTypeEqualEqual
|
||||
tokenTypeGreater
|
||||
tokenTypeGreaterEq
|
||||
tokenTypeLess
|
||||
tokenTypeLessEq
|
||||
|
||||
// literals
|
||||
|
||||
tokenTypeIdentifier
|
||||
tokenTypeString
|
||||
tokenTypeNumber
|
||||
|
||||
// keywords
|
||||
|
||||
tokenTypeAnd
|
||||
tokenTypeClass
|
||||
tokenTypeElse
|
||||
tokenTypeFalse
|
||||
tokenTypeFun
|
||||
tokenTypeFor
|
||||
tokenTypeIf
|
||||
tokenTypeNil
|
||||
tokenTypeOr
|
||||
tokenTypePrint
|
||||
tokenTypeReturn
|
||||
tokenTypeSuper
|
||||
tokenTypeThis
|
||||
tokenTypeTrue
|
||||
tokenTypeVar
|
||||
tokenTypeWhile
|
||||
|
||||
tokenTypeEOF
|
||||
)
|
||||
|
||||
type token struct {
|
||||
Type tokenType
|
||||
Lexeme string
|
||||
Literal any
|
||||
Line int
|
||||
}
|
||||
|
||||
func (t token) String() string {
|
||||
return fmt.Sprintf("%s %s %+v", t.Type, t.Lexeme, t.Literal)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue