Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSL compiler for simple While language #354

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

ailrst
Copy link
Contributor

@ailrst ailrst commented Mar 10, 2025

This PR adds functions to the DSL so that code can be written in a simple while language and compiled to BASIL IR basic blocks, this adds:

If (cond) Then (... blocks ...) Else (... blocks ...)

While (cond) Do (... blocks ...)

For (init, cond, incr) Do (... blocks ...)

It also adds infix operators to build basil IR expressions.

val p = prog(
  proc(
    "is_prime",
    Seq("n" -> bv64),
    Seq("ans" -> bv1),
    blocks(
      If(n <= 1.bv64)
        Then (ret("ans" -> (0.bv1)))
        Else (For(i := (2.bv64), i < n, i := i + (1.bv64))
          Do (If(n % i === (0.bv64)) Then (ret("ans" -> (0.bv1))))),
      ret("ans" -> (1.bv1))
    )
  )
)

@ailrst ailrst requested a review from katrinafyi March 10, 2025 02:58
@katrinafyi
Copy link
Member

Big changes!

Really fast preliminary comments:

  • I think the new higher DSL should be in a separate file.
  • also, it would be nice to organise the internal case classes separately from the user-facing functions as much as possible. for example, maybe they can be inside an object and marked private.
  • are there guards against "invalid syntax". like putting two else clauses on a single if statement?

@ailrst
Copy link
Contributor Author

ailrst commented Mar 10, 2025

Reviewing this can wait until you're at work of course but, I agree with the above.

are there guards against "invalid syntax". like putting two else clauses on a single if statement?

Yes, though in many places the errors can be quite confusing if you don't know the implementation (esp. due to overloading, using implicit conversions seems to work better). For this case it should be fairly reasonable, If is a case class that only defines the method Then which returns a new case class which only defines the method Else, so you'll get a not found error if you write something else.

@ailrst ailrst force-pushed the dsl-while-compiler branch from 84a0833 to adef2e3 Compare March 10, 2025 06:11
@ailrst ailrst force-pushed the interpreter-eval-proc branch from 1080752 to 5afe207 Compare March 12, 2025 04:10
@katrinafyi katrinafyi deleted the branch main March 13, 2025 06:17
@katrinafyi katrinafyi closed this Mar 13, 2025
@katrinafyi katrinafyi deleted the dsl-while-compiler branch March 13, 2025 06:20
@katrinafyi katrinafyi restored the dsl-while-compiler branch March 13, 2025 06:20
@katrinafyi katrinafyi reopened this Mar 13, 2025
@ailrst ailrst changed the base branch from interpreter-eval-proc to main March 13, 2025 06:21
@ailrst ailrst force-pushed the dsl-while-compiler branch from 742c656 to ffee9ee Compare March 13, 2025 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants