restructure
This commit is contained in:
parent
edcbfde351
commit
eebaadc16e
21 changed files with 937 additions and 626 deletions
91
lexer/tokentype.go
Normal file
91
lexer/tokentype.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package lexer
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
var KeywordTokenTypes = []TokenType{
|
||||
TokenTypeClass,
|
||||
TokenTypeFun,
|
||||
TokenTypeVar,
|
||||
TokenTypeFor,
|
||||
TokenTypeIf,
|
||||
TokenTypeReturn,
|
||||
TokenTypeWhile,
|
||||
TokenTypePrint,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func IsKeyword(token *Token) bool {
|
||||
for _, kt := range KeywordTokenTypes {
|
||||
if token.Type == kt {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue