diff --git a/.gitignore b/.gitignore index 0a9893d02e1..142bfde0137 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,7 @@ workspace.xml .gradle build -translator/idea_run.sh +translator/.gradle/2.9/taskArtifacts # Ignore Gradle GUI config gradle-app.setting diff --git a/translator/idea_run.sh b/translator/idea_run.sh deleted file mode 100755 index de4cd777fe6..00000000000 --- a/translator/idea_run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -java -jar build/libs/ast-kotlin-1.0-SNAPSHOT.jar $1 > $2 diff --git a/translator/run_tests.sh b/translator/run_tests.sh index ec0b32640a5..8de2c1ebe87 100755 --- a/translator/run_tests.sh +++ b/translator/run_tests.sh @@ -37,7 +37,7 @@ for i in $( ls "$DIRECTORY/input"); do clang-3.6 -S -emit-llvm "$DIRECTORY/c/$TEST.c" -o $DIRECTORY/linked/$TEST"_c.ll" -Wno-implicit-function-declaration fi - ./idea_run.sh $DIRECTORY/kotlin/$TEST.kt $DIRECTORY/linked/$TEST.ll + java -jar build/libs/ast-kotlin-1.0-SNAPSHOT.jar $DIRECTORY/kotlin/$TEST.kt > $DIRECTORY/linked/$TEST.ll llvm-link-3.6 $DIRECTORY/linked/* > $DIRECTORY/linked/run.ll lli-3.6 $DIRECTORY/linked/run.ll diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index 74c6a0cb156..b9cda9830d6 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -176,7 +176,7 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM private fun evaluateReferenceExpression(expr: KtReferenceExpression, scopeDepth: Int, classScope: ClassCodegen? = null): LLVMSingleValue? = when (expr) { is KtArrayAccessExpression -> evaluateArrayAccessExpression(expr, scopeDepth + 1) else -> if ((expr is KtNameReferenceExpression) && (classScope != null)) evaluatenameReferenceExpression(expr, scopeDepth + 1, classScope) - else variableManager.getLLVMvalue(expr.firstChild.text) + else variableManager.getLLVMvalue(expr.firstChild.text) } private fun evaluateCallExpression(expr: KtCallExpression, scopeDepth: Int, classScope: ClassCodegen? = null): LLVMSingleValue? { @@ -514,12 +514,12 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM val condition = getBrackets.getNextSiblingIgnoringWhitespaceAndComments() ?: return null getBrackets = condition.getNextSiblingIgnoringWhitespaceAndComments() ?: return null val thenExpression = getBrackets.getNextSiblingIgnoringWhitespaceAndComments() ?: return null - val elseKeyword = thenExpression.getNextSiblingIgnoringWhitespaceAndComments() ?: return null - val elseExpression = elseKeyword.getNextSiblingIgnoringWhitespaceAndComments() ?: return null + val elseKeyword = thenExpression.getNextSiblingIgnoringWhitespaceAndComments() + val elseExpression = elseKeyword?.getNextSiblingIgnoringWhitespaceAndComments() return when (ifExpression) { - null -> executeIfBlock(condition.firstChild as KtBinaryExpression, thenExpression.firstChild, elseExpression.firstChild, scopeDepth + 1) - else -> executeIfExpression(condition.firstChild as KtBinaryExpression, thenExpression.firstChild, elseExpression.firstChild, ifExpression, scopeDepth + 1) + null -> executeIfBlock(condition.firstChild as KtBinaryExpression, thenExpression.firstChild, elseExpression?.firstChild, scopeDepth + 1) + else -> executeIfExpression(condition.firstChild as KtBinaryExpression, thenExpression.firstChild, elseExpression!!.firstChild, ifExpression, scopeDepth + 1) } } @@ -552,10 +552,12 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM val elseLabel = codeBuilder.getNewLabel(prefix = "if") val endLabel = codeBuilder.getNewLabel(prefix = "if") - codeBuilder.addCondition(conditionResult, thenLabel, elseLabel) + codeBuilder.addCondition(conditionResult, thenLabel, if (elseExpression != null) elseLabel else endLabel) evaluateCodeBlock(thenExpression, thenLabel, endLabel, scopeDepth + 1) - evaluateCodeBlock(elseExpression, elseLabel, endLabel, scopeDepth + 1) + if (elseExpression != null) { + evaluateCodeBlock(elseExpression, elseLabel, endLabel, scopeDepth + 1) + } codeBuilder.markWithLabel(endLabel) diff --git a/translator/src/test/kotlin/tests/input/if_2.txt b/translator/src/test/kotlin/tests/input/if_2.txt new file mode 100644 index 00000000000..859d8bc2d12 --- /dev/null +++ b/translator/src/test/kotlin/tests/input/if_2.txt @@ -0,0 +1,2 @@ +if_without_else_2(5) == 1256 +if_without_else_2(55) == 1320 diff --git a/translator/src/test/kotlin/tests/kotlin/if_2.kt b/translator/src/test/kotlin/tests/kotlin/if_2.kt new file mode 100644 index 00000000000..a8285242c9e --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/if_2.kt @@ -0,0 +1,7 @@ +fun if_without_else_2(x: Int): Int { + var res = 1256 + if (x > 10) { + res = res + 64 + } + return res +} \ No newline at end of file