Skip to content

Commit de5373f

Browse files
committed
Update docs for new loops release
1 parent c52b4d8 commit de5373f

3 files changed

Lines changed: 82 additions & 13 deletions

File tree

website/docs/compiler/grammar.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,46 @@ block
5252
;
5353
5454
statement
55+
: controlStatement
56+
| nonControlStatement ';'
57+
;
58+
59+
nonControlStatement
5560
: variableDefinition
5661
| tupleAssignment
5762
| assignStatement
5863
| timeOpStatement
5964
| requireStatement
60-
| ifStatement
61-
| loopStatement
6265
| consoleStatement
6366
;
6467
68+
controlStatement
69+
: ifStatement
70+
| loopStatement
71+
;
72+
6573
variableDefinition
66-
: typeName modifier* Identifier '=' expression ';'
74+
: typeName modifier* Identifier '=' expression
6775
;
6876
6977
tupleAssignment
70-
: typeName Identifier ',' typeName Identifier '=' expression ';'
78+
: typeName Identifier ',' typeName Identifier '=' expression
7179
;
7280
7381
assignStatement
74-
: Identifier '=' expression ';'
82+
: Identifier '=' expression
7583
;
7684
7785
timeOpStatement
78-
: 'require' '(' TxVar '>=' expression (',' requireMessage)? ')' ';'
86+
: 'require' '(' TxVar '>=' expression (',' requireMessage)? ')'
7987
;
8088
8189
requireStatement
82-
: 'require' '(' expression (',' requireMessage)? ')' ';'
90+
: 'require' '(' expression (',' requireMessage)? ')'
91+
;
92+
93+
consoleStatement
94+
: 'console.log' consoleParameterList
8395
;
8496
8597
ifStatement
@@ -88,14 +100,25 @@ ifStatement
88100
89101
loopStatement
90102
: doWhileStatement
103+
| whileStatement
104+
| forStatement
91105
;
92106
93107
doWhileStatement
94108
: 'do' block 'while' '(' expression ')' ';'
95109
;
96110
97-
consoleStatement
98-
: 'console.log' consoleParameterList ';'
111+
whileStatement
112+
: 'while' '(' expression ')' block
113+
;
114+
115+
forStatement
116+
: 'for' '(' forInit ';' expression ';' assignStatement ')' block
117+
;
118+
119+
forInit
120+
: variableDefinition
121+
| assignStatement
99122
;
100123
101124
requireMessage

website/docs/language/contracts.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,57 @@ contract OneOfTwo(bytes20 pkh1, bytes32 hash1, bytes20 pkh2, bytes32 hash2) {
151151

152152
### Loops (beta)
153153

154-
Currently, CashScript only supports `do {} while ()` loops in the 0.13.0-next.0 pre-release. More advanced loop constructs will be added in the full 0.13.0 release. Keep in mind that in a `do {} while ()` loop, the condition is checked *after* the block of code within the loop is executed. This means that the block of code within the loop will be executed at least once, even if the condition is initially `false`.
154+
Currently, CashScript supports `for`, `while` and `do-while` loops in the `0.13.0-next` pre-release.
155155

156156
:::caution
157157
Loops in CashScript are currently in beta and may not fully behave as expected with debugging and console.log statements. The syntax for loops may change in the future.
158158
:::
159159

160+
#### for loop
161+
162+
For loops are the main loop construct in CashScript and has a similar syntax to languages like JavaScript or C. The loop header consists of three parts: the initialization, the condition and the update. The initialization is executed only once before the loop starts. The condition is checked before each iteration of the loop. The update is executed after each iteration of the loop.
163+
164+
#### Example
165+
```solidity
166+
pragma cashscript ^0.13.0;
167+
168+
contract NoTokensAllowed() {
169+
function spend() {
170+
int inputIndex = 0;
171+
172+
// Loop over all inputs (variable length), and make sure that none of them contain tokens
173+
for (int inputIndex = 0; inputIndex < tx.inputs.length; inputIndex = inputIndex + 1) {
174+
require(tx.inputs[inputIndex].tokenCategory == 0x);
175+
}
176+
}
177+
}
178+
```
179+
180+
#### while loop
181+
182+
While loops are similar to for loops, but the initialization and update are not specified, so it it loops as long as the condition is true. The condition is checked *before* each iteration of the loop (unlike do-while loops).
183+
184+
#### Example
185+
```solidity
186+
pragma cashscript ^0.13.0;
187+
188+
contract NoTokensAllowed() {
189+
function spend() {
190+
int inputIndex = 0;
191+
192+
// Loop over all inputs (variable length), and make sure that none of them contain tokens
193+
while (inputIndex < tx.inputs.length) {
194+
require(tx.inputs[inputIndex].tokenCategory == 0x);
195+
inputIndex = inputIndex + 1;
196+
}
197+
}
198+
}
199+
```
200+
201+
#### do-while loop
202+
203+
Do-while loops are similar to while loops, but the condition is checked *after* the block of code within the loop is executed. This means that the block of code within the loop will be executed at least once, even if the condition is initially `false`.
204+
160205
#### Example
161206
```solidity
162207
pragma cashscript ^0.13.0;

website/docs/releases/release-notes.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
title: Release Notes
33
---
44

5-
## v0.13.0-next.4
5+
## v0.13.0-next.5
66

77
This release contains several breaking changes, please refer to the [migration notes](/docs/releases/migration-notes) for more information.
88

99
#### cashc compiler
10-
- :sparkles: Add support for `do {} while ()` loops.
10+
- :sparkles: Add support for `for`, `while` and `do-while` loops.
1111
- :sparkles: Add support for bitwise and arithmetic shift operators (`<<`, `>>`) and bitwise inversion (`~`).
1212
- :sparkles: Add `unsafe_bool()` and `unsafe_int()` casting for semantic-only casts.
1313
- :hammer_and_wrench: **BREAKING**: Function parameter types are now strictly enforced (bounded bytes and boolean values).
1414
- :hammer_and_wrench: Add compiler option to `cashc` (programmatic and CLI compilation) to allow for opting out of function parameter type enforcement.
15+
- :bug: Fix issue where casting bytes larger than `bytes8` to `int` was not allowed.
1516
- :bug: **BREAKING**: Fix issue where `bool()` casting did not change the value of the argument.
1617
- :boom: **BREAKING**: Rename `bytes4(int)` and `bytes(int, 4)` to `toPaddedBytes(int, 4)`.
1718
- :boom: **BREAKING**: Rename `bytes4(bytes)` to `unsafe_bytes4(bytes)`.
1819
- :racehorse: Add optimisations for negated number comparisons and boolean comparisons.
1920

2021
#### CashScript SDK
2122

22-
- :sparkles: Add support for `do {} while ()` loops in debug tooling.
23+
- :sparkles: Add support for loops in debug tooling.
2324
- :sparkles: Add support for `p2s` contract type.
2425
- :sparkles: Add `lockingBytecode` property to `Contract` class.
2526
- :sparkles: Add `getUtxosForLockingBytecode()` method to `ElectrumNetworkProvider` class and `MockNetworkProvider` interface.

0 commit comments

Comments
 (0)