Skip to content

Commit 84a0833

Browse files
committed
improve for syntax
1 parent cd37872 commit 84a0833

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

src/main/scala/ir/dsl/DSL.scala

+26-24
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,17 @@ case class EventuallyProgram(
372372
def cloneable = this.copy(mainProcedure = mainProcedure.cloneable, otherProcedures = otherProcedures.map(_.cloneable))
373373
}
374374

375+
/**
376+
* Construction of basil IR programs from high level while langauge.
377+
*
378+
* These functions uniformly return a list of eventually block, where the first block
379+
* is the entry point and the last block is the exit point.
380+
* These lists are then concatenated by the functions [[sequence]] and [[blocks]].
381+
* The function [[blocks]] is essentially list.concat, but it connects each sublist
382+
* with jumps.
383+
*
384+
*/
385+
375386
object Counter {
376387
var count = 1
377388
def next() = {
@@ -421,11 +432,16 @@ def setSuccIfUndef(first: List[EventuallyBlock], rest: EventuallyBlock*) = {
421432
first
422433
}
423434

424-
//def While(cond: Expr, body: EventuallyBlock*): List[EventuallyBlock] = {
425-
// While(cond, body.toList)
426-
//}
435+
case class For(init: List[EventuallyBlock], cond: Expr, after: List[EventuallyBlock]) {
436+
def Do(body: Iterable[EventuallyBlock]) = mkFor(init, cond, after, (body.toList))
437+
@targetName("doBlocks")
438+
def Do(body: EventuallyBlock*) = mkFor(init, cond, after, (body.toList))
439+
@targetName("doStatements")
440+
def Do(sl: (EventuallyCall | NonCallStatement | EventuallyStatement | EventuallyJump)*) =
441+
mkFor(init, cond, after, (List(stmts(sl: _*))))
442+
}
427443

428-
def For(
444+
private def mkFor(
429445
init: List[EventuallyBlock],
430446
cond: Expr,
431447
after: List[EventuallyBlock],
@@ -438,23 +454,6 @@ def For(
438454
sequence(init, loop)
439455
}
440456

441-
//def For(init: EventuallyBlock, cond: Expr, after: EventuallyBlock, body: EventuallyBlock*): List[EventuallyBlock] = {
442-
// For(List(init), cond, List(after), body.toList)
443-
//}
444-
//
445-
//def For(init: NonCallStatement, cond: Expr, after: NonCallStatement, body: EventuallyBlock*): List[EventuallyBlock] = {
446-
// require(body.nonEmpty)
447-
// For(List(stmts(init)), cond, List(stmts(after)), body.toList)
448-
//}
449-
//def For(
450-
// init: NonCallStatement,
451-
// cond: Expr,
452-
// after: NonCallStatement,
453-
// body: List[EventuallyBlock]
454-
//): List[EventuallyBlock] = {
455-
// For(List(stmts(init)), cond, List(stmts(after)), body.toList)
456-
//}
457-
458457
case class WhileDo(cond: Expr) {
459458
def Do(body: Iterable[EventuallyBlock]) = While(cond, (body.toList))
460459
@targetName("doBlocks")
@@ -555,9 +554,12 @@ def progUnresolved(
555554
): EventuallyProgram =
556555
EventuallyProgram(mainProc, procedures, initialMemory)
557556

558-
/**
559-
* Expr construction
560-
*/
557+
/**
558+
* Expr and statement construction; this defined infix operators which construct
559+
* BASIL IR statements and expressions.
560+
*
561+
* Typically with binops we default to signed ops, and provide named unsigned alternatives.
562+
*/
561563

562564
extension (lvar: Variable)
563565
infix def :=(j: Expr) = LocalAssign(lvar, j)

src/test/scala/ir/InterpreterTests.scala

+7-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ class InterpreterTests extends AnyFunSuite with BeforeAndAfter {
101101
"is_prime",
102102
Seq("n" -> bv64),
103103
Seq("ans" -> bv1),
104-
(If(n <= 1.bv64)
105-
Then (ret("ans" -> (0.bv1)))
106-
Else (For(i := (2.bv64), i < n, i := i + (1.bv64), If(n % i === (0.bv64)) Then (ret("ans" -> (0.bv1))))))
107-
`;` stmts(ret("ans" -> (1.bv1)))
104+
blocks(
105+
If(n <= 1.bv64)
106+
Then (ret("ans" -> (0.bv1)))
107+
Else (For(i := (2.bv64), i < n, i := i + (1.bv64))
108+
Do (If(n % i === (0.bv64)) Then (ret("ans" -> (0.bv1))))),
109+
ret("ans" -> (1.bv1))
110+
)
108111
)
109112
)
110113

0 commit comments

Comments
 (0)