This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from writeonly/parser
add parser of calculator
- Loading branch information
Showing
18 changed files
with
84 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...pl/writeonly/re/shared/CalculatorIT.scala → ...y/re/shared/calculator/CalculatorIT.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/pl/writeonly/re/shared/Calculator.scala → ...nly/re/shared/calculator/Calculator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pl.writeonly.re.shared | ||
package pl.writeonly.re.shared.calculator | ||
|
||
class Calculator { | ||
type T = Int | ||
|
27 changes: 27 additions & 0 deletions
27
re/shared/src/main/scala/pl/writeonly/re/shared/calculator/CalculatorParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pl.writeonly.re.shared.calculator | ||
|
||
class CalculatorParser { | ||
def eval(tree: (Int, Seq[(String, Int)])): Int = { | ||
val (base, ops) = tree | ||
ops.foldLeft(base) { | ||
case (left, (op, right)) => evalOp(op, left, right) | ||
} | ||
} | ||
|
||
private def evalOp(op: String, left: Int, right: Int): Int = op match { | ||
case "+" => left + right | ||
case "-" => left - right | ||
case "*" => left * right | ||
case "/" => left / right | ||
} | ||
|
||
import fastparse.all._ | ||
|
||
val number: P[Int] = P(CharIn('0' to '9').rep(1).!.map(_.toInt)) | ||
val parens: P[Int] = P("(" ~/ addSub ~ ")") | ||
val factor: P[Int] = P(number | parens) | ||
|
||
val divMul: P[Int] = P(factor ~ (CharIn("*/").! ~/ factor).rep).map(eval) | ||
val addSub: P[Int] = P(divMul ~ (CharIn("+-").! ~/ divMul).rep).map(eval) | ||
val expr: P[Int] = P(addSub ~ End) | ||
} |
2 changes: 1 addition & 1 deletion
2
...n/scala/pl/writeonly/re/shared/Core.scala → ...la/pl/writeonly/re/shared/core/Core.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pl.writeonly.re.shared | ||
package pl.writeonly.re.shared.core | ||
|
||
trait Core { | ||
def apply(arg: String): Unit | ||
|
2 changes: 1 addition & 1 deletion
2
...writeonly/re/shared/LazyLoggingCore.scala → ...only/re/shared/core/LazyLoggingCore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pl.writeonly.re.shared | ||
package pl.writeonly.re.shared.core | ||
|
||
import slogging.LazyLogging | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...iteonly/re/shared/StrictLoggingCore.scala → ...ly/re/shared/core/StrictLoggingCore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pl.writeonly.re.shared | ||
package pl.writeonly.re.shared.core | ||
|
||
import slogging.StrictLogging | ||
|
||
|
26 changes: 26 additions & 0 deletions
26
re/shared/src/test/scala/pl/writeonly/re/shared/calculator/CalculatorParserTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package pl.writeonly.re.shared.calculator | ||
|
||
import fastparse.core.Parsed | ||
import utest._ | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.Any")) | ||
object CalculatorParserTest extends TestSuite { | ||
override val tests: Tests = Tests { | ||
val parser = new CalculatorParser() | ||
val expr = parser.expr | ||
"1+1" - { | ||
val result = expr.parse("1+1") | ||
|
||
assertMatch(result) { | ||
case Parsed.Success(2, _) => | ||
} | ||
} | ||
"1+1*" - { | ||
val result = expr.parse("1+1*") | ||
|
||
assertMatch(result) { | ||
case _: Parsed.Failure[_, _] => | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ala/pl/writeonly/re/shared/CoreTest.scala → ...l/writeonly/re/shared/core/CoreTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pl.writeonly.re.shared | ||
package pl.writeonly.re.shared.core | ||
|
||
import utest._ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters