Skip to content

Commit 9bb6bdb

Browse files
authored
Merge pull request #5 from tomerghelber/develop
First version
2 parents 396981b + 0af0b0e commit 9bb6bdb

35 files changed

+783
-209
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cache:
1313
- $HOME/.cache/coursier
1414
- $HOME/.ivy2/cache
1515
- $HOME/.sbt/boot/
16+
- $HOME/.m2/repository/
1617

1718
# single test suite, non-parallel build.
1819

build.sbt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ name := "mathimatica-parser"
22

33
scalaVersion := "2.12.8"
44

5-
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.2"
6-
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
7-
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
5+
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.2"
6+
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
87

9-
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test
10-
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.14.2" % Test
8+
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3" % Test
9+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test
10+
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.14.2" % Test
1111

1212
// POM settings for Sonatype
1313
organization := "com.github.tomerghelber"

project/plugins.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
addSbtPlugin("io.stryker-mutator" % "sbt-stryker4s" % "0.6.1")
2-
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.2.6")
1+
addSbtPlugin("io.stryker-mutator" % "sbt-stryker4s" % "0.6.1")
2+
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.2.6")
Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,13 @@
11
package com.github.tomerghelber.mathematica.ast
22

3-
trait ASTNode
3+
sealed trait ASTNode
44

5-
case class FunctionNode(name: ASTNode, arguments: Seq[ASTNode]) extends ASTNode
5+
final case class FunctionNode(name: SymbolNode, arguments: Seq[ASTNode]) extends ASTNode
66

7-
object PartNode extends ApplyManyFunctionNode with UnapplyFunctionNode {
8-
protected val name: String = "Part"
7+
sealed trait TerminalNode extends ASTNode {
8+
def value: String
99
}
1010

11-
object CompositionNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
12-
protected val name: String = "Composition"
13-
}
14-
object RightCompositionNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
15-
protected val name: String = "RightComposition"
16-
}
17-
case class DerivativeNode(num: Int, expr: ASTNode) extends ASTNode
18-
object DifferentialDNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
19-
protected val name: String = "DifferentialD"
20-
}
21-
object IntegrateNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
22-
protected val name: String = "Integrate"
23-
}
24-
25-
object SameQNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
26-
protected val name: String = "SameQ"
27-
}
28-
object UnSameQNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
29-
protected val name: String = "UnSameQ"
30-
}
31-
object ForAllNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
32-
protected val name: String = "ForAll"
33-
}
34-
object ExistsNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
35-
protected val name: String = "Exists"
36-
}
37-
object NotExistsNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
38-
protected val name: String = "NotExists"
39-
}
40-
object EquivalentNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
41-
protected val name = "Equivalent"
42-
}
43-
object ListNode extends ApplyManyFunctionNode with UnapplyFunctionNode {
44-
protected val name = "List"
45-
}
46-
47-
case class LimitNode(expr1: ASTNode, expr3: ASTNode, expr2: ASTNode) extends ASTNode
48-
case class MaxLimitNode(expr1: ASTNode, expr3: ASTNode, expr2: ASTNode) extends ASTNode
49-
case class MinLimitNode(expr1: ASTNode, expr3: ASTNode, expr2: ASTNode) extends ASTNode
50-
51-
case class SpanNode(expr1: ASTNode, expr3: ASTNode, expr2: ASTNode) extends ASTNode
52-
53-
trait ApplyUnaryFunctionNode {
54-
protected def name: String
55-
def apply(node: ASTNode): FunctionNode = createUnary(node)
56-
val createUnary: ASTNode => FunctionNode = first => FunctionNode(SymbolNode(name), Seq(first))
57-
}
58-
trait ApplyBinaryFunctionNode {
59-
protected def name: String
60-
def apply(first: ASTNode, second: ASTNode): FunctionNode = createBinary(first, second)
61-
val createBinary: (ASTNode, ASTNode) => FunctionNode = (first, second) => FunctionNode(SymbolNode(name), Seq(first, second))
62-
}
63-
trait ApplyManyFunctionNode {
64-
protected def name: String
65-
def apply(nodes: Seq[ASTNode]): FunctionNode = createMany(nodes)
66-
val createMany: Seq[ASTNode] => FunctionNode = nodes => FunctionNode(SymbolNode(name), nodes)
67-
}
68-
trait UnapplyFunctionNode {
69-
protected def name: String
70-
def unapply(arg: FunctionNode): Option[Seq[ASTNode]] = arg match {
71-
case FunctionNode(SymbolNode(functionName), arguments) if functionName == name => Some(arguments)
72-
case _ => None
73-
}
74-
}
11+
final case class NumberNode(value: String) extends TerminalNode
12+
final case class StringNode(value: String) extends TerminalNode
13+
final case class SymbolNode (value: String) extends TerminalNode

