docs: document remaining packages and simplify AST types #45

Merged
mvhutz merged 15 commits from docs/rest into main 2026-02-10 01:15:42 +00:00
2 changed files with 49 additions and 59 deletions
Showing only changes of commit d24cbd9d86 - Show all commits

View File

@@ -55,27 +55,27 @@ func scanToken(i *iterator.Iterator[rune]) (*Token, error) {
switch { switch {
case letter == '(': case letter == '(':
return NewTokenOpenParen(index), nil return NewToken(TokenOpenParen, index), nil
case letter == ')': case letter == ')':
return NewTokenCloseParen(index), nil return NewToken(TokenCloseParen, index), nil
case letter == '.': case letter == '.':
return NewTokenDot(index), nil return NewToken(TokenDot, index), nil
case letter == '\\': case letter == '\\':
return NewTokenSlash(index), nil return NewToken(TokenSlash, index), nil
case letter == '\n': case letter == '\n':
return NewTokenSoftBreak(index), nil return NewToken(TokenSoftBreak, index), nil
case letter == '{': case letter == '{':
return NewTokenOpenBrace(index), nil return NewToken(TokenOpenBrace, index), nil
case letter == '}': case letter == '}':
return NewTokenCloseBrace(index), nil return NewToken(TokenCloseBrace, index), nil
case letter == ':': case letter == ':':
if _, err := scanCharacter(i, '='); err != nil { if _, err := scanCharacter(i, '='); err != nil {
return nil, err return nil, err
} else { } else {
return NewTokenAssign(index), nil return NewToken(TokenAssign, index), nil
} }
case letter == ';': case letter == ';':
return NewTokenHardBreak(index), nil return NewToken(TokenHardBreak, index), nil
case letter == '#': case letter == '#':
// Skip everything until the next newline or EOF. // Skip everything until the next newline or EOF.
for !i.Done() { for !i.Done() {

View File

@@ -2,90 +2,80 @@ package saccharine
import "fmt" import "fmt"
// All tokens in the pseudo-lambda language. // A TokenType is an identifier for any token in the Saccharine language.
type TokenType int type TokenType int
// All official tokens of the Saccharine language.
const ( const (
TokenOpenParen TokenType = iota // Denotes the '(' token. // TokenOpenParen denotes the '(' token.
TokenCloseParen // Denotes the ')' token. TokenOpenParen TokenType = iota
TokenOpenBrace // Denotes the '{' token. // TokenCloseParen denotes the ')' token.
TokenCloseBrace // Denotes the '}' token. TokenCloseParen
TokenHardBreak // Denotes the ';' token. // TokenOpenBrace denotes the '{' token.
TokenAssign // Denotes the ':=' token. TokenOpenBrace
TokenAtom // Denotes an alpha-numeric variable. // TokenCloseBrace denotes the '}' token.
TokenSlash // Denotes the '/' token. TokenCloseBrace
TokenDot // Denotes the '.' token. // TokenHardBreak denotes the ';' token.
TokenSoftBreak // Denotes a new-line. 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 representation of a token in source code. // A Token in the Saccharine language.
type Token struct { type Token struct {
Column int // Where the token begins in the source text. Column int // Where the token begins in the source text.
Type TokenType // What type the token is. Type TokenType // What type the token is.
Value string // The value of the token. Value string // The value of the token.
} }
func NewTokenOpenParen(column int) *Token { // NewToken creates a [Token] of the given type at the given column.
return &Token{Type: TokenOpenParen, Column: column, Value: "("} // 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()}
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: "\\"}
} }
// NewTokenAtom creates a [TokenAtom] with the given name at the given column.
func NewTokenAtom(name string, column int) *Token { func NewTokenAtom(name string, column int) *Token {
return &Token{Type: TokenAtom, Column: column, Value: name} return &Token{Type: TokenAtom, Column: column, Value: name}
} }
func NewTokenSoftBreak(column int) *Token { // Name returns the type of the TokenType, as a string.
return &Token{Type: TokenSoftBreak, Column: column, Value: "\\n"}
}
func (t TokenType) Name() string { func (t TokenType) Name() string {
switch t { switch t {
case TokenOpenParen: case TokenOpenParen:
return "(" return "("
case TokenCloseParen: case TokenCloseParen:
return ")" return ")"
case TokenOpenBrace:
return "{"
case TokenCloseBrace:
return "}"
case TokenHardBreak:
return ";"
case TokenAssign:
return ":="
case TokenAtom:
return "ATOM"
case TokenSlash: case TokenSlash:
return "\\" return "\\"
case TokenDot: case TokenDot:
return "." return "."
case TokenAtom:
return "ATOM"
case TokenSoftBreak: case TokenSoftBreak:
return "\\n" return "\\n"
case TokenHardBreak:
return ";"
default: default:
panic(fmt.Errorf("unknown token type %v", t)) panic(fmt.Errorf("unknown token type %v", t))
} }
} }
// Name returns the type of the Token, as a string.
func (t Token) Name() string { func (t Token) Name() string {
return t.Type.Name() return t.Type.Name()
} }