Do numeric type conversion in JS MST compilers

This commit is contained in:
Iaroslav Postovalov 2021-12-07 12:00:31 +07:00
parent f6fa288011
commit a9779fe38b
2 changed files with 7 additions and 4 deletions

View File

@ -28,11 +28,14 @@ internal fun <T> MST.compileWith(algebra: Algebra<T>): Expression<T> {
variable(node.identity) variable(node.identity)
} }
is Numeric -> constant(node.value) is Numeric -> constant(
(algebra as? NumericAlgebra<T>)?.number(node.value) ?: error("Numeric nodes are not supported by $this")
)
is Unary -> when { is Unary -> when {
algebra is NumericAlgebra && node.value is Numeric -> constant( algebra is NumericAlgebra && node.value is Numeric -> constant(
algebra.unaryOperationFunction(node.operation)(algebra.number((node.value as Numeric).value))) algebra.unaryOperationFunction(node.operation)(algebra.number((node.value as Numeric).value))
)
else -> call(algebra.unaryOperationFunction(node.operation), visit(node.value)) else -> call(algebra.unaryOperationFunction(node.operation), visit(node.value))
} }

View File

@ -114,7 +114,7 @@ internal class DoubleWasmBuilder(target: MST) : WasmBuilder<Double, DoubleExpres
override fun createModule() = readBinary(f64StandardFunctions) override fun createModule() = readBinary(f64StandardFunctions)
override fun visitNumeric(node: Numeric) = ctx.f64.const(node.value) override fun visitNumeric(node: Numeric) = ctx.f64.const(node.value.toDouble())
override fun visitUnary(node: Unary): ExpressionRef = when (node.operation) { override fun visitUnary(node: Unary): ExpressionRef = when (node.operation) {
GroupOps.MINUS_OPERATION -> ctx.f64.neg(visit(node.value)) GroupOps.MINUS_OPERATION -> ctx.f64.neg(visit(node.value))
@ -157,7 +157,7 @@ internal class IntWasmBuilder(target: MST) : WasmBuilder<Int, IntExpression>(i32
} }
} }
override fun visitNumeric(node: Numeric) = ctx.i32.const(node.value) override fun visitNumeric(node: Numeric) = ctx.i32.const(node.value.toInt())
override fun visitUnary(node: Unary): ExpressionRef = when (node.operation) { override fun visitUnary(node: Unary): ExpressionRef = when (node.operation) {
GroupOps.MINUS_OPERATION -> ctx.i32.sub(ctx.i32.const(0), visit(node.value)) GroupOps.MINUS_OPERATION -> ctx.i32.sub(ctx.i32.const(0), visit(node.value))