From 5f78969d4a71908fd8ae43c886614ef0e5664eb0 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Mon, 2 Nov 2020 20:52:52 -0700 Subject: define switch statement --- language/statements/index.rst | 2 +- language/statements/structure.rst | 20 ++++++++++++++++++++ syntax.md | 9 --------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/language/statements/index.rst b/language/statements/index.rst index 38bfbb1..e0a0a37 100644 --- a/language/statements/index.rst +++ b/language/statements/index.rst @@ -12,7 +12,7 @@ Statements When a block is executed, each of the containing statements, in linear order, is executed. -.. crowbar:element:: Statement <- VariableDefinition / StructureStatement / FlowControlStatement / AssignmentStatement / FragileStatement / ExpressionStatement +.. crowbar:element:: Statement <- VariableDefinition / StructureStatement / FlowControlStatement / AssignmentStatement / FragileStatement / ExpressionStatement / EmptyStatement Crowbar has many different types of statement. diff --git a/language/statements/structure.rst b/language/statements/structure.rst index 9330bf6..1a412af 100644 --- a/language/statements/structure.rst +++ b/language/statements/structure.rst @@ -19,3 +19,23 @@ Structure Statements First, the expression is evaluated. If the expression evaluates to a ``bool`` value of ``true``, then the first block will be executed. If the expression evaluates to a ``bool`` value of ``false``, either the second block is executed or nothing is executed. + +.. crowbar.element.. SwitchStatement <- 'switch' '(' Expression ')' '{' (CaseSpecifier / Statement)+ '}' + CaseSpecifier <- 'case' Expression ':' / 'default' ':' + + A switch statement allows many different actions to be taken depending on the value of some expression. + + Compile-time Behavior: + + The expression must have a type which is either some integer type or an enum. + The expression in a case specifier must have a value which can always be known at compile time, i.e. its value must be a constant or computed from only constants. + Either there must be a case specifier for every valid value in the type of the switch expression, or there must be a default case specifier. + At most one default case may be present. + + Runtime Behavior: + + First, the switch expression is evaluated. + Whichever case specifier has the same value, or the default case specifier if none is found, is then selected as the matching case specifier. + Any case specifiers immediately following the matching case specifier is ignored. + Subsequent statements are then executed, in linear order, until another case specifier is reached. + The execution of the switch statement then ends. diff --git a/syntax.md b/syntax.md index b8e4554..74e5cc3 100644 --- a/syntax.md +++ b/syntax.md @@ -3,15 +3,6 @@ ### Statements ```PEG -IfStatement ← 'if' Expression Block 'else' Block / - 'if' Expression Block - -SwitchStatement ← 'switch' Expression '{' SwitchCase+ '}' -SwitchCase ← CaseSpecifier Block / - 'default' Block -CaseSpecifier ← 'case' Expression ',' CaseSpecifier / - 'case' Expression ','? - WhileStatement ← 'while' Expression Block DoWhileStatement ← 'do' Block 'while' Expression ';' ForStatement ← 'for' VariableDefinition? ';' Expression ';' AssignmentStatementBody? Block -- cgit v1.2.3