Skip to content

Commit ad79f86

Browse files
committed
Reorganized tabs in language/, consolidated info, removing redundancies, and modularized
1 parent f905ab5 commit ad79f86

File tree

7 files changed

+646
-16
lines changed

7 files changed

+646
-16
lines changed

documentation/language/01_layout.md

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_label: Project Layout
55
---
66
[general tags]: # (project, project_layout, manifest, module)
77

8-
## The Manifest
8+
## Manifest
99

1010
**program.json** is the Leo manifest file that configures our package.
1111
```json title="program.json"
@@ -26,12 +26,109 @@ The program ID in `program` is the official name that other developers will be a
2626

2727
Dependencies will be added to the field of the same name, as they are added. The dependencies are also pegged in the **leo.lock** file.
2828

29-
## The Code
3029

31-
The `src/` directory is where all of your Leo code will live. The main entry point of your project is a file in this directory.appropriately named `main.leo`. Calls to many of the Leo CLI commands will require you to have this file within your project in order to succeed properly.
30+
The `src/` directory is where all of your Leo code will live. The main entry point of your project is a file in this directory appropriately named `main.leo`. Calls to many of the Leo CLI commands will require you to have this file within your project in order to succeed properly.
3231

3332

34-
### Modules
33+
## Programs
34+
35+
A program is a collection of code (its functions) and data (its types) that resides at a
36+
[program ID](#program-id) on the Aleo blockchain. A program is declared as `program {name}.{network} { ... }`.
37+
The body of the program is delimited by curly braces `{}`.
38+
39+
```leo title=main.leo
40+
import foo.aleo;
41+
42+
program hello.aleo {
43+
const FOO: u64 = 1u64;
44+
mapping account: address => u64;
45+
46+
record Token {
47+
owner: address,
48+
amount: u64,
49+
}
50+
51+
struct Message {
52+
sender: address,
53+
object: u64,
54+
}
55+
56+
async transition mint_public(
57+
public receiver: address,
58+
public amount: u64,
59+
) -> (Token, Future) {
60+
return (Token {
61+
owner: receiver,
62+
amount,
63+
}, update_state(receiver, amount));
64+
}
65+
66+
async function update_state(
67+
public receiver: address,
68+
public amount: u64,
69+
) {
70+
let current_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
71+
Mapping::set(account, receiver, current_amount + amount);
72+
}
73+
74+
function compute(a: u64, b: u64) -> u64 {
75+
return a + b + FOO;
76+
}
77+
}
78+
```
79+
80+
81+
82+
The following must be declared inside the scope of a program in a Leo file:
83+
84+
- [Constants](#constant)
85+
- [Mappings](#mapping) and [Storage](#storage)
86+
- [Records](#record)
87+
- [Structs](#struct)
88+
- [Transitions](#transition-function) and [Async Transitions](#async-function)
89+
- [Functions](#transition-function) and [Async Functions](#async-function)
90+
- [Inlines](#inline)
91+
92+
The following must be declared outside the scope of a program in a Leo file:
93+
94+
- [Imports](#imports)
95+
96+
Declarations are locally accessible within a program file. If you need a declaration from another Leo file, you must import it.
97+
### Imports
98+
99+
You can import dependencies that are downloaded to the `imports` directory.
100+
An import is declared as `import {filename}.aleo;`
101+
The dependency resolver will pull the imported program from the network or the local filesystem.
102+
103+
```leo showLineNumbers
104+
import foo.aleo; // Import all `foo.aleo` declarations into the `hello.aleo` program.
105+
106+
program hello.aleo { }
107+
```
108+
109+
### Program ID
110+
111+
A program ID is declared as `{name}.{network}`.
112+
113+
The first character of a `name` must be a lowercase letter.
114+
`name` can only contain lowercase letters, numbers, and underscores, and must not contain a double underscore (`__`) or the keyword `aleo` in it.
115+
116+
Currently, `aleo` is the only supported `network` domain.
117+
118+
```leo showLineNumbers
119+
program hello.aleo; // valid
120+
121+
program Foo.aleo; // invalid
122+
program baR.aleo; // invalid
123+
program 0foo.aleo; // invalid
124+
program 0_foo.aleo; // invalid
125+
program _foo.aleo; // invalid
126+
program foo__bar.aleo; // invalid
127+
program aleo.aleo; // invalid
128+
```
129+
130+
131+
## Modules
35132

36133
In addition to your main file, Leo also supports a module system as of v3.2.0.
37134

@@ -75,6 +172,7 @@ inline increment(x: field) -> field {
75172
```
76173

77174

175+
78176
<!--
79177
80178
## The Tests

documentation/language/03_data_types.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ sidebar_label: Data Types
88

99
## Types
1010

11+
### Addresses
12+
13+
Addresses are defined to enable compiler-optimized routines for parsing and operating over addresses.
14+
These semantics will be accompanied by a standard library in a future sprint.
15+
16+
```leo
17+
let sender: address = aleo1ezamst4pjgj9zfxqq0fwfj8a4cjuqndmasgata3hggzqygggnyfq6kmyd4;
18+
let receiver = aleo129nrpl0dxh4evdsan3f4lyhz5pdgp6klrn5atp37ejlavswx5czsk0j5dj;
19+
```
20+
1121
### Booleans
1222

