feat: stuff

This commit is contained in:
2025-12-26 03:37:05 -05:00
parent f26e7fbdc9
commit e3629acb45
5 changed files with 59 additions and 16 deletions

View File

@@ -7,7 +7,6 @@ import (
"git.maximhutz.com/max/lambda/internal/cli" "git.maximhutz.com/max/lambda/internal/cli"
"git.maximhutz.com/max/lambda/internal/config" "git.maximhutz.com/max/lambda/internal/config"
"git.maximhutz.com/max/lambda/pkg/lambda"
"git.maximhutz.com/max/lambda/pkg/saccharine" "git.maximhutz.com/max/lambda/pkg/saccharine"
) )
@@ -31,24 +30,24 @@ func main() {
// Turn tokens into syntax tree. // Turn tokens into syntax tree.
expression, err := saccharine.GetTree(tokens) expression, err := saccharine.GetTree(tokens)
cli.HandleError(err) cli.HandleError(err)
logger.Info("Parsed syntax tree.", "tree", lambda.Stringify(expression)) logger.Info("Parsed syntax tree.", "tree", saccharine.Stringify(expression))
// Reduce expression. // Reduce expression.
start := time.Now() start := time.Now()
if options.Explanation { if options.Explanation {
fmt.Println(lambda.Stringify(expression)) fmt.Println(saccharine.Stringify(expression))
} }
for lambda.ReduceOnce(&expression) { // for lambda.ReduceOnce(&expression) {
logger.Info("Reduction.", "tree", lambda.Stringify(expression)) // logger.Info("Reduction.", "tree", saccharine.Stringify(expression))
if options.Explanation { // if options.Explanation {
fmt.Println(" =", lambda.Stringify(expression)) // fmt.Println(" =", saccharine.Stringify(expression))
} // }
} // }
elapsed := time.Since(start).Milliseconds() elapsed := time.Since(start).Milliseconds()
fmt.Println(lambda.Stringify(expression)) fmt.Println(saccharine.Stringify(expression))
fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms") fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms")
} }

View File

@@ -1,7 +1,7 @@
package saccharine package saccharine
type Node interface { type Node interface {
IsNode() Accept(Visitor)
} }
/** ------------------------------------------------------------------------- */ /** ------------------------------------------------------------------------- */
@@ -36,6 +36,14 @@ func NewVariable(name string) *Variable {
/** ------------------------------------------------------------------------- */ /** ------------------------------------------------------------------------- */
func (_ Abstraction) IsNode() {} func (a *Abstraction) Accept(x Visitor) { x.VisitAbstraction(a) }
func (_ Application) IsNode() {} func (a *Application) Accept(x Visitor) { x.VisitApplication(a) }
func (_ Variable) IsNode() {} func (v *Variable) Accept(x Visitor) { x.VisitVariable(v) }
/** ------------------------------------------------------------------------- */
type Visitor interface {
VisitAbstraction(*Abstraction)
VisitApplication(*Application)
VisitVariable(*Variable)
}

View File

@@ -0,0 +1,36 @@
package saccharine
import "strings"
type stringifyVisitor struct {
builder strings.Builder
}
func (v *stringifyVisitor) VisitVariable(a *Variable) {
v.builder.WriteString(a.Name)
}
func (v *stringifyVisitor) VisitAbstraction(f *Abstraction) {
v.builder.WriteRune('\\')
v.builder.WriteString(strings.Join(f.Parameters, " "))
v.builder.WriteRune('.')
f.Body.Accept(v)
}
func (v *stringifyVisitor) VisitApplication(c *Application) {
v.builder.WriteRune('(')
c.Abstraction.Accept(v)
for _, argument := range c.Arguments {
v.builder.WriteRune(' ')
argument.Accept(v)
}
v.builder.WriteRune(')')
}
func Stringify(n Node) string {
b := &stringifyVisitor{}
n.Accept(b)
return b.builder.String()
}

View File

@@ -1,5 +1,5 @@
0 := \f x.x 0 := \f x.x
inc n := \f x.(f (n f x)) inc n := \f x.f (n f x)
add n m := m inc n add n m := m inc n
mult n m := m (n f) mult n m := m (n f)
exp m n := m n exp m n := m n

View File

@@ -12,5 +12,5 @@
) )
\n f x.(f (n f x)) \n f x.(f (n f x))
) )
\f x.x \f x.((((((x))))))
) )