style: rename repr to expr (#44)
## Description The `Repr` type name was unclear — it was intended to represent a lambda calculus expression, not a "representation." This PR renames `Repr` to `Expr` throughout the registry package for clarity. - Rename `Repr` interface to `Expr` and `baseRepr` struct to `baseExpr`. - Rename `repr.go` to `expr.go`. - Rename `ID()` method to `Repr()` to indicate the representation type. - Rename `NewRepr` constructor to `NewExpr`. - Update all usages in codec, conversion, engine, process, and registry files. - Add command aliases `conv` and `eng` for `convert` and `engine` subcommands. ## Benefits - The naming better reflects the domain: an `Expr` is an expression, and `Repr()` returns its representation kind. - Command aliases reduce typing for common subcommands. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). Always use underscores. - [x] Tests pass (if applicable). - [x] Documentation updated (if applicable). Reviewed-on: #44 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 #44.
This commit is contained in:
@@ -26,6 +26,7 @@ func LambdaConvert() *cobra.Command {
|
|||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "convert <input-file> <output-file>",
|
Use: "convert <input-file> <output-file>",
|
||||||
|
Aliases: []string{"conv"},
|
||||||
Short: "Convert between lambda calculus representations",
|
Short: "Convert between lambda calculus representations",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ import (
|
|||||||
|
|
||||||
func LambdaEngine() *cobra.Command {
|
func LambdaEngine() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "engine",
|
Use: "engine",
|
||||||
Short: "Information about available engines",
|
Aliases: []string{"eng"},
|
||||||
|
Short: "Information about available engines",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cmd.Help()
|
return cmd.Help()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Codec interface {
|
type Codec interface {
|
||||||
codec.Codec[Repr]
|
codec.Codec[Expr]
|
||||||
|
|
||||||
InType() string
|
InType() string
|
||||||
}
|
}
|
||||||
@@ -18,16 +18,16 @@ type convertedCodec[T any] struct {
|
|||||||
inType string
|
inType string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c convertedCodec[T]) Decode(s string) (Repr, error) {
|
func (c convertedCodec[T]) Decode(s string) (Expr, error) {
|
||||||
t, err := c.codec.Decode(s)
|
t, err := c.codec.Decode(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewRepr(c.inType, t), nil
|
return NewExpr(c.inType, t), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c convertedCodec[T]) Encode(r Repr) (string, error) {
|
func (c convertedCodec[T]) Encode(r Expr) (string, error) {
|
||||||
t, ok := r.Data().(T)
|
t, ok := r.Data().(T)
|
||||||
if !ok {
|
if !ok {
|
||||||
dataType := reflect.TypeOf(r.Data())
|
dataType := reflect.TypeOf(r.Data())
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ type Conversion interface {
|
|||||||
InType() string
|
InType() string
|
||||||
OutType() string
|
OutType() string
|
||||||
|
|
||||||
Run(Repr) (Repr, error)
|
Run(Expr) (Expr, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type convertedConversion[T, U any] struct {
|
type convertedConversion[T, U any] struct {
|
||||||
@@ -18,8 +18,8 @@ type convertedConversion[T, U any] struct {
|
|||||||
inType, outType string
|
inType, outType string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c convertedConversion[T, U]) Run(r Repr) (Repr, error) {
|
func (c convertedConversion[T, U]) Run(expr Expr) (Expr, error) {
|
||||||
t, ok := r.Data().(T)
|
t, ok := expr.Data().(T)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.inType)
|
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.inType)
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ func (c convertedConversion[T, U]) Run(r Repr) (Repr, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewRepr(c.outType, u), nil
|
return NewExpr(c.outType, u), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c convertedConversion[T, U]) InType() string { return c.inType }
|
func (c convertedConversion[T, U]) InType() string { return c.inType }
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Engine interface {
|
type Engine interface {
|
||||||
Load(Repr) (Process, error)
|
Load(Expr) (Process, error)
|
||||||
Name() string
|
Name() string
|
||||||
InType() string
|
InType() string
|
||||||
}
|
}
|
||||||
@@ -22,10 +22,10 @@ func (e convertedEngine[T]) InType() string { return e.inType }
|
|||||||
|
|
||||||
func (e convertedEngine[T]) Name() string { return e.name }
|
func (e convertedEngine[T]) Name() string { return e.name }
|
||||||
|
|
||||||
func (e convertedEngine[T]) Load(r Repr) (Process, error) {
|
func (e convertedEngine[T]) Load(expr Expr) (Process, error) {
|
||||||
t, ok := r.Data().(T)
|
t, ok := expr.Data().(T)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'ncorrent format '%s' for engine '%s'", r.ID(), e.inType)
|
return nil, fmt.Errorf("'ncorrent format '%s' for engine '%s'", expr.Repr(), e.inType)
|
||||||
}
|
}
|
||||||
|
|
||||||
process, err := e.engine(t)
|
process, err := e.engine(t)
|
||||||
|
|||||||
24
internal/registry/expr.go
Normal file
24
internal/registry/expr.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
// A Expr is a lambda calculus expression. It can have any type of
|
||||||
|
// Expresentation, so long as that class is known to the registry it is handled
|
||||||
|
// by.
|
||||||
|
type Expr interface {
|
||||||
|
// Repr returns the name of the underlying Expresentation. It is assumed if
|
||||||
|
// two expressions have the same Repr(), they have the same Expresentation.
|
||||||
|
Repr() string
|
||||||
|
|
||||||
|
// The base expression data.
|
||||||
|
Data() any
|
||||||
|
}
|
||||||
|
|
||||||
|
type baseExpr struct {
|
||||||
|
id string
|
||||||
|
data any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r baseExpr) Repr() string { return r.id }
|
||||||
|
|
||||||
|
func (r baseExpr) Data() any { return r.data }
|
||||||
|
|
||||||
|
func NewExpr(id string, data any) Expr { return baseExpr{id, data} }
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Process interface {
|
type Process interface {
|
||||||
engine.Process[Repr]
|
engine.Process[Expr]
|
||||||
|
|
||||||
InType() string
|
InType() string
|
||||||
}
|
}
|
||||||
@@ -17,13 +17,13 @@ type convertedProcess[T any] struct {
|
|||||||
|
|
||||||
func (e convertedProcess[T]) InType() string { return e.inType }
|
func (e convertedProcess[T]) InType() string { return e.inType }
|
||||||
|
|
||||||
func (b convertedProcess[T]) Get() (Repr, error) {
|
func (b convertedProcess[T]) Get() (Expr, error) {
|
||||||
s, err := b.process.Get()
|
s, err := b.process.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewRepr(b.inType, s), nil
|
return NewExpr(b.inType, s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b convertedProcess[T]) Step(i int) bool {
|
func (b convertedProcess[T]) Step(i int) bool {
|
||||||
|
|||||||
@@ -40,16 +40,18 @@ func (r *Registry) GetDefaultEngine(id string) (Engine, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("no engine for '%s'", id)
|
return r.GetEngine("normalorder")
|
||||||
|
|
||||||
|
// return nil, fmt.Errorf("no engine for '%s'", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) ConvertTo(repr Repr, outType string) (Repr, error) {
|
func (r *Registry) ConvertTo(expr Expr, outType string) (Expr, error) {
|
||||||
path, err := r.ConversionPath(repr.ID(), outType)
|
path, err := r.ConversionPath(expr.Repr(), outType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result := repr
|
result := expr
|
||||||
for _, conversion := range path {
|
for _, conversion := range path {
|
||||||
result, err = conversion.Run(result)
|
result, err = conversion.Run(result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -60,16 +62,16 @@ func (r *Registry) ConvertTo(repr Repr, outType string) (Repr, error) {
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) Marshal(repr Repr) (string, error) {
|
func (r *Registry) Marshal(expr Expr) (string, error) {
|
||||||
m, ok := r.codecs[repr.ID()]
|
m, ok := r.codecs[expr.Repr()]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("no marshaler for '%s'", repr.ID())
|
return "", fmt.Errorf("no marshaler for '%s'", expr.Repr())
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.Encode(repr)
|
return m.Encode(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) Unmarshal(s string, outType string) (Repr, error) {
|
func (r *Registry) Unmarshal(s string, outType string) (Expr, error) {
|
||||||
m, ok := r.codecs[outType]
|
m, ok := r.codecs[outType]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("no marshaler for '%s'", outType)
|
return nil, fmt.Errorf("no marshaler for '%s'", outType)
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package registry
|
|
||||||
|
|
||||||
// A Repr is a lambda calculus expression. It can have any type of
|
|
||||||
// representation, so long as that class is known to the registry it is handled
|
|
||||||
// by.
|
|
||||||
type Repr interface {
|
|
||||||
// ID returns the name of the underlying representation. It is assumed that
|
|
||||||
// if two expressions have the same Id(), they have the same representation.
|
|
||||||
ID() string
|
|
||||||
|
|
||||||
// The base expression data.
|
|
||||||
Data() any
|
|
||||||
}
|
|
||||||
|
|
||||||
type baseRepr struct {
|
|
||||||
id string
|
|
||||||
data any
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r baseRepr) ID() string { return r.id }
|
|
||||||
|
|
||||||
func (r baseRepr) Data() any { return r.data }
|
|
||||||
|
|
||||||
func NewRepr(id string, data any) Repr { return baseRepr{id, data} }
|
|
||||||
Reference in New Issue
Block a user