feat: it works!

This commit is contained in:
2025-12-27 23:51:04 -05:00
parent f038d0a685
commit f4897d53a9
3 changed files with 50 additions and 2 deletions

View File

@@ -11,4 +11,4 @@ thunk: it
@ ./lambda.exe - < ./samples/thunk.txt @ ./lambda.exe - < ./samples/thunk.txt
saccharine: it saccharine: it
@ ./lambda.exe -v - < ./samples/saccharine.txt @ ./lambda.exe -x - < ./samples/saccharine.txt

View File

@@ -47,6 +47,51 @@ func convertApplication(n *ast.Application) lambda.Expression {
return result 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 { func SaccharineToLambda(n ast.Expression) lambda.Expression {
switch n := n.(type) { switch n := n.(type) {
case *ast.Atom: case *ast.Atom:
@@ -55,6 +100,8 @@ func SaccharineToLambda(n ast.Expression) lambda.Expression {
return convertAbstraction(n) return convertAbstraction(n)
case *ast.Application: case *ast.Application:
return convertApplication(n) return convertApplication(n)
case *ast.Clause:
return convertClause(n)
default: default:
panic(fmt.Errorf("unknown expression type: %T", n)) panic(fmt.Errorf("unknown expression type: %T", n))
} }

View File

@@ -6,6 +6,7 @@ left p := (p true)
false p := (p false) false p := (p false)
zero 0 1 x := x zero 0 1 x := x
print n := (n 0 1 end)
inc n := \0 1 x.{ inc n := \0 1 x.{
initial := (pair true x) initial := (pair true x)
@@ -15,4 +16,4 @@ inc n := \0 1 x.{
(n onZero onOne initial) (n onZero onOne initial)
} }
inc (print zero)