Skip to content

Commit

Permalink
Merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
acoglio committed May 6, 2021
2 parents e7f88a9 + ca6495d commit 4fb3d9c
Show file tree
Hide file tree
Showing 46 changed files with 171 additions and 110 deletions.
10 changes: 10 additions & 0 deletions asg/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ impl AsgConvertError {
)
}

pub fn invalid_const_assign(name: &str, span: &Span) -> Self {
Self::new_from_span(
format!(
"failed to create const variable(s) '{}' with non constant values.",
name
),
span,
)
}

pub fn duplicate_function_definition(name: &str, span: &Span) -> Self {
Self::new_from_span(
format!("a function named \"{}\" already exists in this scope", name),
Expand Down
11 changes: 11 additions & 0 deletions asg/src/statement/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {

let value = <&Expression<'a>>::from_ast(scope, &statement.value, type_.clone().map(Into::into))?;

if matches!(statement.declaration_type, leo_ast::Declare::Const) && !value.is_consty() {
let var_names = statement
.variable_names
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(" ,");

return Err(AsgConvertError::invalid_const_assign(&var_names, &statement.span));
}

let type_ = type_.or_else(|| value.get_type());

let mut output_types = vec![];
Expand Down
2 changes: 1 addition & 1 deletion asg/tests/pass/address/ternary.leo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function main(s: bool, c: address) {
const a = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;
const b = aleo18qgam03qe483tdrcc3fkqwpp38ehff4a2xma6lu7hams6lfpgcpq3dq05r;

const r = s? a: b;
let r = s ? a: b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/boolean/not_mutable.leo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function main () {
let b = false;
const b = false;
const a = !b;
}
6 changes: 3 additions & 3 deletions asg/tests/pass/circuits/pedersen_mock.leo
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
circuit PedersenHash {
parameters: [u32; 512]

function new(parameters: [u32; 512]) -> Self {
function new(const parameters: [u32; 512]) -> Self {
return Self { parameters: parameters };
}

function hash(self, bits: [bool; 512]) -> u32 {
function hash(self, const bits: [bool; 512]) -> u32 {
let digest: u32 = 0;
for i in 0..512 {
const base = bits[i] ? self.parameters[i] : 0u32;
let base = bits[i] ? self.parameters[i] : 0u32;
digest += base;
}
return digest;
Expand Down
2 changes: 1 addition & 1 deletion asg/tests/pass/core/blake2s_random.leo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import core.unstable.blake2s.Blake2s;

function main(seed: [u8; 32], message: [u8; 32], expected: [u8; 32]) {
const actual = Blake2s::hash(seed, message);
let actual = Blake2s::hash(seed, message);

console.assert(expected == actual);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/field/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: field, b: field, c: field) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/group/point_input.leo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function main(a: group) {
const b = a;
let b = a;
}
2 changes: 1 addition & 1 deletion asg/tests/pass/group/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: group, b: group, c: group) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/i128/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i128, b: i128, c: i128) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/i16/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i16, b: i16, c: i16) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/i32/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i32, b: i32, c: i32) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/i64/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i64, b: i64, c: i64) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/i8/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i8, b: i8, c: i8) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/u128/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u128, b: u128, c: u128) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/u16/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u16, b: u16, c: u16) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/u32/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u32, b: u32, c: u32) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/u64/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u64, b: u64, c: u64) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/integers/u8/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u8, b: u8, c: u8) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/mutability/let_mut_nested.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main () {
let x = 2u8;
let y = x;
const z = y / 2u8;
let z = y / 2u8;
}
4 changes: 2 additions & 2 deletions asg/tests/pass/mutability/swap.leo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Swap two elements of an array.
function swap(a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
const t = a[i];
let t = a[i];
a[i] = a[j];
a[j] = t;
return a;
Expand All @@ -11,7 +11,7 @@ function main() {
const expected: [u32; 2] = [1, 0];

// Do swap.
const actual = swap(arr, 0, 1);
let actual = swap(arr, 0, 1);

// Check result.
for i in 0..2 {
Expand Down
2 changes: 1 addition & 1 deletion asg/tests/pass/statements/conditional/for_loop.leo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function main(a: bool) {
}
}

const r: u32 = a ? 6 : 0;
let r: u32 = a ? 6 : 0;

console.assert(r == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/statements/ternary_basic.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(a: bool, b: bool) {
const c = a ? true : false;
let c = a ? true : false;

const d = c == b;
let d = c == b;
}
2 changes: 1 addition & 1 deletion compiler/tests/address/ternary.leo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function main(s: bool, c: address) {
const a = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;
const b = aleo18qgam03qe483tdrcc3fkqwpp38ehff4a2xma6lu7hams6lfpgcpq3dq05r;

const r = s? a: b;
let r = s ? a: b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/boolean/not_mutable.leo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function main () {
let b = false;
const b = false;
const a = !b;
}
6 changes: 3 additions & 3 deletions compiler/tests/circuits/pedersen_mock.leo
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
circuit PedersenHash {
parameters: [u32; 512]

function new(parameters: [u32; 512]) -> Self {
function new(const parameters: [u32; 512]) -> Self {
return Self { parameters: parameters };
}

function hash(self, bits: [bool; 512]) -> u32 {
function hash(self, const bits: [bool; 512]) -> u32 {
let digest: u32 = 0;
for i in 0..512 {
const base = bits[i] ? self.parameters[i] : 0u32;
let base = bits[i] ? self.parameters[i] : 0u32;
digest += base;
}
return digest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import core.unstable.blake2s.Blake2s;

function main(seed: [u8; 32], message: [u8; 32], expected: [u8; 32]) {
const actual = Blake2s::hash(seed, message);
let actual = Blake2s::hash(seed, message);

console.assert(expected == actual);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/i128/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i128, b: i128, c: i128) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/i16/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i16, b: i16, c: i16) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/i32/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i32, b: i32, c: i32) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/i64/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i64, b: i64, c: i64) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/i8/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: i8, b: i8, c: i8) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/u128/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u128, b: u128, c: u128) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/u16/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u16, b: u16, c: u16) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/u32/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u32, b: u32, c: u32) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/u64/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u64, b: u64, c: u64) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/integers/u8/ternary.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main(s: bool, a: u8, b: u8, c: u8) {
const r = s ? a : b;
let r = s ? a : b;

console.assert(r == c);
}
2 changes: 1 addition & 1 deletion compiler/tests/mutability/let_mut_nested.leo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function main () {
let x = 2u8;
let y = x;
const z = y / 2u8;
let z = y / 2u8;
}
4 changes: 2 additions & 2 deletions compiler/tests/mutability/swap.leo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Swap two elements of an array.
function swap(a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
const t = a[i];
let t = a[i];
a[i] = a[j];
a[j] = t;
return a;
Expand All @@ -11,7 +11,7 @@ function main() {
const expected: [u32; 2] = [1, 0];

// Do swap.
const actual = swap(arr, 0, 1);
let actual = swap(arr, 0, 1);

// Check result.
for i in 0..2 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests/statements/conditional/for_loop.leo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function main(a: bool) {
}
}

const r: u32 = a ? 6 : 0;
let r: u32 = a ? 6 : 0;

console.assert(r == b);
}
8 changes: 8 additions & 0 deletions compiler/tests/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ fn test_ternary_basic() {
assert_satisfied(program);
}

#[test]
fn test_ternary_non_const_conditional_fail() {
let program_string = include_str!("ternary_non_const_conditional_fail.leo");
let error = parse_program(program_string).err().unwrap();

expect_asg_error(error);
}

// Iteration for i {start}..{stop} { statements }

#[test]
Expand Down
Loading

0 comments on commit 4fb3d9c

Please sign in to comment.