Skip to content

Commit

Permalink
Dev alpha.7 (shramee#111)
Browse files Browse the repository at this point in the history
* chore: bump to alpha.7 (shramee#101)

* chore: bump to alpha.7

* chore:fmt

* feat: loops (shramee#104)

* chore: change doc links to cairo book (shramee#106)

---------

Co-authored-by: Mathieu <[email protected]>
  • Loading branch information
shramee and enitrat committed Apr 26, 2023
1 parent 0cc010a commit 6466b03
Show file tree
Hide file tree
Showing 65 changed files with 3,165 additions and 2,460 deletions.
940 changes: 428 additions & 512 deletions Cargo.lock

Large diffs are not rendered by default.

40 changes: 21 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,31 @@ home = "0.5.3"
glob = "0.3.0"

# Cairo runner dependencies
cairo-lang-runner = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-debug = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-sierra-ap-change = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-sierra-gas = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-sierra-generator = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-semantic = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-sierra-to-casm = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-filesystem = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-plugins = { git = "https://github.com/starkware-libs/cairo",rev = "v1.0.0-alpha.6"}
cairo-lang-runner = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-debug = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-sierra-ap-change = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-sierra-gas = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-sierra-generator = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-semantic = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-sierra-to-casm = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-filesystem = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-plugins = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}
cairo-lang-lowering = { git = "https://github.com/starkware-libs/cairo",rev = "7b4bbf3d57230e0e307fbb634394f6e57b3c63cc"}


anyhow = "1.0.66"
ark-ff = "0.4.0-alpha.7"
ark-std = "0.3.0"
cairo-felt = "0.1.1"
cairo-vm = "0.1.1"
cairo-felt = "0.3.0-rc1"
cairo-vm = "0.3.0-rc1"
clap = { version = "4.0", features = ["derive"] }
itertools = "0.10.3"
num-bigint = "0.4"
Expand All @@ -55,6 +56,7 @@ salsa = "0.16.1"
thiserror = "1.0.32"
rayon = "0.9.0"
colored = "2"
unescaper = "0.1.1"

[dev-dependencies]
assert_cmd = "0.11.0"
Expand Down
2 changes: 1 addition & 1 deletion corelib/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core"
version = "1.0.0-alpha.6"
version = "1.0.0-alpha.7"

# NOTE: This is non-public, unstable Scarb's field, which instructs resolver that this package does not
# depend on `core`, which is only true for this particular package. Nobody else should use it.
Expand Down
88 changes: 57 additions & 31 deletions corelib/src/array.cairo
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
use gas::withdraw_gas;
use traits::IndexView;

use box::BoxTrait;
use gas::withdraw_gas;
use option::OptionTrait;

extern type Array<T>;
extern fn array_new<T>() -> Array<T> nopanic;
extern fn array_append<T>(ref arr: Array<T>, value: T) nopanic;
extern fn array_pop_front<T>(ref arr: Array<T>) -> Option<Box<T>> nopanic;
extern fn array_snapshot_pop_front<T>(ref arr: @Array<T>) -> Option<Box<@T>> nopanic;
extern fn array_snapshot_pop_back<T>(ref arr: @Array<T>) -> Option<Box<@T>> nopanic;
#[panic_with('Index out of bounds', array_at)]
extern fn array_get<T>(
arr: @Array<T>, index: usize
) -> Option<Box<@T>> implicits(RangeCheck) nopanic;
extern fn array_slice<T>(
arr: @Array<T>, start: usize, length: usize
) -> Option<@Array<T>> implicits(RangeCheck) nopanic;
extern fn array_len<T>(arr: @Array<T>) -> usize nopanic;

trait ArrayTrait<T> {
fn new() -> Array<T>;
fn append(ref self: Array<T>, value: T);
fn pop_front(ref self: Array<T>) -> Option<T>;
fn pop_front(ref self: Array<T>) -> Option<T> nopanic;
fn get(self: @Array<T>, index: usize) -> Option<Box<@T>>;
fn at(self: @Array<T>, index: usize) -> @T;
fn len(self: @Array<T>) -> usize;
fn is_empty(self: @Array<T>) -> bool;
fn span(self: @Array<T>) -> Span<T>;
}
impl ArrayImpl<T> of ArrayTrait::<T> {
impl ArrayImpl<T> of ArrayTrait<T> {
#[inline(always)]
fn new() -> Array<T> {
array_new()
Expand All @@ -32,7 +39,7 @@ impl ArrayImpl<T> of ArrayTrait::<T> {
array_append(ref self, value)
}
#[inline(always)]
fn pop_front(ref self: Array<T>) -> Option<T> {
fn pop_front(ref self: Array<T>) -> Option<T> nopanic {
match array_pop_front(ref self) {
Option::Some(x) => Option::Some(x.unbox()),
Option::None(_) => Option::None(()),
Expand Down Expand Up @@ -60,26 +67,33 @@ impl ArrayImpl<T> of ArrayTrait::<T> {
}
}

// Impls for common generic types
impl ArrayDrop<T, impl TDrop: Drop::<T>> of Drop::<Array::<T>>;
impl ArrayIndex<T> of IndexView<Array<T>, usize, @T> {
fn index(self: @Array<T>, index: usize) -> @T {
array_at(self, index).unbox()
}
}

// Impls for common generic types
impl ArrayDrop<T, impl TDrop: Drop<T>> of Drop<Array<T>>;

// Span.
struct Span<T> {
snapshot: @Array<T>
}

impl SpanCopy<T> of Copy::<Span::<T>>;
impl SpanDrop<T> of Drop::<Span::<T>>;
impl SpanCopy<T> of Copy<Span<T>>;
impl SpanDrop<T> of Drop<Span<T>>;

trait SpanTrait<T> {
fn pop_front(ref self: Span<T>) -> Option<@T>;
fn pop_back(ref self: Span<T>) -> Option<@T>;
fn get(self: Span<T>, index: usize) -> Option<Box<@T>>;
fn at(self: Span<T>, index: usize) -> @T;
fn slice(self: Span<T>, start: usize, length: usize) -> Span<T>;
fn len(self: Span<T>) -> usize;
fn is_empty(self: Span<T>) -> bool;
}
impl SpanImpl<T> of SpanTrait::<T> {
impl SpanImpl<T> of SpanTrait<T> {
#[inline(always)]
fn pop_front(ref self: Span<T>) -> Option<@T> {
let mut snapshot = self.snapshot;
Expand All @@ -91,6 +105,17 @@ impl SpanImpl<T> of SpanTrait::<T> {
}
}

#[inline(always)]
fn pop_back(ref self: Span<T>) -> Option<@T> {
let mut snapshot = self.snapshot;
let item = array_snapshot_pop_back(ref snapshot);
self = Span { snapshot };
match item {
Option::Some(x) => Option::Some(x.unbox()),
Option::None(_) => Option::None(()),
}
}

#[inline(always)]
fn get(self: Span<T>, index: usize) -> Option<Box<@T>> {
array_get(self.snapshot, index)
Expand All @@ -100,6 +125,10 @@ impl SpanImpl<T> of SpanTrait::<T> {
array_at(self.snapshot, index).unbox()
}
#[inline(always)]
fn slice(self: Span<T>, start: usize, length: usize) -> Span<T> {
Span { snapshot: array_slice(self.snapshot, start, length).expect('Index out of bounds') }
}
#[inline(always)]
fn len(self: Span<T>) -> usize {
array_len(self.snapshot)
}
Expand All @@ -109,31 +138,28 @@ impl SpanImpl<T> of SpanTrait::<T> {
}
}

impl ArrayTCloneImpl<T, impl TClone: Clone::<T>, impl TDrop: Drop::<T>> of Clone::<Array<T>> {
fn clone(self: @Array<T>) -> Array<T> {
let mut response = array_new();
clone_loop(self.span(), ref response);
response
impl SpanIndex<T> of IndexView<Span<T>, usize, @T> {
#[inline(always)]
fn index(self: @Span<T>, index: usize) -> @T {
array_at(*self.snapshot, index).unbox()
}
}

// TODO(spapini): Remove TDrop. It is necessary to get rid of response in case of panic.
fn clone_loop<T, impl TClone: Clone::<T>, impl TDrop: Drop::<T>>(
mut span: Span<T>, ref response: Array<T>
) {
match withdraw_gas() {
Option::Some(_) => {},
Option::None(_) => {
let mut data = array_new();
array_append(ref data, 'Out of gas');
panic(data);
},
}
match span.pop_front() {
Option::Some(v) => {
response.append(TClone::clone(v));
clone_loop(span, ref response);
},
Option::None(_) => {},
impl ArrayTCloneImpl<T, impl TClone: Clone<T>, impl TDrop: Drop<T>> of Clone<Array<T>> {
fn clone(self: @Array<T>) -> Array<T> {
let mut response = array_new();
let mut span = self.span();
loop {
match span.pop_front() {
Option::Some(v) => {
response.append(TClone::clone(v));
},
Option::None(_) => {
break ();
},
};
};
response
}
}
14 changes: 7 additions & 7 deletions corelib/src/box.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern type Box<T>;
impl BoxTCopy<T, impl TCopy: Copy::<T>> of Copy::<Box::<T>>;
impl BoxTDrop<T, impl TDrop: Drop::<T>> of Drop::<Box::<T>>;
impl BoxTCopy<T, impl TCopy: Copy<T>> of Copy<Box<T>>;
impl BoxTDrop<T, impl TDrop: Drop<T>> of Drop<Box<T>>;

// These functions are only exposed in the corelib through the trait below since calling them
// directly with tuples panics due to auto unpacking of the tuple.
Expand All @@ -9,17 +9,17 @@ extern fn into_box<T>(value: T) -> Box<T> nopanic;
extern fn unbox<T>(box: Box<T>) -> T nopanic;

trait BoxTrait<T> {
fn new(value: T) -> Box<T>;
fn unbox(self: Box<T>) -> T;
fn new(value: T) -> Box<T> nopanic;
fn unbox(self: Box<T>) -> T nopanic;
}

impl BoxImpl<T> of BoxTrait::<T> {
impl BoxImpl<T> of BoxTrait<T> {
#[inline(always)]
fn new(value: T) -> Box<T> {
fn new(value: T) -> Box<T> nopanic {
into_box(value)
}
#[inline(always)]
fn unbox(self: Box<T>) -> T {
fn unbox(self: Box<T>) -> T nopanic {
unbox(self)
}
}
2 changes: 1 addition & 1 deletion corelib/src/clone.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ trait Clone<T> {
fn clone(self: @T) -> T;
}

impl TCopyClone<T, impl TCopy: Copy::<T>> of Clone::<T> {
impl TCopyClone<T, impl TCopy: Copy<T>> of Clone<T> {
fn clone(self: @T) -> T {
*self
}
Expand Down
37 changes: 21 additions & 16 deletions corelib/src/debug.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use array::ArrayTrait;
use traits::Into;
use starknet::ContractAddressIntoFelt252;
use option::Option;

// Usage:
Expand Down Expand Up @@ -31,13 +30,13 @@ trait PrintTrait<T> {
fn print(self: T);
}

impl Felt252PrintImpl of PrintTrait::<felt252> {
impl Felt252PrintImpl of PrintTrait<felt252> {
fn print(self: felt252) {
print_felt252(self);
}
}

impl BoolPrintImpl of PrintTrait::<bool> {
impl BoolPrintImpl of PrintTrait<bool> {
fn print(self: bool) {
if self {
'true'.print();
Expand All @@ -47,45 +46,51 @@ impl BoolPrintImpl of PrintTrait::<bool> {
}
}

impl ContractAddressPrintImpl of PrintTrait::<starknet::ContractAddress> {
impl ContractAddressPrintImpl of PrintTrait<starknet::ContractAddress> {
fn print(self: starknet::ContractAddress) {
self.into().print();
Into::<_, felt252>::into(self).print();
}
}

impl U8PrintImpl of PrintTrait::<u8> {
impl U8PrintImpl of PrintTrait<u8> {
fn print(self: u8) {
self.into().print();
Into::<_, felt252>::into(self).print();
}
}

impl U32PrintImpl of PrintTrait::<u32> {
impl U16PrintImpl of PrintTrait<u16> {
fn print(self: u16) {
Into::<_, felt252>::into(self).print();
}
}

impl U32PrintImpl of PrintTrait<u32> {
fn print(self: u32) {
self.into().print();
Into::<_, felt252>::into(self).print();
}
}

impl U64PrintImpl of PrintTrait::<u64> {
impl U64PrintImpl of PrintTrait<u64> {
fn print(self: u64) {
self.into().print();
Into::<_, felt252>::into(self).print();
}
}

impl U128PrintImpl of PrintTrait::<u128> {
impl U128PrintImpl of PrintTrait<u128> {
fn print(self: u128) {
self.into().print();
Into::<_, felt252>::into(self).print();
}
}

impl U256PrintImpl of PrintTrait::<u256> {
impl U256PrintImpl of PrintTrait<u256> {
fn print(self: u256) {
self.low.into().print();
self.high.into().print();
}
}

impl ArrayGenericPrintImpl of PrintTrait::<Array::<felt252>> {
fn print(mut self: Array::<felt252>) {
impl ArrayGenericPrintImpl of PrintTrait<Array<felt252>> {
fn print(mut self: Array<felt252>) {
print(self);
}
}
Loading

0 comments on commit 6466b03

Please sign in to comment.