aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/coffee/liz/lambda/LambdaDriver.java
blob: 475e14af7c6808901ce737ff779e473d2cac8c86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package coffee.liz.lambda;

import coffee.liz.lambda.parser.ArrowParser;
import coffee.liz.lambda.parser.LambdaParser;
import coffee.liz.lambda.parser.ParseException;
import coffee.liz.lambda.ast.Expression;
import coffee.liz.lambda.ast.LambdaProgram;
import coffee.liz.lambda.ast.Macro;
import coffee.liz.lambda.ast.SourceCode;
import coffee.liz.lambda.bind.ExternalBinding;
import coffee.liz.lambda.eval.Environment;
import coffee.liz.lambda.eval.NormalOrderEvaluator;
import coffee.liz.lambda.eval.Value;

import java.io.StringReader;
import java.util.List;

/**
 * Entry point for parsing and interpreting lambda calculus programs.
 */
public class LambdaDriver {

    /**
     * Parses source code into an AST.
     *
     * @param sourceCode
     *            the source code (either Lambda or Arrow syntax)
     * @return the parsed program
     */
    public static LambdaProgram parse(final SourceCode sourceCode) {
        return switch (sourceCode) {
            case SourceCode.Lambda(String source) -> parseLambda(source);
            case SourceCode.Arrow(String source) -> parseArrow(source);
        };
    }

    private static LambdaProgram parseLambda(final String source) {
        try (final StringReader reader = new StringReader(source)) {
            return new LambdaParser(reader).Program();
        } catch (final ParseException parseException) {
            throw new RuntimeException("Failed to parse program", parseException);
        }
    }

    private static LambdaProgram parseArrow(final String source) {
        try (final StringReader reader = new StringReader(source)) {
            return new ArrowParser(reader).Program();
        } catch (final ParseException parseException) {
            throw new RuntimeException("Failed to parse program", parseException);
        }
    }

    /**
     * Parses and evaluates lambda calculus programs.
     *
     * @param sourceCode
     *            the source code
     * @return the evaluated result
     */
    public static Value interpret(final SourceCode sourceCode) {
        return interpret(sourceCode, List.of());
    }

    /**
     * Parses and evaluates lambda calculus programs with "FFI"'s.
     *
     * @param sourceCode
     *            the source code
     * @param bindings
     *            external Java functions available during evaluation
     * @return the evaluated result
     */
    public static Value interpret(final SourceCode sourceCode, final List<ExternalBinding> bindings) {
        return interpret(sourceCode, bindings, Environment.root());
    }

    public static Value interpret(final SourceCode sourceCode, final List<ExternalBinding> bindings,
            final Environment baseEnvironment) {
        final LambdaProgram program = parse(sourceCode);
        final Expression expression = program.expression();
        final List<Macro> macros = program.macros();
        return NormalOrderEvaluator.evaluate(expression, baseEnvironment.extend(Environment.from(macros, bindings)));
    }
}