provided code

This commit is contained in:
LabTS
2025-01-19 23:10:54 +00:00
commit 2b50e3e1f6
9 changed files with 147 additions and 0 deletions

15
src/main/wacc/Main.scala Normal file
View 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
View 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)
}

View 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
View 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`.