feat: fmt.Stringer
This commit is contained in:
@@ -2,10 +2,14 @@
|
|||||||
// expression types in the lambda interpreter.
|
// expression types in the lambda interpreter.
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
// Expression is the base interface for all evaluatable expression types.
|
// Expression is the base interface for all evaluatable expression types.
|
||||||
// Different evaluation modes (lambda calculus, SKI combinators, typed lambda
|
// Different evaluation modes (lambda calculus, SKI combinators, typed lambda
|
||||||
// calculus, etc.) implement this interface with their own concrete types.
|
// calculus, etc.) implement this interface with their own concrete types.
|
||||||
type Expression interface {
|
type Expression interface {
|
||||||
// String returns a human-readable representation of the expression.
|
// The expression should have a human-readable representation.
|
||||||
String() string
|
fmt.Stringer
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package lambda
|
package lambda
|
||||||
|
|
||||||
import "git.maximhutz.com/max/lambda/pkg/expr"
|
import (
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/expr"
|
||||||
|
)
|
||||||
|
|
||||||
// Expression is the interface for all lambda calculus expression types.
|
// Expression is the interface for all lambda calculus expression types.
|
||||||
// It embeds the general expr.Expression interface for cross-mode compatibility.
|
// It embeds the general expr.Expression interface for cross-mode compatibility.
|
||||||
@@ -15,6 +17,8 @@ type Abstraction struct {
|
|||||||
body Expression
|
body Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Expression = (*Abstraction)(nil)
|
||||||
|
|
||||||
func (a *Abstraction) Parameter() string {
|
func (a *Abstraction) Parameter() string {
|
||||||
return a.parameter
|
return a.parameter
|
||||||
}
|
}
|
||||||
@@ -24,11 +28,11 @@ func (a *Abstraction) Body() Expression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Abstraction) String() string {
|
func (a *Abstraction) String() string {
|
||||||
return Stringify(a)
|
return "\\" + a.parameter + "." + a.body.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
||||||
return &Abstraction{parameter: parameter, body: body}
|
return &Abstraction{parameter, body}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
@@ -38,6 +42,8 @@ type Application struct {
|
|||||||
argument Expression
|
argument Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Expression = (*Application)(nil)
|
||||||
|
|
||||||
func (a *Application) Abstraction() Expression {
|
func (a *Application) Abstraction() Expression {
|
||||||
return a.abstraction
|
return a.abstraction
|
||||||
}
|
}
|
||||||
@@ -47,11 +53,11 @@ func (a *Application) Argument() Expression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Application) String() string {
|
func (a *Application) String() string {
|
||||||
return Stringify(a)
|
return "(" + a.abstraction.String() + " " + a.argument.String() + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApplication(abstraction Expression, argument Expression) *Application {
|
func NewApplication(abstraction Expression, argument Expression) *Application {
|
||||||
return &Application{abstraction: abstraction, argument: argument}
|
return &Application{abstraction, argument}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** ------------------------------------------------------------------------- */
|
/** ------------------------------------------------------------------------- */
|
||||||
@@ -60,14 +66,16 @@ type Variable struct {
|
|||||||
value string
|
value string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Expression = (*Variable)(nil)
|
||||||
|
|
||||||
func (v *Variable) Value() string {
|
func (v *Variable) Value() string {
|
||||||
return v.value
|
return v.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Variable) String() string {
|
func (v *Variable) String() string {
|
||||||
return Stringify(v)
|
return v.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVariable(name string) *Variable {
|
func NewVariable(name string) *Variable {
|
||||||
return &Variable{value: name}
|
return &Variable{name}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
package lambda
|
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
// Stringify converts a lambda calculus expression to its string representation.
|
|
||||||
func Stringify(e Expression) string {
|
|
||||||
var builder strings.Builder
|
|
||||||
stringify(&builder, e)
|
|
||||||
return builder.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func stringify(b *strings.Builder, e Expression) {
|
|
||||||
switch e := e.(type) {
|
|
||||||
case *Variable:
|
|
||||||
b.WriteString(e.value)
|
|
||||||
case *Abstraction:
|
|
||||||
b.WriteRune('\\')
|
|
||||||
b.WriteString(e.parameter)
|
|
||||||
b.WriteRune('.')
|
|
||||||
stringify(b, e.body)
|
|
||||||
case *Application:
|
|
||||||
b.WriteRune('(')
|
|
||||||
stringify(b, e.abstraction)
|
|
||||||
b.WriteRune(' ')
|
|
||||||
stringify(b, e.argument)
|
|
||||||
b.WriteRune(')')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user