Skip to content

Commit

Permalink
fix: ci runner (cairo-book#158)
Browse files Browse the repository at this point in the history
* fix: add --available-gas option to cairo-runner

* fix: CI
  • Loading branch information
enitrat authored May 19, 2023
1 parent bb5d78d commit eaf9a47
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
book
target
output

# Editors tmp files.
*~
2 changes: 1 addition & 1 deletion mdbook-cairo/scripts/cairo_programs_verifier.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fi
has_error=false

for prog in *.cairo; do
cairo-run "$prog" > output/"$prog".out 2> output/"$prog".err
cairo-run --available-gas=20000000 "$prog" > output/"$prog".out 2> output/"$prog".err

compile_code="$?"

Expand Down
12 changes: 6 additions & 6 deletions po/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ msgstr ""

#: src/title-page.md:5
msgid ""
"This version of the text assumes you’re using Cairo v1.0.0-alpha.7 (released "
"This version of the text assumes you’re using Cairo v1.0.0 (released "
"2023-04-13). See the “Installation” section of Chapter 1 to install or "
"update Cairo."
msgstr ""
"Esta versión del texto asume que estás usando Cairo v1.0.0-alpha.7 "
"Esta versión del texto asume que estás usando Cairo v1.0.0 "
"(publicado el 2023-04-13). Consulte la sección \"Instalación\" del Capítulo "
"1 para instalar o actualizar Cairo."

Expand Down Expand Up @@ -522,11 +522,11 @@ msgstr "### Instalación"
msgid ""
"If you wish to install a specific release of Cairo rather than the latest "
"head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export "
"CAIRO_GIT_TAG=v1.0.0-alpha.6`)."
"CAIRO_GIT_TAG=v1.0.0`)."
msgstr ""
"Si deseas instalar una versión específica de Cairo en lugar de la última "
"versión, debes establecer la variable de entorno `CAIRO_GIT_TAG` (por "
"ejemplo,`export CAIRO_GIT_TAG=v1.0.0-alpha.6`)."
"ejemplo,`export CAIRO_GIT_TAG=v1.0.0`)."

#: src/ch01-01-installation.md:22
msgid ""
Expand Down Expand Up @@ -855,7 +855,7 @@ msgid ""
"# View tags (you can also do this in the cairo compiler repository)\n"
"git describe --tags `git rev-list --tags`\n"
"# Checkout the version you want\n"
"git checkout tags/v1.0.0-alpha.6\n"
"git checkout tags/v1.0.0\n"
"\n"
"# Generate release binaries\n"
"cargo build --all --release\n"
Expand All @@ -878,7 +878,7 @@ msgstr ""
"# View tags (you can also do this in the cairo compiler repository)\n"
"git describe --tags `git rev-list --tags`\n"
"# Checkout the version you want\n"
"git checkout tags/v1.0.0-alpha.6\n"
"git checkout tags/v1.0.0\n"
"\n"
"# Generate release binaries\n"
"cargo build --all --release\n"
Expand Down
8 changes: 4 additions & 4 deletions po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ msgstr "par la communauté de Cairo et ses [contributeurs](https://github.com/ca

#: src/title-page.md:5
msgid ""
"This version of the text assumes you’re using Cairo v1.0.0-alpha.7 (released "
"This version of the text assumes you’re using Cairo v1.0.0 (released "
"2023-04-13). See the “Installation” section of Chapter 1 to install or "
"update Cairo."
msgstr "La version actuelle considère que vous utilisez Cairo v1.0.0-alpha.7 (version 2023-04-13). "
msgstr "La version actuelle considère que vous utilisez Cairo v1.0.0 (version 2023-04-13). "
"Se référer à la section “Installation” du chapitre 1 pour installer ou actualiser Cairo."

#: src/ch00-01-foreword.md:1
Expand Down Expand Up @@ -427,7 +427,7 @@ msgstr ""
msgid ""
"If you wish to install a specific release of Cairo rather than the latest "
"head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export "
"CAIRO_GIT_TAG=v1.0.0-alpha.6`)."
"CAIRO_GIT_TAG=v1.0.0`)."
msgstr ""

#: src/ch01-01-installation.md:22
Expand Down Expand Up @@ -645,7 +645,7 @@ msgid ""
"# View tags (you can also do this in the cairo compiler repository)\n"
"git describe --tags `git rev-list --tags`\n"
"# Checkout the version you want\n"
"git checkout tags/v1.0.0-alpha.6\n"
"git checkout tags/v1.0.0\n"
"\n"
"# Generate release binaries\n"
"cargo build --all --release\n"
Expand Down
6 changes: 3 additions & 3 deletions po/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ msgstr ""

#: src/title-page.md:5
msgid ""
"This version of the text assumes you’re using Cairo v1.0.0-alpha.7 (released "
"This version of the text assumes you’re using Cairo v1.0.0 (released "
"2023-04-13). See the “Installation” section of Chapter 1 to install or "
"update Cairo."
msgstr ""
Expand Down Expand Up @@ -416,7 +416,7 @@ msgstr ""
msgid ""
"If you wish to install a specific release of Cairo rather than the latest "
"head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export "
"CAIRO_GIT_TAG=v1.0.0-alpha.6`)."
"CAIRO_GIT_TAG=v1.0.0`)."
msgstr ""

#: src/ch01-01-installation.md:22
Expand Down Expand Up @@ -638,7 +638,7 @@ msgid ""
"# View tags (you can also do this in the cairo compiler repository)\n"
"git describe --tags `git rev-list --tags`\n"
"# Checkout the version you want\n"
"git checkout tags/v1.0.0-alpha.6\n"
"git checkout tags/v1.0.0\n"
"\n"
"# Generate release binaries\n"
"cargo build --all --release\n"
Expand Down
102 changes: 52 additions & 50 deletions src/ch01-01-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Install [Git](https://git-scm.com/).

If you wish to install a specific release of Cairo rather than the latest head, set the `CAIRO_GIT_TAG` environment variable (e.g. `export CAIRO_GIT_TAG=v1.0.0`).

```bash
```bash
curl -L https://github.com/franalgaba/cairo-installer/raw/main/bin/cairo-installer | bash
```

Expand Down Expand Up @@ -54,81 +54,83 @@ exec $SHELL

### Set up your shell environment for Cairo

* Define environment variable `CAIRO_ROOT` to point to the path where
- Define environment variable `CAIRO_ROOT` to point to the path where
Cairo will store its data. `$HOME/.cairo` is the default.
If you installed Cairo via Git checkout, we recommend
to set it to the same location as where you cloned it.
* Add the `cairo-*` executables to your `PATH` if it's not already there
- Add the `cairo-*` executables to your `PATH` if it's not already there

The below setup should work for the vast majority of users for common use cases.

- For **bash**:
- For **bash**:

Stock Bash startup files vary widely between distributions in which of them source
which, under what circumstances, in what order and what additional configuration they perform.
As such, the most reliable way to get Cairo in all environments is to append Cairo
configuration commands to both `.bashrc` (for interactive shells)
and the profile file that Bash would use (for login shells).
Stock Bash startup files vary widely between distributions in which of them source
which, under what circumstances, in what order and what additional configuration they perform.
As such, the most reliable way to get Cairo in all environments is to append Cairo
configuration commands to both `.bashrc` (for interactive shells)
and the profile file that Bash would use (for login shells).

First, add the commands to `~/.bashrc` by running the following in your terminal:
First, add the commands to `~/.bashrc` by running the following in your terminal:

~~~ bash
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bashrc
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bashrc
~~~
```bash
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bashrc
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bashrc
```

Then, if you have `~/.profile`, `~/.bash_profile` or `~/.bash_login`, add the commands there as well.
If you have none of these, add them to `~/.profile`.
Then, if you have `~/.profile`, `~/.bash_profile` or `~/.bash_login`, add the commands there as well.
If you have none of these, add them to `~/.profile`.

* to add to `~/.profile`:
~~~ bash
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.profile
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.profile
~~~
- to add to `~/.profile`:

* to add to `~/.bash_profile`:
~~~ bash
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bash_profile
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bash_profile
~~~
```bash
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.profile
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.profile
```

- For **Zsh**:
~~~ zsh
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.zshrc
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.zshrc
~~~
- to add to `~/.bash_profile`:
```bash
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.bash_profile
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.bash_profile
```

If you wish to get Cairo in non-interactive login shells as well, also add the commands to `~/.zprofile` or `~/.zlogin`.
- For **Zsh**:

- For **Fish shell**:
```zsh
echo 'export CAIRO_ROOT="$HOME/.cairo"' >> ~/.zshrc
echo 'command -v cairo-compile >/dev/null || export PATH="$CAIRO_ROOT/target/release:$PATH"' >> ~/.zshrc
```

If you have Fish 3.2.0 or newer, execute this interactively:
If you wish to get Cairo in non-interactive login shells as well, also add the commands to `~/.zprofile` or `~/.zlogin`.

~~~ fish
set -Ux CAIRO_ROOT $HOME/.cairo
fish_add_path $CAIRO_ROOT/target/release
~~~
- For **Fish shell**:

Otherwise, execute the snippet below:
If you have Fish 3.2.0 or newer, execute this interactively:

```fish
set -Ux CAIRO_ROOT $HOME/.cairo
fish_add_path $CAIRO_ROOT/target/release
```

~~~ fish
set -Ux CAIRO_ROOT $HOME/.cairo
set -U fish_user_paths $CAIRO_ROOT/target/release $fish_user_paths
~~~
Otherwise, execute the snippet below:

In MacOS, you might also want to install [Fig](https://fig.io/) which
```fish
set -Ux CAIRO_ROOT $HOME/.cairo
set -U fish_user_paths $CAIRO_ROOT/target/release $fish_user_paths
```

In MacOS, you might also want to install [Fig](https://fig.io/) which
provides alternative shell completions for many command line tools with an
IDE-like popup interface in the terminal window.
(Note that their completions are independent from Cairo's codebase
so they might be slightly out of sync for bleeding-edge interface changes.)
### Restart your shell
for the `PATH` changes to take effect.
for the `PATH` changes to take effect.
```sh
exec "$SHELL"
```
```sh
exec "$SHELL"
```
## Installing Cairo Manually ([Guide](https://github.com/auditless/cairo-template) by [Abdel](https://github.com/abdelhamidbakhta))
Expand Down Expand Up @@ -160,7 +162,7 @@ git checkout tags/v1.0.0
cargo build --all --release
```
.
.
**NOTE: Keeping Cairo up to date**
Expand All @@ -185,7 +187,7 @@ export PATH="$CAIRO_ROOT/target/release:$PATH"
- Disable previous Cairo 0.x extension
- Install the Cairo 1 extension for proper syntax highlighting and code navigation.
Just follow the steps indicated [here](https://github.com/starkware-libs/cairo/blob/main/vscode-cairo/README.md).
Just follow the steps indicated [here](https://github.com/starkware-libs/cairo/blob/main/vscode-cairo/README.md).
#### Cairo Language Server
Expand Down
32 changes: 26 additions & 6 deletions src/ch07-01-generic-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ fn main() {

Compiling the above code would error due to the `derive` macro not working well with generics. When using generic types is best to directly write the traits you want to use:

<!-- TODO This is no longer true after in version 1.1.x and should be removed in the next versions -->

```rust
struct Wallet<T> {
balance: T,
Expand Down Expand Up @@ -202,14 +204,15 @@ struct Wallet<T> {
}

impl WalletDrop<T, impl TDrop: Drop<T>> of Drop<Wallet<T>>;
impl WalletCopy<T, impl TCopy: Copy<T>> of Copy<Wallet<T>>;

trait WalletTrait<T> {
fn balance(self: @Wallet<T>) -> @T;
fn balance(self: @Wallet<T>) -> T;
}

impl WalletImpl<T> of WalletTrait<T> {
fn balance(self: @Wallet<T>) -> @T {
return self.balance;
impl WalletImpl<T, impl TCopy: Copy<T>> of WalletTrait<T> {
fn balance(self: @Wallet<T>) -> T {
return *self.balance;
}
}

Expand All @@ -224,6 +227,23 @@ We first define `WalletTrait<T>` trait using a generic type `T` which defines a
We can also specify constraints on generic types when defining methods on the type. We could, for example, implement methods only for `Wallet<u128>` instances rather than `Wallet<T>`. In the code example we define an implementation for wallets which have a concrete type of `u128` for the `balance` field.

```rust
struct Wallet<T> {
balance: T,
}
impl WalletDrop<T, impl TDrop: Drop<T>> of Drop<Wallet<T>>;
impl WalletCopy<T, impl TCopy: Copy<T>> of Copy<Wallet<T>>;
/// Generic trait for wallets
trait WalletTrait<T> {
fn balance(self: @Wallet<T>) -> T;
}

impl WalletImpl<T, impl TCopy: Copy<T>> of WalletTrait<T> {
fn balance(self: @Wallet<T>) -> T {
return *self.balance;
}
}

/// Trait for wallets of type u128
trait WalletReceiveTrait {
fn receive(ref self: Wallet<u128>, value: u128);
}
Expand All @@ -238,7 +258,7 @@ fn main() {
let mut w = Wallet { balance: 50_u128 };
assert(w.balance() == 50_u128, 0);

w.receive(100_u128)
w.receive(100_u128);
assert(w.balance() == 150_u128, 0);
}
```
Expand Down Expand Up @@ -289,7 +309,7 @@ impl WalletMixImpl<T1, impl T1Drop: Drop<T1>, U1, impl U1Drop: Drop<U1>> of Wall

We add the requirements for `T1` and `U1` to be droppable on `WalletMixImpl` declaration. Then we do the same for `T2` and `U2`, this time as part of `mixup` signature. We can now try the `mixup` function:

```rs
```rs, does_not_compile
fn main() {
let w1 = Wallet { balance: true, address: 10_u128 };
let w2 = Wallet { balance: 32, address: 100_u8 };
Expand Down
19 changes: 15 additions & 4 deletions src/ch07-02-traits-in-cairo.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ In the example below, we use generic type `T` and our method signatures can use
```rust
use debug::PrintTrait;

#[derive(Copy,Drop)]
struct Rectangle{
height: u64,
width: u64,
}

#[derive(Copy,Drop)]
struct Circle{
radius: u64,
}

// Here T is an alias type which will be provided buring implementation
trait ShapeGeometry<T> {
fn boundary(self: T) -> u64;
Expand Down Expand Up @@ -92,26 +103,26 @@ impl CircleGeometry of ShapeGeometry<Circle> {
}

fn main() {
let rect = Rectangle { height: 5_u128, width: 7_u128 };
let rect = Rectangle { height: 5_u64, width: 7_u64 };
rect.area().print(); // 35
rect.boundary().print(); // 24

let circ = Circle { radius: 5_u128 };
let circ = Circle { radius: 5_u64 };
circ.area().print(); // 78
circ.boundary().print(); // 31
}
```

## Managing and using external trait implementations

To use traits methods, you need to make sure the correct traits/implementation(s) are imported. In the code above we imported `PrintTrait` from `debug` with `use debug::PrintTrait;` to use `print()` methods.
To use traits methods, you need to make sure the correct traits/implementation(s) are imported. In the code above we imported `PrintTrait` from `debug` with `use debug::PrintTrait;` to use the `print()` methods on supported types.

In some cases you might need to import not only the trait but also the implementation if they are declared in separate modules.
If `CircleGeometry` was in a separate module/file `circle` then to use `boundary` on `circ: Circle`, we'd need to import `CircleGeometry` in addition to `ShapeGeometry`.

If the code was organised into modules like this,

```rust
```rust, does_not_compile
use debug::PrintTrait;
// struct Circle { ... } and struct Rectangle { ... }
Expand Down

0 comments on commit eaf9a47

Please sign in to comment.