Generating Kotlin sources for functions and tuples

This commit is contained in:
Andrey Breslav
2012-03-28 13:14:04 +04:00
parent cd6f3139f0
commit 98887a09d1
5 changed files with 274 additions and 0 deletions
@@ -0,0 +1,103 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* @author abreslav
*/
public class TuplesAndFunctionsGenerator {
private static int TUPLE_COUNT = 23;
private static void generateTuples(PrintStream out, int count) {
generated(out);
out.println("class Tuple0() {}");
for (int i = 1; i < count; i++) {
out.print("class Tuple" + i);
out.print("<");
for (int j = 1; j <= i; j++) {
out.print("out T" + j);
if (j < i) {
out.print(", ");
}
}
out.print(">");
out.print("(");
for (int j = 1; j <= i; j++) {
out.print("_" + j + ": " + "T" + j);
if (j < i) {
out.print(", ");
}
}
out.print(") {}");
out.println();
}
}
private static void generateFunctions(PrintStream out, int count, boolean extension) {
generated(out);
for (int i = 0; i < count; i++) {
out.print("trait Function" + i);
out.print("<");
if (extension) {
out.print("in T");
if (count > 0) {
out.print(", ");
}
}
for (int j = 1; j <= i; j++) {
out.print("in P" + j);
out.print(", ");
}
out.print("out R> {\n");
out.print(" fun " + (extension ? "T." : "") +
"invoke(");
for (int j = 1; j <= i; j++) {
out.print("p" + j + ": " + "P" + j);
if (j < i) {
out.print(", ");
}
}
out.print(") : R\n");
out.println("}");
}
}
private static void generated(PrintStream out) {
out.println("// Generated by " + TuplesAndFunctionsGenerator.class.getCanonicalName());
out.println("// NOTE: this code is not loaded for JetStandardLibrary, but its equivalent is manually created in JetStandardClasses");
out.println();
}
public static void main(String[] args) throws FileNotFoundException {
File baseDir = new File("compiler/frontend/src/jet/");
assert baseDir.exists() : "Base dir does not exist: " + baseDir.getAbsolutePath();
PrintStream tuples = new PrintStream(new File(baseDir, "Tuples.jet.src"));
generateTuples(tuples, TUPLE_COUNT);
tuples.close();
PrintStream functions = new PrintStream(new File(baseDir, "Functions.jet.src"));
generateFunctions(functions, TUPLE_COUNT, false);
functions.close();
PrintStream extensionFunctions = new PrintStream(new File(baseDir, "ExtensionFunctions.jet.src"));
generateFunctions(extensionFunctions, TUPLE_COUNT, true);
extensionFunctions.close();
}
}