-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
ExpressionCompiler.java
80 lines (71 loc) · 3.21 KB
/
ExpressionCompiler.java
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
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.internal.expression.invoke;
import com.sk89q.worldedit.antlr.ExpressionParser;
import com.sk89q.worldedit.internal.expression.CompiledExpression;
import com.sk89q.worldedit.internal.expression.Functions;
import java.lang.invoke.LambdaConversionException;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import static java.lang.invoke.MethodType.methodType;
/**
* Compiles an expression from an AST into {@link MethodHandle}s.
*/
public class ExpressionCompiler {
private static final String CE_EXECUTE = "execute";
private static final MethodType HANDLE_TO_CE =
methodType(CompiledExpression.class, MethodHandle.class);
private static final MethodHandle HANDLE_TO_CE_CONVERTER;
static {
MethodHandle handleInvoker = MethodHandles.invoker(ExpressionHandles.COMPILED_EXPRESSION_SIG);
try {
HANDLE_TO_CE_CONVERTER = LambdaMetafactory.metafactory(
MethodHandles.lookup(),
// Implementing CompiledExpression.execute
CE_EXECUTE,
// Take a handle, to be converted to CompiledExpression
HANDLE_TO_CE,
// Raw signature for SAM type
ExpressionHandles.COMPILED_EXPRESSION_SIG,
// Handle to call the captured handle.
handleInvoker,
// Actual signature at invoke time
ExpressionHandles.COMPILED_EXPRESSION_SIG
).dynamicInvoker().asType(HANDLE_TO_CE);
} catch (LambdaConversionException e) {
throw new IllegalStateException("Failed to load ExpressionCompiler MetaFactory", e);
}
}
public CompiledExpression compileExpression(ExpressionParser.AllStatementsContext root,
Functions functions) {
MethodHandle invokable = root.accept(new CompilingVisitor(functions));
// catch ReturnExpression and substitute its result
invokable = MethodHandles.catchException(
invokable,
ReturnException.class,
ExpressionHandles.RETURN_EXCEPTION_GET_RESULT
);
MethodHandle finalInvokable = invokable;
return (CompiledExpression) ExpressionHandles.safeInvoke(
HANDLE_TO_CE_CONVERTER, h -> h.invoke(finalInvokable)
);
}
}