Dev #194

Merged
altavir merged 266 commits from dev into master 2021-01-20 17:32:32 +03:00
Showing only changes of commit 8014b3df0b - Show all commits

View File

@ -19,39 +19,37 @@ import kotlin.reflect.KClass
* @author Alexander Nozik * @author Alexander Nozik
*/ */
public fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<T> { public fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<T> {
fun AsmBuilder<T>.visit(node: MST) { fun AsmBuilder<T>.visit(node: MST): Unit = when (node) {
when (node) { is MST.Symbolic -> {
is MST.Symbolic -> { val symbol = try {
val symbol = try { algebra.symbol(node.value)
algebra.symbol(node.value) } catch (ignored: Throwable) {
} catch (ignored: Throwable) { null
null
}
if (symbol != null)
loadTConstant(symbol)
else
loadVariable(node.value)
} }
is MST.Numeric -> loadNumeric(node.value) if (symbol != null)
loadTConstant(symbol)
else
loadVariable(node.value)
}
is MST.Unary -> buildAlgebraOperationCall( is MST.Numeric -> loadNumeric(node.value)
context = algebra,
name = node.operation,
fallbackMethodName = "unaryOperation",
parameterTypes = arrayOf(MstType.fromMst(node.value))
) { visit(node.value) }
is MST.Binary -> buildAlgebraOperationCall( is MST.Unary -> buildAlgebraOperationCall(
context = algebra, context = algebra,
name = node.operation, name = node.operation,
fallbackMethodName = "binaryOperation", fallbackMethodName = "unaryOperation",
parameterTypes = arrayOf(MstType.fromMst(node.left), MstType.fromMst(node.right)) parameterTypes = arrayOf(MstType.fromMst(node.value))
) { ) { visit(node.value) }
visit(node.left)
visit(node.right) 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)
} }
} }