Skip to content

feat: support switch statement in ZkC #1623

@DavePearce

Description

@DavePearce

Summary

There is a need to support switch statements in ZkC. This should differ slightly in semantics (i.e. meaning) from switch statements in e.g. C. Specifically, it should not support automatic fall thru semantics. Instead, it should follow go semantics for switch statements (see here).

Example

Here is a proposed example syntax:

pub input data(address:u16) -> (byte:u8)

// prove that two numbers add up to a third.
fn main() {
  var val:u8 = data[0]
  var res:u8 = data[1]

  if example(val) != res {
    fail
  }
}

fn example2(val:u8) -> (r:u8) {
   switch val {
   case 0:
     r = 0
   case 1:
     r = 1
   case 2,3:
     r = 1
   default:
     r = 2
   }
   
   return
}

Note here that case 2,3: illustrates the ability to specify multiple cases together. Furthermore, example is equivalent to the following:

fn example(val:u8) -> (r:u8) {
   if val == 0 || val == 1 {
      r = 0
   } else if val == 2 {
      r = 1
   } else if val == 3 {
      r = 1
   } else {
      r = 2
   }
   //
   return
}

Some test cases would be:

{ "data": "0x0000" }
{ "data": "0x0301" }

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestzkcIssues related to ZkC language

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions