-
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.
Improve support for string interpolators using macros
- Loading branch information
1 parent
c4b154c
commit 012ac77
Showing
11 changed files
with
122 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ | |
target | ||
.bsp | ||
releases | ||
.vscode | ||
.metals | ||
.bloop |
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
6 changes: 5 additions & 1 deletion
6
...in/scala/me/cassayre/florian/masterproject/front/fol/definitions/FormulaDefinitions.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
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
60 changes: 60 additions & 0 deletions
60
src/main/scala/me/cassayre/florian/masterproject/front/parser/FrontMacro.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,60 @@ | ||
package me.cassayre.florian.masterproject.front.parser | ||
|
||
import me.cassayre.florian.masterproject.front.fol.FOL.* | ||
import me.cassayre.florian.masterproject.front.proof.Proof.* | ||
import me.cassayre.florian.masterproject.front.printer.FrontPositionedPrinter | ||
|
||
// Note: on Intellij you may want to disable syntax highlighting for this file | ||
// ("File Types" => "Text" => "Ignored Files and Folders", add "FrontMacro.scala") | ||
|
||
object FrontMacro { | ||
|
||
import scala.quoted.* | ||
|
||
extension (inline sc: StringContext) | ||
inline def term(inline args: Any*): Term = ${ termImpl('sc, 'args) } | ||
inline def formula(inline args: Any*): Formula = ${ formulaImpl('sc, 'args) } | ||
inline def sequent(inline args: Any*): Sequent = ${ sequentImpl('sc, 'args) } | ||
inline def partial(inline args: Any*): PartialSequent = ${ partialImpl('sc, 'args) } | ||
|
||
private def termImpl(sc: Expr[StringContext], args: Expr[Seq[Any]])(using Quotes): Expr[Term] = | ||
'{ FrontReader.readTerm(${rawStringImpl(sc, args)}) } | ||
private def formulaImpl(sc: Expr[StringContext], args: Expr[Seq[Any]])(using Quotes): Expr[Formula] = | ||
'{ FrontReader.readFormula(${rawStringImpl(sc, args)}) } | ||
private def sequentImpl(sc: Expr[StringContext], args: Expr[Seq[Any]])(using Quotes): Expr[Sequent] = | ||
'{ FrontReader.readSequent(${rawStringImpl(sc, args)}) } | ||
private def partialImpl(sc: Expr[StringContext], args: Expr[Seq[Any]])(using Quotes): Expr[PartialSequent] = | ||
'{ FrontReader.readPartialSequent(${rawStringImpl(sc, args)}) } | ||
|
||
|
||
private def rawStringImpl(sc: Expr[StringContext], args: Expr[Seq[Any]])(using q: Quotes): Expr[String] = { | ||
import q.reflect._ | ||
|
||
val argsSeq: Seq[Expr[Any]] = args match { | ||
case Varargs(es) => es | ||
} | ||
val argsSeqString: Seq[Expr[String]] = argsSeq.map { | ||
case '{ $s: String } => s | ||
case '{ $f: SchematicConnectorLabel[?] } => '{ s"??${${f}.id}" } | ||
case '{ $f: SchematicFunctionLabel[?] | SchematicPredicateLabel[?] } => '{ s"?${${f}.id}" } | ||
case '{ $f: ConstantFunctionLabel[?] | ConstantPredicateLabel[?] | ConstantConnectorLabel[?] } => '{ s"${${f}.id}" } | ||
case '{ $other } => '{ ${other}.toString } | ||
} | ||
val partsSeq: Seq[Expr[String]] = sc match { | ||
case '{ StringContext($vs: _*) } => | ||
vs match { case Varargs(es) => es } | ||
} | ||
val rawParts: Seq[String] = partsSeq.map(_.asTerm).map { | ||
case Literal(c: Constant) => c.value.toString | ||
} | ||
val argsSeqWithEmpty: Seq[Expr[String]] = argsSeqString :+ '{""} | ||
val allExprs: Seq[Expr[Any]] = rawParts.zip(argsSeqWithEmpty).flatMap { case (p: String, a: Expr[Any]) => | ||
Seq(Expr(p), a) | ||
} | ||
val exprString: Expr[String] = allExprs.map(e => '{ ${e}.toString }).reduce { | ||
case (l: Expr[Any], r: Expr[Any]) => '{ $l + $r } | ||
} | ||
exprString | ||
} | ||
|
||
} |
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: 4 additions & 0 deletions
4
src/main/scala/me/cassayre/florian/masterproject/front/proof/Proof.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
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
18 changes: 18 additions & 0 deletions
18
src/test/scala/me/cassayre/florian/masterproject/front/FrontMacroTests.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,18 @@ | ||
package me.cassayre.florian.masterproject.front | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import me.cassayre.florian.masterproject.front.* | ||
|
||
class FrontMacroTests extends AnyFunSuite { | ||
|
||
test("string interpolation macros") { | ||
term"g(x, y)" | ||
formula"a /\ b \/ c => d" | ||
sequent"a; b |- c" | ||
partial"a |- b; ..." | ||
|
||
val p = ConstantPredicateLabel("p", 2) | ||
assert(formula"$p(x, y)".toString == s"p(x, y)") // Currently limited support for label interpolation, very few safety guarantees | ||
} | ||
|
||
} |