translator: add unary operators

This commit is contained in:
Alexey Stepanov
2016-08-10 13:02:38 +03:00
parent 9d0d213a09
commit c24c199bbd
3 changed files with 32 additions and 1 deletions
@@ -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)
@@ -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
@@ -0,0 +1,7 @@
fun unary_operator_1_minus(x : Int): Int {
return -x
}
fun unary_operator_1_plus(x : Int): Int {
return x
}