translator: add unary operators
This commit is contained in:
@@ -83,6 +83,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
|
|||||||
return when (expr) {
|
return when (expr) {
|
||||||
is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth)
|
is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth)
|
||||||
is KtPostfixExpression -> evaluatePostfixExpression(expr, scopeDepth)
|
is KtPostfixExpression -> evaluatePostfixExpression(expr, scopeDepth)
|
||||||
|
is KtPrefixExpression -> evaluatePrefixExpression(expr, scopeDepth)
|
||||||
is KtConstantExpression -> evaluateConstantExpression(expr)
|
is KtConstantExpression -> evaluateConstantExpression(expr)
|
||||||
is KtCallExpression -> evaluateCallExpression(expr, scopeDepth)
|
is KtCallExpression -> evaluateCallExpression(expr, scopeDepth)
|
||||||
is KtWhenExpression -> evaluateWhenExpression(expr, scopeDepth)
|
is KtWhenExpression -> evaluateWhenExpression(expr, scopeDepth)
|
||||||
@@ -175,7 +176,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
|
|||||||
var receiver = when (receiverExpr) {
|
var receiver = when (receiverExpr) {
|
||||||
is KtCallExpression,
|
is KtCallExpression,
|
||||||
is KtBinaryExpression,
|
is KtBinaryExpression,
|
||||||
is KtDotQualifiedExpression-> evaluateExpression(receiverExpr, scopeDepth) as LLVMVariable
|
is KtDotQualifiedExpression -> evaluateExpression(receiverExpr, scopeDepth) as LLVMVariable
|
||||||
is KtNameReferenceExpression -> {
|
is KtNameReferenceExpression -> {
|
||||||
val referenceContext = state.bindingContext.get(BindingContext.REFERENCE_TARGET, receiverExpr)
|
val referenceContext = state.bindingContext.get(BindingContext.REFERENCE_TARGET, receiverExpr)
|
||||||
variableManager[receiverName]
|
variableManager[receiverName]
|
||||||
@@ -528,9 +529,18 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
|
|||||||
return executePostfixExpression(operator, expr.operationReference, left as LLVMVariable)
|
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?
|
private fun executePostfixExpression(operator: IElementType?, operationReference: KtSimpleNameExpression, left: LLVMVariable): LLVMSingleValue?
|
||||||
= addPrimitivePostfixOperation(operator, operationReference, left)
|
= 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? {
|
private fun addPrimitivePostfixOperation(operator: IElementType?, operationReference: KtSimpleNameExpression, firstOp: LLVMVariable): LLVMSingleValue? {
|
||||||
val firstNativeOp = codeBuilder.receiveNativeValue(firstOp)
|
val firstNativeOp = codeBuilder.receiveNativeValue(firstOp)
|
||||||
when (operator) {
|
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)
|
fun executeBinaryExpression(operator: IElementType, referenceName: KtSimpleNameExpression?, left: LLVMSingleValue, right: LLVMSingleValue)
|
||||||
= addPrimitiveBinaryOperation(operator, referenceName, left, right)
|
= 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user