Skip to content

Commit

Permalink
doc: Document loop statements
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Mar 8, 2022
1 parent 0034e73 commit 52b4b34
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const SmartML = require('@tezwell/smartts-sdk/compiler');
// A Lambda that returns "YES" if the argument is greater than or equal to Nat(10), returns "NO" otherwise.
const lambda = Lambda()
.code((arg) => [
If(Comparison.GreaterThanOrEqual(arg, Nat(1)))
If(Comparison.GreaterThanOrEqual(arg, Nat(10)))
.Then([Return(String('YES'))])
.Else([Return(String('NO'))]),
]);
Expand All @@ -94,7 +94,7 @@ SmartML.compileValue(lambda);
// prim: "nat"
// },
// {
// int: "1"
// int: "10"
// }
// ]
// },
Expand Down
4 changes: 2 additions & 2 deletions documentation/docs/contract_structure.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Contract Structure

## Set the contract initial storage
## Define the initial storage

```js
const { Contract } = require('@tezwell/smartts-sdk');

const contract = new Contract().setStorage(Nat(0));
```

## Set the contract storage type
## Specify the storage type

```js
const { Contract } = require('@tezwell/smartts-sdk');
Expand Down
1 change: 1 addition & 0 deletions documentation/docs/expressions/crypto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Crypto
1 change: 0 additions & 1 deletion documentation/docs/expressions/test.md

This file was deleted.

4 changes: 2 additions & 2 deletions documentation/docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const SmartML = require('@tezwell/smartts-sdk/compiler');
// A Lambda that returns "YES" if the argument is greater than or equal to Nat(10), returns "NO" otherwise.
const lambda = Lambda()
.code((arg) => [
If(Comparison.GreaterThanOrEqual(arg, Nat(1)))
If(Comparison.GreaterThanOrEqual(arg, Nat(10)))
.Then([Return(String('YES'))])
.Else([Return(String('NO'))]),
]);
Expand All @@ -99,7 +99,7 @@ SmartML.compileValue(lambda);
// prim: "nat"
// },
// {
// int: "1"
// int: "10"
// }
// ]
// },
Expand Down
1 change: 0 additions & 1 deletion documentation/docs/statements/Loops.md

This file was deleted.

126 changes: 126 additions & 0 deletions documentation/docs/statements/loops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Loop Controls

## For

The statement `For` lets you repeat a block of statements a specific number of times.

- [TypeDoc](https://romarq.github.io/smartts-sdk/api/modules/statement.html#For)

```ts
const {
Contract,
EntryPoint,
TUnit,
For,
NewVariable,
SetValue,
ContractStorage,
GetVariable,
List,
Nat,
PrependToList
} = require('@tezwell/smartts-sdk');

const contract = new Contract()
// let <contract>.<storage> = [];
.setStorage(List([]))
.addEntrypoint(
new EntryPoint('entry_point_1')
.setInputType(TUnit())
.code(() => [
// let some_list = [];
NewVariable("some_list", List([])),
// for(let i = 0; i < 3; i=+1) {
// some_list = [...some_list, i];
// }
For(
Nat(0), /* let i = 0; */
Nat(3), /* i < 3; */
Nat(1) /* i =+ 1 */
).Do((i) => [
SetValue(GetVariable("some_list"), PrependToList(GetVariable("some_list"), i))
]),
// <contract>.<storage> = some_list;
SetValue(ContractStorage(), GetVariable("some_list"))
])
);
```

## ForEachOf

The `ForEachOf` lets you iterate over all elements of a `TList(...)` and apply a given block of statements on every iteration.

- [TypeDoc](https://romarq.github.io/smartts-sdk/api/modules/statement.html#ForEachOf)

```ts
const {
Contract,
EntryPoint,
ContractStorage,
ForEachOf,
NewVariable,
SetValue,
Add,
PrependToList,
GetVariable,
TUnit,
List,
Nat
} = require('@tezwell/smartts-sdk');

const contract = new Contract()
// let <contract>.<storage> = [1, 2, 3];
.setStorage(List([Nat(1), Nat(2), Nat(3)]))
.addEntrypoint(
new EntryPoint('entry_point_1')
.setInputType(TUnit())
.code(() => [
// let sum = 0;
NewVariable("sum", Nat(0)),
// <contract>.<storage>.forEach((el) => {
// sum += el;
// })
ForEachOf(ContractStorage()).Do((el) => [
SetValue(GetVariable("sum"), Add(GetVariable("sum"), el))
]),
// <contract>.<storage> = [...<contract>.<storage>, sum];
SetValue(ContractStorage(), PrependToList(ContractStorage(), GetVariable("sum")))
])
);
```

## While

The `While` statement lets you repeat a block of statements while a given condition evaluates to `true`.

- [TypeDoc](https://romarq.github.io/smartts-sdk/api/modules/statement.html#While)

```ts
const {
Contract,
EntryPoint,
While,
SetValue,
Add,
LessThanOrEqual,
ContractStorage,
Nat,
TNat,
} = require('@tezwell/smartts-sdk');

