chipsalliance / chisel

Chisel: A Modern Hardware Design Language
https://www.chisel-lang.org/
Apache License 2.0
3.99k stars 597 forks source link

Cheatsheet switch statement #506

Closed jchang0 closed 7 years ago

jchang0 commented 7 years ago

By experimentation, it looks like the for the switch-is statement, the is statement must be on a different line than the closing brace of the previous is statement

e.g. this as written on the cheetsheet does not work: (it gives this compilation error message: overloaded method value apply with alternatives: (x: chisel3.core.UInt)chisel3.core.Bool (x: Int)chisel3.core.Bool (x: BigInt)chisel3.core.Bool cannot be applied to (Unit))

switch(x) {
  is(value1) {
    // run if x === value1
  } is (value2) {
    // run if x === value2
  }
}

But if I put the is statement on a different line, it works:

switch(x) {
  is(value1) {
    // run if x === value1
  }
  is (value2) {
    // run if x === value2
  }
}

Not sure if it's documentation error or chisel3 issue.

ducky64 commented 7 years ago

It looks like the first is just tripping up the Scala parser.

One attempt to solve it would be allowing iss to chain, parsing as is (value1) {block1} .is (value2) {block2}. However, that ends up in the same situation as when and .elsewhen, where if you omit the explicit dot, Scala parses block2 as an argument for value2.

Will update the cheatsheet, this is bad.

ducky64 commented 7 years ago

Cheatsheet source updated. @chick or @ucbjrl, can one of you update the compiled version on the website?

chick commented 7 years ago

@ducky64 website updated