From e59e69118302b6a8a9d8c09ed2d5032d432ad33a Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Mon, 2 Nov 2020 21:36:36 -0700 Subject: add loops --- language/source-file.rst | 2 +- language/statements/structure.rst | 42 ++++++++++++++++++++++++++++++++++++++- syntax.md | 4 ---- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/language/source-file.rst b/language/source-file.rst index 162bb88..079ee39 100644 --- a/language/source-file.rst +++ b/language/source-file.rst @@ -2,7 +2,7 @@ Source Files ------------ .. crowbar:element:: HeaderFile <- IncludeStatement* HeaderFileElement+ - HeaderFileElement <- TypeDefinition / FunctionDeclaration / ConstantDefinition / VariableDeclaration + HeaderFileElement <- TypeDefinition / FunctionDeclaration / VariableDefinition / VariableDeclaration A Crowbar header file defines an API boundary, either at the surface of a library or between pieces of a library or application. :crowbar:ref:`IncludeStatement`\ s can only appear at the beginning of the header file, and header files cannot define behavior directly. diff --git a/language/statements/structure.rst b/language/statements/structure.rst index 1a412af..0586608 100644 --- a/language/statements/structure.rst +++ b/language/statements/structure.rst @@ -20,7 +20,7 @@ Structure Statements 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)+ '}' +.. 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. @@ -39,3 +39,43 @@ Structure Statements 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. + +.. crowbar:element:: WhileStatement <- 'while' '(' Expression ')' Block + + Compile-time Behavior: + + The expression must have type bool. + + Runtime Behavior: + + The expression is evaluated, and, if it evaluates to true, the block is executed. + This process repeats until the expression evaluates to false. + +.. crowbar:element:: DoWhileStatement <- 'do' Block 'while' '(' Expression ')' ';' + + Compile-time Behavior: + + The expression must have type bool. + + Runtime Behavior: + + The block is executed. + Then, the expression is evaluated, and if it is true the process repeats. + +.. crowbar:element:: ForStatement <- 'for' '(' ForInit? ';' Expression ';' ForUpdate? ')' Block + ForInit <- ForInitializer (',' ForInitializer)* ','? + ForInitializer <- Type identifier '=' Expression + ForUpdate <- AssignmentBody (',' AssignmentBody)* ','? + + Compile-time Behavior: + + The individual initializers each have the same behavior as a :crowbar:ref:`VariableDefinition`, but for scope purposes they are treated as though they are inside the block. + The top-level expression in the for statement must have type bool, and will be treated for scope purposes as though it is inside the block. + The update assignments will be treated for scope purposes as though they are inside the block. + + Runtime Behavior: + + First, the initializers are executed the same way variable definitions would be, in the order they are presented. + Then, the top-level expression is evaluated, and if it is false the for statement ends. + If the top-level expression was true, the block is executed, and then the update assignments are executed in the order they are presented. + The process repeats starting with expression evaluation. diff --git a/syntax.md b/syntax.md index 74e5cc3..9f7fc59 100644 --- a/syntax.md +++ b/syntax.md @@ -3,10 +3,6 @@ ### Statements ```PEG -WhileStatement ← 'while' Expression Block -DoWhileStatement ← 'do' Block 'while' Expression ';' -ForStatement ← 'for' VariableDefinition? ';' Expression ';' AssignmentStatementBody? Block - FlowControlStatement ← 'continue' ';' / 'break' ';' / 'return' Expression? ';' -- cgit v1.2.3