const contract = new Contract()
// let <contract>.<storage> = 0;
.setStorage(Nat(0))
.addEntrypoint(
new EntryPoint('entry_point_1')
.setInputType(TNat())
.code((argument) => [
// while (<contract>.<storage> <= argument) {
// <contract>.<storage> += 1;
// }
While(LessThanOrEqual(ContractStorage(), argument)).Do([
SetValue(ContractStorage(), Add(ContractStorage(), Nat(1))),
]),
]),
);
```
6 changes: 3 additions & 3 deletions tests/__snapshots__/Compilation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`Compile Contract Contract 1 1`] = `
template_id (static_id 0 (\\"Compilation.test.ts\\" 52))
storage (list (\\"Compilation.test.ts\\" 54) )
storage_type ((list \\"nat\\"))
messages ((ep1 True False False True (\\"Compilation.test.ts\\" 60) ((set_type (params (\\"Compilation.test.ts\\" 60)) (list \\"nat\\") (\\"Compilation.test.ts\\" 74)) (defineLocal \\"some_address\\" (literal (address tz1) (\\"Compilation.test.ts\\" 63)) True (\\"Compilation.test.ts\\" 63)) (verify (eq (getLocal \\"some_address\\" (\\"Compilation.test.ts\\" 63)) (sender) (\\"Compilation.test.ts\\" 63)) (literal (string \\"Not Admin!\\") (\\"Compilation.test.ts\\" 63)) (\\"Compilation.test.ts\\" 63)) (set (data) (params (\\"Compilation.test.ts\\" 60)) (\\"Compilation.test.ts\\" 63)))))
messages ((ep1 True False False True (\\"Compilation.test.ts\\" 60) ((set_type (params (\\"Compilation.test.ts\\" 60)) (list \\"nat\\") (\\"Compilation.test.ts\\" 74)) (defineLocal \\"some_address\\" (literal (address KT1R9M3MDffw7qSVSnbJs46aMC9YzzZz3aGT) (\\"Compilation.test.ts\\" 63)) True (\\"Compilation.test.ts\\" 63)) (verify (eq (getLocal \\"some_address\\" (\\"Compilation.test.ts\\" 63)) (sender) (\\"Compilation.test.ts\\" 63)) (literal (string \\"Not Admin!\\") (\\"Compilation.test.ts\\" 63)) (\\"Compilation.test.ts\\" 63)) (set (data) (params (\\"Compilation.test.ts\\" 60)) (\\"Compilation.test.ts\\" 63)))))
flags ((erase-comments))
privates ()
views ((onchain view True (\\"Compilation.test.ts\\" 72) False \\"\\" ((set_type (params (\\"Compilation.test.ts\\" 72)) \\"bool\\" (\\"Compilation.test.ts\\" 74)) (result (params (\\"Compilation.test.ts\\" 72)) (\\"Compilation.test.ts\\" 72)))))
Expand All @@ -19,7 +19,7 @@ exports[`Compile Contract Contract 1 1`] = `

exports[`Compile Contract Contract 1 2`] = `
"{
\\"micheline\\": \\"parameter (list %ep1 nat);\\\\nstorage (list nat);\\\\ncode\\\\n {\\\\n CAR; # @parameter\\\\n PUSH address \\\\\\"tz1\\\\\\"; # address : @parameter\\\\n SENDER; # @sender : address : @parameter\\\\n COMPARE; # int : @parameter\\\\n EQ; # bool : @parameter\\\\n IF\\\\n {}\\\\n {\\\\n PUSH string \\\\\\"Not Admin!\\\\\\"; # string : @parameter\\\\n FAILWITH; # FAILED\\\\n }; # @parameter\\\\n NIL operation; # list operation : @parameter\\\\n PAIR; # pair (list operation) @parameter\\\\n };\\\\nview\\\\n \\\\\\"view\\\\\\" bool bool\\\\n CAR; ;\\",
\\"micheline\\": \\"parameter (list %ep1 nat);\\\\nstorage (list nat);\\\\ncode\\\\n {\\\\n CAR; # @parameter\\\\n PUSH address \\\\\\"KT1R9M3MDffw7qSVSnbJs46aMC9YzzZz3aGT\\\\\\"; # address : @parameter\\\\n SENDER; # @sender : address : @parameter\\\\n COMPARE; # int : @parameter\\\\n EQ; # bool : @parameter\\\\n IF\\\\n {}\\\\n {\\\\n PUSH string \\\\\\"Not Admin!\\\\\\"; # string : @parameter\\\\n FAILWITH; # FAILED\\\\n }; # @parameter\\\\n NIL operation; # list operation : @parameter\\\\n PAIR; # pair (list operation) @parameter\\\\n };\\\\nview\\\\n \\\\\\"view\\\\\\" bool bool\\\\n CAR; ;\\",
\\"json\\": [
{
\\"prim\\": \\"storage\\",
Expand Down Expand Up @@ -64,7 +64,7 @@ exports[`Compile Contract Contract 1 2`] = `
\\"prim\\": \\"address\\"
},
{
\\"string\\": \\"tz1\\"
\\"string\\": \\"KT1R9M3MDffw7qSVSnbJs46aMC9YzzZz3aGT\\"
}
]
},
Expand Down
Loading

0 comments on commit 52b4b34

Please sign in to comment.