Refactor GenerateFunctions
Introduce FunctionKind enum to store differences between functions and extension functions (and possible future function-like classes) Also remove some useless checks
This commit is contained in:
@@ -23,25 +23,35 @@ import java.io.PrintStream;
|
|||||||
public class GenerateFunctions {
|
public class GenerateFunctions {
|
||||||
private static final int MAX_PARAM_COUNT = 22;
|
private static final int MAX_PARAM_COUNT = 22;
|
||||||
|
|
||||||
private static void generateFunctions(PrintStream out, int count, boolean extension) {
|
private enum FunctionKind {
|
||||||
|
FUNCTION("Functions.jet", "Function", false),
|
||||||
|
EXTENSION_FUNCTION("ExtensionFunctions.jet", "ExtensionFunction", true);
|
||||||
|
|
||||||
|
private final String fileName;
|
||||||
|
private final String classNamePrefix;
|
||||||
|
private final boolean hasReceiverParameter;
|
||||||
|
|
||||||
|
private FunctionKind(String fileName, String classNamePrefix, boolean hasReceiverParameter) {
|
||||||
|
this.fileName = fileName;
|
||||||
|
this.classNamePrefix = classNamePrefix;
|
||||||
|
this.hasReceiverParameter = hasReceiverParameter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void generateFunctions(PrintStream out, int count, FunctionKind kind) {
|
||||||
generated(out);
|
generated(out);
|
||||||
for (int i = 0; i <= count; i++) {
|
for (int i = 0; i <= count; i++) {
|
||||||
out.print("public abstract class " + (extension ? "ExtensionFunction" : "Function") + i);
|
out.print("public abstract class " + kind.classNamePrefix + i);
|
||||||
out.print("<");
|
out.print("<");
|
||||||
if (extension) {
|
if (kind.hasReceiverParameter) {
|
||||||
out.print("in T");
|
out.print("in T, ");
|
||||||
if (count > 0) {
|
|
||||||
out.print(", ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (int j = 1; j <= i; j++) {
|
for (int j = 1; j <= i; j++) {
|
||||||
out.print("in P" + j);
|
out.print("in P" + j + ", ");
|
||||||
out.print(", ");
|
|
||||||
}
|
}
|
||||||
out.print("out R> {");
|
out.print("out R> {");
|
||||||
out.println();
|
out.println();
|
||||||
out.print(" public abstract fun " + (extension ? "T." : "") +
|
out.print(" public abstract fun " + (kind.hasReceiverParameter ? "T." : "") + "invoke(");
|
||||||
"invoke(");
|
|
||||||
for (int j = 1; j <= i; j++) {
|
for (int j = 1; j <= i; j++) {
|
||||||
out.print("p" + j + ": " + "P" + j);
|
out.print("p" + j + ": " + "P" + j);
|
||||||
if (j < i) {
|
if (j < i) {
|
||||||
@@ -65,13 +75,11 @@ public class GenerateFunctions {
|
|||||||
File baseDir = new File("compiler/frontend/src/jet/");
|
File baseDir = new File("compiler/frontend/src/jet/");
|
||||||
assert baseDir.exists() : "Base dir does not exist: " + baseDir.getAbsolutePath();
|
assert baseDir.exists() : "Base dir does not exist: " + baseDir.getAbsolutePath();
|
||||||
|
|
||||||
PrintStream functions = new PrintStream(new File(baseDir, "Functions.jet"));
|
for (FunctionKind kind : FunctionKind.values()) {
|
||||||
generateFunctions(functions, MAX_PARAM_COUNT, false);
|
PrintStream functions = new PrintStream(new File(baseDir, kind.fileName));
|
||||||
functions.close();
|
generateFunctions(functions, MAX_PARAM_COUNT, kind);
|
||||||
|
functions.close();
|
||||||
PrintStream extensionFunctions = new PrintStream(new File(baseDir, "ExtensionFunctions.jet"));
|
}
|
||||||
generateFunctions(extensionFunctions, MAX_PARAM_COUNT, true);
|
|
||||||
extensionFunctions.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private GenerateFunctions() {
|
private GenerateFunctions() {
|
||||||
|
|||||||
Reference in New Issue
Block a user