-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add multiple slice builtin funcs * initial new stdlib Vec method that uses slices * a couple more methods and starting to test * remove Vec keyword from lexer and finish Vec test * enable array index panic in acir gen when enable_side_effects_var is None * Update crates/nargo_cli/tests/test_data_ssa_refactor/vectors/src/main.nr * Update crates/nargo_cli/tests/test_data_ssa_refactor/slices/src/main.nr * when slices are not enabled do not include collections module in stdlib * fix in collect_defs * remove append builtin for now * Update noir_stdlib/src/collections/vec.nr Co-authored-by: jfecher <[email protected]> * Update noir_stdlib/src/collections/vec.nr Co-authored-by: jfecher <[email protected]> * revert check on option and merge in pr #1906 * use from_slice in test * switch retain for collections and slice modules to && --------- Co-authored-by: jfecher <[email protected]>
- Loading branch information
Showing
9 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/vectors/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.7.1" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/test_data_ssa_refactor/vectors/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "5" | ||
y = "10" |
32 changes: 32 additions & 0 deletions
32
crates/nargo_cli/tests/test_data_ssa_refactor/vectors/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use dep::std::collections::vec::Vec; | ||
|
||
fn main(x: Field, y: pub Field) { | ||
let mut vector = Vec::new(); | ||
|
||
assert(vector.len() == 0); | ||
for i in 0..5 { | ||
vector.push(i); | ||
} | ||
assert(vector.len() == 5); | ||
for i in 0..5 { | ||
assert(i == vector.get(i)); | ||
} | ||
|
||
let last_elem = vector.pop(); | ||
assert(last_elem == 4); | ||
assert(vector.len() == 4); | ||
|
||
vector.insert(2, 100); | ||
assert(vector.get(2) == 100); | ||
assert(vector.get(4) == 3); | ||
assert(vector.len() == 5); | ||
|
||
let removed_elem = vector.remove(3); | ||
assert(removed_elem == 2); | ||
assert(vector.get(3) == 3); | ||
assert(vector.len() == 4); | ||
|
||
let mut inputs_vector = Vec::from_slice([x, y]); | ||
assert(inputs_vector.get(0) == x); | ||
assert(inputs_vector.get(1) == y); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
struct Vec<T> { | ||
slice: [T] | ||
} | ||
|
||
// A mutable vector type implemented as a wrapper around immutable slices. | ||
// A separate type is technically not needed but helps differentiate which operations are mutable. | ||
impl<T> Vec<T> { | ||
fn new() -> Self { | ||
Self { slice: [] } | ||
} | ||
|
||
// Create a Vec containing each element from the given slice. | ||
// Mutations to the resulting Vec will not affect the original slice. | ||
fn from_slice(slice: [T]) -> Self { | ||
Self { slice } | ||
} | ||
|
||
/// Get an element from the vector at the given index. | ||
/// Panics if the given index | ||
/// points beyond the end of the vector. | ||
fn get(&mut self, index: Field) -> T { | ||
self.slice[index] | ||
} | ||
|
||
/// Push a new element to the end of the vector, returning a | ||
/// new vector with a length one greater than the | ||
/// original unmodified vector. | ||
fn push(&mut self, elem: T) { | ||
self.slice = self.slice.push_back(elem); | ||
} | ||
|
||
/// Pop an element from the end of the given vector, returning | ||
/// a new vector with a length of one less than the given vector, | ||
/// as well as the popped element. | ||
/// Panics if the given vector's length is zero. | ||
fn pop(&mut self) -> T { | ||
let (popped_slice, last_elem) = self.slice.pop_back(); | ||
self.slice = popped_slice; | ||
last_elem | ||
} | ||
|
||
/// Insert an element at a specified index, shifting all elements | ||
/// after it to the right | ||
fn insert(&mut self, index: Field, elem: T) { | ||
self.slice = self.slice.insert(index, elem); | ||
} | ||
|
||
/// Remove an element at a specified index, shifting all elements | ||
/// after it to the left, returning the removed element | ||
fn remove(&mut self, index: Field) -> T { | ||
let (new_slice, elem) = self.slice.remove(index); | ||
self.slice = new_slice; | ||
elem | ||
} | ||
|
||
/// Returns the number of elements in the vector | ||
fn len(self: Self) -> Field { | ||
self.slice.len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ mod sha512; | |
mod field; | ||
mod ec; | ||
mod unsafe; | ||
mod collections; | ||
mod compat; | ||
|
||
#[builtin(println)] | ||
|