1323
Leo supports the traditional `true` or `false` boolean values.
@@ -87,15 +97,6 @@ let b = 211111543735709260606220623469538663283887092640840819519368524639472136
8797
let c: scalar = 0;
8898
```
8999

90-
### Addresses
91-
92-
Addresses are defined to enable compiler-optimized routines for parsing and operating over addresses.
93-
These semantics will be accompanied by a standard library in a future sprint.
94-
95-
```leo
96-
let sender: address = aleo1ezamst4pjgj9zfxqq0fwfj8a4cjuqndmasgata3hggzqygggnyfq6kmyd4;
97-
let receiver = aleo129nrpl0dxh4evdsan3f4lyhz5pdgp6klrn5atp37ejlavswx5czsk0j5dj;
98-
```
99100

100101
### Signatures
101102

@@ -126,7 +127,7 @@ program test.aleo {
126127
```
127128

128129

129-
### Array
130+
### Arrays
130131

131132
Leo supports static arrays. Array types are declared as `[type; length]` and can be nested. Arrays cannot be empty nor modified.
132133

@@ -179,7 +180,7 @@ transition sum_with_loop(a: [u64; 4]) -> u64 {
179180
}
180181
```
181182

182-
### Tuple
183+
### Tuples
183184

184185
Leo supports tuples. Tuple types are declared as `(type1, type2, ...)` and can be nested. Tuples cannot be empty or modified.
185186

@@ -232,8 +233,25 @@ let m = Matrix::[2, 2] { data: [0, 1, 2, 3] };
232233
```
233234
Note that generic structs cannot currently be imported outside a program, but can be declared and used in submodules. Acceptable types for const generic parameters include integer types, `bool`, `scalar`, `group`, `field`, and `address`.
234235

236+
### Records
237+
238+
A [record](https://developer.aleo.org/concepts/fundamentals/records) data type is the method of encoding private state on Aleo. Records are declared as `record {name} {}`. A record name must not contain the keyword `aleo`, and must not be a prefix of any other record name.
239+
240+
Records contain component declarations `{visibility} {name}: {type},`. Names of record components must not contain the keyword `aleo`. The visibility qualifier may be specified as `constant`, `public`, or `private`. If no qualifier is provided, Leo defaults to `private`.
241+
242+
Record data structures must always contain a component named `owner` of type `address`, as shown below. When passing a record as input to a program function, the `_nonce: group` and `_version: u8` components are also required but do not need to be declared in the Leo program. They are inserted automatically by the compiler.
243+
244+
```aleo showLineNumbers
245+
record Token {
246+
// The token owner.
247+
owner: address,
248+
// The token amount.
249+
amount: u64,
250+
}
251+
```
252+
235253
## Option Types
236-
As of v3.3.0, Leo supports first-class option types using the `T?` syntax, where `T` is any of the types previously mentioned, excluding `address`, `signature`, and `tuple`. A value of type `T?` can be initialized into two states: either a value of type `T`, or `none`:
254+
As of v3.3.0, Leo supports first-class option types using the `T?` syntax, where `T` is any of the types previously mentioned, excluding `record`, `address`, `signature`, and `tuple`. A value of type `T?` can be initialized into two states: either a value of type `T`, or `none`:
237255
```leo
238256
let w: u8? = 42u8;
239257
let x: u8? = none;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
id: control_flow
3+
title: Control Flow
4+
sidebar_label: Control Flow
5+
---
6+
[general tags]: # (loop, conditional, return)
7+
8+
## Conditional Statements
9+
10+
Conditional statements are declared as `if {condition} { ... } else if {condition} { ... } else { ... }`.
11+
Conditional statements can be nested.
12+
```leo
13+
let a: u8 = 1u8;
14+
15+
if a == 1u8 {
16+
a += 1u8;
17+
} else if a == 2u8 {
18+
a += 2u8;
19+
} else {
20+
a += 3u8;
21+
}
22+
```
23+
24+
Leo also supports ternary expressions. Ternary expressions are declared as `{condition} ? {then} : {else}`, and can be nested.
25+
```leo
26+
let a: u8 = 1u8;
27+
a = (a == 1u8) ? a + 1u8 : ((a == 2u8) ? a + 2u8 : a + 3u8);
28+
```
29+
30+
## For Loops
31+
32+
For loops are declared as `for {variable: type} in {lower bound}..{upper bound}`.
33+
The loop bounds must be integer constants of the same type. Furthermore, if
34+
the lower bound is superior or equal to the upper bound, the loop will result in no operations.
35+
Nested loops are supported.
36+
37+
38+
```leo
39+
let count: u32 = 0u32;
40+
41+
for i: u32 in 0u32..5u32 {
42+
count += 1u32;
43+
}
44+
45+
return count; // returns 5u32
46+
```
47+
48+
## Return Statements
49+
50+
Return statements are declared as `return {expression};`.
51+
52+
```leo
53+
let a: u8 = 1u8;
54+
55+
if a == 1u8 {
56+
return a + 1u8;
57+
} else if a == 2u8 {
58+
return a + 2u8;
59+
} else {
60+
return a + 3u8;
61+
}
62+
```

0 commit comments

Comments
 (0)