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

Unexpected syntax error #682

Open
metanest opened this issue Mar 11, 2024 · 1 comment
Open

Unexpected syntax error #682

metanest opened this issue Mar 11, 2024 · 1 comment

Comments

@metanest
Copy link

This BlueSpec Classic code ...

package PrioEnc where

interface PrioEnc_IFC =
  prioEnc :: Bit 3 -> Bit 2

mkPrioEnc :: Module PrioEnc_IFC
mkPrioEnc =
  module
    interface
      prioEnc i = f i[2:2] i[1:1] i[0:0]
        where
          f 1 _ _ = 0b11
          f 0 1 _ = 0b10
          f 0 0 1 = 0b01
          f 0 0 0 = 0b00
          f _ _ _ = 0  -- dummy

result is following unexpected syntax error

Error: "src/PrioEnc.bs", line 11, column 8: (P0005)
  Unexpected "where"; expected ".", "[", "_", "<var>", "(", "<con>", "<real>",
  "<integer>", "<string>", "<char>", "do", "action", "<op>", "`", ";", "; from
  layout", "when", "}", or "} from layout"
@quark17
Copy link
Collaborator

quark17 commented Mar 11, 2024

The BH Language Manual mentions the keyword where as used in the package, class, and instance syntax, but does not describe any other usage. I don't know if this omission is intentional or not.

I do see that the BSC parser accepts where in expressions. That is, anywhere that an expression is expected, you can write e where ... -- however, this is only for certain e. Specifically, e has to be a simple expression, like a variable, constructor, tuple, literal, etc, and it can optionally have field selection or bit extraction. It turns out that (e) is considered a tuple of 1 element, so your example can be made to compile by putting the expression in parentheses:

prioEnc i = (f i[2:2] i[1:1] i[0:0])
  where

Of course, you can also use a let-statement instead of where:

prioEnc i =
  let f = ...
  in  f i[2:2] i[1:1] i[0:0]

These two forms are equivalent, because the where is part of the expression, and not part of the declaration of prioEnc.

You could also get it to compile by using a simpler expression preceding the where keyword:

prioEnc i = res
  where
    res = f i[2:2] i[1:1] i[0:0]
    f = ...

I assume that you attempted to use where because you are familiar with Haskell? In Haskell, the where keyword is supported as part of the RHS (right hand side) of a declaration. So in Haskell, the where would be part of the syntax for declaring prioEnc; but that's not what BSC supports.

Where do we go from here? I guess there are four options:

  1. Remove the parser support for expression where so that BSC matches the documentation. (This is probably not what we want to do.)
  2. Leave the BH parser as it is, but document its current behavior in the language manual.
  3. Expand the current where expression syntax, so that it can apply to any expression (not just simple expressions) -- which would allow your example to compile -- and of course document it in the language manual.
  4. Change BSC to support where as a declaration RHS syntax and not as an expression syntax, to match what Haskell supports -- which would also allow your example to compiled -- and of course document it in the language manual.

I'm not sure which is preferable. Maybe @rsnikhil and @mieszko can weigh in.

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

No branches or pull requests

2 participants