merging statements into a single file, making code altogether nicer

This commit is contained in:
Dmitry Jemerov
2012-05-30 18:34:02 +02:00
committed by Pavel V. Talanov
parent d66b5e32f9
commit feab374ee3
20 changed files with 126 additions and 389 deletions
@@ -15,14 +15,12 @@ public open class ArrayWithoutInitializationExpression(val `type` : Type, val ex
}
private fun constructInnerType(hostType : ArrayType, expressions: List<Expression>) : String {
if (expressions.size() == 1)
{
if (expressions.size() == 1) {
return oneDim(hostType, expressions[0])
}
val innerType = hostType.elementType
if (expressions.size() > 1 && innerType is ArrayType)
{
if (expressions.size() > 1 && innerType is ArrayType) {
return oneDim(hostType, expressions[0], "{" + constructInnerType(innerType, expressions.subList(1, expressions.size())) + "}")
}
@@ -35,11 +33,7 @@ public open class ArrayWithoutInitializationExpression(val `type` : Type, val ex
}
private open fun oneDim(`type` : Type, size : Expression, init : String) : String {
val commaWithInit = (if (init.isEmpty())
""
else
", " + init)
return getConstructorName(`type`) + "(" + size.toKotlin() + commaWithInit + ")"
return getConstructorName(`type`) + "(" + size.toKotlin() + init.withPrefix(", ") + ")"
}
private open fun getConstructorName(`type` : Type) : String = `type`.convertedToNotNull().toKotlin()
@@ -2,7 +2,5 @@ package org.jetbrains.jet.j2k.ast
public open class AssignmentExpression(val left : Expression, val right : Expression, val op : String) : Expression() {
public override fun toKotlin() : String {
return left.toKotlin() + " "+ op + " "+ right.toKotlin()
}
public override fun toKotlin() : String = left.toKotlin() + " "+ op + " "+ right.toKotlin()
}
@@ -1,8 +0,0 @@
package org.jetbrains.jet.j2k.ast
public open class BreakStatement(val label: Identifier = Identifier.EMPTY_IDENTIFIER) : Statement() {
public override fun toKotlin() : String {
return if (label.isEmpty()) "break" else "break@" + label.toKotlin()
}
}
@@ -1,18 +0,0 @@
package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.util.AstUtil
import java.util.LinkedList
import java.util.List
public open class CaseContainer(val caseStatement: List<Statement>, statements: List<Statement>): Statement() {
private val myBlock: Block
{
val newStatements: List<Statement> = statements.filterNot { it is BreakStatement || it is ContinueStatement }
myBlock = Block(newStatements, false)
}
public override fun toKotlin(): String {
return AstUtil.joinNodes(caseStatement, ", ") + " -> " + myBlock.toKotlin()
}
}
@@ -1,8 +0,0 @@
package org.jetbrains.jet.j2k.ast
public open class CatchStatement(val variable: Parameter, val block: Block): Statement() {
public override fun toKotlin(): String {
return "catch (" + variable.toKotlin() + ") " + block.toKotlin()
}
}
+18 -43
View File
@@ -36,10 +36,9 @@ public open class Class(converter : Converter,
}
open fun primaryConstructorBodyToKotlin() : String? {
var maybeConstructor : Constructor? = getPrimaryConstructor()
if (maybeConstructor != null && !(maybeConstructor?.block?.isEmpty() ?: true))
{
return maybeConstructor?.primaryBodyToKotlin()
val maybeConstructor : Constructor? = getPrimaryConstructor()
if (maybeConstructor != null && !(maybeConstructor.block?.isEmpty() ?: true)) {
return maybeConstructor.primaryBodyToKotlin()
}
return ""
@@ -48,8 +47,7 @@ public open class Class(converter : Converter,
private fun hasWhere() : Boolean = typeParameters.any { it is TypeParameter && it.hasWhere() }
open fun typeParameterWhereToKotlin() : String {
if (hasWhere())
{
if (hasWhere()) {
val wheres = typeParameters.filter { it is TypeParameter }.map { (it as TypeParameter).getWhereToKotlin() }
return " where " + wheres.makeString(", ") + " "
}
@@ -75,26 +73,20 @@ public open class Class(converter : Converter,
constructorTypeParameters, f.params, block)
}
open fun typeParametersToKotlin() : String {
return (if (typeParameters.size() > 0)
"<" + typeParameters.map { it.toKotlin() }.makeString(", ") + ">"
else
"")
}
open fun typeParametersToKotlin() : String = typeParameters.toKotlin(", ", "<", ">")
open fun baseClassSignatureWithParams() : List<String?> {
if (TYPE.equals("class") && extendsTypes.size() == 1)
{
val baseParams = baseClassParams.map { it.toKotlin() }.makeString(", ")
open fun baseClassSignatureWithParams() : List<String> {
if (TYPE.equals("class") && extendsTypes.size() == 1) {
val baseParams = baseClassParams.toKotlin(", ")
return arrayList(extendsTypes[0].toKotlin() + "(" + baseParams + ")")
}
return nodesToKotlin(extendsTypes)
return extendsTypes.map { it.toKotlin() }
}
open fun implementTypesToKotlin() : String {
val allTypes : List<String?> = arrayList()
allTypes.addAll(baseClassSignatureWithParams())
allTypes.addAll(nodesToKotlin(implementsTypes))
allTypes.addAll(implementsTypes.map { it.toKotlin() })
return if (allTypes.size() == 0)
""
else
@@ -104,18 +96,15 @@ public open class Class(converter : Converter,
open fun modifiersToKotlin() : String {
val modifierList : List<String> = arrayList()
modifierList.add(accessModifier())
if (needAbstractModifier())
{
if (needAbstractModifier()) {
modifierList.add(Modifier.ABSTRACT)
}
if (needOpenModifier())
{
if (needOpenModifier()) {
modifierList.add(Modifier.OPEN)
}
if (modifierList.size() > 0)
{
if (modifierList.size() > 0) {
return modifierList.makeString(" ") + " "
}
@@ -127,20 +116,16 @@ public open class Class(converter : Converter,
open fun needAbstractModifier() = isAbstract()
open fun bodyToKotlin() : String {
return " {\n" + AstUtil.joinNodes(getNonStatic(membersExceptConstructors()), "\n") + "\n" + primaryConstructorBodyToKotlin() + "\n" + classObjectToKotlin() + "\n}"
return " {\n" + getNonStatic(membersExceptConstructors()).toKotlin("\n") + "\n" + primaryConstructorBodyToKotlin() + "\n" + classObjectToKotlin() + "\n}"
}
private fun classObjectToKotlin() : String {
val staticMembers : List<Member> = arrayList()
staticMembers.addAll(secondaryConstructorsAsStaticInitFunction())
staticMembers.addAll(getStatic(membersExceptConstructors()))
if (staticMembers.size() > 0)
{
return "class object {\n" + AstUtil.joinNodes(staticMembers, "\n") + "\n}"
}
return ""
return staticMembers.toKotlin("\n", "class object {\n", "\n}")
}
public override fun toKotlin() : String =
modifiersToKotlin() +
TYPE + " " + name.toKotlin() +
@@ -152,18 +137,8 @@ public open class Class(converter : Converter,
class object {
open fun getMembers(members : List<Member>, converter : Converter) : List<Member> {
if (converter.hasFlag(J2KConverterFlags.SKIP_NON_PUBLIC_MEMBERS).sure())
{
val withoutPrivate : List<Member> = arrayList()
for (m : Member in members)
{
if (m.accessModifier().equals("public") || m.accessModifier().equals("protected"))
{
withoutPrivate.add(m)
}
}
return withoutPrivate
if (converter.hasFlag(J2KConverterFlags.SKIP_NON_PUBLIC_MEMBERS)) {
return members.filter { it.accessModifier() == "public" || it.accessModifier() == "protected" }
}
return members
}
@@ -1,12 +0,0 @@
package org.jetbrains.jet.j2k.ast
public open class ContinueStatement(val label: Identifier = Identifier.EMPTY_IDENTIFIER): Statement() {
public override fun toKotlin(): String {
if (label.isEmpty()) {
return "continue"
}
return "continue@" + label.toKotlin()
}
}
@@ -1,19 +0,0 @@
package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.util.AstUtil
import java.util.LinkedList
import java.util.List
public open class DeclarationStatement(val elements: List<Element>): Statement() {
public override fun toKotlin(): String {
return elements.filter { it is LocalVariable }.map { convertDeclaration(it as LocalVariable) }.makeString("\n")
}
private fun convertDeclaration(v: LocalVariable): String {
val varKeyword: String? = (if (v.hasModifier(Modifier.FINAL))
"val"
else
"var")
return varKeyword + " " + v.toKotlin()
}
}
@@ -1,6 +0,0 @@
package org.jetbrains.jet.j2k.ast
public open class DefaultSwitchLabelStatement(): Statement() {
public override fun toKotlin() = "else"
}
@@ -1,8 +0,0 @@
package org.jetbrains.jet.j2k.ast
public open class DoWhileStatement(condition: Expression, statement: Statement): WhileStatement(condition, statement) {
public override fun toKotlin(): String {
return "do\n" + statement.toKotlin() + "\nwhile (" + condition.toKotlin() + ")"
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2010-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.
*/
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/**
* @author ignatov
*/
public class ExpressionListStatement extends Expression {
private final List<Expression> myExpressions;
public ExpressionListStatement(List<Expression> expressions) {
myExpressions = expressions;
}
@NotNull
@Override
public String toKotlin() {
return AstUtil.joinNodes(myExpressions, N);
}
}
@@ -1,38 +0,0 @@
/*
* Copyright 2010-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.
*/
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class LabelStatement extends Statement {
private final Identifier myName;
private final Statement myStatement;
public LabelStatement(Identifier name, Statement statement) {
myName = name;
myStatement = statement;
}
@NotNull
@Override
public String toKotlin() {
return AT + myName.toKotlin() + SPACE + myStatement.toKotlin();
}
}
@@ -1,5 +0,0 @@
package org.jetbrains.jet.j2k.ast
public open class ReturnStatement(val expression: Expression): Statement() {
public override fun toKotlin() = "return " + expression.toKotlin()
}
@@ -0,0 +1,91 @@
package org.jetbrains.jet.j2k.ast
import java.util.List
public open class DeclarationStatement(val elements: List<Element>): Statement() {
public override fun toKotlin(): String {
return elements.filter { it is LocalVariable }.map { convertDeclaration(it as LocalVariable) }.makeString("\n")
}
private fun convertDeclaration(v: LocalVariable): String {
val varKeyword: String? = (if (v.hasModifier(Modifier.FINAL))
"val"
else
"var")
return varKeyword + " " + v.toKotlin()
}
}
public open class ExpressionListStatement(val expressions: List<Expression>): Expression() {
public override fun toKotlin() = expressions.toKotlin("\n")
}
public open class LabelStatement(val name: Identifier, val statement: Statement): Statement() {
public override fun toKotlin(): String = "@" + name.toKotlin() + " " + statement.toKotlin()
}
public open class ReturnStatement(val expression: Expression): Statement() {
public override fun toKotlin() = "return " + expression.toKotlin()
}
// Loops --------------------------------------------------------------------------------------------------
public open class WhileStatement(val condition: Expression, val statement: Statement): Statement() {
public override fun toKotlin() = "while (" + condition.toKotlin() + ")\n" + statement.toKotlin()
}
public open class DoWhileStatement(condition: Expression, statement: Statement): WhileStatement(condition, statement) {
public override fun toKotlin() = "do\n" + statement.toKotlin() + "\nwhile (" + condition.toKotlin() + ")"
}
public open class BreakStatement(val label: Identifier = Identifier.EMPTY_IDENTIFIER) : Statement() {
public override fun toKotlin() = "break" + label.withPrefix("@")
}
public open class ContinueStatement(val label: Identifier = Identifier.EMPTY_IDENTIFIER): Statement() {
public override fun toKotlin() = "continue" + label.withPrefix("@")
}
// Exceptions ----------------------------------------------------------------------------------------------
public open class TryStatement(val block: Block, val catches: List<CatchStatement>, val finallyBlock: Block): Statement() {
public override fun toKotlin(): String {
return "try\n" + block.toKotlin() + "\n" + catches.toKotlin("\n") + "\n" + (if (finallyBlock.isEmpty())
""
else
"finally\n" + finallyBlock.toKotlin())
}
}
public open class ThrowStatement(val expression: Expression): Expression() {
public override fun toKotlin() = "throw " + expression.toKotlin()
}
public open class CatchStatement(val variable: Parameter, val block: Block): Statement() {
public override fun toKotlin(): String = "catch (" + variable.toKotlin() + ") " + block.toKotlin()
}
// Switch --------------------------------------------------------------------------------------------------
public open class SwitchContainer(val expression: Expression, val caseContainers: List<CaseContainer>): Statement() {
public override fun toKotlin() = "when (" + expression.toKotlin() + ") {\n" + caseContainers.toKotlin("\n") + "\n}"
}
public open class CaseContainer(val caseStatement: List<Statement>, statements: List<Statement>): Statement() {
private val myBlock: Block
{
val newStatements: List<Statement> = statements.filterNot { it is BreakStatement || it is ContinueStatement }
myBlock = Block(newStatements, false)
}
public override fun toKotlin() = caseStatement.toKotlin(", ") + " -> " + myBlock.toKotlin()
}
public open class SwitchLabelStatement(val expression: Expression): Statement() {
public override fun toKotlin() = expression.toKotlin()
}
public open class DefaultSwitchLabelStatement(): Statement() {
public override fun toKotlin() = "else"
}
@@ -1,43 +0,0 @@
/*
* Copyright 2010-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.
*/
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/**
* @author ignatov
*/
public class SwitchContainer extends Statement {
private final Expression myExpression;
private final List<CaseContainer> myCaseContainers;
public SwitchContainer(final Expression expression, final List<CaseContainer> caseContainers) {
myExpression = expression;
myCaseContainers = caseContainers;
}
@NotNull
@Override
public String toKotlin() {
return "when" + SPACE + "(" + myExpression.toKotlin() + ")" + SPACE + "{" + N +
AstUtil.joinNodes(myCaseContainers, N) + N +
"}";
}
}
@@ -1,36 +0,0 @@
/*
* Copyright 2010-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.
*/
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class SwitchLabelStatement extends Statement {
private final Expression myExpression;
public SwitchLabelStatement(final Expression expression) {
myExpression = expression;
}
@NotNull
@Override
public String toKotlin() {
return myExpression.toKotlin();
}
}
@@ -1,36 +0,0 @@
/*
* Copyright 2010-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.
*/
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class ThrowStatement extends Expression {
private final Expression myExpression;
public ThrowStatement(Expression expression) {
myExpression = expression;
}
@NotNull
@Override
public String toKotlin() {
return "throw" + SPACE + myExpression.toKotlin();
}
}
@@ -1,46 +0,0 @@
/*
* Copyright 2010-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.
*/
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/**
* @author ignatov
*/
public class TryStatement extends Statement {
private final Block myBlock;
private final List<CatchStatement> myCatches;
private final Block myFinallyBlock;
public TryStatement(Block block, List<CatchStatement> catches, Block finallyBlock) {
myBlock = block;
myCatches = catches;
myFinallyBlock = finallyBlock;
}
@NotNull
@Override
public String toKotlin() {
return "try" + N +
myBlock.toKotlin() + N +
AstUtil.joinNodes(myCatches, N) + N +
(myFinallyBlock.isEmpty() ? EMPTY : "finally" + N + myFinallyBlock.toKotlin());
}
}
+13 -4
View File
@@ -2,11 +2,20 @@ package org.jetbrains.jet.j2k.ast
import java.util.List
fun List<out Node>.toKotlin(separator: String): String {
fun List<out Node>.toKotlin(separator: String, prefix: String = "", suffix: String = ""): String {
val result = StringBuilder()
for(x in this) {
if (result.length() > 0) result.append(separator)
result.append(x.toKotlin())
if (size() > 0) {
result.append(prefix)
var first = true
for(x in this) {
if (!first) result.append(separator)
first = false
result.append(x.toKotlin())
}
result.append(suffix)
}
return result.toString()!!
}
fun String.withPrefix(prefix: String) = if (isEmpty()) "" else prefix + this
fun Expression.withPrefix(prefix: String) = if (isEmpty()) "" else prefix + toKotlin()
@@ -1,8 +0,0 @@
package org.jetbrains.jet.j2k.ast
public open class WhileStatement(val condition: Expression, val statement: Statement): Statement() {
public override fun toKotlin(): String {
return "while (" + condition.toKotlin() + ")\n" + statement.toKotlin()
}
}