From f4897d53a9d0d78808b6754c14184417bd7a731e Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 27 Dec 2025 23:51:04 -0500 Subject: [PATCH] feat: it works! --- Makefile | 2 +- pkg/convert/saccharine_to_lambda.go | 47 +++++++++++++++++++++++++++++ samples/saccharine.txt | 3 +- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ac569fb..db2ace9 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,4 @@ thunk: it @ ./lambda.exe - < ./samples/thunk.txt saccharine: it - @ ./lambda.exe -v - < ./samples/saccharine.txt \ No newline at end of file + @ ./lambda.exe -x - < ./samples/saccharine.txt \ No newline at end of file diff --git a/pkg/convert/saccharine_to_lambda.go b/pkg/convert/saccharine_to_lambda.go index b311d89..b3632cb 100644 --- a/pkg/convert/saccharine_to_lambda.go +++ b/pkg/convert/saccharine_to_lambda.go @@ -47,6 +47,51 @@ func convertApplication(n *ast.Application) lambda.Expression { return result } +func reduceLet(s *ast.LetStatement, e lambda.Expression) lambda.Expression { + var value lambda.Expression + + if len(s.Parameters) == 0 { + value = SaccharineToLambda(s.Body) + } else { + value = convertAbstraction(ast.NewAbstraction(s.Parameters, s.Body)) + } + + return lambda.NewApplication( + lambda.NewAbstraction(s.Name, e), + value, + ) +} + +func reduceDeclare(s *ast.DeclareStatement, e lambda.Expression) lambda.Expression { + freshVar := lambda.GenerateFreshName(lambda.GetFreeVariables(e)) + + return lambda.NewApplication( + lambda.NewAbstraction(freshVar, e), + SaccharineToLambda(s.Value), + ) +} + +func reduceStatement(s ast.Statement, e lambda.Expression) lambda.Expression { + switch s := s.(type) { + case *ast.DeclareStatement: + return reduceDeclare(s, e) + case *ast.LetStatement: + return reduceLet(s, e) + default: + panic(fmt.Errorf("unknown statement type: %v", s)) + } +} + +func convertClause(n *ast.Clause) lambda.Expression { + result := SaccharineToLambda(n.Returns) + + for i := len(n.Statements) - 1; i >= 0; i-- { + result = reduceStatement(n.Statements[i], result) + } + + return result +} + func SaccharineToLambda(n ast.Expression) lambda.Expression { switch n := n.(type) { case *ast.Atom: @@ -55,6 +100,8 @@ func SaccharineToLambda(n ast.Expression) lambda.Expression { return convertAbstraction(n) case *ast.Application: return convertApplication(n) + case *ast.Clause: + return convertClause(n) default: panic(fmt.Errorf("unknown expression type: %T", n)) } diff --git a/samples/saccharine.txt b/samples/saccharine.txt index 8f1fe12..e9fb362 100644 --- a/samples/saccharine.txt +++ b/samples/saccharine.txt @@ -6,6 +6,7 @@ left p := (p true) false p := (p false) zero 0 1 x := x +print n := (n 0 1 end) inc n := \0 1 x.{ initial := (pair true x) @@ -15,4 +16,4 @@ inc n := \0 1 x.{ (n onZero onOne initial) } -inc +(print zero)