update readme

This commit is contained in:
collin 2020-05-21 11:03:43 -07:00
parent 9cf7d68aef
commit 2ebfed2faa

250
README.md
View File

@ -1,41 +1,86 @@
# The Leo Language
* All code examples can be copied and pasted into `main.leo` directly and executed with cargo run
# The Leo Programming Language
* Programs should be formatted:
1. Import definitions
2. Struct definitions
2. Circuit definitions
3. Function definitions
### Integers:
Currently, all integers are parsed as u32.
You can choose to explicitly add the type or let the compiler interpret implicitly.
## Mutability
* All defined variables in Leo are immutable by default.
* Variables can be made mutable with the `mut` keyword.
```rust
function main() -> u32 {
let a: u32 = 1u32 + 1u32; // explicit type
let b = 1 - 1; // implicit type
let c = 2 * 2;
let d = 4 / 2;
let e = 2 ** 3;
function main() {
let a = 0u32;
//a = 1 <- Will fail
let mut b = 0u32;
b = 1; // <- Ok
}
```
## Booleans
Explicit types are optional.
```rust
function main() -> bool {
let a: bool = true || false;
let b = false && false;
let c = 1u32 == 1u32;
return a
}
```
### Field Elements:
Field elements must have the type added explicitly.
## Numbers
* The definition of a number must include an explict type.
* After assignment, you can choose to explicitly add the type or let the compiler interpret implicitly.
* Type casting is not supported.
* Comparators are not supported.
### Integers
Supported integer types: `u8`, `u16`, `u32`, `u64`, `u128`
```rust
function main() -> fe {
let f: fe = 21888242871839275222246405745257275088548364400416034343698204186575808495617fe;
let a = 1fe + 1fe;
let b = 1fe - 1fe;
let c = 2fe * 2fe;
let d = 4fe / 2fe;
return a
function main() -> u32 {
let a = 2u32; // explicit type
let a: u32 = 1 + 1; // explicit type
let b = a - 1; // implicit type
let c = b * 4;
let d = c / 2;
let e = d ** 3;
return e
}
```
### Operator Assignment Statements:
### Field Elements
```rust
function main() -> field {
let a = 1000field; // explicit type
let a: field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; // explicit type
let b = a + 1; // implicit type
let c = b - 1;
let d = c * 4;
let e = d / 2;
let f = e ** 3u32; //Field exponents must be a `u32` type.
return f
}
```
### Group Elements
```rust
function main() -> group {
let a = 1000group; // explicit type
let a: group = 21888242871839275222246405745257275088548364400416034343698204186575808495617; // explicit type
let b = a + 1; // implicit type
let c = b - 1;
return c
}
```
### Operator Assignment Statements
```rust
function main() -> u32 {
let a = 10;
let mut a = 10;
a += 5;
a -= 10;
a *= 5;
@ -46,30 +91,18 @@ function main() -> u32 {
}
```
### Booleans:
```rust
function main() -> bool {
let a: bool = true || false;
let b = false && false;
let c = 1 == 1;
return a
}
```
### Arrays:
## Arrays
Leo supports static arrays with fixed length.
Array type must be explicitly stated
```rust
function main() -> u32[2] {
// initialize an integer array with integer values
let a: u32[3] = [1, 2, 3];
let mut a: u32[3] = [1, 2, 3];
// set a member to a value
// set a mutable member to a value
a[2] = 4;
// initialize an array of 4 values all equal to 42
let b = [42; 4];
let b = [42u8; 4];
// initialize an array of 5 values copying all elements of b using a spread
let c = [1, ...b];
@ -78,63 +111,40 @@ function main() -> u32[2] {
let d = c[1..3];
// initialize a field array
let e = [5fe; 2];
let e = [5field; 2];
// initialize a boolean array
let f = [true, false || true, true];
// return an array
return d
}
```
### Structs:
### Multidimensional Arrays
```rust
struct Point {
x: u32
y: u32
}
function main() -> u32 {
Point p = Point {x: 1, y: 0}
return p.x
}
```
```rust
struct Foo {
x: bool
}
function main() -> Foo {
let f = Foo {x: true};
f.x = false;
return f
function main() -> u32[3][2] {
let m = [[0u32, 0u32], [0u32, 0u32]];
let m: u32[3][2] = [[0; 3]; 2];
return m
}
```
### Assert Equals:
This will enforce that the two values are equal in the constraint system.
## Conditionals
```rust
function main() {
assert_eq(45, 45);
assert_eq(2fe, 2fe);
assert_eq(true, true);
}
```
### Conditionals:
#### If Else Ternary Expression
### If Else Ternary Expression
```rust
function main() -> u32 {
let y = if 3==3 ? 1 : 5;
return y
}
```
#### If Else Conditional Statement
### If Else Conditional Statement
```rust
function main(a: private bool, b: private bool) -> u32 {
let res = 0;
let mut res = 0u32;
if (a) {
res = 1;
} else if (b) {
@ -145,25 +155,26 @@ function main(a: private bool, b: private bool) -> u32 {
return res
}
```
#### For loop
### For loop
```rust
function main() -> fe {
let a = 1fe;
function main() -> field {
let mut a = 1field;
for i in 0..4 {
a = a + 1fe;
a = a + 1;
}
return a
}
```
### Functions:
## Functions
```rust
function test1(a : u32) -> u32 {
return a + 1
}
function test2(b: fe) -> fe {
return b * 2fe
function test2(b: field) -> field {
return b * 2field
}
function test3(c: bool) -> bool {
@ -176,20 +187,20 @@ function main() -> u32 {
```
#### Function Scope:
### Function Scope
```rust
function foo() -> field {
// return myGlobal <- not allowed
return 42fe
return 42field
}
function main() -> field {
let myGlobal = 42fe;
let myGlobal = 42field;
return foo()
}
```
#### Multiple returns:
### Multiple returns
Functions can return tuples whose types are specified in the function signature.
```rust
function test() -> (u32, u32[2]) {
@ -203,21 +214,21 @@ function main() -> u32[3] {
}
```
#### Main function inputs:
### Main function inputs
Main function inputs are allocated as public or private variables in the program's constaint system.
```rust
function main(a: private fe) -> fe {
function main(a: private field) -> field {
return a
}
```
```rust
function main(a: public fe) -> fe {
function main(a: public field) -> field {
return a
}
```
Function inputs are passed by value.
```rust
function test(a: u32) {
function test(mut a: u32) {
a = 0;
}
@ -229,15 +240,41 @@ function main() -> u32 {
}
```
### Imports:
## Circuits
```rust
bab
```
```rust
circuit Point {
x: u32
y: u32
}
function main() -> u32 {
let p = Point {x: 1, y: 0}
return p.x
}
```
```rust
circuit Foo {
x: bool
}
function main() -> Foo {
let mut f = Foo {x: true};
f.x = false;
return f
}
```
## Imports
Both struct and function imports are supported.
import all: `*`
import alias: `symbol as alias`
/simple_import.leo
`src/simple_import.leo`
```rust
struct Point {
circuit Point {
x: u32
y: u32
}
@ -247,7 +284,7 @@ function test() -> (u32, u32[2]) {
}
```
/simple.leo
`src/simple.leo`
```rust
from "./simple_import" import {
Point as Foo,
@ -265,6 +302,20 @@ function main() -> (u32[3]) {
}
```
## Constraints
### Assert Equals
This will enforce that the two values are equal in the constraint system.
```rust
function main() {
assert_eq(45, 45);
assert_eq(2field, 2field);
assert_eq(true, true);
}
```
# Leo CLI
@ -285,7 +336,7 @@ This will create a new directory with a given package name. The new package will
- lib.leo # Your program library
- main.leo # Your program
- tests
- tests.leo # Your program tests
- test.leo # Your program tests
- Leo.toml # Your program manifest
```
@ -379,6 +430,13 @@ To deploy your program to Aleo, run:
leo deploy
```
# Install
To install Leo from source, in the root directory of the repository, run:
```
cargo install --path .
```
## TODO
- Change `target` directory to some other directory to avoid collision.