Some refactoring of AST rendering #370

Merged
CommanderTvis merged 1 commits from commandertvis/astrendering into dev 2021-06-17 21:01:13 +03:00
3 changed files with 68 additions and 68 deletions
Showing only changes of commit 5d5f84ad55 - Show all commits

View File

@ -54,7 +54,7 @@ public open class FeaturedMathRenderer(public val features: List<RenderFeature>)
@UnstableKMathAPI @UnstableKMathAPI
public open class FeaturedMathRendererWithPostProcess( public open class FeaturedMathRendererWithPostProcess(
features: List<RenderFeature>, features: List<RenderFeature>,
public val stages: List<PostProcessStage>, public val stages: List<PostProcessPhase>,
) : FeaturedMathRenderer(features) { ) : FeaturedMathRenderer(features) {
public override fun render(mst: MST): MathSyntax { public override fun render(mst: MST): MathSyntax {
val res = super.render(mst) val res = super.render(mst)
@ -65,7 +65,7 @@ public open class FeaturedMathRendererWithPostProcess(
/** /**
* Logical unit of [MathSyntax] post-processing. * Logical unit of [MathSyntax] post-processing.
*/ */
public fun interface PostProcessStage { public fun interface PostProcessPhase {
/** /**
* Performs the specified action over [MathSyntax]. * Performs the specified action over [MathSyntax].
*/ */
@ -102,7 +102,7 @@ public open class FeaturedMathRendererWithPostProcess(
// Printing terminal nodes as string // Printing terminal nodes as string
PrintNumeric, PrintNumeric,
PrintSymbolic, PrintSymbol,
), ),
listOf( listOf(
BetterExponent, BetterExponent,

View File

@ -13,13 +13,12 @@ import space.kscience.kmath.operations.*
import kotlin.reflect.KClass import kotlin.reflect.KClass
/** /**
* Prints any [MST.Symbolic] as a [SymbolSyntax] containing the [MST.Symbolic.value] of it. * Prints any [Symbol] as a [SymbolSyntax] containing the [Symbol.value] of it.
* *
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public object PrintSymbolic : RenderFeature { public val PrintSymbol: RenderFeature = RenderFeature { _, node ->
public override fun render(renderer: FeaturedMathRenderer, node: MST): SymbolSyntax? =
if (node !is Symbol) null if (node !is Symbol) null
else SymbolSyntax(string = node.identity) else SymbolSyntax(string = node.identity)
} }
@ -30,8 +29,8 @@ public object PrintSymbolic : RenderFeature {
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public object PrintNumeric : RenderFeature { public val PrintNumeric: RenderFeature = RenderFeature { _, node ->
public override fun render(renderer: FeaturedMathRenderer, node: MST): NumberSyntax? = if (node !is MST.Numeric) if (node !is MST.Numeric)
null null
else else
NumberSyntax(string = node.value.toString()) NumberSyntax(string = node.value.toString())
@ -141,7 +140,7 @@ public class PrettyPrintIntegers(public val types: Set<KClass<out Number>>) : Re
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class PrettyPrintPi(public val symbols: Set<String>) : RenderFeature { public class PrettyPrintPi(public val symbols: Set<String>) : RenderFeature {
public override fun render(renderer: FeaturedMathRenderer, node: MST): SpecialSymbolSyntax? = public override fun render(renderer: FeaturedMathRenderer, node: MST): MathSyntax? =
if (node !is Symbol || node.identity !in symbols) if (node !is Symbol || node.identity !in symbols)
null null
else else
@ -203,7 +202,7 @@ public abstract class Binary(public val operations: Collection<String>?) : Rende
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class BinaryPlus(operations: Collection<String>?) : Binary(operations) { public class BinaryPlus(operations: Collection<String>?) : Binary(operations) {
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): BinaryPlusSyntax = public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryPlusSyntax( BinaryPlusSyntax(
operation = node.operation, operation = node.operation,
left = OperandSyntax(parent.render(node.left), true), left = OperandSyntax(parent.render(node.left), true),
@ -225,7 +224,7 @@ public class BinaryPlus(operations: Collection<String>?) : Binary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class BinaryMinus(operations: Collection<String>?) : Binary(operations) { public class BinaryMinus(operations: Collection<String>?) : Binary(operations) {
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): BinaryMinusSyntax = public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryMinusSyntax( BinaryMinusSyntax(
operation = node.operation, operation = node.operation,
left = OperandSyntax(operand = parent.render(node.left), parentheses = true), left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
@ -247,7 +246,7 @@ public class BinaryMinus(operations: Collection<String>?) : Binary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class UnaryPlus(operations: Collection<String>?) : Unary(operations) { public class UnaryPlus(operations: Collection<String>?) : Unary(operations) {
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryPlusSyntax = UnaryPlusSyntax( public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryPlusSyntax(
operation = node.operation, operation = node.operation,
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true), operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
) )
@ -267,7 +266,7 @@ public class UnaryPlus(operations: Collection<String>?) : Unary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class UnaryMinus(operations: Collection<String>?) : Unary(operations) { public class UnaryMinus(operations: Collection<String>?) : Unary(operations) {
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryMinusSyntax = UnaryMinusSyntax( public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = UnaryMinusSyntax(
operation = node.operation, operation = node.operation,
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true), operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
) )
@ -287,7 +286,7 @@ public class UnaryMinus(operations: Collection<String>?) : Unary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class Fraction(operations: Collection<String>?) : Binary(operations) { public class Fraction(operations: Collection<String>?) : Binary(operations) {
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): FractionSyntax = FractionSyntax( public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax = FractionSyntax(
operation = node.operation, operation = node.operation,
left = OperandSyntax(operand = parent.render(node.left), parentheses = true), left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
right = OperandSyntax(operand = parent.render(node.right), parentheses = true), right = OperandSyntax(operand = parent.render(node.right), parentheses = true),
@ -309,7 +308,7 @@ public class Fraction(operations: Collection<String>?) : Binary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class BinaryOperator(operations: Collection<String>?) : Binary(operations) { public class BinaryOperator(operations: Collection<String>?) : Binary(operations) {
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): BinaryOperatorSyntax = public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
BinaryOperatorSyntax( BinaryOperatorSyntax(
operation = node.operation, operation = node.operation,
prefix = OperatorNameSyntax(name = node.operation), prefix = OperatorNameSyntax(name = node.operation),
@ -332,7 +331,7 @@ public class BinaryOperator(operations: Collection<String>?) : Binary(operations
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class UnaryOperator(operations: Collection<String>?) : Unary(operations) { public class UnaryOperator(operations: Collection<String>?) : Unary(operations) {
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryOperatorSyntax = public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax( UnaryOperatorSyntax(
operation = node.operation, operation = node.operation,
prefix = OperatorNameSyntax(node.operation), prefix = OperatorNameSyntax(node.operation),
@ -354,7 +353,7 @@ public class UnaryOperator(operations: Collection<String>?) : Unary(operations)
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class Power(operations: Collection<String>?) : Binary(operations) { public class Power(operations: Collection<String>?) : Binary(operations) {
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): SuperscriptSyntax = public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
SuperscriptSyntax( SuperscriptSyntax(
operation = node.operation, operation = node.operation,
left = OperandSyntax(parent.render(node.left), true), left = OperandSyntax(parent.render(node.left), true),
@ -374,7 +373,7 @@ public class Power(operations: Collection<String>?) : Binary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class SquareRoot(operations: Collection<String>?) : Unary(operations) { public class SquareRoot(operations: Collection<String>?) : Unary(operations) {
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): RadicalSyntax = public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
RadicalSyntax(operation = node.operation, operand = parent.render(node.value)) RadicalSyntax(operation = node.operation, operand = parent.render(node.value))
public companion object { public companion object {
@ -392,7 +391,7 @@ public class SquareRoot(operations: Collection<String>?) : Unary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class Exponent(operations: Collection<String>?) : Unary(operations) { public class Exponent(operations: Collection<String>?) : Unary(operations) {
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): ExponentSyntax = ExponentSyntax( public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax = ExponentSyntax(
operation = node.operation, operation = node.operation,
operand = OperandSyntax(operand = parent.render(node.value), parentheses = true), operand = OperandSyntax(operand = parent.render(node.value), parentheses = true),
useOperatorForm = true, useOperatorForm = true,
@ -413,7 +412,7 @@ public class Exponent(operations: Collection<String>?) : Unary(operations) {
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class Multiplication(operations: Collection<String>?) : Binary(operations) { public class Multiplication(operations: Collection<String>?) : Binary(operations) {
public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MultiplicationSyntax = public override fun renderBinary(parent: FeaturedMathRenderer, node: MST.Binary): MathSyntax =
MultiplicationSyntax( MultiplicationSyntax(
operation = node.operation, operation = node.operation,
left = OperandSyntax(operand = parent.render(node.left), parentheses = true), left = OperandSyntax(operand = parent.render(node.left), parentheses = true),
@ -436,7 +435,7 @@ public class Multiplication(operations: Collection<String>?) : Binary(operations
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class InverseTrigonometricOperations(operations: Collection<String>?) : Unary(operations) { public class InverseTrigonometricOperations(operations: Collection<String>?) : Unary(operations) {
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryOperatorSyntax = public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax( UnaryOperatorSyntax(
operation = node.operation, operation = node.operation,
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "arc")), prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "arc")),
@ -463,7 +462,7 @@ public class InverseTrigonometricOperations(operations: Collection<String>?) : U
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class InverseHyperbolicOperations(operations: Collection<String>?) : Unary(operations) { public class InverseHyperbolicOperations(operations: Collection<String>?) : Unary(operations) {
public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): UnaryOperatorSyntax = public override fun renderUnary(parent: FeaturedMathRenderer, node: MST.Unary): MathSyntax =
UnaryOperatorSyntax( UnaryOperatorSyntax(
operation = node.operation, operation = node.operation,
prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "ar")), prefix = OperatorNameSyntax(name = node.operation.replaceFirst("a", "ar")),

View File

@ -5,6 +5,7 @@
package space.kscience.kmath.ast.rendering package space.kscience.kmath.ast.rendering
import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess.PostProcessPhase
import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.FieldOperations import space.kscience.kmath.operations.FieldOperations
import space.kscience.kmath.operations.GroupOperations import space.kscience.kmath.operations.GroupOperations
@ -17,8 +18,8 @@ import space.kscience.kmath.operations.RingOperations
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public object BetterMultiplication : FeaturedMathRendererWithPostProcess.PostProcessStage { public val BetterMultiplication: PostProcessPhase = PostProcessPhase { node ->
public override fun perform(node: MathSyntax): Unit = when (node) { fun perform(node: MathSyntax): Unit = when (node) {
is NumberSyntax -> Unit is NumberSyntax -> Unit
is SymbolSyntax -> Unit is SymbolSyntax -> Unit
is OperatorNameSyntax -> Unit is OperatorNameSyntax -> Unit
@ -81,6 +82,8 @@ public object BetterMultiplication : FeaturedMathRendererWithPostProcess.PostPro
perform(node.right) perform(node.right)
} }
} }
perform(node)
} }
/** /**
@ -89,68 +92,68 @@ public object BetterMultiplication : FeaturedMathRendererWithPostProcess.PostPro
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public object BetterFraction : FeaturedMathRendererWithPostProcess.PostProcessStage { public val BetterFraction: PostProcessPhase = PostProcessPhase { node ->
private fun perform0(node: MathSyntax, infix: Boolean = false): Unit = when (node) { fun perform(node: MathSyntax, infix: Boolean = false): Unit = when (node) {
is NumberSyntax -> Unit is NumberSyntax -> Unit
is SymbolSyntax -> Unit is SymbolSyntax -> Unit
is OperatorNameSyntax -> Unit is OperatorNameSyntax -> Unit
is SpecialSymbolSyntax -> Unit is SpecialSymbolSyntax -> Unit
is OperandSyntax -> perform0(node.operand, infix) is OperandSyntax -> perform(node.operand, infix)
is UnaryOperatorSyntax -> { is UnaryOperatorSyntax -> {
perform0(node.prefix, infix) perform(node.prefix, infix)
perform0(node.operand, infix) perform(node.operand, infix)
} }
is UnaryPlusSyntax -> perform0(node.operand, infix) is UnaryPlusSyntax -> perform(node.operand, infix)
is UnaryMinusSyntax -> perform0(node.operand, infix) is UnaryMinusSyntax -> perform(node.operand, infix)
is RadicalSyntax -> perform0(node.operand, infix) is RadicalSyntax -> perform(node.operand, infix)
is ExponentSyntax -> perform0(node.operand, infix) is ExponentSyntax -> perform(node.operand, infix)
is SuperscriptSyntax -> { is SuperscriptSyntax -> {
perform0(node.left, true) perform(node.left, true)
perform0(node.right, true) perform(node.right, true)
} }
is SubscriptSyntax -> { is SubscriptSyntax -> {
perform0(node.left, true) perform(node.left, true)
perform0(node.right, true) perform(node.right, true)
} }
is BinaryOperatorSyntax -> { is BinaryOperatorSyntax -> {
perform0(node.prefix, infix) perform(node.prefix, infix)
perform0(node.left, infix) perform(node.left, infix)
perform0(node.right, infix) perform(node.right, infix)
} }
is BinaryPlusSyntax -> { is BinaryPlusSyntax -> {
perform0(node.left, infix) perform(node.left, infix)
perform0(node.right, infix) perform(node.right, infix)
} }
is BinaryMinusSyntax -> { is BinaryMinusSyntax -> {
perform0(node.left, infix) perform(node.left, infix)
perform0(node.right, infix) perform(node.right, infix)
} }
is FractionSyntax -> { is FractionSyntax -> {
node.infix = infix node.infix = infix
perform0(node.left, infix) perform(node.left, infix)
perform0(node.right, infix) perform(node.right, infix)
} }
is RadicalWithIndexSyntax -> { is RadicalWithIndexSyntax -> {
perform0(node.left, true) perform(node.left, true)
perform0(node.right, true) perform(node.right, true)
} }
is MultiplicationSyntax -> { is MultiplicationSyntax -> {
perform0(node.left, infix) perform(node.left, infix)
perform0(node.right, infix) perform(node.right, infix)
} }
} }
public override fun perform(node: MathSyntax): Unit = perform0(node) perform(node)
} }
/** /**
@ -160,39 +163,37 @@ public object BetterFraction : FeaturedMathRendererWithPostProcess.PostProcessSt
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public object BetterExponent : FeaturedMathRendererWithPostProcess.PostProcessStage { public val BetterExponent: PostProcessPhase = PostProcessPhase { node ->
private fun perform0(node: MathSyntax): Boolean { fun perform(node: MathSyntax): Boolean {
return when (node) { return when (node) {
is NumberSyntax -> false is NumberSyntax -> false
is SymbolSyntax -> false is SymbolSyntax -> false
is OperatorNameSyntax -> false is OperatorNameSyntax -> false
is SpecialSymbolSyntax -> false is SpecialSymbolSyntax -> false
is OperandSyntax -> perform0(node.operand) is OperandSyntax -> perform(node.operand)
is UnaryOperatorSyntax -> perform0(node.prefix) || perform0(node.operand) is UnaryOperatorSyntax -> perform(node.prefix) || perform(node.operand)
is UnaryPlusSyntax -> perform0(node.operand) is UnaryPlusSyntax -> perform(node.operand)
is UnaryMinusSyntax -> perform0(node.operand) is UnaryMinusSyntax -> perform(node.operand)
is RadicalSyntax -> true is RadicalSyntax -> true
is ExponentSyntax -> { is ExponentSyntax -> {
val r = perform0(node.operand) val r = perform(node.operand)
node.useOperatorForm = r node.useOperatorForm = r
r r
} }
is SuperscriptSyntax -> true is SuperscriptSyntax -> true
is SubscriptSyntax -> true is SubscriptSyntax -> true
is BinaryOperatorSyntax -> perform0(node.prefix) || perform0(node.left) || perform0(node.right) is BinaryOperatorSyntax -> perform(node.prefix) || perform(node.left) || perform(node.right)
is BinaryPlusSyntax -> perform0(node.left) || perform0(node.right) is BinaryPlusSyntax -> perform(node.left) || perform(node.right)
is BinaryMinusSyntax -> perform0(node.left) || perform0(node.right) is BinaryMinusSyntax -> perform(node.left) || perform(node.right)
is FractionSyntax -> true is FractionSyntax -> true
is RadicalWithIndexSyntax -> true is RadicalWithIndexSyntax -> true
is MultiplicationSyntax -> perform0(node.left) || perform0(node.right) is MultiplicationSyntax -> perform(node.left) || perform(node.right)
} }
} }
public override fun perform(node: MathSyntax) { perform(node)
perform0(node)
}
} }
/** /**
@ -203,7 +204,7 @@ public object BetterExponent : FeaturedMathRendererWithPostProcess.PostProcessSt
*/ */
@UnstableKMathAPI @UnstableKMathAPI
public class SimplifyParentheses(public val precedenceFunction: (MathSyntax) -> Int) : public class SimplifyParentheses(public val precedenceFunction: (MathSyntax) -> Int) :
FeaturedMathRendererWithPostProcess.PostProcessStage { PostProcessPhase {
public override fun perform(node: MathSyntax): Unit = when (node) { public override fun perform(node: MathSyntax): Unit = when (node) {
is NumberSyntax -> Unit is NumberSyntax -> Unit
is SymbolSyntax -> Unit is SymbolSyntax -> Unit