92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package saccharine
|
|
|
|
import "fmt"
|
|
|
|
// All tokens in the pseudo-lambda language.
|
|
type TokenType int
|
|
|
|
const (
|
|
TokenOpenParen TokenType = iota // Denotes the '(' token.
|
|
TokenCloseParen // Denotes the ')' token.
|
|
TokenOpenBrace // Denotes the '{' token.
|
|
TokenCloseBrace // Denotes the '}' token.
|
|
TokenHardBreak // Denotes the ';' token.
|
|
TokenAssign // Denotes the ':=' token.
|
|
TokenAtom // Denotes an alpha-numeric variable.
|
|
TokenSlash // Denotes the '/' token.
|
|
TokenDot // Denotes the '.' token.
|
|
TokenSoftBreak // Denotes a new-line.
|
|
)
|
|
|
|
// A representation of a token in source code.
|
|
type Token struct {
|
|
Column int // Where the token begins in the source text.
|
|
Type TokenType // What type the token is.
|
|
Value string // The value of the token.
|
|
}
|
|
|
|
func NewTokenOpenParen(column int) *Token {
|
|
return &Token{Type: TokenOpenParen, Column: column, Value: "("}
|
|
}
|
|
|
|
func NewTokenCloseParen(column int) *Token {
|
|
return &Token{Type: TokenCloseParen, Column: column, Value: ")"}
|
|
}
|
|
|
|
func NewTokenOpenBrace(column int) *Token {
|
|
return &Token{Type: TokenOpenBrace, Column: column, Value: "{"}
|
|
}
|
|
|
|
func NewTokenCloseBrace(column int) *Token {
|
|
return &Token{Type: TokenCloseBrace, Column: column, Value: "}"}
|
|
}
|
|
|
|
func NewTokenDot(column int) *Token {
|
|
return &Token{Type: TokenDot, Column: column, Value: "."}
|
|
}
|
|
|
|
func NewTokenHardBreak(column int) *Token {
|
|
return &Token{Type: TokenHardBreak, Column: column, Value: ";"}
|
|
}
|
|
|
|
func NewTokenAssign(column int) *Token {
|
|
return &Token{Type: TokenAssign, Column: column, Value: ":="}
|
|
}
|
|
|
|
func NewTokenSlash(column int) *Token {
|
|
return &Token{Type: TokenSlash, Column: column, Value: "\\"}
|
|
}
|
|
|
|
func NewTokenAtom(name string, column int) *Token {
|
|
return &Token{Type: TokenAtom, Column: column, Value: name}
|
|
}
|
|
|
|
func NewTokenSoftBreak(column int) *Token {
|
|
return &Token{Type: TokenSoftBreak, Column: column, Value: "\\n"}
|
|
}
|
|
|
|
func (t TokenType) Name() string {
|
|
switch t {
|
|
case TokenOpenParen:
|
|
return "("
|
|
case TokenCloseParen:
|
|
return ")"
|
|
case TokenSlash:
|
|
return "\\"
|
|
case TokenDot:
|
|
return "."
|
|
case TokenAtom:
|
|
return "ATOM"
|
|
case TokenSoftBreak:
|
|
return "\\n"
|
|
case TokenHardBreak:
|
|
return ";"
|
|
default:
|
|
panic(fmt.Errorf("unknown token type %v", t))
|
|
}
|
|
}
|
|
|
|
func (t Token) Name() string {
|
|
return t.Type.Name()
|
|
}
|