init + lexer

This commit is contained in:
basil 2025-06-07 17:24:28 -04:00
commit 9bebc6e307
Signed by: basil
SSH key fingerprint: SHA256:y04xIFL/yqNaG9ae9Vl95vELtHfApGAIoOGLeVLP/fE
5 changed files with 493 additions and 0 deletions

71
tokentype.go Normal file
View 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)
}