refactor: rewrite CLI and internal architecture (#41)
## Description The old architecture used a monolithic `main()` with a custom arg parser, an event-emitter-based runtime, and a plugin system for optional features. This PR rewrites the CLI and internal architecture to be modular, extensible, and built around a registry of interchangeable components. - Replace custom CLI arg parsing with Cobra subcommands (`convert`, `reduce`, `engine list`). - Introduce a registry system (`internal/registry`) for marshalers, codecs, and engines, with BFS-based conversion path resolution. - Add type-erased adapter layer (`internal/cli`) with `Repr`, `Engine`, `Process`, `Marshaler`, and `Conversion` interfaces wrapping generic `pkg/` types. - Replace the event-emitter-based `Runtime` with a simpler `Engine`/`Process` model (`pkg/engine`). - Add generic `Codec[T, U]` and `Marshaler[T]` interfaces (`pkg/codec`). - Merge `saccharine/token` sub-package into `saccharine` and rename scanner functions from `parse*` to `scan*`. - Make saccharine-to-lambda conversion bidirectional (encode and decode). - Add `lambda.Marshaler` and `saccharine.Marshaler` implementing `codec.Marshaler`. - Remove old infrastructure: `pkg/runtime`, `pkg/expr`, `internal/plugins`, `internal/statistics`. - Add `make lint` target and update golangci-lint config. ### Decisions - Cobra was chosen for the CLI framework to support nested subcommands and standard flag handling. - The registry uses BFS to find conversion paths between representations, allowing multi-hop conversions without hardcoding routes. - Type erasure via `cli.Repr` (wrapping `any`) enables the registry to work with heterogeneous types while keeping `pkg/` generics type-safe. - The old plugin/event system was removed entirely rather than adapted, since the new `Process` model can support hooks differently in the future. ## Benefits - Subcommands make the CLI self-documenting and easier to extend with new functionality. - The registry pattern decouples representations, conversions, and engines, making it trivial to add new ones. - BFS conversion routing means adding a single codec automatically enables transitive conversions. - Simpler `Engine`/`Process` model reduces complexity compared to the event-emitter runtime. - Consolidating the `token` sub-package reduces import depth and package sprawl. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). Always use underscores. - [ ] Tests pass (if applicable). - [ ] Documentation updated (if applicable). Reviewed-on: #41 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #41.
This commit is contained in:
91
pkg/saccharine/token.go
Normal file
91
pkg/saccharine/token.go
Normal file
@@ -0,0 +1,91 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user