refactor: extract abstract Expression interface
Introduce pkg/expr.Expression as a base interface for all evaluatable expression types, enabling future support for multiple evaluation modes (SKI combinators, typed lambda calculus, etc.). - Add pkg/expr/expr.go with Expression interface requiring String() method. - Update lambda.Expression to embed expr.Expression. - Add String() method to Abstraction, Application, and Variable types. - Update plugins to use String() instead of lambda.Stringify().
This commit is contained in:
11
pkg/expr/expr.go
Normal file
11
pkg/expr/expr.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Package expr provides the abstract Expression interface for all evaluatable
|
||||
// expression types in the lambda interpreter.
|
||||
package expr
|
||||
|
||||
// Expression is the base interface for all evaluatable expression types.
|
||||
// Different evaluation modes (lambda calculus, SKI combinators, typed lambda
|
||||
// calculus, etc.) implement this interface with their own concrete types.
|
||||
type Expression interface {
|
||||
// String returns a human-readable representation of the expression.
|
||||
String() string
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package lambda
|
||||
|
||||
import "git.maximhutz.com/max/lambda/pkg/expr"
|
||||
|
||||
// Expression is the interface for all lambda calculus expression types.
|
||||
// It embeds the general expr.Expression interface for cross-mode compatibility.
|
||||
type Expression interface {
|
||||
expr.Expression
|
||||
Accept(Visitor)
|
||||
}
|
||||
|
||||
@@ -23,6 +28,10 @@ func (a *Abstraction) Accept(v Visitor) {
|
||||
v.VisitAbstraction(a)
|
||||
}
|
||||
|
||||
func (a *Abstraction) String() string {
|
||||
return Stringify(a)
|
||||
}
|
||||
|
||||
func NewAbstraction(parameter string, body Expression) *Abstraction {
|
||||
return &Abstraction{parameter: parameter, body: body}
|
||||
}
|
||||
@@ -46,6 +55,10 @@ func (a *Application) Accept(v Visitor) {
|
||||
v.VisitApplication(a)
|
||||
}
|
||||
|
||||
func (a *Application) String() string {
|
||||
return Stringify(a)
|
||||
}
|
||||
|
||||
func NewApplication(abstraction Expression, argument Expression) *Application {
|
||||
return &Application{abstraction: abstraction, argument: argument}
|
||||
}
|
||||
@@ -64,6 +77,10 @@ func (v *Variable) Accept(visitor Visitor) {
|
||||
visitor.VisitVariable(v)
|
||||
}
|
||||
|
||||
func (v *Variable) String() string {
|
||||
return Stringify(v)
|
||||
}
|
||||
|
||||
func NewVariable(name string) *Variable {
|
||||
return &Variable{value: name}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user