src/main/scala/com/github/tomerghelber/mathematica/ast/BinaryOperationNode.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package com.github.tomerghelber.mathematica.ast
33
object NotNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
44
protected val name: String = "Not"
55
}
6-
object AndNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
6+
object AndNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
77
protected val name: String = "And"
88
}
9-
object NandNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
9+
object NandNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
1010
protected val name: String = "Nand"
1111
}
12-
object XorNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
12+
object XorNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
1313
protected val name: String = "Xor"
1414
}
15-
object XnorNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
15+
object XnorNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
1616
protected val name: String = "Xnor"
1717
}
18-
object OrNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
18+
object OrNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
1919
protected val name: String = "Or"
2020
}
21-
object NorNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
21+
object NorNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
2222
protected val name: String = "Nor"
2323
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.tomerghelber.mathematica.ast
2+
3+
object DNode extends ApplyBinaryFunctionNode {
4+
override protected val name: String = "D"
5+
}
6+
object DelNode extends ApplyBinaryFunctionNode {
7+
override protected val name: String = "Del"
8+
}
9+
object DiscreteShiftNode extends ApplyBinaryFunctionNode {
10+
override protected val name: String = "DiscreteShift"
11+
}
12+
object DiscreteRatioNode extends ApplyBinaryFunctionNode {
13+
override protected val name: String = "DiscreteRatio"
14+
}
15+
object DifferenceDeltaNode extends ApplyBinaryFunctionNode {
16+
override protected val name: String = "DifferenceDelta"
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.tomerghelber.mathematica.ast
2+
3+
object SameQNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
4+
protected val name: String = "SameQ"
5+
}
6+
object UnSameQNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
7+
protected val name: String = "UnSameQ"
8+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.tomerghelber.mathematica.ast
2+
3+
sealed trait FunctionNodeSymbol {
4+
protected val name: String
5+
lazy val symbol: SymbolNode = SymbolNode(name)
6+
}
7+
trait ApplyUnaryFunctionNode extends FunctionNodeSymbol {
8+
def apply(node: ASTNode): FunctionNode = createUnary(node)
9+
val createUnary: ASTNode => FunctionNode = first => FunctionNode(SymbolNode(name), Seq(first))
10+
}
11+
trait ApplyBinaryFunctionNode extends FunctionNodeSymbol {
12+
def apply(first: ASTNode, second: ASTNode): FunctionNode = createBinary(first, second)
13+
val createBinary: (ASTNode, ASTNode) => FunctionNode =
14+
(first, second) => FunctionNode(SymbolNode(name), Seq(first, second))
15+
}
16+
trait ApplyTernaryFunctionNode extends FunctionNodeSymbol {
17+
def apply(first: ASTNode, second: ASTNode, third: ASTNode): FunctionNode = createTernary(first, second, third)
18+
val createTernary: (ASTNode, ASTNode, ASTNode) => FunctionNode =
19+
(first, second, third) => FunctionNode(SymbolNode(name), Seq(first, second, third))
20+
}
21+
trait ApplyManyFunctionNode extends FunctionNodeSymbol {
22+
def apply(nodes: Seq[ASTNode]): FunctionNode = createMany(nodes)
23+
val createMany: Seq[ASTNode] => FunctionNode = nodes => FunctionNode(SymbolNode(name), nodes)
24+
}
25+
trait UnapplyFunctionNode extends FunctionNodeSymbol {
26+
def unapply(arg: ASTNode): Option[Seq[ASTNode]] = arg match {
27+
case FunctionNode(functionSymbol, arguments) if functionSymbol == symbol => Some(arguments)
28+
case _ => None
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.tomerghelber.mathematica.ast
2+
3+
import scala.util.Try
4+
5+
object DerivativeNode {
6+
private val name: String = "Derivative"
7+
lazy val symbol: SymbolNode = SymbolNode(name)
8+
9+
def apply(num: Int, expr: ASTNode): FunctionNode = create(num, expr)
10+
11+
def create: (Int, ASTNode) => FunctionNode =
12+
(num: Int, expr: ASTNode) => FunctionNode(symbol, Seq(NumberNode(num.toString), expr))
13+
14+
def unapply(arg: ASTNode): Option[(Int, ASTNode)] = arg match {
15+
case FunctionNode(functionSymbol, Seq(NumberNode(num), expr))
16+
if functionSymbol == symbol && Try(num.toInt).isSuccess =>
17+
Some((num.toInt, expr))
18+
case _ => None
19+
}
20+
}
21+
object DifferentialDNode extends ApplyUnaryFunctionNode with UnapplyFunctionNode {
22+
protected val name: String = "DifferentialD"
23+
}
24+
object IntegrateNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
25+
protected val name: String = "Integrate"
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.tomerghelber.mathematica.ast
2+
3+
object PartNode extends ApplyManyFunctionNode with UnapplyFunctionNode {
4+
protected val name: String = "Part"
5+
}
6+
object CompositionNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
7+
protected val name: String = "Composition"
8+
}
9+
object RightCompositionNode extends ApplyBinaryFunctionNode with UnapplyFunctionNode {
10+
protected val name: String = "RightComposition"
11+
}

0 commit comments

Comments
 (0)