From e3629acb4544525fa9a979e829efa2029e949527 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 26 Dec 2025 03:37:05 -0500 Subject: [PATCH] feat: stuff --- cmd/lambda/lambda.go | 19 +++++++++---------- pkg/saccharine/ast.go | 16 ++++++++++++---- pkg/saccharine/stringify.go | 36 ++++++++++++++++++++++++++++++++++++ samples/saccharine.txt | 2 +- samples/simple.txt | 2 +- 5 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 pkg/saccharine/stringify.go diff --git a/cmd/lambda/lambda.go b/cmd/lambda/lambda.go index 76b2df3..d4366db 100644 --- a/cmd/lambda/lambda.go +++ b/cmd/lambda/lambda.go @@ -7,7 +7,6 @@ import ( "git.maximhutz.com/max/lambda/internal/cli" "git.maximhutz.com/max/lambda/internal/config" - "git.maximhutz.com/max/lambda/pkg/lambda" "git.maximhutz.com/max/lambda/pkg/saccharine" ) @@ -31,24 +30,24 @@ func main() { // Turn tokens into syntax tree. expression, err := saccharine.GetTree(tokens) cli.HandleError(err) - logger.Info("Parsed syntax tree.", "tree", lambda.Stringify(expression)) + logger.Info("Parsed syntax tree.", "tree", saccharine.Stringify(expression)) // Reduce expression. start := time.Now() if options.Explanation { - fmt.Println(lambda.Stringify(expression)) + fmt.Println(saccharine.Stringify(expression)) } - for lambda.ReduceOnce(&expression) { - logger.Info("Reduction.", "tree", lambda.Stringify(expression)) - if options.Explanation { - fmt.Println(" =", lambda.Stringify(expression)) - } - } + // for lambda.ReduceOnce(&expression) { + // logger.Info("Reduction.", "tree", saccharine.Stringify(expression)) + // if options.Explanation { + // fmt.Println(" =", saccharine.Stringify(expression)) + // } + // } elapsed := time.Since(start).Milliseconds() - fmt.Println(lambda.Stringify(expression)) + fmt.Println(saccharine.Stringify(expression)) fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms") } diff --git a/pkg/saccharine/ast.go b/pkg/saccharine/ast.go index e0c4b05..17ddd0e 100644 --- a/pkg/saccharine/ast.go +++ b/pkg/saccharine/ast.go @@ -1,7 +1,7 @@ package saccharine type Node interface { - IsNode() + Accept(Visitor) } /** ------------------------------------------------------------------------- */ @@ -36,6 +36,14 @@ func NewVariable(name string) *Variable { /** ------------------------------------------------------------------------- */ -func (_ Abstraction) IsNode() {} -func (_ Application) IsNode() {} -func (_ Variable) IsNode() {} +func (a *Abstraction) Accept(x Visitor) { x.VisitAbstraction(a) } +func (a *Application) Accept(x Visitor) { x.VisitApplication(a) } +func (v *Variable) Accept(x Visitor) { x.VisitVariable(v) } + +/** ------------------------------------------------------------------------- */ + +type Visitor interface { + VisitAbstraction(*Abstraction) + VisitApplication(*Application) + VisitVariable(*Variable) +} diff --git a/pkg/saccharine/stringify.go b/pkg/saccharine/stringify.go new file mode 100644 index 0000000..0a08226 --- /dev/null +++ b/pkg/saccharine/stringify.go @@ -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() +} diff --git a/samples/saccharine.txt b/samples/saccharine.txt index fdcd518..b1f35b1 100644 --- a/samples/saccharine.txt +++ b/samples/saccharine.txt @@ -1,5 +1,5 @@ 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 mult n m := m (n f) exp m n := m n diff --git a/samples/simple.txt b/samples/simple.txt index c2fad93..5e8fabc 100644 --- a/samples/simple.txt +++ b/samples/simple.txt @@ -12,5 +12,5 @@ ) \n f x.(f (n f x)) ) - \f x.x + \f x.((((((x)))))) ) \ No newline at end of file