-
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.
- Loading branch information
Showing
3 changed files
with
99 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package lore.DSL | ||
import scala.quoted.* | ||
import rescala.default._ | ||
import lore.Parser | ||
|
||
type Source[A] = Var[A] | ||
type Derived[A] = Signal[A] | ||
|
||
object Source: | ||
inline def apply[A](inline expr: A) = Var(expr) | ||
|
||
object Derived: | ||
inline def apply[A](inline expr: A) = Signal { expr } | ||
|
||
// S = source type, A = argument type | ||
// case class Interaction( | ||
// req: List[String], | ||
// ens: List[Boolean], | ||
// exec: List[Any] | ||
// ) | ||
// class InteractionWithModifiesAndExecutes[A]( | ||
// modifies: List[Source[A]], | ||
// executes: () => List[A] | ||
// ) | ||
private class InteractionWithTypes[S, A]: | ||
inline def requires(inline expr: (S, A) => Boolean) = InteractionWithRequires( | ||
expr | ||
) | ||
|
||
private class InteractionWithRequires[S, A](requires: (S, A) => Boolean): | ||
inline def modifies(inline expr: Source[S]) = | ||
InteractionWithRequiresAndModifies(requires, expr) | ||
|
||
// private class InteractionWithModifies[A](modifies: Source[A]): | ||
// inline def executes(inline expr: A => A) = | ||
// () => modifies.transform(expr) | ||
// inline def requires(inline expr: () => Boolean) = | ||
// InteractionWithRequiresAndModifies(expr, modifies) | ||
|
||
private class InteractionWithRequiresAndModifies[S, A]( | ||
requires: (S, A) => Boolean, | ||
modifies: Source[S] | ||
): | ||
inline def executes(inline expr: (S, A) => S) = | ||
(arg: A) => | ||
modifies.transform(currVal => | ||
if requires(currVal, arg) then expr(currVal, arg) | ||
else | ||
println(s"Requirement $requires evaluated to false!") | ||
currVal | ||
) | ||
|
||
object Interaction: | ||
inline def apply[S, A] = InteractionWithTypes[S, A] | ||
// inline def modifies[A](source: Source[A]) = InteractionWithModifies(source) | ||
inline def requires[S, A](b: Boolean): Boolean = | ||
${ | ||
makeFromRequires('b) | ||
} | ||
|
||
def makeFromRequires(b: Expr[Boolean])(using Quotes) = | ||
println(b.show) | ||
Parser.parse(b.toString()) match | ||
case Left(err) => println(err) | ||
case Right(value) => println(value) | ||
b |
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,24 @@ | ||
import lore.DSL._ | ||
|
||
import rescala.default | ||
|
||
object DSLTest: | ||
@main | ||
def main = | ||
val a: Source[Int] = Source(0) | ||
val b: Derived[Int] = Derived { a() + a() } | ||
val add10 = | ||
Interaction[Int, Int] | ||
.requires((curr, _) => curr < 20) | ||
.modifies(a) | ||
.executes((curr, _) => curr + 10) | ||
// .requires((curr, _) => curr < 20) | ||
// .modifies(a) | ||
// .requires(() => false) | ||
// .executes((curr) => curr + 10) | ||
add10(0) | ||
println(s"a: ${a.now}, b: ${b.now}") | ||
add10(0) | ||
println(s"a: ${a.now}, b: ${b.now}") | ||
add10(0) | ||
println(s"a: ${a.now}, b: ${b.now}") |