## Summary - Added doc comments across the codebase: `pkg/lambda`, `pkg/saccharine`, `pkg/codec`, `pkg/engine`, `pkg/iterator`, `pkg/set`, `pkg/convert`, `internal/registry`, and `cmd/lambda`. - Made lambda and saccharine expression structs use public fields instead of getters, matching `go/ast` conventions. - Removed superfluous constructors for saccharine and lambda expression/statement types in favor of struct literals. - Consolidated saccharine token constructors into a single `NewToken` function. - Removed the unused `trace` package. ## Test plan - [x] `go build ./...` passes. - [x] `go test ./...` passes. - [ ] Verify `go doc` output renders correctly for documented packages. Reviewed-on: #45 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package saccharine
|
|
|
|
import "fmt"
|
|
|
|
// A TokenType is an identifier for any token in the Saccharine language.
|
|
type TokenType int
|
|
|
|
// All official tokens of the Saccharine language.
|
|
const (
|
|
// TokenOpenParen denotes the '(' token.
|
|
TokenOpenParen TokenType = iota
|
|
// TokenCloseParen denotes the ')' token.
|
|
TokenCloseParen
|
|
// TokenOpenBrace denotes the '{' token.
|
|
TokenOpenBrace
|
|
// TokenCloseBrace denotes the '}' token.
|
|
TokenCloseBrace
|
|
// TokenHardBreak denotes the ';' token.
|
|
TokenHardBreak
|
|
// TokenAssign denotes the ':=' token.
|
|
TokenAssign
|
|
// TokenAtom denotes an alpha-numeric variable.
|
|
TokenAtom
|
|
// TokenSlash denotes the '/' token.
|
|
TokenSlash
|
|
// TokenDot denotes the '.' token.
|
|
TokenDot
|
|
// TokenSoftBreak denotes a new-line.
|
|
TokenSoftBreak
|
|
)
|
|
|
|
// A Token in the Saccharine language.
|
|
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.
|
|
}
|
|
|
|
// NewToken creates a [Token] of the given type at the given column.
|
|
// The token's value is derived from its [TokenType].
|
|
func NewToken(typ TokenType, column int) *Token {
|
|
return &Token{Type: typ, Column: column, Value: typ.Name()}
|
|
}
|
|
|
|
// NewTokenAtom creates a [TokenAtom] with the given name at the given column.
|
|
func NewTokenAtom(name string, column int) *Token {
|
|
return &Token{Type: TokenAtom, Column: column, Value: name}
|
|
}
|
|
|
|
// Name returns the type of the TokenType, as a string.
|
|
func (t TokenType) Name() string {
|
|
switch t {
|
|
case TokenOpenParen:
|
|
return "("
|
|
case TokenCloseParen:
|
|
return ")"
|
|
case TokenOpenBrace:
|
|
return "{"
|
|
case TokenCloseBrace:
|
|
return "}"
|
|
case TokenHardBreak:
|
|
return ";"
|
|
case TokenAssign:
|
|
return ":="
|
|
case TokenAtom:
|
|
return "ATOM"
|
|
case TokenSlash:
|
|
return "\\"
|
|
case TokenDot:
|
|
return "."
|
|
case TokenSoftBreak:
|
|
return "\\n"
|
|
default:
|
|
panic(fmt.Errorf("unknown token type %v", t))
|
|
}
|
|
}
|
|
|
|
// Name returns the type of the Token, as a string.
|
|
func (t Token) Name() string {
|
|
return t.Type.Name()
|
|
}
|