Cheddar icon indicating copy to clipboard operation
Cheddar copied to clipboard

Proposal: Switch lambdas

Open vihanb opened this issue 8 years ago • 13 comments

This proposal details the workings of the planned switch (optionally *) statement.

Overview

The switch lambda is a block statement which functions both as a literal and a conditional block. In it's literal form it owns its own class rather than a TC link.

switch foo {
    case 1, 2: print "Foo is one or two"
    case 3, 4: print "Foo is three or four"
    else: print "Foo is something else"
}

Definition

The syntax is either switch or switch * where switch * specifies a switch literal. There may be any amount of whitespace between switch and *, if * exists. Following is the expression to be evaluated if the switch matches. This is followed by a block surrounded with braces {} of newline-seperated statements, which are the conditionals. Conditionals either begin with case or else, if case, then the beginning description is optional. Following is an expression which returns the value to be matched against, commands in these expressions serve as a logical OR during matching. These are followed by a : representing a single statement may follow. Or curly braces {} which allows for a multi-line code block

Formal Grammar

EBNF

@         = "switch" , [ "*" ] , expression , "{" , @case

@case     = [ ["case"] | "default" ] , @caselist , @block , @case
          | "}"

@caselist = expression , { "," , expression }

@block    = "{" code block "}"
          | ":" statement

vihanb avatar Jun 15 '16 22:06 vihanb