-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add sway language * Heuristics for .sw * Add XML regex and sample * Add space after abi in regex * Update samples * XML heuristic test * Update regex
- Loading branch information
Showing
14 changed files
with
653 additions
and
0 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
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,34 @@ | ||
script; | ||
|
||
struct Foo { | ||
f1: u32, | ||
f2: b256, | ||
} | ||
|
||
fn main() { | ||
// Array of integers with type ascription | ||
let array_of_integers: [u8; 5] = [1, 2, 3, 4, 5]; | ||
|
||
// Array of strings | ||
let array_of_strings = ["Bob", "Jan", "Ron"]; | ||
|
||
// Array of structs | ||
let array_of_structs: [Foo; 2] = [ | ||
Foo { | ||
f1: 11, | ||
f2: 0x1111111111111111111111111111111111111111111111111111111111111111, | ||
}, | ||
Foo { | ||
f1: 22, | ||
f2: 0x2222222222222222222222222222222222222222222222222222222222222222, | ||
}, | ||
]; | ||
|
||
// Accessing an element of an array | ||
let mut array_of_bools: [bool; 2] = [true, false]; | ||
assert(array_of_bools[0]); | ||
|
||
// Mutating the element of an array | ||
array_of_bools[1] = true; | ||
assert(array_of_bools[1]); | ||
} |
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,220 @@ | ||
script; | ||
|
||
dep my_double; | ||
dep my_point; | ||
dep my_triple; | ||
|
||
use my_point::MyPoint; | ||
use my_triple::MyTriple; | ||
|
||
trait Setter<T> { | ||
fn set(self, new_value: T) -> Self; | ||
} | ||
|
||
struct FooBarData<T> { | ||
value: T | ||
} | ||
|
||
impl<T> Setter<T> for FooBarData<T> { | ||
fn set(self, new_value: T) -> Self { | ||
FooBarData { | ||
value: new_value, | ||
} | ||
} | ||
} | ||
|
||
trait Returner<T> { | ||
fn return_it(self, the_value: T) -> T; | ||
} | ||
|
||
impl<T, F> Returner<T> for FooBarData<F> { | ||
fn return_it(self, the_value: T) -> T { | ||
the_value | ||
} | ||
} | ||
|
||
trait MyAdd<T> { | ||
fn my_add(self, a: T, b: T) -> T; | ||
} | ||
|
||
// impl<T> MyAdd<u8> for FooBarData<T> { | ||
// fn my_add(self, a: u8, b: u8) -> u8 { | ||
// a + b | ||
// } | ||
// } | ||
|
||
impl<T> MyAdd<u64> for FooBarData<T> { | ||
fn my_add(self, a: u64, b: u64) -> u64 { | ||
a + b | ||
} | ||
} | ||
|
||
trait MySub<T> { | ||
fn my_sub(a: T, b: T) -> T; | ||
} | ||
|
||
// impl<T> MySub<u8> for FooBarData<T> { | ||
// fn my_sub(a: u8, b: u8) -> u8 { | ||
// if a >= b { | ||
// a - b | ||
// } else { | ||
// b - a | ||
// } | ||
// } | ||
// } | ||
|
||
impl<T> MySub<u64> for FooBarData<T> { | ||
fn my_sub(a: u64, b: u64) -> u64 { | ||
if a >= b { | ||
a - b | ||
} else { | ||
b - a | ||
} | ||
} | ||
} | ||
|
||
struct OtherData<T> { | ||
a: T, | ||
b: T, | ||
} | ||
|
||
// impl<T> MyAdd<u8> for OtherData<T> { | ||
// fn my_add(self, a: u8, b: u8) -> u8 { | ||
// a + b | ||
// } | ||
// } | ||
|
||
impl<T> MyAdd<u64> for OtherData<T> { | ||
fn my_add(self, a: u64, b: u64) -> u64 { | ||
a + b | ||
} | ||
} | ||
|
||
// impl<T> MySub<u8> for OtherData<T> { | ||
// fn my_sub(a: u8, b: u8) -> u8 { | ||
// if a >= b { | ||
// a - b | ||
// } else { | ||
// b - a | ||
// } | ||
// } | ||
// } | ||
|
||
impl<T> MySub<u64> for OtherData<T> { | ||
fn my_sub(a: u64, b: u64) -> u64 { | ||
if a >= b { | ||
a - b | ||
} else { | ||
b - a | ||
} | ||
} | ||
} | ||
|
||
impl MyTriple<u64> for MyPoint<u64> { | ||
fn my_triple(self, value: u64) -> u64 { | ||
(self.x*3) + (self.y*3) + (value*3) | ||
} | ||
} | ||
|
||
struct MyU64 { | ||
inner: u64 | ||
} | ||
|
||
impl MyTriple<u64> for MyU64 { | ||
fn my_triple(self, value: u64) -> u64 { | ||
(self.inner*3) + (value*3) | ||
} | ||
} | ||
|
||
pub struct A { | ||
a: u64, | ||
} | ||
|
||
pub struct B { | ||
b: u64, | ||
} | ||
|
||
pub struct C { | ||
c: u64, | ||
} | ||
|
||
pub trait Convert<T> { | ||
fn convert(t: T) -> Self; | ||
} | ||
|
||
impl Convert<B> for A { | ||
fn convert(t: B) -> Self { | ||
A { | ||
a: t.b | ||
} | ||
} | ||
} | ||
|
||
impl Convert<C> for A { | ||
fn convert(t: C) -> Self { | ||
A { | ||
a: t.c | ||
} | ||
} | ||
} | ||
|
||
fn main() -> u64 { | ||
let a = FooBarData { | ||
value: 1u8 | ||
}; | ||
let b = a.set(42); | ||
let c = b.value; | ||
let d = b.return_it(true); | ||
let e = b.return_it(9u64); | ||
let f = FooBarData { | ||
value: 1u64 | ||
}; | ||
let g = f.my_add(a.my_add(1u8, 2u8), a.my_add(3u8, 4u8)); | ||
let h = FooBarData::<u64>::my_sub( | ||
FooBarData::<u8>::my_sub(100, 10), | ||
FooBarData::<u8>::my_sub(50, 10), | ||
); | ||
let i = OtherData { | ||
a: true, | ||
b: false, | ||
}; | ||
let j = OtherData { | ||
a: 10u32, | ||
b: 11u32, | ||
}; | ||
let k = j.my_add(i.my_add(1u8, 2u8), i.my_add(3u8, 4u8)); | ||
let l = FooBarData::<u16>::my_sub( | ||
FooBarData::<u32>::my_sub(100, 10), | ||
FooBarData::<u32>::my_sub(50, 10), | ||
); | ||
let m = MyPoint { | ||
x: 10u64, | ||
y: 10u64, | ||
}; | ||
let n = m.my_double(100); | ||
let o = m.my_triple(100); | ||
let p = MyU64 { | ||
inner: 30u64 | ||
}; | ||
let q = p.my_triple(1); | ||
|
||
let r_b = B { b: 42 }; | ||
let r_c = C { c: 42 }; | ||
|
||
if c == 42u8 | ||
&& d | ||
&& e == 9u64 | ||
&& g == 10 | ||
&& h == 50 | ||
&& k == 10 | ||
&& l == 50 | ||
&& n == 240 | ||
&& o == 360 | ||
&& q == 93 | ||
&& A::convert(r_b).a == 42 | ||
&& A::convert(r_c).a == 42 { | ||
42 | ||
} else { | ||
7 | ||
} | ||
} |
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,81 @@ | ||
library utils; | ||
|
||
dep data_structures; | ||
use data_structures::{Foo, Line, Point, TupleInStruct}; | ||
|
||
fn hardcoded_instantiation() -> Foo { | ||
// Instantiate `foo` as `Foo` | ||
let mut foo = Foo { | ||
bar: 42, | ||
baz: false, | ||
}; | ||
|
||
// Access and write to "baz" | ||
foo.baz = true; | ||
|
||
// Return the struct | ||
foo | ||
} | ||
|
||
fn variable_instantiation() -> Foo { | ||
// Declare variables with the same names as the fields in `Foo` | ||
let number = 42; | ||
let truthness = false; | ||
|
||
// Instantiate `foo` as `Foo` | ||
let mut foo = Foo { | ||
bar: number, | ||
baz: truthness, | ||
}; | ||
|
||
// Access and write to "baz" | ||
foo.baz = true; | ||
|
||
// Return the struct | ||
foo | ||
} | ||
|
||
fn shorthand_instantiation() -> Foo { | ||
// Declare variables with the same names as the fields in `Foo` | ||
let bar = 42; | ||
let baz = false; | ||
|
||
// Instantiate `foo` as `Foo` | ||
let mut foo = Foo { bar, baz }; | ||
|
||
// Access and write to "baz" | ||
foo.baz = true; | ||
|
||
// Return the struct | ||
foo | ||
} | ||
|
||
fn struct_destructuring() { | ||
let point1 = Point { x: 0, y: 0 }; | ||
// Destructure the values from the struct into variables | ||
let Point { x, y } = point1; | ||
|
||
let point2 = Point { x: 1, y: 1 }; | ||
// If you do not care about specific struct fields then use ".." at the end of your variable list | ||
let Point { x, .. } = point2; | ||
|
||
let line = Line { | ||
p1: point1, | ||
p2: point2, | ||
}; | ||
// Destructure the values from the nested structs into variables | ||
let Line { | ||
p1: Point { x: x0, y: y0 }, | ||
p2: Point { x: x1, y: y1 }, | ||
} = line; | ||
// You may also destructure tuples nested in structs and structs nested in tuples | ||
let tuple_in_struct = TupleInStruct { | ||
nested_tuple: (42u64, (42u32, (true, "ok"))), | ||
}; | ||
let TupleInStruct { | ||
nested_tuple: (a, (b, (c, d))), | ||
} = tuple_in_struct; | ||
|
||
let struct_in_tuple = (Point { x: 2, y: 4 }, Point { x: 3, y: 6 }); | ||
let (Point { x: x0, y: y0 }, Point { x: x1, y: y1 }) = struct_in_tuple; | ||
} |
Oops, something went wrong.