refactor: remove visitor pattern #37

Merged
mvhutz merged 2 commits from refactor/no-visitor-pattern into main 2026-01-17 20:46:07 +00:00
2 changed files with 22 additions and 47 deletions
Showing only changes of commit dbb988b633 - Show all commits

View File

@@ -6,7 +6,6 @@ import "git.maximhutz.com/max/lambda/pkg/expr"
// It embeds the general expr.Expression interface for cross-mode compatibility. // It embeds the general expr.Expression interface for cross-mode compatibility.
type Expression interface { type Expression interface {
expr.Expression expr.Expression
Accept(Visitor)
} }
/** ------------------------------------------------------------------------- */ /** ------------------------------------------------------------------------- */
@@ -24,10 +23,6 @@ func (a *Abstraction) Body() Expression {
return a.body return a.body
} }
func (a *Abstraction) Accept(v Visitor) {
v.VisitAbstraction(a)
}
func (a *Abstraction) String() string { func (a *Abstraction) String() string {
return Stringify(a) return Stringify(a)
} }
@@ -51,10 +46,6 @@ func (a *Application) Argument() Expression {
return a.argument return a.argument
} }
func (a *Application) Accept(v Visitor) {
v.VisitApplication(a)
}
func (a *Application) String() string { func (a *Application) String() string {
return Stringify(a) return Stringify(a)
} }
@@ -73,10 +64,6 @@ func (v *Variable) Value() string {
return v.value return v.value
} }
func (v *Variable) Accept(visitor Visitor) {
visitor.VisitVariable(v)
}
func (v *Variable) String() string { func (v *Variable) String() string {
return Stringify(v) return Stringify(v)
} }
@@ -84,11 +71,3 @@ func (v *Variable) String() string {
func NewVariable(name string) *Variable { func NewVariable(name string) *Variable {
return &Variable{value: name} return &Variable{value: name}
} }
/** ------------------------------------------------------------------------- */
type Visitor interface {
VisitAbstraction(*Abstraction)
VisitApplication(*Application)
VisitVariable(*Variable)
}

View File

@@ -2,31 +2,27 @@ package lambda
import "strings" import "strings"
type stringifyVisitor struct { // Stringify converts a lambda calculus expression to its string representation.
builder strings.Builder
}
func (v *stringifyVisitor) VisitVariable(a *Variable) {
v.builder.WriteString(a.value)
}
func (v *stringifyVisitor) VisitAbstraction(f *Abstraction) {
v.builder.WriteRune('\\')
v.builder.WriteString(f.parameter)
v.builder.WriteRune('.')
f.body.Accept(v)
}
func (v *stringifyVisitor) VisitApplication(c *Application) {
v.builder.WriteRune('(')
c.abstraction.Accept(v)
v.builder.WriteRune(' ')
c.argument.Accept(v)
v.builder.WriteRune(')')
}
func Stringify(e Expression) string { func Stringify(e Expression) string {
b := &stringifyVisitor{builder: strings.Builder{}} var builder strings.Builder
e.Accept(b) stringify(&builder, e)
return b.builder.String() 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(')')
}
} }