translator: fix support if without else, fix gitignore for gradle

This commit is contained in:
Alexey Stepanov
2016-07-21 14:26:39 +03:00
parent d8ab5e9e5f
commit 10216da2f9
6 changed files with 20 additions and 12 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ workspace.xml
.gradle
build
translator/idea_run.sh
translator/.gradle/2.9/taskArtifacts
# Ignore Gradle GUI config
gradle-app.setting
-3
View File
@@ -1,3 +0,0 @@
#!/bin/bash
java -jar build/libs/ast-kotlin-1.0-SNAPSHOT.jar $1 > $2
+1 -1
View File
@@ -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
@@ -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)
@@ -0,0 +1,2 @@
if_without_else_2(5) == 1256
if_without_else_2(55) == 1320
@@ -0,0 +1,7 @@
fun if_without_else_2(x: Int): Int {
var res = 1256
if (x > 10) {
res = res + 64
}
return res
}