Skip to content

Commit 8045929

Browse files
committed
renamed Node to AST
1 parent 1edee5b commit 8045929

File tree

12 files changed

+42
-41
lines changed

12 files changed

+42
-41
lines changed

math-parser-spire/src/main/scala/mathParser/algebra/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package mathParser
22

33
package object algebra {
44
type SpireLanguage[A, V] = Language[SpireUnitaryOperator, SpireBinaryOperator, A, V]
5-
type SpireNode[A, V] = Node[SpireUnitaryOperator, SpireBinaryOperator, A, V]
5+
type SpireNode[A, V] = AbstractSyntaxTree[SpireUnitaryOperator, SpireBinaryOperator, A, V]
66
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package mathParser
22

3-
sealed trait Node[UO, BO, S, V] {
3+
enum AbstractSyntaxTree[UO, BO, S, V]:
4+
case ConstantNode[UO, BO, S, V](value: S) extends AbstractSyntaxTree[UO, BO, S, V]
5+
case UnitaryNode[UO, BO, S, V](op: UO, child: AbstractSyntaxTree[UO, BO, S, V]) extends AbstractSyntaxTree[UO, BO, S, V]
6+
case BinaryNode[UO, BO, S, V](op: BO, childLeft: AbstractSyntaxTree[UO, BO, S, V], childRight: AbstractSyntaxTree[UO, BO, S, V]) extends AbstractSyntaxTree[UO, BO, S, V]
7+
case VariableNode[UO, BO, S, V](v: V) extends AbstractSyntaxTree[UO, BO, S, V]
48

59
// todo: make tailrec
610
def fold[A](ifConstant: S => A, ifUnitary: (UO, A) => A, ifBinary: (BO, A, A) => A, ifVariable: V => A): A = {
7-
def rec(node: Node[UO, BO, S, V]): A = node match {
11+
def rec(node: AbstractSyntaxTree[UO, BO, S, V]): A = node match {
812
case ConstantNode(value) => ifConstant(value)
913
case UnitaryNode(op, child) => ifUnitary(op, rec(child))
1014
case BinaryNode(op, left, right) => ifBinary(op, rec(left), rec(right))
@@ -13,9 +17,5 @@ sealed trait Node[UO, BO, S, V] {
1317

1418
rec(this)
1519
}
16-
}
1720

18-
case class ConstantNode[UO, BO, S, V](value: S) extends Node[UO, BO, S, V]
19-
case class UnitaryNode[UO, BO, S, V](op: UO, child: Node[UO, BO, S, V]) extends Node[UO, BO, S, V]
20-
case class BinaryNode[UO, BO, S, V](op: BO, childLeft: Node[UO, BO, S, V], childRight: Node[UO, BO, S, V]) extends Node[UO, BO, S, V]
21-
case class VariableNode[UO, BO, S, V](v: V) extends Node[UO, BO, S, V]
21+
export AbstractSyntaxTree.*

math-parser/src/main/scala/mathParser/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package mathParser
33
import scala.util.Try
44

55
trait Compiler[UO, BO, S, V, F] {
6-
def compile(node: Node[UO, BO, S, V]): Try[F]
6+
def compile(node: AbstractSyntaxTree[UO, BO, S, V]): Try[F]
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package mathParser
22

33
trait Derive[UO, BO, S, V] {
4-
def derive(node: Node[UO, BO, S, V])(variable: V): Node[UO, BO, S, V]
4+
def derive(node: AbstractSyntaxTree[UO, BO, S, V])(variable: V): AbstractSyntaxTree[UO, BO, S, V]
55
}

math-parser/src/main/scala/mathParser/Evaluate.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ trait Evaluate[UO, BO, S, V] {
55

66
def executeBinaryOperator(bo: BO, left: S, right: S): S
77

8-
def evaluate(node: Node[UO, BO, S, V])(variableAssignment: V => S): S =
8+
def evaluate(node: AbstractSyntaxTree[UO, BO, S, V])(variableAssignment: V => S): S =
99
node.fold[S](identity, executeUnitary, executeBinaryOperator, variableAssignment)
1010
}

math-parser/src/main/scala/mathParser/Language.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final case class Language[UO, BO, S, V](
1919
def withBinaryOperators[BO2](prefix: List[(String, BO2)], infix: List[(String, BO2)]): Language[UO, BO2, S, V] =
2020
copy(binaryPrefixOperators = prefix, binaryInfixOperators = infix)
2121

22+
def addConstant[S2](name: String, constant: S2): Language[UO, BO, S | S2, V] =
23+
copy(constants = (name -> constant) :: constants)
24+
2225
def withConstants[S2](constants: List[(String, S2)]): Language[UO, BO, S2, V] =
2326
copy(constants = constants)
2427

25-
def addConstant(name: String, value: S): Language[UO, BO, S, V] =
26-
copy(constants = (name -> value) :: constants)
27-
2828
def withVariables[V2](variables: List[(String, V2)]): Language[UO, BO, S, V2] =
2929
copy(variables = variables)
3030

@@ -39,22 +39,21 @@ final case class Language[UO, BO, S, V](
3939

4040
// todo: remove
4141
def variable(variable: V): VariableNode[UO, BO, S, V] = VariableNode(variable)
42-
4342
def constantNode(value: S): ConstantNode[UO, BO, S, V] = ConstantNode(value)
4443

45-
def parse(term: String)(using literalParser: LiteralParser[S]): Option[Node[UO, BO, S, V]] =
44+
def parse(term: String)(using literalParser: LiteralParser[S]): Option[AbstractSyntaxTree[UO, BO, S, V]] =
4645
Parser.parse(this, literalParser)(term)
4746

48-
def evaluate(node: Node[UO, BO, S, V])(variableAssignment: V => S)(using evaluate: Evaluate[UO, BO, S, V]): S =
47+
def evaluate(node: AbstractSyntaxTree[UO, BO, S, V])(variableAssignment: V => S)(using evaluate: Evaluate[UO, BO, S, V]): S =
4948
evaluate.evaluate(node)(variableAssignment)
5049

51-
def derive(node: Node[UO, BO, S, V])(variable: V)(using derive: Derive[UO, BO, S, V]): Node[UO, BO, S, V] =
50+
def derive(node: AbstractSyntaxTree[UO, BO, S, V])(variable: V)(using derive: Derive[UO, BO, S, V]): AbstractSyntaxTree[UO, BO, S, V] =
5251
derive.derive(node)(variable)
5352

54-
def optimize(node: Node[UO, BO, S, V])(using optimizer: Optimizer[UO, BO, S, V]): Node[UO, BO, S, V] =
53+
def optimize(node: AbstractSyntaxTree[UO, BO, S, V])(using optimizer: Optimizer[UO, BO, S, V]): AbstractSyntaxTree[UO, BO, S, V] =
5554
optimizer.optimize(node)
5655

57-
def compile[F](node: Node[UO, BO, S, V])(using compiler: Compiler[UO, BO, S, V, F]): Try[F] =
56+
def compile[F](node: AbstractSyntaxTree[UO, BO, S, V])(using compiler: Compiler[UO, BO, S, V, F]): Try[F] =
5857
compiler.compile(node)
5958
}
6059

math-parser/src/main/scala/mathParser/Optimize.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package mathParser
22

33
import scala.annotation.tailrec
4+
import mathParser.AbstractSyntaxTree.*
45

5-
type OptimizationRule[UO, BO, S, V] = PartialFunction[Node[UO, BO, S, V], Node[UO, BO, S, V]]
6+
type OptimizationRule[UO, BO, S, V] = PartialFunction[AbstractSyntaxTree[UO, BO, S, V], AbstractSyntaxTree[UO, BO, S, V]]
67

78
trait Optimizer[UO, BO, S, V] {
89
def rules: List[OptimizationRule[UO, BO, S, V]]
910

1011
@tailrec
11-
final def optimize(term: Node[UO, BO, S, V]): Node[UO, BO, S, V] = {
12-
def applyRules(node: Node[UO, BO, S, V]): Node[UO, BO, S, V] =
12+
final def optimize(term: AbstractSyntaxTree[UO, BO, S, V]): AbstractSyntaxTree[UO, BO, S, V] = {
13+
def applyRules(node: AbstractSyntaxTree[UO, BO, S, V]): AbstractSyntaxTree[UO, BO, S, V] =
1314
rules.reduce(_ orElse _).lift(node).getOrElse(node)
1415

15-
val optimized = term.fold[Node[UO, BO, S, V]](
16+
val optimized = term.fold[AbstractSyntaxTree[UO, BO, S, V]](
1617
ifConstant = ConstantNode.apply,
1718
ifUnitary = (op, child) => applyRules(UnitaryNode(op, child)),
1819
ifBinary = (op, childLeft, childRight) => applyRules(BinaryNode(op, childLeft, childRight)),

math-parser/src/main/scala/mathParser/Parser.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,47 @@ object Parser {
66

77
private type TokenList = List[String]
88

9-
def parse[UO, BO, S, V](lang: Language[UO, BO, S, V], literalParser: LiteralParser[S])(input: String): Option[Node[UO, BO, S, V]] = {
9+
def parse[UO, BO, S, V](lang: Language[UO, BO, S, V], literalParser: LiteralParser[S])(input: String): Option[AbstractSyntaxTree[UO, BO, S, V]] = {
1010

1111
def binaryNode(
1212
input: TokenList,
1313
splitter: String,
14-
f: (Node[UO, BO, S, V], Node[UO, BO, S, V]) => Node[UO, BO, S, V]
15-
): Option[Node[UO, BO, S, V]] =
14+
f: (AbstractSyntaxTree[UO, BO, S, V], AbstractSyntaxTree[UO, BO, S, V]) => AbstractSyntaxTree[UO, BO, S, V]
15+
): Option[AbstractSyntaxTree[UO, BO, S, V]] =
1616
for {
1717
(sub1, sub2) <- splitByRegardingParenthesis(input, splitter)
1818
p1 <- loop(sub1)
1919
p2 <- loop(sub2)
2020
} yield f(p1, p2)
2121

22-
def constant(input: String): Option[Node[UO, BO, S, V]] =
22+
def constant(input: String): Option[AbstractSyntaxTree[UO, BO, S, V]] =
2323
lang.constants
2424
.find(_._1 == input)
2525
.map(c => ConstantNode[UO, BO, S, V](c._2))
2626

27-
def variable(input: String): Option[Node[UO, BO, S, V]] =
27+
def variable(input: String): Option[AbstractSyntaxTree[UO, BO, S, V]] =
2828
lang.variables
2929
.find(_._1 == input)
3030
.map(v => VariableNode(v._2))
3131

32-
def literal(input: String): Option[Node[UO, BO, S, V]] =
32+
def literal(input: String): Option[AbstractSyntaxTree[UO, BO, S, V]] =
3333
literalParser
3434
.tryToParse(input)
3535
.map(literal => ConstantNode[UO, BO, S, V](literal))
3636

37-
def parenthesis(input: TokenList): Option[Node[UO, BO, S, V]] =
37+
def parenthesis(input: TokenList): Option[AbstractSyntaxTree[UO, BO, S, V]] =
3838
input match {
3939
case "(" +: remaining :+ ")" => loop(remaining)
4040
case _ => None
4141
}
4242

43-
def binaryInfixOperation(input: TokenList): Option[Node[UO, BO, S, V]] =
43+
def binaryInfixOperation(input: TokenList): Option[AbstractSyntaxTree[UO, BO, S, V]] =
4444
lang.binaryInfixOperators
4545
.to(LazyList)
4646
.flatMap(op => binaryNode(input, op._1, BinaryNode[UO, BO, S, V](op._2, _, _)))
4747
.headOption
4848

49-
def binaryPrefixOperation(input: TokenList): Option[Node[UO, BO, S, V]] =
49+
def binaryPrefixOperation(input: TokenList): Option[AbstractSyntaxTree[UO, BO, S, V]] =
5050
input match {
5151
case head :: ("(" +: remaining :+ ")") =>
5252
for {
@@ -56,13 +56,13 @@ object Parser {
5656
case _ => None
5757
}
5858

59-
def unitaryPrefixOperation(input: TokenList): Option[Node[UO, BO, S, V]] =
59+
def unitaryPrefixOperation(input: TokenList): Option[AbstractSyntaxTree[UO, BO, S, V]] =
6060
for {
6161
operator <- lang.unitaryOperators.find(_._1 == input.head)
6262
looped <- loop(input.tail)
6363
} yield UnitaryNode[UO, BO, S, V](operator._2, looped)
6464

65-
def loop(input: TokenList): Option[Node[UO, BO, S, V]] =
65+
def loop(input: TokenList): Option[AbstractSyntaxTree[UO, BO, S, V]] =
6666
input match {
6767
case Nil =>
6868
None

math-parser/src/main/scala/mathParser/Syntax.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package mathParser
33
import scala.util.Try
44

55
object Syntax {
6-
extension [UO, BO, S, V](node: Node[UO, BO, S, V]) {
6+
extension [UO, BO, S, V](node: AbstractSyntaxTree[UO, BO, S, V]) {
77
def evaluate(variableAssignment: V => S)(using evaluate: Evaluate[UO, BO, S, V]): S =
88
evaluate.evaluate(node)(variableAssignment)
99

10-
def optimize(using optimizer: Optimizer[UO, BO, S, V]): Node[UO, BO, S, V] =
10+
def optimize(using optimizer: Optimizer[UO, BO, S, V]): AbstractSyntaxTree[UO, BO, S, V] =
1111
optimizer.optimize(node)
1212

13-
def derive(variable: V)(using derive: Derive[UO, BO, S, V]): Node[UO, BO, S, V] =
13+
def derive(variable: V)(using derive: Derive[UO, BO, S, V]): AbstractSyntaxTree[UO, BO, S, V] =
1414
derive.derive(node)(variable)
1515

1616
def compile[F](using compiler: Compiler[UO, BO, S, V, F]): Try[F] =

math-parser/src/main/scala/mathParser/complex/ComplexDerive.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package mathParser.complex
22

3-
import mathParser.{BinaryNode, ConstantNode, Derive, UnitaryNode, VariableNode}
3+
import mathParser.{Derive}
44
import mathParser.complex.ComplexUnitaryOperator.*
55
import mathParser.complex.ComplexBinaryOperator.*
66
import mathParser.complex.Syntax.*
7+
import mathParser.*
78

89
class ComplexDerive[V] extends Derive[ComplexUnitaryOperator, ComplexBinaryOperator, Complex, V] {
910
def derive(term: ComplexNode[V])(variable: V): ComplexNode[V] = {

math-parser/src/main/scala/mathParser/complex/ComplexOptimize.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package mathParser.complex
22

3-
import mathParser.{BinaryNode, ConstantNode, Optimize, Optimizer, UnitaryNode}
3+
import mathParser.*
44
import mathParser.complex.Syntax.*
55
import mathParser.complex.ComplexBinaryOperator.*
66
import mathParser.complex.ComplexUnitaryOperator.*

math-parser/src/main/scala/mathParser/complex/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package mathParser
22

33
package object complex {
44
type ComplexLanguage[V] = Language[ComplexUnitaryOperator, ComplexBinaryOperator, Complex, V]
5-
type ComplexNode[V] = Node[ComplexUnitaryOperator, ComplexBinaryOperator, Complex, V]
5+
type ComplexNode[V] = AbstractSyntaxTree[ComplexUnitaryOperator, ComplexBinaryOperator, Complex, V]
66
}

0 commit comments

Comments
 (0)