68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package token
|
|
|
|
// All tokens in the pseudo-lambda language.
|
|
type Type int
|
|
|
|
const (
|
|
OpenParen Type = iota // Denotes the '(' token.
|
|
CloseParen // Denotes the ')' token.
|
|
Atom // Denotes an alpha-numeric variable.
|
|
Slash // Denotes the '/' token.
|
|
Dot // Denotes the '.' token.
|
|
Newline // Denotes a new-line.
|
|
)
|
|
|
|
// A representation of a token in source code.
|
|
type Token struct {
|
|
Index int // Where the token begins in the source text.
|
|
Type Type // What type the token is.
|
|
Value string // The value of the token.
|
|
}
|
|
|
|
func NewOpenParen(index int) *Token {
|
|
return &Token{Type: OpenParen, Index: index, Value: "("}
|
|
}
|
|
|
|
func NewCloseParen(index int) *Token {
|
|
return &Token{Type: CloseParen, Index: index, Value: ")"}
|
|
}
|
|
|
|
func NewDot(index int) *Token {
|
|
return &Token{Type: Dot, Index: index, Value: "."}
|
|
}
|
|
|
|
func NewSlash(index int) *Token {
|
|
return &Token{Type: Slash, Index: index, Value: "\\"}
|
|
}
|
|
|
|
func NewAtom(name string, index int) *Token {
|
|
return &Token{Type: Atom, Index: index, Value: name}
|
|
}
|
|
|
|
func NewNewline(index int) *Token {
|
|
return &Token{Type: Newline, Index: index, Value: "\\n"}
|
|
}
|
|
|
|
func Name(typ Type) string {
|
|
switch typ {
|
|
case OpenParen:
|
|
return "("
|
|
case CloseParen:
|
|
return ")"
|
|
case Slash:
|
|
return "\\"
|
|
case Dot:
|
|
return "."
|
|
case Atom:
|
|
return "ATOM"
|
|
case Newline:
|
|
return "\\n"
|
|
default:
|
|
return "?"
|
|
}
|
|
}
|
|
|
|
func (t Token) Name() string {
|
|
return Name(t.Type)
|
|
}
|