provided code
This commit is contained in:
15
src/main/wacc/Main.scala
Normal file
15
src/main/wacc/Main.scala
Normal file
@@ -0,0 +1,15 @@
|
||||
package wacc
|
||||
|
||||
import parsley.{Success, Failure}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
println("hello WACC!")
|
||||
|
||||
args.headOption match {
|
||||
case Some(expr) => parser.parse(expr) match {
|
||||
case Success(x) => println(s"$expr = $x")
|
||||
case Failure(msg) => println(msg)
|
||||
}
|
||||
case None => println("please enter an expression")
|
||||
}
|
||||
}
|
16
src/main/wacc/lexer.scala
Normal file
16
src/main/wacc/lexer.scala
Normal file
@@ -0,0 +1,16 @@
|
||||
package wacc
|
||||
|
||||
import parsley.Parsley
|
||||
import parsley.token.Lexer
|
||||
import parsley.token.descriptions.*
|
||||
|
||||
object lexer {
|
||||
private val desc = LexicalDesc.plain.copy(
|
||||
// your configuration goes here
|
||||
)
|
||||
private val lexer = Lexer(desc)
|
||||
|
||||
val integer = lexer.lexeme.integer.decimal
|
||||
val implicits = lexer.lexeme.symbol.implicits
|
||||
def fully[A](p: Parsley[A]): Parsley[A] = lexer.fully(p)
|
||||
}
|
20
src/main/wacc/parser.scala
Normal file
20
src/main/wacc/parser.scala
Normal file
@@ -0,0 +1,20 @@
|
||||
package wacc
|
||||
|
||||
import parsley.{Parsley, Result}
|
||||
import parsley.expr.chain
|
||||
|
||||
import lexer.implicits.implicitSymbol
|
||||
import lexer.{integer, fully}
|
||||
|
||||
object parser {
|
||||
def parse(input: String): Result[String, BigInt] = parser.parse(input)
|
||||
private val parser = fully(expr)
|
||||
|
||||
private val add = (x: BigInt, y: BigInt) => x + y
|
||||
private val sub = (x: BigInt, y: BigInt) => x - y
|
||||
|
||||
private lazy val expr: Parsley[BigInt] =
|
||||
chain.left1(integer | "(" ~> expr <~ ")")(
|
||||
("+" as add) | ("-" as sub)
|
||||
)
|
||||
}
|
2
src/test/wacc/README.md
Normal file
2
src/test/wacc/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
This is where you may wish to develop your tests. Remember that `scala-cli` picks up any files
|
||||
in a `test` sub-directory, or files ending in `.test.scala`.
|
Reference in New Issue
Block a user