mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-24 18:52:58 +03:00
tuple const fixes, compiler tests
This commit is contained in:
parent
f7314625ff
commit
e970194ed3
@ -52,12 +52,17 @@ impl GlobalConsts {
|
||||
}
|
||||
}
|
||||
|
||||
let values = match self.value.clone() {
|
||||
Expression::TupleInit(expr) => expr.elements,
|
||||
_ => vec![self.value.clone()],
|
||||
};
|
||||
|
||||
for (i, variable_name) in self.variable_names.iter().enumerate() {
|
||||
global_consts.push(GlobalConst {
|
||||
declaration_type: self.declaration_type.clone(),
|
||||
variable_name: variable_name.clone(),
|
||||
type_: types.get(i).unwrap_or(&None).clone(),
|
||||
value: self.value.clone(),
|
||||
value: values.get(i).unwrap_or(&self.value.clone()).clone(),
|
||||
span: self.span.clone(),
|
||||
});
|
||||
}
|
||||
|
46
compiler/tests/global_consts/global_const_types.leo
Normal file
46
compiler/tests/global_consts/global_const_types.leo
Normal file
@ -0,0 +1,46 @@
|
||||
const basic: u32 = 8;
|
||||
const array: [u8; (3, 2)] = [[0u8; 2]; 3];
|
||||
const tuple = (1u32, 2u32);
|
||||
const (a, b) = (1u32, 2u32);
|
||||
const simple_group: group = 1group;
|
||||
const complex_group = (_, 1)group;
|
||||
const field_test: field = 2;
|
||||
const use_another_const = basic + 1;
|
||||
const foo = Foo { width: 10, height: 20 };
|
||||
const uno = uno();
|
||||
|
||||
circuit Foo {
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
function uno() -> u32 {
|
||||
return 1u32
|
||||
}
|
||||
|
||||
function main() {
|
||||
// basic test
|
||||
console.assert(basic == 8u32);
|
||||
// array test
|
||||
console.assert(array[0][0] == 0);
|
||||
console.assert(array[2][1] == 0);
|
||||
// tuple test
|
||||
let (x, y) = (1u32, 2u32);
|
||||
console.assert(x == 1u32);
|
||||
console.assert(y == 2u32);
|
||||
// tuple extraction test
|
||||
console.assert(a == 1u32);
|
||||
console.assert(b == 2u32);
|
||||
// group test
|
||||
console.assert(simple_group == 1group);
|
||||
console.assert(complex_group == (_, 1)group);
|
||||
// field test
|
||||
console.assert(field_test == 2field);
|
||||
// use another const test
|
||||
console.assert(use_another_const == 9u32);
|
||||
// circuit test
|
||||
console.assert(foo.width == 10u32);
|
||||
console.assert(foo.height == 20u32);
|
||||
// function test
|
||||
console.assert(uno == 1u32);
|
||||
}
|
36
compiler/tests/global_consts/mod.rs
Normal file
36
compiler/tests/global_consts/mod.rs
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{assert_satisfied, expect_compiler_error, parse_program};
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_global_consts() {
|
||||
let program_string = include_str!("global_consts.leo");
|
||||
|
||||
let program = parse_program(program_string).unwrap();
|
||||
|
||||
assert_satisfied(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_modify_global_const() {
|
||||
let program_string = include_str!("modify_global_const.leo");
|
||||
|
||||
let program = parse_program(program_string).unwrap();
|
||||
|
||||
assert!(parse_program(program_string).is_err());
|
||||
}
|
6
compiler/tests/global_consts/modify_global_const.leo
Normal file
6
compiler/tests/global_consts/modify_global_const.leo
Normal file
@ -0,0 +1,6 @@
|
||||
const basic: u32 = 8;
|
||||
|
||||
function main() {
|
||||
// Cannot re-assign!
|
||||
basic = 2u32;
|
||||
}
|
Loading…
Reference in New Issue
Block a user