From c24c199bbd06cf2f864a03127f266a1c0f7d8ff5 Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Wed, 10 Aug 2016 13:02:38 +0300 Subject: [PATCH] translator: add unary operators --- .../kotlinnative/translator/BlockCodegen.kt | 22 ++++++++++++++++++- .../kotlin/tests/input/unary_operators_1.txt | 4 ++++ .../kotlin/tests/kotlin/unary_operators_1.kt | 7 ++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 translator/src/test/kotlin/tests/input/unary_operators_1.txt create mode 100644 translator/src/test/kotlin/tests/kotlin/unary_operators_1.kt diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index 7a6d5cbbedb..dc17150d779 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -83,6 +83,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va return when (expr) { is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth) is KtPostfixExpression -> evaluatePostfixExpression(expr, scopeDepth) + is KtPrefixExpression -> evaluatePrefixExpression(expr, scopeDepth) is KtConstantExpression -> evaluateConstantExpression(expr) is KtCallExpression -> evaluateCallExpression(expr, scopeDepth) is KtWhenExpression -> evaluateWhenExpression(expr, scopeDepth) @@ -175,7 +176,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va var receiver = when (receiverExpr) { is KtCallExpression, is KtBinaryExpression, - is KtDotQualifiedExpression-> evaluateExpression(receiverExpr, scopeDepth) as LLVMVariable + is KtDotQualifiedExpression -> evaluateExpression(receiverExpr, scopeDepth) as LLVMVariable is KtNameReferenceExpression -> { val referenceContext = state.bindingContext.get(BindingContext.REFERENCE_TARGET, receiverExpr) variableManager[receiverName] @@ -528,9 +529,18 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va return executePostfixExpression(operator, expr.operationReference, left as LLVMVariable) } + private fun evaluatePrefixExpression(expr: KtPrefixExpression, scopeDepth: Int): LLVMSingleValue? { + val operator = expr.operationToken + val left = evaluateExpression(expr.baseExpression, scopeDepth) ?: throw UnsupportedOperationException("Wrong binary exception") + return executePrefixExpression(operator, expr.operationReference, left as LLVMSingleValue) + } + private fun executePostfixExpression(operator: IElementType?, operationReference: KtSimpleNameExpression, left: LLVMVariable): LLVMSingleValue? = addPrimitivePostfixOperation(operator, operationReference, left) + private fun executePrefixExpression(operator: IElementType?, operationReference: KtSimpleNameExpression, left: LLVMSingleValue): LLVMSingleValue? + = addPrimitivePrefixOperation(operator, operationReference, left) + private fun addPrimitivePostfixOperation(operator: IElementType?, operationReference: KtSimpleNameExpression, firstOp: LLVMVariable): LLVMSingleValue? { val firstNativeOp = codeBuilder.receiveNativeValue(firstOp) when (operator) { @@ -555,6 +565,16 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va } } + private fun addPrimitivePrefixOperation(operator: IElementType?, operationReference: KtSimpleNameExpression, firstOp: LLVMSingleValue): LLVMSingleValue? { + when (operator) { + KtTokens.MINUS, + KtTokens.PLUS -> { + return addPrimitiveBinaryOperation(operator!!, operationReference, LLVMConstant("0", firstOp.type), firstOp) + } + else -> throw UnsupportedOperationException() + } + } + fun executeBinaryExpression(operator: IElementType, referenceName: KtSimpleNameExpression?, left: LLVMSingleValue, right: LLVMSingleValue) = addPrimitiveBinaryOperation(operator, referenceName, left, right) diff --git a/translator/src/test/kotlin/tests/input/unary_operators_1.txt b/translator/src/test/kotlin/tests/input/unary_operators_1.txt new file mode 100644 index 00000000000..2e18cb0bbea --- /dev/null +++ b/translator/src/test/kotlin/tests/input/unary_operators_1.txt @@ -0,0 +1,4 @@ +unary_operator_1_minus_Int(-5) == 5 +unary_operator_1_minus_Int(5) == -5 +unary_operator_1_plus_Int(-5) == -5 +unary_operator_1_plus_Int(5) == 5 diff --git a/translator/src/test/kotlin/tests/kotlin/unary_operators_1.kt b/translator/src/test/kotlin/tests/kotlin/unary_operators_1.kt new file mode 100644 index 00000000000..eec10598eaf --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/unary_operators_1.kt @@ -0,0 +1,7 @@ +fun unary_operator_1_minus(x : Int): Int { + return -x +} + +fun unary_operator_1_plus(x : Int): Int { + return x +} \ No newline at end of file