From 8014b3df0bec4acc3e9d0dabbe6422b325329968 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 25 Sep 2020 20:49:08 +0700 Subject: [PATCH] Simplify asm.kt --- .../jvmMain/kotlin/kscience/kmath/asm/asm.kt | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt index edfe7f01d..53425e7e3 100644 --- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt +++ b/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt @@ -19,39 +19,37 @@ import kotlin.reflect.KClass * @author Alexander Nozik */ public fun MST.compileWith(type: KClass, algebra: Algebra): Expression { - fun AsmBuilder.visit(node: MST) { - when (node) { - is MST.Symbolic -> { - val symbol = try { - algebra.symbol(node.value) - } catch (ignored: Throwable) { - null - } - - if (symbol != null) - loadTConstant(symbol) - else - loadVariable(node.value) + fun AsmBuilder.visit(node: MST): Unit = when (node) { + is MST.Symbolic -> { + val symbol = try { + algebra.symbol(node.value) + } catch (ignored: Throwable) { + null } - is MST.Numeric -> loadNumeric(node.value) + if (symbol != null) + loadTConstant(symbol) + else + loadVariable(node.value) + } - is MST.Unary -> buildAlgebraOperationCall( - context = algebra, - name = node.operation, - fallbackMethodName = "unaryOperation", - parameterTypes = arrayOf(MstType.fromMst(node.value)) - ) { visit(node.value) } + is MST.Numeric -> loadNumeric(node.value) - is MST.Binary -> buildAlgebraOperationCall( - context = algebra, - name = node.operation, - fallbackMethodName = "binaryOperation", - parameterTypes = arrayOf(MstType.fromMst(node.left), MstType.fromMst(node.right)) - ) { - visit(node.left) - visit(node.right) - } + is MST.Unary -> buildAlgebraOperationCall( + context = algebra, + name = node.operation, + fallbackMethodName = "unaryOperation", + parameterTypes = arrayOf(MstType.fromMst(node.value)) + ) { visit(node.value) } + + is MST.Binary -> buildAlgebraOperationCall( + context = algebra, + name = node.operation, + fallbackMethodName = "binaryOperation", + parameterTypes = arrayOf(MstType.fromMst(node.left), MstType.fromMst(node.right)) + ) { + visit(node.left) + visit(node.right) } }