style: rename repr to expr #44

Merged
mvhutz merged 1 commits from style/repr-to-expr into main 2026-02-07 15:26:50 +00:00
9 changed files with 54 additions and 50 deletions
Showing only changes of commit a790a293cf - Show all commits

View File

@@ -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 {

View File

@@ -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()
}, },

View File

@@ -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())

View File

@@ -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 }

View File

@@ -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
View 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} }

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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} }