feat: it works!
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user