translator: function argument evalution

This commit is contained in:
e5l
2016-07-13 19:13:47 +03:00
parent 82d8293e56
commit fe27c80648
@@ -84,7 +84,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
is KtProperty -> evaluateLeafPsiElement(expr.firstChild as LeafPsiElement, scopeDepth) is KtProperty -> evaluateLeafPsiElement(expr.firstChild as LeafPsiElement, scopeDepth)
is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth) is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth)
is KtCallExpression -> { is KtCallExpression -> {
val expression = evaluateCallExpression(expr) as LLVMCall val expression = evaluateCallExpression(expr, scopeDepth) as LLVMCall
codeBuilder.addLLVMCode(expression.toString()) codeBuilder.addLLVMCode(expression.toString())
} }
is PsiElement -> evaluateExpression(expr.firstChild, scopeDepth + 1) is PsiElement -> evaluateExpression(expr.firstChild, scopeDepth + 1)
@@ -102,10 +102,10 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
return when (expr) { return when (expr) {
is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth) is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth)
is KtConstantExpression -> evaluateConstantExpression(expr) is KtConstantExpression -> evaluateConstantExpression(expr)
is KtCallExpression -> evaluateCallExpression(expr) is KtCallExpression -> evaluateCallExpression(expr, scopeDepth)
is KtReferenceExpression -> evaluateReferenceExpression(expr) is KtReferenceExpression -> evaluateReferenceExpression(expr)
is KtIfExpression -> evaluateIfOperator(expr.firstChild as LeafPsiElement, scopeDepth + 1, true) is KtIfExpression -> evaluateIfOperator(expr.firstChild as LeafPsiElement, scopeDepth + 1, true)
is KtDotQualifiedExpression -> evaluteDotExpression(expr, scopeDepth) is KtDotQualifiedExpression -> evaluateDotExpression(expr, scopeDepth)
is PsiWhiteSpace -> null is PsiWhiteSpace -> null
is PsiElement -> evaluatePsiElement(expr, scopeDepth) is PsiElement -> evaluatePsiElement(expr, scopeDepth)
null -> null null -> null
@@ -113,7 +113,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
} }
} }
private fun evaluteDotExpression(expr: KtDotQualifiedExpression, scopeDepth: Int): LLVMSingleValue? { private fun evaluateDotExpression(expr: KtDotQualifiedExpression, scopeDepth: Int): LLVMSingleValue? {
val receiverName = expr.receiverExpression.text val receiverName = expr.receiverExpression.text
val selectorName = expr.selectorExpression!!.text val selectorName = expr.selectorExpression!!.text
@@ -132,28 +132,27 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
return variableManager.getLLVMvalue(variableName) return variableManager.getLLVMvalue(variableName)
} }
private fun evaluateCallExpression(expr: KtCallExpression): LLVMSingleValue? { private fun evaluateCallExpression(expr: KtCallExpression, scopeDepth: Int): LLVMSingleValue? {
val function = expr.firstChild.firstChild.text val function = expr.firstChild.firstChild.text
if (state.functions.containsKey(function)) { if (state.functions.containsKey(function)) {
return evaluateFunctionCallExpression(expr) return evaluateFunctionCallExpression(expr, scopeDepth)
} }
if (state.classes.containsKey(function)) { if (state.classes.containsKey(function)) {
return evaluateConstructorCallExpression(expr) return evaluateConstructorCallExpression(expr, scopeDepth)
} }
return null return null
} }
private fun evaluateConstructorCallExpression(expr: KtCallExpression): LLVMSingleValue? { private fun evaluateConstructorCallExpression(expr: KtCallExpression, scopeDepth: Int): LLVMSingleValue? {
val function = expr.firstChild.firstChild val function = expr.firstChild.firstChild
val descriptor = state.classes[function.text] ?: return null val descriptor = state.classes[function.text] ?: return null
val names = parseArgList(expr).mapIndexed { i: Int, s: String -> val names = parseArgList(expr, scopeDepth).mapIndexed { i: Int, v: LLVMSingleValue ->
LLVMVariable(s, descriptor.fields[i].type, pointer = descriptor.fields[i].pointer) LLVMVariable(v.toString(), descriptor.fields[i].type, pointer = descriptor.fields[i].pointer)
}.toList() }.toList()
return LLVMConstructorCall( return LLVMConstructorCall(
descriptor.type, fun(thisVar): LLVMCall { descriptor.type, fun(thisVar): LLVMCall {
val args = ArrayList<LLVMVariable>() val args = ArrayList<LLVMVariable>()
@@ -163,29 +162,25 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
}) })
} }
private fun evaluateFunctionCallExpression(expr: KtCallExpression): LLVMSingleValue? { private fun evaluateFunctionCallExpression(expr: KtCallExpression, scopeDepth: Int): LLVMSingleValue? {
val function = expr.firstChild.firstChild val function = expr.firstChild.firstChild
val descriptor = state.functions[function.text] ?: return null val descriptor = state.functions[function.text] ?: return null
val names = parseArgList(expr) val names = parseArgList(expr, scopeDepth)
return LLVMCall(descriptor.returnType, "@${descriptor.name}", descriptor.args?.mapIndexed { return LLVMCall(descriptor.returnType, "@${descriptor.name}", descriptor.args?.mapIndexed {
i: Int, variable: LLVMVariable -> i: Int, variable: LLVMVariable ->
LLVMVariable(names[i], variable.type, pointer = variable.pointer) LLVMVariable(names[i].toString(), variable.type, pointer = variable.pointer)
} ?: listOf()) } ?: listOf())
} }
private fun parseArgList(expr: KtCallExpression): List<String> { private fun parseArgList(expr: KtCallExpression, scopeDepth: Int): List<LLVMSingleValue> {
val args = expr.getValueArgumentsInParentheses() val args = expr.getValueArgumentsInParentheses()
val result = ArrayList<String>() val result = ArrayList<LLVMSingleValue>()
for (arg in args) { for (arg in args) {
var text = (arg as KtValueArgument).text val expr = evaluateExpression(arg.getArgumentExpression(), scopeDepth) as LLVMSingleValue
if (text.startsWith("::")) { result.add(expr)
text = "@${text.substring(2)}"
}
result.add(text)
} }
return result return result