From 1e2b1dcd93fcb01cf4fdaec0cc11ee0ed207785e Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 21 Jan 2021 15:39:39 -0500 Subject: [PATCH 1/2] use expect_asg_error in appropriate tests --- asg/src/checks/mod.rs | 15 ++ asg/src/checks/return_path.rs | 59 +++++- asg/src/const_value.rs | 61 ++++-- asg/src/error/mod.rs | 113 ++++++++-- asg/src/expression/array_access.rs | 65 +++++- asg/src/expression/array_init.rs | 92 ++++++-- asg/src/expression/array_inline.rs | 150 +++++++++---- asg/src/expression/array_range_access.rs | 82 ++++++-- asg/src/expression/binary.rs | 198 ++++++++++++------ asg/src/expression/call.rs | 152 +++++++++++--- asg/src/expression/circuit_access.rs | 142 ++++++++++--- asg/src/expression/circuit_init.rs | 88 ++++++-- asg/src/expression/conditional.rs | 33 ++- asg/src/expression/constant.rs | 192 +++++++++++------ asg/src/expression/mod.rs | 83 ++++++-- asg/src/expression/tuple_access.rs | 50 ++++- asg/src/expression/tuple_init.rs | 62 +++++- asg/src/expression/unary.rs | 59 ++++-- asg/src/expression/variable_ref.rs | 63 +++++- asg/src/import.rs | 32 ++- asg/src/input.rs | 77 +++++-- asg/src/lib.rs | 27 ++- asg/src/node.rs | 19 +- asg/src/prelude.rs | 28 ++- asg/src/program/circuit.rs | 84 +++++--- asg/src/program/function.rs | 132 ++++++++---- asg/src/program/mod.rs | 195 +++++++++++------ asg/src/reducer/mod.rs | 15 ++ asg/src/reducer/monoid/bool_and.rs | 20 +- asg/src/reducer/monoid/mod.rs | 17 +- asg/src/reducer/monoid/set_append.rs | 20 +- asg/src/reducer/monoid/vec_append.rs | 20 +- asg/src/reducer/monoidal_director.rs | 74 +++++-- asg/src/reducer/monoidal_reducer.rs | 64 ++++-- asg/src/scope.rs | 57 ++++- asg/src/statement/assign.rs | 170 +++++++++++---- asg/src/statement/block.rs | 35 +++- asg/src/statement/conditional.rs | 45 +++- asg/src/statement/console.rs | 69 ++++-- asg/src/statement/definition.rs | 90 ++++++-- asg/src/statement/expression.rs | 31 ++- asg/src/statement/iteration.rs | 61 +++++- asg/src/statement/mod.rs | 66 +++--- asg/src/statement/return_.rs | 35 +++- asg/src/type_.rs | 50 +++-- asg/src/variable.rs | 24 ++- asg/tests/fail/address/mod.rs | 18 +- asg/tests/fail/array/mod.rs | 18 +- asg/tests/fail/boolean/mod.rs | 18 +- asg/tests/fail/circuits/mod.rs | 16 ++ asg/tests/fail/console/mod.rs | 18 +- asg/tests/fail/core/mod.rs | 16 ++ asg/tests/fail/definition/mod.rs | 16 ++ asg/tests/fail/field/mod.rs | 16 ++ asg/tests/fail/function/mod.rs | 18 +- asg/tests/fail/group/mod.rs | 16 ++ asg/tests/fail/import/mod.rs | 16 ++ asg/tests/fail/integers/i128/mod.rs | 20 +- asg/tests/fail/integers/i16/mod.rs | 20 +- asg/tests/fail/integers/i32/mod.rs | 20 +- asg/tests/fail/integers/i64/mod.rs | 20 +- asg/tests/fail/integers/i8/mod.rs | 20 +- asg/tests/fail/integers/int_macro.rs | 16 ++ asg/tests/fail/integers/integer_tester.rs | 17 +- asg/tests/fail/integers/mod.rs | 16 ++ asg/tests/fail/integers/u128/mod.rs | 20 +- asg/tests/fail/integers/u16/mod.rs | 20 +- asg/tests/fail/integers/u32/mod.rs | 20 +- asg/tests/fail/integers/u64/mod.rs | 20 +- asg/tests/fail/integers/u8/mod.rs | 20 +- asg/tests/fail/integers/uint_macro.rs | 16 ++ asg/tests/fail/mod.rs | 16 ++ asg/tests/fail/mutability/mod.rs | 18 +- asg/tests/fail/statements/mod.rs | 18 +- asg/tests/fail/tuples/mod.rs | 16 ++ asg/tests/mod.rs | 24 ++- asg/tests/pass/address/mod.rs | 18 +- asg/tests/pass/array/mod.rs | 18 +- asg/tests/pass/boolean/mod.rs | 18 +- asg/tests/pass/circuits/mod.rs | 16 ++ asg/tests/pass/console/mod.rs | 18 +- asg/tests/pass/core/mod.rs | 16 ++ asg/tests/pass/definition/mod.rs | 18 +- asg/tests/pass/field/mod.rs | 18 +- asg/tests/pass/form_ast.rs | 34 ++- asg/tests/pass/function/mod.rs | 18 +- asg/tests/pass/group/mod.rs | 16 ++ asg/tests/pass/import/mod.rs | 111 ++++++++-- asg/tests/pass/input_files/mod.rs | 16 ++ .../pass/input_files/program_input/mod.rs | 18 +- .../program_input_and_program_state/mod.rs | 18 +- .../pass/input_files/program_state/mod.rs | 20 +- asg/tests/pass/integers/i128/mod.rs | 20 +- asg/tests/pass/integers/i16/mod.rs | 20 +- asg/tests/pass/integers/i32/mod.rs | 20 +- asg/tests/pass/integers/i64/mod.rs | 20 +- asg/tests/pass/integers/i8/mod.rs | 20 +- asg/tests/pass/integers/int_macro.rs | 16 ++ asg/tests/pass/integers/integer_tester.rs | 16 ++ asg/tests/pass/integers/mod.rs | 16 ++ asg/tests/pass/integers/u128/mod.rs | 20 +- asg/tests/pass/integers/u16/mod.rs | 20 +- asg/tests/pass/integers/u32/mod.rs | 20 +- asg/tests/pass/integers/u64/mod.rs | 20 +- asg/tests/pass/integers/u8/mod.rs | 20 +- asg/tests/pass/integers/uint_macro.rs | 16 ++ asg/tests/pass/mod.rs | 18 +- asg/tests/pass/mutability/mod.rs | 18 +- asg/tests/pass/statements/conditional/mod.rs | 18 +- asg/tests/pass/statements/mod.rs | 18 +- asg/tests/pass/tuples/mod.rs | 18 +- compiler/src/compiler.rs | 22 +- compiler/src/console/console.rs | 9 +- compiler/src/constraints/constraints.rs | 10 +- compiler/src/definition/definition.rs | 13 +- compiler/src/errors/statement.rs | 2 +- compiler/src/expression/array/array.rs | 17 +- compiler/src/expression/binary/binary.rs | 6 +- compiler/src/expression/binary/operand.rs | 2 +- compiler/src/expression/circuit/access.rs | 21 +- compiler/src/expression/circuit/circuit.rs | 27 ++- .../src/expression/conditional/conditional.rs | 9 +- compiler/src/expression/expression.rs | 87 +++----- .../src/expression/function/core_circuit.rs | 2 +- compiler/src/expression/function/function.rs | 39 ++-- compiler/src/expression/tuple/access.rs | 3 +- compiler/src/expression/tuple/tuple.rs | 1 - .../expression/variable_ref/variable_ref.rs | 9 +- compiler/src/function/function.rs | 24 +-- compiler/src/function/input/array.rs | 11 +- compiler/src/function/input/function_input.rs | 2 +- compiler/src/function/input/input_keyword.rs | 12 +- compiler/src/function/input/input_section.rs | 10 +- .../src/function/input/main_function_input.rs | 2 +- compiler/src/function/input/tuple.rs | 2 +- compiler/src/function/main_function.rs | 37 +++- compiler/src/function/result/result.rs | 6 +- compiler/src/program/program.rs | 4 +- compiler/src/stage.rs | 2 +- compiler/src/statement/assign/assign.rs | 22 +- compiler/src/statement/assign/assignee.rs | 9 +- compiler/src/statement/block/block.rs | 11 +- .../src/statement/conditional/conditional.rs | 33 +-- .../src/statement/definition/definition.rs | 20 +- compiler/src/statement/iteration/iteration.rs | 2 +- compiler/src/statement/return_/return_.rs | 9 +- compiler/src/statement/statement.rs | 38 ++-- compiler/src/value/group/input.rs | 13 +- .../src/value/group/targets/edwards_bls12.rs | 9 +- compiler/src/value/integer/integer.rs | 7 +- compiler/src/value/value.rs | 6 +- compiler/tests/address/mod.rs | 6 +- compiler/tests/array/mod.rs | 57 ++--- .../array/type_tuple_value_nested_3x2.leo | 2 +- .../array/type_tuple_value_tuple_3x2.leo | 2 +- compiler/tests/boolean/mod.rs | 6 +- compiler/tests/circuits/member_function.leo | 10 +- compiler/tests/circuits/mod.rs | 14 +- compiler/tests/console/mod.rs | 31 +-- compiler/tests/function/mod.rs | 18 +- compiler/tests/integers/i128/mod.rs | 2 +- compiler/tests/integers/i16/mod.rs | 2 +- compiler/tests/integers/i32/mod.rs | 2 +- compiler/tests/integers/i64/mod.rs | 2 +- compiler/tests/integers/i8/mod.rs | 2 +- compiler/tests/integers/u128/mod.rs | 4 +- compiler/tests/integers/u16/mod.rs | 4 +- compiler/tests/integers/u32/mod.rs | 4 +- compiler/tests/integers/u64/mod.rs | 4 +- compiler/tests/integers/u8/mod.rs | 4 +- compiler/tests/syntax/mod.rs | 26 +-- imports/src/errors/import_parser.rs | 2 +- imports/src/parser/import_parser.rs | 13 +- imports/src/parser/mod.rs | 1 - imports/src/parser/parse_package.rs | 32 ++- imports/src/parser/parse_symbol.rs | 10 +- 176 files changed, 4083 insertions(+), 1351 deletions(-) diff --git a/asg/src/checks/mod.rs b/asg/src/checks/mod.rs index 0c345aefd2..98f973c3e5 100644 --- a/asg/src/checks/mod.rs +++ b/asg/src/checks/mod.rs @@ -1,3 +1,18 @@ +// Copyright (C) 2019-2020 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 . mod return_path; pub use return_path::*; diff --git a/asg/src/checks/return_path.rs b/asg/src/checks/return_path.rs index 23ef3be975..15dcacc3bf 100644 --- a/asg/src/checks/return_path.rs +++ b/asg/src/checks/return_path.rs @@ -1,4 +1,29 @@ -use crate::{ BoolAnd, MonoidalReducerExpression, MonoidalReducerStatement, Monoid, Expression, statement::*, Span, Node }; +// Copyright (C) 2019-2020 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 . + +use crate::{ + statement::*, + BoolAnd, + Expression, + Monoid, + MonoidalReducerExpression, + MonoidalReducerStatement, + Node, + Span, +}; use std::sync::Arc; pub struct ReturnPathReducer { @@ -11,9 +36,7 @@ impl ReturnPathReducer { } pub fn new() -> ReturnPathReducer { - ReturnPathReducer { - errors: vec![], - } + ReturnPathReducer { errors: vec![] } } } @@ -38,16 +61,28 @@ impl MonoidalReducerStatement for ReturnPathReducer { if statements.len() == 0 { BoolAnd(false) } else if let Some(index) = statements[..statements.len() - 1].iter().map(|x| x.0).position(|x| x) { - self.record_error(input.statements[index].span(), "dead code due to unconditional early return".to_string()); + self.record_error( + input.statements[index].span(), + "dead code due to unconditional early return".to_string(), + ); BoolAnd(true) } else { BoolAnd(statements[statements.len() - 1].0) } } - fn reduce_conditional_statement(&mut self, input: &ConditionalStatement, condition: BoolAnd, if_true: BoolAnd, if_false: Option) -> BoolAnd { + fn reduce_conditional_statement( + &mut self, + input: &ConditionalStatement, + condition: BoolAnd, + if_true: BoolAnd, + if_false: Option, + ) -> BoolAnd { if if_false.as_ref().map(|x| x.0).unwrap_or(false) != if_true.0 { - self.record_error(input.span(), "cannot have asymmetrical return in if statement".to_string()); + self.record_error( + input.span(), + "cannot have asymmetrical return in if statement".to_string(), + ); } if_true.append(if_false.unwrap_or_else(|| BoolAnd(false))) } @@ -68,7 +103,13 @@ impl MonoidalReducerStatement for ReturnPathReducer { BoolAnd(false) } - fn reduce_iteration(&mut self, input: &IterationStatement, start: BoolAnd, stop: BoolAnd, body: BoolAnd) -> BoolAnd { + fn reduce_iteration( + &mut self, + input: &IterationStatement, + start: BoolAnd, + stop: BoolAnd, + body: BoolAnd, + ) -> BoolAnd { // loops are const defined ranges, so we could probably check if they run one and emit here BoolAnd(false) } @@ -76,4 +117,4 @@ impl MonoidalReducerStatement for ReturnPathReducer { fn reduce_return(&mut self, input: &ReturnStatement, value: BoolAnd) -> BoolAnd { BoolAnd(true) } -} \ No newline at end of file +} diff --git a/asg/src/const_value.rs b/asg/src/const_value.rs index 53178ab8c8..010f3930a4 100644 --- a/asg/src/const_value.rs +++ b/asg/src/const_value.rs @@ -1,7 +1,22 @@ -use crate::{ Type, IntegerType, AsgConvertError, Span }; -use std::convert::TryInto; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, IntegerType, Span, Type}; use num_bigint::BigInt; -use std::fmt; +use std::{convert::TryInto, fmt}; #[derive(Clone, Debug, PartialEq)] pub enum ConstInt { @@ -137,7 +152,6 @@ macro_rules! const_int_map { }; } - macro_rules! const_int_bimap { ($name: ident, $x: ident, $y: ident, $transform: expr) => { pub fn $name(&self, other: &ConstInt) -> Option { @@ -159,6 +173,29 @@ macro_rules! const_int_bimap { } impl ConstInt { + const_int_op!(raw_value, String, x, format!("{}", x)); + + const_int_map!(value_negate, x, x.checked_neg()?); + + const_int_bimap!(value_add, x, y, x.checked_add(*y)?); + + const_int_bimap!(value_sub, x, y, x.checked_sub(*y)?); + + const_int_bimap!(value_mul, x, y, x.checked_mul(*y)?); + + const_int_bimap!(value_div, x, y, x.checked_div(*y)?); + + const_int_bimap!(value_pow, x, y, x.checked_pow((*y).try_into().ok()?)?); + + // TODO: limited to 32 bit exponents + const_int_biop!(value_lt, bool, x, y, Some(x < y)); + + const_int_biop!(value_le, bool, x, y, Some(x <= y)); + + const_int_biop!(value_gt, bool, x, y, Some(x > y)); + + const_int_biop!(value_ge, bool, x, y, Some(x >= y)); + pub fn get_int_type(&self) -> IntegerType { match self { ConstInt::I8(_) => IntegerType::I8, @@ -178,18 +215,6 @@ impl ConstInt { Type::Integer(self.get_int_type()) } - const_int_op!(raw_value, String, x, format!("{}", x)); - const_int_map!(value_negate, x, x.checked_neg()?); - const_int_bimap!(value_add, x, y, x.checked_add(*y)?); - const_int_bimap!(value_sub, x, y, x.checked_sub(*y)?); - const_int_bimap!(value_mul, x, y, x.checked_mul(*y)?); - const_int_bimap!(value_div, x, y, x.checked_div(*y)?); - const_int_bimap!(value_pow, x, y, x.checked_pow((*y).try_into().ok()?)?); // TODO: limited to 32 bit exponents - const_int_biop!(value_lt, bool, x, y, Some(x < y)); - const_int_biop!(value_le, bool, x, y, Some(x <= y)); - const_int_biop!(value_gt, bool, x, y, Some(x > y)); - const_int_biop!(value_ge, bool, x, y, Some(x >= y)); - pub fn parse(int_type: &IntegerType, value: &str, span: &Span) -> Result { Ok(match int_type { IntegerType::I8 => ConstInt::I8(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?), @@ -214,7 +239,9 @@ impl ConstValue { ConstValue::Field(_) => Type::Field, ConstValue::Address(_) => Type::Address, ConstValue::Boolean(_) => Type::Boolean, - ConstValue::Tuple(sub_consts) => Type::Tuple(sub_consts.iter().map(|x| x.get_type()).collect::>>()?), + ConstValue::Tuple(sub_consts) => { + Type::Tuple(sub_consts.iter().map(|x| x.get_type()).collect::>>()?) + } ConstValue::Array(values) => Type::Array(Box::new(values.get(0)?.get_type()?), values.len()), }) } diff --git a/asg/src/error/mod.rs b/asg/src/error/mod.rs index 7870f0a1d5..df36ad13a3 100644 --- a/asg/src/error/mod.rs +++ b/asg/src/error/mod.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + use crate::Span; use leo_ast::Error as FormattedError; use leo_grammar::ParserError; @@ -28,39 +44,84 @@ impl AsgConvertError { } pub fn unresolved_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("illegal reference to non-existant member '{}' of circuit '{}'", name, circuit_name), span) + Self::new_from_span( + format!( + "illegal reference to non-existant member '{}' of circuit '{}'", + name, circuit_name + ), + span, + ) } pub fn missing_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("missing circuit member '{}' for initialization of circuit '{}'", name, circuit_name), span) + Self::new_from_span( + format!( + "missing circuit member '{}' for initialization of circuit '{}'", + name, circuit_name + ), + span, + ) } pub fn extra_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("extra circuit member '{}' for initialization of circuit '{}' is not allowed", name, circuit_name), span) + Self::new_from_span( + format!( + "extra circuit member '{}' for initialization of circuit '{}' is not allowed", + name, circuit_name + ), + span, + ) } - pub fn illegal_function_assign( name: &str, span: &Span) -> Self { + pub fn illegal_function_assign(name: &str, span: &Span) -> Self { Self::new_from_span(format!("attempt to assign to function '{}'", name), span) } pub fn circuit_variable_call(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("cannot call variable member '{}' of circuit '{}'", name, circuit_name), span) + Self::new_from_span( + format!("cannot call variable member '{}' of circuit '{}'", name, circuit_name), + span, + ) } pub fn circuit_static_call_invalid(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("cannot call static function '{}' of circuit '{}' from target", name, circuit_name), span) + Self::new_from_span( + format!( + "cannot call static function '{}' of circuit '{}' from target", + name, circuit_name + ), + span, + ) } pub fn circuit_member_mut_call_invalid(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("cannot call mutable member function '{}' of circuit '{}' from immutable context", name, circuit_name), span) + Self::new_from_span( + format!( + "cannot call mutable member function '{}' of circuit '{}' from immutable context", + name, circuit_name + ), + span, + ) } pub fn circuit_member_call_invalid(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("cannot call member function '{}' of circuit '{}' from static context", name, circuit_name), span) + Self::new_from_span( + format!( + "cannot call member function '{}' of circuit '{}' from static context", + name, circuit_name + ), + span, + ) } pub fn circuit_function_ref(circuit_name: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("cannot reference function member '{}' of circuit '{}' as value", name, circuit_name), span) + Self::new_from_span( + format!( + "cannot reference function member '{}' of circuit '{}' as value", + name, circuit_name + ), + span, + ) } pub fn index_into_non_array(name: &str, span: &Span) -> Self { @@ -76,7 +137,10 @@ impl AsgConvertError { } pub fn unexpected_call_argument_count(expected: usize, got: usize, span: &Span) -> Self { - Self::new_from_span(format!("function call expected {} arguments, got {}", expected, got), span) + Self::new_from_span( + format!("function call expected {} arguments, got {}", expected, got), + span, + ) } pub fn unresolved_function(name: &str, span: &Span) -> Self { @@ -84,12 +148,22 @@ impl AsgConvertError { } pub fn unresolved_type(name: &str, span: &Span) -> Self { - Self::new_from_span(format!("failed to resolve type for variable definition '{}'", name), span) + Self::new_from_span( + format!("failed to resolve type for variable definition '{}'", name), + span, + ) } pub fn unexpected_type(expected: &str, received: Option<&str>, span: &Span) -> Self { // panic!(format!("unexpected type, expected: '{}', received: '{}'", expected, received.unwrap_or("unknown"))); - Self::new_from_span(format!("unexpected type, expected: '{}', received: '{}'", expected, received.unwrap_or("unknown")), span) + Self::new_from_span( + format!( + "unexpected type, expected: '{}', received: '{}'", + expected, + received.unwrap_or("unknown") + ), + span, + ) } pub fn unresolved_reference(name: &str, span: &Span) -> Self { @@ -113,15 +187,24 @@ impl AsgConvertError { } pub fn function_return_validation(name: &str, description: &str, span: &Span) -> Self { - Self::new_from_span(format!("function '{}' failed to validate return path: '{}'", name, description), span) + Self::new_from_span( + format!("function '{}' failed to validate return path: '{}'", name, description), + span, + ) } pub fn input_ref_needs_type(category: &str, name: &str, span: &Span) -> Self { - Self::new_from_span(format!("could not infer type for input in '{}': '{}'", category, name), span) + Self::new_from_span( + format!("could not infer type for input in '{}': '{}'", category, name), + span, + ) } pub fn invalid_self_in_global(span: &Span) -> Self { - Self::new_from_span("cannot have `mut self` or `self` arguments in global functions".to_string(), span) + Self::new_from_span( + "cannot have `mut self` or `self` arguments in global functions".to_string(), + span, + ) } pub fn parse_index_error() -> Self { diff --git a/asg/src/expression/array_access.rs b/asg/src/expression/array_access.rs index 7fe55213ea..71f7dccef3 100644 --- a/asg/src/expression/array_access.rs +++ b/asg/src/expression/array_access.rs @@ -1,7 +1,36 @@ -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, AsgConvertError, FromAst, Scope, ConstValue, ConstInt, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + ConstInt, + ConstValue, + Expression, + ExpressionNode, + FromAst, + Node, + PartialType, + Scope, + Span, + Type, +}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct ArrayAccessExpression { pub parent: RefCell>>, @@ -58,18 +87,36 @@ impl ExpressionNode for ArrayAccessExpression { } impl FromAst for ArrayAccessExpression { - fn from_ast(scope: &Scope, value: &leo_ast::ArrayAccessExpression, expected_type: Option) -> Result { - let array = Arc::::from_ast(scope, &*value.array, Some(PartialType::Array(expected_type.clone().map(Box::new), None)))?; + fn from_ast( + scope: &Scope, + value: &leo_ast::ArrayAccessExpression, + expected_type: Option, + ) -> Result { + let array = Arc::::from_ast( + scope, + &*value.array, + Some(PartialType::Array(expected_type.clone().map(Box::new), None)), + )?; match array.get_type() { Some(Type::Array(..)) => (), - type_ => return Err(AsgConvertError::unexpected_type("array", type_.map(|x| x.to_string()).as_deref(), &value.span)), + type_ => { + return Err(AsgConvertError::unexpected_type( + "array", + type_.map(|x| x.to_string()).as_deref(), + &value.span, + )); + } } Ok(ArrayAccessExpression { parent: RefCell::new(None), span: Some(value.span.clone()), array, - index: Arc::::from_ast(scope, &*value.index, Some(Type::Integer(leo_ast::IntegerType::U32).partial()))?, + index: Arc::::from_ast( + scope, + &*value.index, + Some(Type::Integer(leo_ast::IntegerType::U32).partial()), + )?, }) } } @@ -82,4 +129,4 @@ impl Into for &ArrayAccessExpression { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/array_init.rs b/asg/src/expression/array_init.rs index ccfbbc5426..ae7f12d483 100644 --- a/asg/src/expression/array_init.rs +++ b/asg/src/expression/array_init.rs @@ -1,7 +1,24 @@ -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct ArrayInitExpression { pub parent: RefCell>>, @@ -44,35 +61,69 @@ impl ExpressionNode for ArrayInitExpression { } impl FromAst for ArrayInitExpression { - fn from_ast(scope: &Scope, value: &leo_ast::ArrayInitExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::ArrayInitExpression, + expected_type: Option, + ) -> Result { let (mut expected_item, expected_len) = match expected_type { Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims), None => (None, None), - Some(type_) => return Err(AsgConvertError::unexpected_type(&type_.to_string(), Some("array"), &value.span)), + Some(type_) => { + return Err(AsgConvertError::unexpected_type( + &type_.to_string(), + Some("array"), + &value.span, + )); + } }; - let dimensions = value.dimensions.0.iter().map(|x| x.value.parse::().map_err(|_| AsgConvertError::parse_dimension_error())).collect::, AsgConvertError>>()?; + let dimensions = value + .dimensions + .0 + .iter() + .map(|x| { + x.value + .parse::() + .map_err(|_| AsgConvertError::parse_dimension_error()) + }) + .collect::, AsgConvertError>>()?; - let len = *dimensions.get(0).ok_or_else(|| AsgConvertError::parse_dimension_error())?; + let len = *dimensions + .get(0) + .ok_or_else(|| AsgConvertError::parse_dimension_error())?; if let Some(expected_len) = expected_len { if expected_len != len { - return Err(AsgConvertError::unexpected_type(&*format!("array of length {}", expected_len), Some(&*format!("array of length {}", len)), &value.span)); + return Err(AsgConvertError::unexpected_type( + &*format!("array of length {}", expected_len), + Some(&*format!("array of length {}", len)), + &value.span, + )); } } - for dimension in (&dimensions[1..]).iter().copied() { expected_item = match expected_item { Some(PartialType::Array(item, len)) => { if let Some(len) = len { if len != dimension { - return Err(AsgConvertError::unexpected_type(&*format!("array of length {}", dimension), Some(&*format!("array of length {}", len)), &value.span)); - } + return Err(AsgConvertError::unexpected_type( + &*format!("array of length {}", dimension), + Some(&*format!("array of length {}", len)), + &value.span, + )); + } } - + item.map(|x| *x) - }, + } None => None, - Some(type_) => return Err(AsgConvertError::unexpected_type("array", Some(&type_.to_string()), &value.span)), + Some(type_) => { + return Err(AsgConvertError::unexpected_type( + "array", + Some(&type_.to_string()), + &value.span, + )); + } } } let mut element = Some(Arc::::from_ast(scope, &*value.element, expected_item)?); @@ -82,7 +133,10 @@ impl FromAst for ArrayInitExpression { output = Some(ArrayInitExpression { parent: RefCell::new(None), span: Some(value.span.clone()), - element: output.map(Expression::ArrayInit).map(Arc::new).unwrap_or_else(|| element.take().unwrap()), + element: output + .map(Expression::ArrayInit) + .map(Arc::new) + .unwrap_or_else(|| element.take().unwrap()), len: dimension, }); } @@ -94,8 +148,10 @@ impl Into for &ArrayInitExpression { fn into(self) -> leo_ast::ArrayInitExpression { leo_ast::ArrayInitExpression { element: Box::new(self.element.as_ref().into()), - dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber { value: self.len.to_string() }]), + dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber { + value: self.len.to_string(), + }]), span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/array_inline.rs b/asg/src/expression/array_inline.rs index b2a8102143..abbdeb72a5 100644 --- a/asg/src/expression/array_inline.rs +++ b/asg/src/expression/array_inline.rs @@ -1,8 +1,25 @@ -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use leo_ast::SpreadOrExpression; -use std::cell::RefCell; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct ArrayInlineExpression { pub parent: RefCell>>, @@ -12,14 +29,19 @@ pub struct ArrayInlineExpression { impl ArrayInlineExpression { pub fn len(&self) -> usize { - self.elements.iter().map(|(expr, is_spread)| if *is_spread { - match expr.get_type() { - Some(Type::Array(_item, len)) => len, - _ => 0, - } - } else { - 1 - }).sum() + self.elements + .iter() + .map(|(expr, is_spread)| { + if *is_spread { + match expr.get_type() { + Some(Type::Array(_item, len)) => len, + _ => 0, + } + } else { + 1 + } + }) + .sum() } } @@ -37,7 +59,7 @@ impl ExpressionNode for ArrayInlineExpression { fn get_parent(&self) -> Option> { self.parent.borrow().as_ref().map(Weak::upgrade).flatten() } - + fn enforce_parents(&self, expr: &Arc) { self.elements.iter().for_each(|(element, _)| { element.set_parent(Arc::downgrade(expr)); @@ -69,11 +91,21 @@ impl ExpressionNode for ArrayInlineExpression { } impl FromAst for ArrayInlineExpression { - fn from_ast(scope: &Scope, value: &leo_ast::ArrayInlineExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::ArrayInlineExpression, + expected_type: Option, + ) -> Result { let (mut expected_item, expected_len) = match expected_type { Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims), None => (None, None), - Some(type_) => return Err(AsgConvertError::unexpected_type(&type_.to_string(), Some("array"), &value.span)), + Some(type_) => { + return Err(AsgConvertError::unexpected_type( + &type_.to_string(), + Some("array"), + &value.span, + )); + } }; // todo: make sure to add some tests with spreading into multidimensional arrays @@ -82,35 +114,57 @@ impl FromAst for ArrayInlineExpression { let output = ArrayInlineExpression { parent: RefCell::new(None), span: Some(value.span.clone()), - elements: value.elements.iter().map(|e| match e { - SpreadOrExpression::Expression(e) => { - let expr = Arc::::from_ast(scope, e, expected_item.clone())?; - if expected_item.is_none() { - expected_item = expr.get_type().map(Type::partial); + elements: value + .elements + .iter() + .map(|e| match e { + SpreadOrExpression::Expression(e) => { + let expr = Arc::::from_ast(scope, e, expected_item.clone())?; + if expected_item.is_none() { + expected_item = expr.get_type().map(Type::partial); + } + len += 1; + Ok((expr, false)) } - len += 1; - Ok((expr, false)) - }, - SpreadOrExpression::Spread(e) => { - let expr = Arc::::from_ast(scope, e, Some(PartialType::Array(expected_item.clone().map(Box::new), None)))?; + SpreadOrExpression::Spread(e) => { + let expr = Arc::::from_ast( + scope, + e, + Some(PartialType::Array(expected_item.clone().map(Box::new), None)), + )?; - match expr.get_type() { - Some(Type::Array(item, spread_len)) => { - if expected_item.is_none() { - expected_item = Some((*item).partial()); + match expr.get_type() { + Some(Type::Array(item, spread_len)) => { + if expected_item.is_none() { + expected_item = Some((*item).partial()); + } + + len += spread_len; + } + type_ => { + return Err(AsgConvertError::unexpected_type( + expected_item + .as_ref() + .map(|x| x.to_string()) + .as_deref() + .unwrap_or("unknown"), + type_.map(|x| x.to_string()).as_deref(), + &value.span, + )); } - - len += spread_len; - }, - type_ => return Err(AsgConvertError::unexpected_type(expected_item.as_ref().map(|x| x.to_string()).as_deref().unwrap_or("unknown"), type_.map(|x| x.to_string()).as_deref(), &value.span)), + } + Ok((expr, true)) } - Ok((expr, true)) - }, - }).collect::, AsgConvertError>>()?, + }) + .collect::, AsgConvertError>>()?, }; if let Some(expected_len) = expected_len { if len != expected_len { - return Err(AsgConvertError::unexpected_type(&*format!("array of length {}", expected_len), Some(&*format!("array of length {}", len)), &value.span)); + return Err(AsgConvertError::unexpected_type( + &*format!("array of length {}", expected_len), + Some(&*format!("array of length {}", len)), + &value.span, + )); } } Ok(output) @@ -120,15 +174,19 @@ impl FromAst for ArrayInlineExpression { impl Into for &ArrayInlineExpression { fn into(self) -> leo_ast::ArrayInlineExpression { leo_ast::ArrayInlineExpression { - elements: self.elements.iter().map(|(element, spread)| { - let element = element.as_ref().into(); - if *spread { - SpreadOrExpression::Spread(element) - } else { - SpreadOrExpression::Expression(element) - } - }).collect(), + elements: self + .elements + .iter() + .map(|(element, spread)| { + let element = element.as_ref().into(); + if *spread { + SpreadOrExpression::Spread(element) + } else { + SpreadOrExpression::Expression(element) + } + }) + .collect(), span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/array_range_access.rs b/asg/src/expression/array_range_access.rs index 9ae5ffe599..80716ecf8f 100644 --- a/asg/src/expression/array_range_access.rs +++ b/asg/src/expression/array_range_access.rs @@ -1,8 +1,37 @@ -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, ConstInt, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + ConstInt, + ConstValue, + Expression, + ExpressionNode, + FromAst, + Node, + PartialType, + Scope, + Span, + Type, +}; use leo_ast::IntegerType; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct ArrayRangeAccessExpression { pub parent: RefCell>>, @@ -83,32 +112,55 @@ impl ExpressionNode for ArrayRangeAccessExpression { return None; } - Some(ConstValue::Array(array.drain(const_left as usize..const_right as usize).collect())) + Some(ConstValue::Array( + array.drain(const_left as usize..const_right as usize).collect(), + )) } } impl FromAst for ArrayRangeAccessExpression { - fn from_ast(scope: &Scope, value: &leo_ast::ArrayRangeAccessExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::ArrayRangeAccessExpression, + expected_type: Option, + ) -> Result { let expected_array = match expected_type { - Some(PartialType::Array(element, _len)) => { - Some(PartialType::Array(element, None)) - }, + Some(PartialType::Array(element, _len)) => Some(PartialType::Array(element, None)), None => None, - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("array"), &value.span)), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some("array"), + &value.span, + )); + } }; let array = Arc::::from_ast(scope, &*value.array, expected_array)?; let array_type = array.get_type(); match array_type { Some(Type::Array(_, _)) => (), - type_ => - return Err(AsgConvertError::unexpected_type("array", type_.map(|x| x.to_string()).as_deref(), &value.span)), + type_ => { + return Err(AsgConvertError::unexpected_type( + "array", + type_.map(|x| x.to_string()).as_deref(), + &value.span, + )); + } } Ok(ArrayRangeAccessExpression { parent: RefCell::new(None), span: Some(value.span.clone()), array, - left: value.left.as_deref().map(|left| Arc::::from_ast(scope, left, Some(Type::Integer(IntegerType::U32).partial()))).transpose()?, - right: value.right.as_deref().map(|right| Arc::::from_ast(scope, right, Some(Type::Integer(IntegerType::U32).partial()))).transpose()?, + left: value + .left + .as_deref() + .map(|left| Arc::::from_ast(scope, left, Some(Type::Integer(IntegerType::U32).partial()))) + .transpose()?, + right: value + .right + .as_deref() + .map(|right| Arc::::from_ast(scope, right, Some(Type::Integer(IntegerType::U32).partial()))) + .transpose()?, }) } } @@ -122,4 +174,4 @@ impl Into for &ArrayRangeAccessExpression { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/binary.rs b/asg/src/expression/binary.rs index 1832580845..cae726c8c5 100644 --- a/asg/src/expression/binary.rs +++ b/asg/src/expression/binary.rs @@ -1,8 +1,25 @@ -pub use leo_ast::{ BinaryOperation, BinaryOperationClass }; -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; +pub use leo_ast::{BinaryOperation, BinaryOperationClass}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct BinaryExpression { pub parent: RefCell>>, @@ -49,22 +66,20 @@ impl ExpressionNode for BinaryExpression { let right = self.right.const_value()?; match (left, right) { - (ConstValue::Int(left), ConstValue::Int(right)) => { - Some(match self.operation { - Add => ConstValue::Int(left.value_add(&right)?), - Sub => ConstValue::Int(left.value_sub(&right)?), - Mul => ConstValue::Int(left.value_mul(&right)?), - Div => ConstValue::Int(left.value_div(&right)?), - Pow => ConstValue::Int(left.value_pow(&right)?), - Eq => ConstValue::Boolean(left == right), - Ne => ConstValue::Boolean(left != right), - Ge => ConstValue::Boolean(left.value_ge(&right)?), - Gt => ConstValue::Boolean(left.value_gt(&right)?), - Le => ConstValue::Boolean(left.value_le(&right)?), - Lt => ConstValue::Boolean(left.value_lt(&right)?), - _ => return None, - }) - }, + (ConstValue::Int(left), ConstValue::Int(right)) => Some(match self.operation { + Add => ConstValue::Int(left.value_add(&right)?), + Sub => ConstValue::Int(left.value_sub(&right)?), + Mul => ConstValue::Int(left.value_mul(&right)?), + Div => ConstValue::Int(left.value_div(&right)?), + Pow => ConstValue::Int(left.value_pow(&right)?), + Eq => ConstValue::Boolean(left == right), + Ne => ConstValue::Boolean(left != right), + Ge => ConstValue::Boolean(left.value_ge(&right)?), + Gt => ConstValue::Boolean(left.value_gt(&right)?), + Le => ConstValue::Boolean(left.value_le(&right)?), + Lt => ConstValue::Boolean(left.value_lt(&right)?), + _ => return None, + }), // (ConstValue::Field(left), ConstValue::Field(right)) => { // Some(match self.operation { // Add => ConstValue::Field(left.checked_add(&right)?), @@ -76,45 +91,54 @@ impl ExpressionNode for BinaryExpression { // _ => return None, // }) // }, - (ConstValue::Boolean(left), ConstValue::Boolean(right)) => { - Some(match self.operation { - Eq => ConstValue::Boolean(left == right), - Ne => ConstValue::Boolean(left != right), - And => ConstValue::Boolean(left && right), - Or => ConstValue::Boolean(left || right), - _ => return None, - }) - }, + (ConstValue::Boolean(left), ConstValue::Boolean(right)) => Some(match self.operation { + Eq => ConstValue::Boolean(left == right), + Ne => ConstValue::Boolean(left != right), + And => ConstValue::Boolean(left && right), + Or => ConstValue::Boolean(left || right), + _ => return None, + }), //todo: group? - (left, right) => { - Some(match self.operation { - Eq => ConstValue::Boolean(left == right), - Ne => ConstValue::Boolean(left != right), - _ => return None, - }) - }, + (left, right) => Some(match self.operation { + Eq => ConstValue::Boolean(left == right), + Ne => ConstValue::Boolean(left != right), + _ => return None, + }), } } } impl FromAst for BinaryExpression { - fn from_ast(scope: &Scope, value: &leo_ast::BinaryExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::BinaryExpression, + expected_type: Option, + ) -> Result { let class = value.op.class(); let expected_type = match class { - BinaryOperationClass::Boolean => { - match expected_type { - Some(PartialType::Type(Type::Boolean)) | None => None, - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&*Type::Boolean.to_string()), &value.span)), + BinaryOperationClass::Boolean => match expected_type { + Some(PartialType::Type(Type::Boolean)) | None => None, + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&*Type::Boolean.to_string()), + &value.span, + )); } }, - BinaryOperationClass::Numeric => { - match expected_type { - Some(PartialType::Type(x @ Type::Integer(_))) => Some(x), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("integer"), &value.span)), - None => None, + BinaryOperationClass::Numeric => match expected_type { + Some(PartialType::Type(x @ Type::Integer(_))) => Some(x), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some("integer"), + &value.span, + )); } + None => None, }, - }.map(Type::partial); + } + .map(Type::partial); // left let (left, right) = match Arc::::from_ast(scope, &*value.left, expected_type.clone()) { @@ -125,16 +149,22 @@ impl FromAst for BinaryExpression { } else { let right = Arc::::from_ast(scope, &*value.right, expected_type.clone())?; if let Some(right_type) = right.get_type() { - (Arc::::from_ast(scope, &*value.left, Some(right_type.partial()))?, right) + ( + Arc::::from_ast(scope, &*value.left, Some(right_type.partial()))?, + right, + ) } else { (left, right) } } - }, + } Err(e) => { let right = Arc::::from_ast(scope, &*value.right, expected_type.clone())?; if let Some(right_type) = right.get_type() { - (Arc::::from_ast(scope, &*value.left, Some(right_type.partial()))?, right) + ( + Arc::::from_ast(scope, &*value.left, Some(right_type.partial()))?, + right, + ) } else { return Err(e); } @@ -145,23 +175,43 @@ impl FromAst for BinaryExpression { match class { BinaryOperationClass::Numeric => match left_type { Some(Type::Integer(_)) => (), - Some(Type::Group) | Some(Type::Field) if value.op == BinaryOperation::Add || value.op == BinaryOperation::Sub => (), + Some(Type::Group) | Some(Type::Field) + if value.op == BinaryOperation::Add || value.op == BinaryOperation::Sub => + { + () + } Some(Type::Field) if value.op == BinaryOperation::Mul || value.op == BinaryOperation::Div => (), - type_ => return Err(AsgConvertError::unexpected_type("integer", type_.map(|x| x.to_string()).as_deref(), &value.span)), + type_ => { + return Err(AsgConvertError::unexpected_type( + "integer", + type_.map(|x| x.to_string()).as_deref(), + &value.span, + )); + } }, - BinaryOperationClass::Boolean => - match &value.op { - BinaryOperation::And | BinaryOperation::Or => - match left_type { - Some(Type::Boolean) | None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&*Type::Boolean.to_string()), &value.span)), - }, - BinaryOperation::Eq | BinaryOperation::Ne => (), // all types allowed - _ => match left_type { - Some(Type::Integer(_)) | None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("integer"), &value.span)), + BinaryOperationClass::Boolean => match &value.op { + BinaryOperation::And | BinaryOperation::Or => match left_type { + Some(Type::Boolean) | None => (), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&*Type::Boolean.to_string()), + &value.span, + )); } - } + }, + BinaryOperation::Eq | BinaryOperation::Ne => (), // all types allowed + _ => match left_type { + Some(Type::Integer(_)) | None => (), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some("integer"), + &value.span, + )); + } + }, + }, } let right_type = right.get_type(); @@ -169,10 +219,20 @@ impl FromAst for BinaryExpression { match (left_type, right_type) { (Some(left_type), Some(right_type)) => { if !left_type.is_assignable_from(&right_type) { - return Err(AsgConvertError::unexpected_type(&left_type.to_string(), Some(&*right_type.to_string()), &value.span)); + return Err(AsgConvertError::unexpected_type( + &left_type.to_string(), + Some(&*right_type.to_string()), + &value.span, + )); } - }, - (None, None) => return Err(AsgConvertError::unexpected_type("any type", Some("unknown type"), &value.span)), + } + (None, None) => { + return Err(AsgConvertError::unexpected_type( + "any type", + Some("unknown type"), + &value.span, + )); + } (_, _) => (), //todo: improve this with type drilling } Ok(BinaryExpression { @@ -194,4 +254,4 @@ impl Into for &BinaryExpression { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/call.rs b/asg/src/expression/call.rs index 55db6d404c..685a328501 100644 --- a/asg/src/expression/call.rs +++ b/asg/src/expression/call.rs @@ -1,8 +1,39 @@ +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + CircuitMember, + ConstValue, + Expression, + ExpressionNode, + FromAst, + Function, + FunctionQualifier, + Node, + PartialType, + Scope, + Span, + Type, +}; pub use leo_ast::BinaryOperation; -use crate::Span; -use crate::{ Expression, Function, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, CircuitMember, ConstValue, PartialType, FunctionQualifier }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct CallExpression { pub parent: RefCell>>, @@ -51,73 +82,136 @@ impl ExpressionNode for CallExpression { } impl FromAst for CallExpression { - fn from_ast(scope: &Scope, value: &leo_ast::CallExpression, expected_type: Option) -> Result { - + fn from_ast( + scope: &Scope, + value: &leo_ast::CallExpression, + expected_type: Option, + ) -> Result { let (target, function) = match &*value.function { - leo_ast::Expression::Identifier(name) => (None, scope.borrow().resolve_function(&name.name).ok_or_else(|| AsgConvertError::unresolved_function(&name.name, &name.span))?), - leo_ast::Expression::CircuitMemberAccess(leo_ast::CircuitMemberAccessExpression { circuit: ast_circuit, name, span }) => { + leo_ast::Expression::Identifier(name) => ( + None, + scope + .borrow() + .resolve_function(&name.name) + .ok_or_else(|| AsgConvertError::unresolved_function(&name.name, &name.span))?, + ), + leo_ast::Expression::CircuitMemberAccess(leo_ast::CircuitMemberAccessExpression { + circuit: ast_circuit, + name, + span, + }) => { let target = Arc::::from_ast(scope, &**ast_circuit, None)?; let circuit = match target.get_type() { Some(Type::Circuit(circuit)) => circuit, - type_ => return Err(AsgConvertError::unexpected_type("circuit", type_.map(|x| x.to_string()).as_deref(), &span)), + type_ => { + return Err(AsgConvertError::unexpected_type( + "circuit", + type_.map(|x| x.to_string()).as_deref(), + &span, + )); + } }; let circuit_name = circuit.name.borrow().name.clone(); let member = circuit.members.borrow(); - let member = member.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?; + let member = member + .get(&name.name) + .ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?; match member { CircuitMember::Function(body) => { if body.qualifier == FunctionQualifier::Static { - return Err(AsgConvertError::circuit_static_call_invalid(&circuit_name, &name.name, &span)); + return Err(AsgConvertError::circuit_static_call_invalid( + &circuit_name, + &name.name, + &span, + )); } else if body.qualifier == FunctionQualifier::MutSelfRef { if !target.is_mut_ref() { - return Err(AsgConvertError::circuit_member_mut_call_invalid(&circuit_name, &name.name, &span)); + return Err(AsgConvertError::circuit_member_mut_call_invalid( + &circuit_name, + &name.name, + &span, + )); } } (Some(target), body.clone()) - }, - CircuitMember::Variable(_) => return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span))?, + } + CircuitMember::Variable(_) => { + return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span))?; + } } - }, - leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression { circuit: ast_circuit, name, span }) => { + } + leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression { + circuit: ast_circuit, + name, + span, + }) => { let circuit = if let leo_ast::Expression::Identifier(circuit_name) = &**ast_circuit { - scope.borrow().resolve_circuit(&circuit_name.name).ok_or_else(|| AsgConvertError::unresolved_circuit(&circuit_name.name, &circuit_name.span))? + scope + .borrow() + .resolve_circuit(&circuit_name.name) + .ok_or_else(|| AsgConvertError::unresolved_circuit(&circuit_name.name, &circuit_name.span))? } else { return Err(AsgConvertError::unexpected_type("circuit", None, &span)); }; let circuit_name = circuit.name.borrow().name.clone(); let member = circuit.members.borrow(); - let member = member.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?; + let member = member + .get(&name.name) + .ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?; match member { CircuitMember::Function(body) => { if body.qualifier != FunctionQualifier::Static { - return Err(AsgConvertError::circuit_member_call_invalid(&circuit_name, &name.name, &span)); + return Err(AsgConvertError::circuit_member_call_invalid( + &circuit_name, + &name.name, + &span, + )); } (None, body.clone()) - }, - CircuitMember::Variable(_) => return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span))?, + } + CircuitMember::Variable(_) => { + return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span))?; + } } - }, - _ => return Err(AsgConvertError::illegal_ast_structure("non Identifier/CircuitMemberAccess/CircuitStaticFunctionAccess as call target")), + } + _ => { + return Err(AsgConvertError::illegal_ast_structure( + "non Identifier/CircuitMemberAccess/CircuitStaticFunctionAccess as call target", + )); + } }; match expected_type { Some(expected) => { let output: Type = function.output.clone().into(); if !expected.matches(&output) { - return Err(AsgConvertError::unexpected_type(&expected.to_string(), Some(&*output.to_string()), &value.span)); + return Err(AsgConvertError::unexpected_type( + &expected.to_string(), + Some(&*output.to_string()), + &value.span, + )); } - }, + } _ => (), } if value.arguments.len() != function.argument_types.len() { - return Err(AsgConvertError::unexpected_call_argument_count(function.argument_types.len(), value.arguments.len(), &value.span)); + return Err(AsgConvertError::unexpected_call_argument_count( + function.argument_types.len(), + value.arguments.len(), + &value.span, + )); } Ok(CallExpression { parent: RefCell::new(None), span: Some(value.span.clone()), - arguments: value.arguments.iter().zip(function.argument_types.iter()) - .map(|(expr, argument)| Arc::::from_ast(scope, expr, Some(argument.clone().strong().partial()))) + arguments: value + .arguments + .iter() + .zip(function.argument_types.iter()) + .map(|(expr, argument)| { + Arc::::from_ast(scope, expr, Some(argument.clone().strong().partial())) + }) .collect::, AsgConvertError>>()?, function, target, @@ -147,4 +241,4 @@ impl Into for &CallExpression { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/circuit_access.rs b/asg/src/expression/circuit_access.rs index d35068984a..5c71fc1f70 100644 --- a/asg/src/expression/circuit_access.rs +++ b/asg/src/expression/circuit_access.rs @@ -1,7 +1,55 @@ -use crate::Span; -use crate::{ Expression, Identifier, Type, Node, ExpressionNode, FromAst, Scope, AsgConvertError, Circuit, CircuitMember, ConstValue, PartialType, CircuitMemberBody }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + Circuit, + CircuitMember, + CircuitMemberBody, + ConstValue, + Expression, + ExpressionNode, + FromAst, + Identifier, + Node, + PartialType, + Scope, + Span, + Type, +}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct CircuitAccessExpression { pub parent: RefCell>>, @@ -59,14 +107,23 @@ impl ExpressionNode for CircuitAccessExpression { } impl FromAst for CircuitAccessExpression { - fn from_ast(scope: &Scope, value: &leo_ast::CircuitMemberAccessExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::CircuitMemberAccessExpression, + expected_type: Option, + ) -> Result { let target = Arc::::from_ast(scope, &*value.circuit, None)?; let circuit = match target.get_type() { Some(Type::Circuit(circuit)) => circuit, - x => return Err(AsgConvertError::unexpected_type("circuit", x.map(|x| x.to_string()).as_deref(), &value.span)), + x => { + return Err(AsgConvertError::unexpected_type( + "circuit", + x.map(|x| x.to_string()).as_deref(), + &value.span, + )); + } }; - // scoping refcell reference let found_member = { if let Some(member) = circuit.members.borrow().get(&value.name.name) { @@ -74,7 +131,11 @@ impl FromAst for CircuitAccessExpression if let CircuitMember::Variable(type_) = &member { let type_: Type = type_.clone().into(); if !expected_type.matches(&type_) { - return Err(AsgConvertError::unexpected_type(&expected_type.to_string(), Some(&type_.to_string()), &value.span)); + return Err(AsgConvertError::unexpected_type( + &expected_type.to_string(), + Some(&type_.to_string()), + &value.span, + )); } } // used by call expression } @@ -86,27 +147,31 @@ impl FromAst for CircuitAccessExpression if found_member { // skip - } else if circuit.is_input_psuedo_circuit() { // add new member to implicit input + } else if circuit.is_input_psuedo_circuit() { + // add new member to implicit input if let Some(expected_type) = expected_type.map(PartialType::full).flatten() { circuit.members.borrow_mut().insert( value.name.name.clone(), - CircuitMember::Variable( - expected_type.clone().into() - ) + CircuitMember::Variable(expected_type.clone().into()), ); let body = circuit.body.borrow().upgrade().expect("stale input circuit body"); - body.members.borrow_mut().insert( - value.name.name.clone(), - CircuitMemberBody::Variable( - expected_type - ) - ); + body.members + .borrow_mut() + .insert(value.name.name.clone(), CircuitMemberBody::Variable(expected_type)); } else { - return Err(AsgConvertError::input_ref_needs_type(&circuit.name.borrow().name, &value.name.name, &value.span)); + return Err(AsgConvertError::input_ref_needs_type( + &circuit.name.borrow().name, + &value.name.name, + &value.span, + )); } } else { - return Err(AsgConvertError::unresolved_circuit_member(&circuit.name.borrow().name, &value.name.name, &value.span)); + return Err(AsgConvertError::unresolved_circuit_member( + &circuit.name.borrow().name, + &value.name.name, + &value.span, + )); } Ok(CircuitAccessExpression { @@ -120,22 +185,41 @@ impl FromAst for CircuitAccessExpression } impl FromAst for CircuitAccessExpression { - fn from_ast(scope: &Scope, value: &leo_ast::CircuitStaticFunctionAccessExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::CircuitStaticFunctionAccessExpression, + expected_type: Option, + ) -> Result { let circuit = match &*value.circuit { - leo_ast::Expression::Identifier(name) => { - scope.borrow().resolve_circuit(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))? - }, - _ => return Err(AsgConvertError::unexpected_type("circuit", Some("unknown"), &value.span)), + leo_ast::Expression::Identifier(name) => scope + .borrow() + .resolve_circuit(&name.name) + .ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?, + _ => { + return Err(AsgConvertError::unexpected_type( + "circuit", + Some("unknown"), + &value.span, + )); + } }; if let Some(expected_type) = expected_type { - return Err(AsgConvertError::unexpected_type(&expected_type.to_string(), Some("none"), &value.span)); + return Err(AsgConvertError::unexpected_type( + &expected_type.to_string(), + Some("none"), + &value.span, + )); } - + if let Some(CircuitMember::Function(_)) = circuit.members.borrow().get(&value.name.name) { // okay } else { - return Err(AsgConvertError::unresolved_circuit_member(&circuit.name.borrow().name, &value.name.name, &value.span)); + return Err(AsgConvertError::unresolved_circuit_member( + &circuit.name.borrow().name, + &value.name.name, + &value.span, + )); } Ok(CircuitAccessExpression { @@ -164,4 +248,4 @@ impl Into for &CircuitAccessExpression { }) } } -} \ No newline at end of file +} diff --git a/asg/src/expression/circuit_init.rs b/asg/src/expression/circuit_init.rs index 76c7bb9dbc..c9597eac62 100644 --- a/asg/src/expression/circuit_init.rs +++ b/asg/src/expression/circuit_init.rs @@ -1,8 +1,39 @@ -use crate::Span; -use crate::{ Expression, Circuit, Identifier, Type, Node, ExpressionNode, FromAst, Scope, AsgConvertError, CircuitMember, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + Circuit, + CircuitMember, + ConstValue, + Expression, + ExpressionNode, + FromAst, + Identifier, + Node, + PartialType, + Scope, + Span, + Type, +}; use indexmap::IndexMap; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct CircuitInitExpression { pub parent: RefCell>>, @@ -46,14 +77,31 @@ impl ExpressionNode for CircuitInitExpression { } impl FromAst for CircuitInitExpression { - fn from_ast(scope: &Scope, value: &leo_ast::CircuitInitExpression, expected_type: Option) -> Result { - let circuit = scope.borrow().resolve_circuit(&value.name.name).ok_or_else(|| AsgConvertError::unresolved_circuit(&value.name.name, &value.name.span))?; + fn from_ast( + scope: &Scope, + value: &leo_ast::CircuitInitExpression, + expected_type: Option, + ) -> Result { + let circuit = scope + .borrow() + .resolve_circuit(&value.name.name) + .ok_or_else(|| AsgConvertError::unresolved_circuit(&value.name.name, &value.name.span))?; match expected_type { Some(PartialType::Type(Type::Circuit(expected_circuit))) if expected_circuit == circuit => (), None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&circuit.name.borrow().name), &value.span)), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&circuit.name.borrow().name), + &value.span, + )); + } } - let members: IndexMap<&String, (&Identifier, &leo_ast::Expression)> = value.members.iter().map(|x| (&x.identifier.name, (&x.identifier, &x.expression))).collect(); + let members: IndexMap<&String, (&Identifier, &leo_ast::Expression)> = value + .members + .iter() + .map(|x| (&x.identifier.name, (&x.identifier, &x.expression))) + .collect(); let mut values: Vec<(Identifier, Arc)> = vec![]; @@ -69,13 +117,21 @@ impl FromAst for CircuitInitExpression { let received = Arc::::from_ast(scope, *receiver, Some(type_.partial()))?; values.push(((*identifier).clone(), received)); } else { - return Err(AsgConvertError::missing_circuit_member(&circuit.name.borrow().name, name, &value.span)); + return Err(AsgConvertError::missing_circuit_member( + &circuit.name.borrow().name, + name, + &value.span, + )); } } for (name, (identifier, _expression)) in members.iter() { if circuit_members.get(*name).is_none() { - return Err(AsgConvertError::extra_circuit_member(&circuit.name.borrow().name, *name, &identifier.span)); + return Err(AsgConvertError::extra_circuit_member( + &circuit.name.borrow().name, + *name, + &identifier.span, + )); } } } @@ -93,13 +149,15 @@ impl Into for &CircuitInitExpression { fn into(self) -> leo_ast::CircuitInitExpression { leo_ast::CircuitInitExpression { name: self.circuit.name.borrow().clone(), - members: self.values.iter().map(|(name, value)| { - leo_ast::CircuitVariableDefinition { + members: self + .values + .iter() + .map(|(name, value)| leo_ast::CircuitVariableDefinition { identifier: name.clone(), expression: value.as_ref().into(), - } - }).collect(), + }) + .collect(), span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/conditional.rs b/asg/src/expression/conditional.rs index 780d1ecebd..bba227b36a 100644 --- a/asg/src/expression/conditional.rs +++ b/asg/src/expression/conditional.rs @@ -1,7 +1,24 @@ -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct ConditionalExpression { pub parent: RefCell>>, @@ -54,7 +71,11 @@ impl ExpressionNode for ConditionalExpression { } impl FromAst for ConditionalExpression { - fn from_ast(scope: &Scope, value: &leo_ast::ConditionalExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::ConditionalExpression, + expected_type: Option, + ) -> Result { Ok(ConditionalExpression { parent: RefCell::new(None), span: Some(value.span.clone()), @@ -74,4 +95,4 @@ impl Into for &ConditionalExpression { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/constant.rs b/asg/src/expression/constant.rs index 52c895d8b9..28336cd11c 100644 --- a/asg/src/expression/constant.rs +++ b/asg/src/expression/constant.rs @@ -1,6 +1,37 @@ -use crate::{ Node, Type, Span, Expression, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, GroupValue, ConstInt, PartialType }; -use std::sync::{ Arc, Weak }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + ConstInt, + ConstValue, + Expression, + ExpressionNode, + FromAst, + GroupValue, + Node, + PartialType, + Scope, + Span, + Type, +}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct Constant { pub parent: RefCell>>, @@ -23,8 +54,7 @@ impl ExpressionNode for Constant { self.parent.borrow().as_ref().map(Weak::upgrade).flatten() } - fn enforce_parents(&self, _expr: &Arc) { - } + fn enforce_parents(&self, _expr: &Arc) {} fn get_type(&self) -> Option { self.value.get_type() @@ -40,98 +70,127 @@ impl ExpressionNode for Constant { } impl FromAst for Constant { - fn from_ast(_scope: &Scope, value: &leo_ast::ValueExpression, expected_type: Option) -> Result { + fn from_ast( + _scope: &Scope, + value: &leo_ast::ValueExpression, + expected_type: Option, + ) -> Result { use leo_ast::ValueExpression::*; Ok(match value { Address(value, span) => { match expected_type.map(PartialType::full).flatten() { Some(Type::Address) | None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&*Type::Address.to_string()), span)), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&*Type::Address.to_string()), + span, + )); + } } Constant { parent: RefCell::new(None), span: Some(span.clone()), value: ConstValue::Address(value.clone()), } - }, + } Boolean(value, span) => { match expected_type.map(PartialType::full).flatten() { Some(Type::Boolean) | None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&*Type::Boolean.to_string()), span)), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&*Type::Boolean.to_string()), + span, + )); + } } Constant { parent: RefCell::new(None), span: Some(span.clone()), - value: ConstValue::Boolean(value.parse::().map_err(|_| AsgConvertError::invalid_boolean(&value, span))?), + value: ConstValue::Boolean( + value + .parse::() + .map_err(|_| AsgConvertError::invalid_boolean(&value, span))?, + ), } - }, + } Field(value, span) => { match expected_type.map(PartialType::full).flatten() { Some(Type::Field) | None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&*Type::Field.to_string()), span)), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&*Type::Field.to_string()), + span, + )); + } } Constant { parent: RefCell::new(None), span: Some(span.clone()), value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?), } - }, + } Group(value) => { match expected_type.map(PartialType::full).flatten() { Some(Type::Group) | None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&*Type::Group.to_string()), value.span())), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&*Type::Group.to_string()), + value.span(), + )); + } } Constant { parent: RefCell::new(None), span: Some(value.span().clone()), value: ConstValue::Group(match &**value { leo_ast::GroupValue::Single(value, _) => GroupValue::Single(value.clone()), - leo_ast::GroupValue::Tuple(leo_ast::GroupTuple { x, y, .. }) => GroupValue::Tuple( - x.into(), - y.into(), - ) - }), - } - }, - Implicit(value, span) => { - match expected_type.map(PartialType::full).flatten() { - None => return Err(AsgConvertError::unresolved_type("unknown", span)), - Some(Type::Integer(int_type)) => { - Constant { - parent: RefCell::new(None), - span: Some(span.clone()), - value: ConstValue::Int(ConstInt::parse(&int_type, value, span)?), - } - }, - Some(Type::Field) => { - Constant { - parent: RefCell::new(None), - span: Some(span.clone()), - value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?), + leo_ast::GroupValue::Tuple(leo_ast::GroupTuple { x, y, .. }) => { + GroupValue::Tuple(x.into(), y.into()) } - }, - Some(Type::Address) => { - Constant { - parent: RefCell::new(None), - span: Some(span.clone()), - value: ConstValue::Address(value.to_string()), - } - }, - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("unknown"), span)), + }), } + } + Implicit(value, span) => match expected_type.map(PartialType::full).flatten() { + None => return Err(AsgConvertError::unresolved_type("unknown", span)), + Some(Type::Integer(int_type)) => Constant { + parent: RefCell::new(None), + span: Some(span.clone()), + value: ConstValue::Int(ConstInt::parse(&int_type, value, span)?), + }, + Some(Type::Field) => Constant { + parent: RefCell::new(None), + span: Some(span.clone()), + value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?), + }, + Some(Type::Address) => Constant { + parent: RefCell::new(None), + span: Some(span.clone()), + value: ConstValue::Address(value.to_string()), + }, + Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("unknown"), span)), }, Integer(int_type, value, span) => { match expected_type.map(PartialType::full).flatten() { Some(Type::Integer(sub_type)) if &sub_type == int_type => (), None => (), - Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&*int_type.to_string()), span)), + Some(x) => { + return Err(AsgConvertError::unexpected_type( + &x.to_string(), + Some(&*int_type.to_string()), + span, + )); + } } Constant { parent: RefCell::new(None), span: Some(span.clone()), value: ConstValue::Int(ConstInt::parse(int_type, value, span)?), } - }, + } }) } } @@ -139,21 +198,32 @@ impl FromAst for Constant { impl Into for &Constant { fn into(self) -> leo_ast::ValueExpression { match &self.value { - ConstValue::Address(value) => leo_ast::ValueExpression::Address(value.clone(), self.span.clone().unwrap_or_default()), - ConstValue::Boolean(value) => leo_ast::ValueExpression::Boolean(value.to_string(), self.span.clone().unwrap_or_default()), - ConstValue::Field(value) => leo_ast::ValueExpression::Field(value.to_string(), self.span.clone().unwrap_or_default()), - ConstValue::Group(value) => - leo_ast::ValueExpression::Group(Box::new(match value { - GroupValue::Single(single) => leo_ast::GroupValue::Single(single.clone(), self.span.clone().unwrap_or_default()), - GroupValue::Tuple(left, right) => leo_ast::GroupValue::Tuple(leo_ast::GroupTuple { - x: left.into(), - y: right.into(), - span: self.span.clone().unwrap_or_default(), - }), - })), - ConstValue::Int(int) => leo_ast::ValueExpression::Integer(int.get_int_type(), int.raw_value(), self.span.clone().unwrap_or_default()), + ConstValue::Address(value) => { + leo_ast::ValueExpression::Address(value.clone(), self.span.clone().unwrap_or_default()) + } + ConstValue::Boolean(value) => { + leo_ast::ValueExpression::Boolean(value.to_string(), self.span.clone().unwrap_or_default()) + } + ConstValue::Field(value) => { + leo_ast::ValueExpression::Field(value.to_string(), self.span.clone().unwrap_or_default()) + } + ConstValue::Group(value) => leo_ast::ValueExpression::Group(Box::new(match value { + GroupValue::Single(single) => { + leo_ast::GroupValue::Single(single.clone(), self.span.clone().unwrap_or_default()) + } + GroupValue::Tuple(left, right) => leo_ast::GroupValue::Tuple(leo_ast::GroupTuple { + x: left.into(), + y: right.into(), + span: self.span.clone().unwrap_or_default(), + }), + })), + ConstValue::Int(int) => leo_ast::ValueExpression::Integer( + int.get_int_type(), + int.raw_value(), + self.span.clone().unwrap_or_default(), + ), ConstValue::Tuple(_) => unimplemented!(), ConstValue::Array(_) => unimplemented!(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/mod.rs b/asg/src/expression/mod.rs index 46f08250ea..12ad96e1f9 100644 --- a/asg/src/expression/mod.rs +++ b/asg/src/expression/mod.rs @@ -1,3 +1,18 @@ +// Copyright (C) 2019-2020 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 . mod variable_ref; pub use variable_ref::*; @@ -28,8 +43,8 @@ pub use circuit_access::*; mod call; pub use call::*; -use std::sync::{ Arc, Weak }; -use crate::{ Node, Type, PartialType, Span, FromAst, AsgConvertError, Scope, ConstValue }; +use crate::{AsgConvertError, ConstValue, FromAst, Node, PartialType, Scope, Span, Type}; +use std::sync::{Arc, Weak}; pub enum Expression { VariableRef(VariableRef), @@ -53,7 +68,6 @@ pub enum Expression { } impl Node for Expression { - fn span(&self) -> Option<&Span> { use Expression::*; match self { @@ -208,27 +222,56 @@ impl ExpressionNode for Expression { } impl FromAst for Arc { - fn from_ast(scope: &Scope, value: &leo_ast::Expression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::Expression, + expected_type: Option, + ) -> Result { use leo_ast::Expression::*; let expression = match value { Identifier(identifier) => Self::from_ast(scope, identifier, expected_type)?, Value(value) => Arc::new(Constant::from_ast(scope, value, expected_type).map(Expression::Constant)?), - Binary(binary) => Arc::new(BinaryExpression::from_ast(scope, binary, expected_type).map(Expression::Binary)?), + Binary(binary) => { + Arc::new(BinaryExpression::from_ast(scope, binary, expected_type).map(Expression::Binary)?) + } Unary(unary) => Arc::new(UnaryExpression::from_ast(scope, unary, expected_type).map(Expression::Unary)?), - Conditional(conditional) => Arc::new(ConditionalExpression::from_ast(scope, conditional, expected_type).map(Expression::Conditional)?), - - ArrayInline(array_inline) => Arc::new(ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?), - ArrayInit(array_init) => Arc::new(ArrayInitExpression::from_ast(scope, array_init, expected_type).map(Expression::ArrayInit)?), - ArrayAccess(array_access) => Arc::new(ArrayAccessExpression::from_ast(scope, array_access, expected_type).map(Expression::ArrayAccess)?), - ArrayRangeAccess(array_range_access) => Arc::new(ArrayRangeAccessExpression::from_ast(scope, array_range_access, expected_type).map(Expression::ArrayRangeAccess)?), - - TupleInit(tuple_init) => Arc::new(TupleInitExpression::from_ast(scope, tuple_init, expected_type).map(Expression::TupleInit)?), - TupleAccess(tuple_access) => Arc::new(TupleAccessExpression::from_ast(scope, tuple_access, expected_type).map(Expression::TupleAccess)?), - - CircuitInit(circuit_init) => Arc::new(CircuitInitExpression::from_ast(scope, circuit_init, expected_type).map(Expression::CircuitInit)?), - CircuitMemberAccess(circuit_member) => Arc::new(CircuitAccessExpression::from_ast(scope, circuit_member, expected_type).map(Expression::CircuitAccess)?), - CircuitStaticFunctionAccess(circuit_member) => Arc::new(CircuitAccessExpression::from_ast(scope, circuit_member, expected_type).map(Expression::CircuitAccess)?), - + Conditional(conditional) => Arc::new( + ConditionalExpression::from_ast(scope, conditional, expected_type).map(Expression::Conditional)?, + ), + + ArrayInline(array_inline) => Arc::new( + ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?, + ), + ArrayInit(array_init) => { + Arc::new(ArrayInitExpression::from_ast(scope, array_init, expected_type).map(Expression::ArrayInit)?) + } + ArrayAccess(array_access) => Arc::new( + ArrayAccessExpression::from_ast(scope, array_access, expected_type).map(Expression::ArrayAccess)?, + ), + ArrayRangeAccess(array_range_access) => Arc::new( + ArrayRangeAccessExpression::from_ast(scope, array_range_access, expected_type) + .map(Expression::ArrayRangeAccess)?, + ), + + TupleInit(tuple_init) => { + Arc::new(TupleInitExpression::from_ast(scope, tuple_init, expected_type).map(Expression::TupleInit)?) + } + TupleAccess(tuple_access) => Arc::new( + TupleAccessExpression::from_ast(scope, tuple_access, expected_type).map(Expression::TupleAccess)?, + ), + + CircuitInit(circuit_init) => Arc::new( + CircuitInitExpression::from_ast(scope, circuit_init, expected_type).map(Expression::CircuitInit)?, + ), + CircuitMemberAccess(circuit_member) => Arc::new( + CircuitAccessExpression::from_ast(scope, circuit_member, expected_type) + .map(Expression::CircuitAccess)?, + ), + CircuitStaticFunctionAccess(circuit_member) => Arc::new( + CircuitAccessExpression::from_ast(scope, circuit_member, expected_type) + .map(Expression::CircuitAccess)?, + ), + Call(call) => Arc::new(CallExpression::from_ast(scope, call, expected_type).map(Expression::Call)?), }; expression.enforce_parents(&expression); @@ -256,4 +299,4 @@ impl Into for &Expression { Call(x) => leo_ast::Expression::Call(x.into()), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/tuple_access.rs b/asg/src/expression/tuple_access.rs index c88a7b4bcf..5739f4a910 100644 --- a/asg/src/expression/tuple_access.rs +++ b/asg/src/expression/tuple_access.rs @@ -1,7 +1,24 @@ -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct TupleAccessExpression { pub parent: RefCell>>, @@ -11,7 +28,6 @@ pub struct TupleAccessExpression { } impl Node for TupleAccessExpression { - fn span(&self) -> Option<&Span> { self.span.as_ref() } @@ -51,8 +67,16 @@ impl ExpressionNode for TupleAccessExpression { } impl FromAst for TupleAccessExpression { - fn from_ast(scope: &Scope, value: &leo_ast::TupleAccessExpression, expected_type: Option) -> Result { - let index = value.index.value.parse::().map_err(|_| AsgConvertError::parse_index_error())?; + fn from_ast( + scope: &Scope, + value: &leo_ast::TupleAccessExpression, + expected_type: Option, + ) -> Result { + let index = value + .index + .value + .parse::() + .map_err(|_| AsgConvertError::parse_index_error())?; let mut expected_tuple = vec![None; index + 1]; expected_tuple[index] = expected_type.clone(); @@ -61,7 +85,11 @@ impl FromAst for TupleAccessExpression { let tuple_type = tuple.get_type(); if let Some(Type::Tuple(_items)) = tuple_type { } else { - return Err(AsgConvertError::unexpected_type("a tuple", tuple_type.map(|x| x.to_string()).as_deref(), &value.span)); + return Err(AsgConvertError::unexpected_type( + "a tuple", + tuple_type.map(|x| x.to_string()).as_deref(), + &value.span, + )); } Ok(TupleAccessExpression { @@ -77,8 +105,10 @@ impl Into for &TupleAccessExpression { fn into(self) -> leo_ast::TupleAccessExpression { leo_ast::TupleAccessExpression { tuple: Box::new(self.tuple_ref.as_ref().into()), - index: leo_ast::PositiveNumber { value: self.index.to_string() }, + index: leo_ast::PositiveNumber { + value: self.index.to_string(), + }, span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/tuple_init.rs b/asg/src/expression/tuple_init.rs index 4181d94e6d..a076a18787 100644 --- a/asg/src/expression/tuple_init.rs +++ b/asg/src/expression/tuple_init.rs @@ -1,7 +1,24 @@ -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct TupleInitExpression { pub parent: RefCell>>, @@ -56,23 +73,46 @@ impl ExpressionNode for TupleInitExpression { } impl FromAst for TupleInitExpression { - fn from_ast(scope: &Scope, value: &leo_ast::TupleInitExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::TupleInitExpression, + expected_type: Option, + ) -> Result { let tuple_types = match expected_type { Some(PartialType::Tuple(sub_types)) => Some(sub_types), None => None, - x => return Err(AsgConvertError::unexpected_type("tuple", x.map(|x| x.to_string()).as_deref(), &value.span)), + x => { + return Err(AsgConvertError::unexpected_type( + "tuple", + x.map(|x| x.to_string()).as_deref(), + &value.span, + )); + } }; if let Some(tuple_types) = tuple_types.as_ref() { if tuple_types.len() != value.elements.len() { - return Err(AsgConvertError::unexpected_type(&*format!("tuple of length {}", tuple_types.len()), Some(&*format!("tuple of length {}", value.elements.len())), &value.span)); + return Err(AsgConvertError::unexpected_type( + &*format!("tuple of length {}", tuple_types.len()), + Some(&*format!("tuple of length {}", value.elements.len())), + &value.span, + )); } } - let elements = value.elements.iter().enumerate() - .map(|(i, e)| Arc::::from_ast(scope, e, tuple_types.as_ref().map(|x| x.get(i)).flatten().cloned().flatten())) + let elements = value + .elements + .iter() + .enumerate() + .map(|(i, e)| { + Arc::::from_ast( + scope, + e, + tuple_types.as_ref().map(|x| x.get(i)).flatten().cloned().flatten(), + ) + }) .collect::, AsgConvertError>>()?; - + Ok(TupleInitExpression { parent: RefCell::new(None), span: Some(value.span.clone()), @@ -88,4 +128,4 @@ impl Into for &TupleInitExpression { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/unary.rs b/asg/src/expression/unary.rs index d716bb414a..36b4546eab 100644 --- a/asg/src/expression/unary.rs +++ b/asg/src/expression/unary.rs @@ -1,8 +1,25 @@ +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; pub use leo_ast::UnaryOperation; -use crate::Span; -use crate::{ Expression, Node, Type, ExpressionNode, Scope, AsgConvertError, FromAst, ConstValue, PartialType }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct UnaryExpression { pub parent: RefCell>>, @@ -41,11 +58,9 @@ impl ExpressionNode for UnaryExpression { fn const_value(&self) -> Option { if let Some(inner) = self.inner.const_value() { match self.operation { - UnaryOperation::Not => { - match inner { - ConstValue::Boolean(value) => Some(ConstValue::Boolean(!value)), - _ => None, - } + UnaryOperation::Not => match inner { + ConstValue::Boolean(value) => Some(ConstValue::Boolean(!value)), + _ => None, }, UnaryOperation::Negate => { match inner { @@ -54,7 +69,7 @@ impl ExpressionNode for UnaryExpression { // ConstValue::Field(value) => Some(ConstValue::Field(-value)), _ => None, } - }, + } } } else { None @@ -63,18 +78,34 @@ impl ExpressionNode for UnaryExpression { } impl FromAst for UnaryExpression { - fn from_ast(scope: &Scope, value: &leo_ast::UnaryExpression, expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::UnaryExpression, + expected_type: Option, + ) -> Result { let expected_type = match value.op { UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() { Some(Type::Boolean) | None => Some(Type::Boolean), - Some(type_) => return Err(AsgConvertError::unexpected_type(&type_.to_string(), Some(&*Type::Boolean.to_string()), &value.span)), + Some(type_) => { + return Err(AsgConvertError::unexpected_type( + &type_.to_string(), + Some(&*Type::Boolean.to_string()), + &value.span, + )); + } }, UnaryOperation::Negate => match expected_type.map(|x| x.full()).flatten() { Some(type_ @ Type::Integer(_)) => Some(type_), Some(Type::Group) => Some(Type::Group), Some(Type::Field) => Some(Type::Field), None => None, - Some(type_) => return Err(AsgConvertError::unexpected_type(&type_.to_string(), Some("integer, group, field"), &value.span)), + Some(type_) => { + return Err(AsgConvertError::unexpected_type( + &type_.to_string(), + Some("integer, group, field"), + &value.span, + )); + } }, }; Ok(UnaryExpression { @@ -94,4 +125,4 @@ impl Into for &UnaryExpression { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/expression/variable_ref.rs b/asg/src/expression/variable_ref.rs index bef507f438..ab842ccf15 100644 --- a/asg/src/expression/variable_ref.rs +++ b/asg/src/expression/variable_ref.rs @@ -1,7 +1,37 @@ -use crate::Span; -use crate::{ Expression, Variable, Node, Type, PartialType, ExpressionNode, FromAst, Scope, AsgConvertError, ConstValue, Constant }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + ConstValue, + Constant, + Expression, + ExpressionNode, + FromAst, + Node, + PartialType, + Scope, + Span, + Type, + Variable, +}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct VariableRef { pub parent: RefCell>>, @@ -24,8 +54,7 @@ impl ExpressionNode for VariableRef { self.parent.borrow().as_ref().map(Weak::upgrade).flatten() } - fn enforce_parents(&self, _expr: &Arc) { - } + fn enforce_parents(&self, _expr: &Arc) {} fn get_type(&self) -> Option { Some(self.variable.borrow().type_.clone()) @@ -41,7 +70,11 @@ impl ExpressionNode for VariableRef { } impl FromAst for Arc { - fn from_ast(scope: &Scope, value: &leo_ast::Identifier, expected_type: Option) -> Result, AsgConvertError> { + fn from_ast( + scope: &Scope, + value: &leo_ast::Identifier, + expected_type: Option, + ) -> Result, AsgConvertError> { let variable = if value.name == "input" { if let Some(function) = scope.borrow().resolve_current_function() { if !function.has_input { @@ -53,7 +86,9 @@ impl FromAst for Arc { if let Some(input) = scope.borrow().resolve_input() { input.container } else { - return Err(AsgConvertError::InternalError("attempted to reference input when none is in scope".to_string())) + return Err(AsgConvertError::InternalError( + "attempted to reference input when none is in scope".to_string(), + )); } } else { match scope.borrow().resolve_variable(&value.name) { @@ -79,9 +114,15 @@ impl FromAst for Arc { let expression = Arc::new(Expression::VariableRef(variable_ref)); if let Some(expected_type) = expected_type { - let type_ = expression.get_type().ok_or_else(|| AsgConvertError::unresolved_reference(&value.name, &value.span))?; + let type_ = expression + .get_type() + .ok_or_else(|| AsgConvertError::unresolved_reference(&value.name, &value.span))?; if !expected_type.matches(&type_) { - return Err(AsgConvertError::unexpected_type(&expected_type.to_string(), Some(&*type_.to_string()), &value.span)); + return Err(AsgConvertError::unexpected_type( + &expected_type.to_string(), + Some(&*type_.to_string()), + &value.span, + )); } } @@ -96,4 +137,4 @@ impl Into for &VariableRef { fn into(self) -> leo_ast::Identifier { self.variable.borrow().name.clone() } -} \ No newline at end of file +} diff --git a/asg/src/import.rs b/asg/src/import.rs index b9682467f4..c5859d4905 100644 --- a/asg/src/import.rs +++ b/asg/src/import.rs @@ -1,5 +1,21 @@ +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, Program, Span}; use indexmap::IndexMap; -use crate::{ Program, AsgConvertError, Span }; pub trait ImportResolver { fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result, AsgConvertError>; @@ -8,7 +24,11 @@ pub trait ImportResolver { pub struct NullImportResolver; impl ImportResolver for NullImportResolver { - fn resolve_package(&mut self, _package_segments: &[&str], _span: &Span) -> Result, AsgConvertError> { + fn resolve_package( + &mut self, + _package_segments: &[&str], + _span: &Span, + ) -> Result, AsgConvertError> { Ok(None) } } @@ -29,7 +49,11 @@ pub struct StandardImportResolver; //todo: move compiler ImportParser here impl ImportResolver for StandardImportResolver { - fn resolve_package(&mut self, _package_segments: &[&str], _span: &Span) -> Result, AsgConvertError> { + fn resolve_package( + &mut self, + _package_segments: &[&str], + _span: &Span, + ) -> Result, AsgConvertError> { Ok(None) } } @@ -42,4 +66,4 @@ impl ImportResolver for MockedImportResolver { fn resolve_package(&mut self, package_segments: &[&str], _span: &Span) -> Result, AsgConvertError> { Ok(self.packages.get(&package_segments.join(".")).cloned()) } -} \ No newline at end of file +} diff --git a/asg/src/input.rs b/asg/src/input.rs index 5c4e526024..c3137d64da 100644 --- a/asg/src/input.rs +++ b/asg/src/input.rs @@ -1,7 +1,25 @@ -use std::sync::{ Arc, Weak }; -use crate::{ Circuit, CircuitBody, Variable, Identifier, Type, CircuitMember, CircuitMemberBody, WeakType, Scope }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{Circuit, CircuitBody, CircuitMember, CircuitMemberBody, Identifier, Scope, Type, Variable, WeakType}; use indexmap::IndexMap; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; #[derive(Clone)] pub struct Input { @@ -34,23 +52,35 @@ impl Input { scope: scope.clone(), span: None, circuit: circuit.clone(), - members: RefCell::new(IndexMap::new()), + members: RefCell::new(IndexMap::new()), }); circuit.body.replace(Arc::downgrade(&body)); body } - + pub fn new(scope: &Scope) -> Self { let registers = Self::make_header(REGISTERS_PSUEDO_CIRCUIT); let record = Self::make_header(RECORD_PSUEDO_CIRCUIT); let state = Self::make_header(STATE_PSUEDO_CIRCUIT); let state_leaf = Self::make_header(STATE_LEAF_PSUEDO_CIRCUIT); - + let mut container_members = IndexMap::new(); - container_members.insert("registers".to_string(), CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(®isters)))); - container_members.insert("record".to_string(), CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&record)))); - container_members.insert("state".to_string(), CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state)))); - container_members.insert("state_leaf".to_string(), CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state_leaf)))); + container_members.insert( + "registers".to_string(), + CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(®isters))), + ); + container_members.insert( + "record".to_string(), + CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&record))), + ); + container_members.insert( + "state".to_string(), + CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state))), + ); + container_members.insert( + "state_leaf".to_string(), + CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state_leaf))), + ); let container_circuit = Arc::new(Circuit { id: uuid::Uuid::new_v4(), @@ -65,10 +95,22 @@ impl Input { let state_leaf_body = Self::make_body(scope, &state_leaf); let mut container_body_members = IndexMap::new(); - container_body_members.insert("registers".to_string(), CircuitMemberBody::Variable(Type::Circuit(registers.clone()))); - container_body_members.insert("record".to_string(), CircuitMemberBody::Variable(Type::Circuit(record.clone()))); - container_body_members.insert("state".to_string(), CircuitMemberBody::Variable(Type::Circuit(state.clone()))); - container_body_members.insert("state_leaf".to_string(), CircuitMemberBody::Variable(Type::Circuit(state_leaf.clone()))); + container_body_members.insert( + "registers".to_string(), + CircuitMemberBody::Variable(Type::Circuit(registers.clone())), + ); + container_body_members.insert( + "record".to_string(), + CircuitMemberBody::Variable(Type::Circuit(record.clone())), + ); + container_body_members.insert( + "state".to_string(), + CircuitMemberBody::Variable(Type::Circuit(state.clone())), + ); + container_body_members.insert( + "state_leaf".to_string(), + CircuitMemberBody::Variable(Type::Circuit(state_leaf.clone())), + ); let container_circuit_body = Arc::new(CircuitBody { scope: scope.clone(), @@ -101,11 +143,8 @@ impl Input { impl Circuit { pub fn is_input_psuedo_circuit(&self) -> bool { match &*self.name.borrow().name { - REGISTERS_PSUEDO_CIRCUIT | - RECORD_PSUEDO_CIRCUIT | - STATE_PSUEDO_CIRCUIT | - STATE_LEAF_PSUEDO_CIRCUIT => true, + REGISTERS_PSUEDO_CIRCUIT | RECORD_PSUEDO_CIRCUIT | STATE_PSUEDO_CIRCUIT | STATE_LEAF_PSUEDO_CIRCUIT => true, _ => false, } } -} \ No newline at end of file +} diff --git a/asg/src/lib.rs b/asg/src/lib.rs index f12222b7e0..9d3d9d8c82 100644 --- a/asg/src/lib.rs +++ b/asg/src/lib.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + #[macro_use] extern crate thiserror; @@ -43,23 +59,26 @@ pub use reducer::*; pub mod checks; pub use checks::*; -pub use leo_ast::{ Span, Identifier }; +pub use leo_ast::{Identifier, Span}; use std::path::Path; pub fn load_ast, Y: AsRef>(path: T, content: Y) -> Result { // Parses the Leo file and constructs a grammar ast. - let ast = leo_grammar::Grammar::new(path.as_ref().clone(), content.as_ref()) + let ast = leo_grammar::Grammar::new(path.as_ref(), content.as_ref()) .map_err(|e| AsgConvertError::InternalError(format!("ast: {:?}", e)))?; // Parses the pest ast and constructs a Leo ast. Ok(leo_ast::Ast::new("load_ast", &ast).into_repr()) } -pub fn load_asg_from_ast(content: leo_ast::Program, resolver: &mut T) -> Result { +pub fn load_asg_from_ast( + content: leo_ast::Program, + resolver: &mut T, +) -> Result { InnerProgram::new(&content, resolver) } pub fn load_asg(content: &str, resolver: &mut T) -> Result { InnerProgram::new(&load_ast("input.leo", content)?, resolver) -} \ No newline at end of file +} diff --git a/asg/src/node.rs b/asg/src/node.rs index 48fca47247..3e56ce9a0c 100644 --- a/asg/src/node.rs +++ b/asg/src/node.rs @@ -1,7 +1,22 @@ -use crate::{ PartialType, Span, Scope, AsgConvertError }; +// Copyright (C) 2019-2020 Aleo Systems Inc. +// This file is part of the Leo library. -pub trait Node { +// 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 . +use crate::{AsgConvertError, PartialType, Scope, Span}; + +pub trait Node { fn span(&self) -> Option<&Span>; } diff --git a/asg/src/prelude.rs b/asg/src/prelude.rs index 5bfe6b1aed..17da5b4fef 100644 --- a/asg/src/prelude.rs +++ b/asg/src/prelude.rs @@ -1,22 +1,38 @@ +// Copyright (C) 2019-2020 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 . //todo: we should merge this with core -use crate::{ Program, AsgConvertError }; +use crate::{AsgConvertError, Program}; // todo: make asg deep copy so we can cache resolved core modules // todo: figure out how to do headers without bogus returns pub fn resolve_core_module(module: &str) -> Result, AsgConvertError> { match module { - "unstable.blake2s" => { - Ok(Some(crate::load_asg(r#" + "unstable.blake2s" => Ok(Some(crate::load_asg( + r#" circuit Blake2s { function hash(seed: [u8; 32], message: [u8; 32]) -> [u8; 32] { return [0; 32] } } - "#, &mut crate::NullImportResolver)?)) - }, + "#, + &mut crate::NullImportResolver, + )?)), _ => Ok(None), } -} \ No newline at end of file +} diff --git a/asg/src/program/circuit.rs b/asg/src/program/circuit.rs index e3fc7d8467..d8fcc5f5d2 100644 --- a/asg/src/program/circuit.rs +++ b/asg/src/program/circuit.rs @@ -1,8 +1,26 @@ -use crate::{ Type, Identifier, Function, FunctionBody, Span, Node, AsgConvertError, InnerScope, Scope, WeakType }; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, Function, FunctionBody, Identifier, InnerScope, Node, Scope, Span, Type, WeakType}; use indexmap::IndexMap; -use std::sync::{ Arc, Weak }; -use std::cell::{ RefCell }; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; use uuid::Uuid; pub enum CircuitMemberBody { @@ -15,7 +33,6 @@ pub enum CircuitMember { Function(Arc), } - pub struct Circuit { pub id: Uuid, pub name: RefCell, @@ -48,23 +65,20 @@ impl PartialEq for CircuitBody { impl Eq for CircuitBody {} impl Node for CircuitMemberBody { - fn span(&self) -> Option<&Span> { None } } - impl Circuit { pub(super) fn init(value: &leo_ast::Circuit) -> Result, AsgConvertError> { - let circuit = Arc::new(Circuit { id: Uuid::new_v4(), name: RefCell::new(value.circuit_name.clone()), body: RefCell::new(Weak::new()), members: RefCell::new(IndexMap::new()), }); - + Ok(circuit) } @@ -76,13 +90,16 @@ impl Circuit { for member in value.members.iter() { match member { leo_ast::CircuitMember::CircuitVariable(name, type_) => { - members.insert(name.name.clone(), CircuitMember::Variable(new_scope.borrow().resolve_ast_type(type_)?.into())); - }, + members.insert( + name.name.clone(), + CircuitMember::Variable(new_scope.borrow().resolve_ast_type(type_)?.into()), + ); + } leo_ast::CircuitMember::CircuitFunction(function) => { let asg_function = Arc::new(Function::from_ast(&new_scope, function)?); members.insert(function.identifier.name.clone(), CircuitMember::Function(asg_function)); - }, + } } } @@ -91,15 +108,17 @@ impl Circuit { func.circuit.borrow_mut().replace(Arc::downgrade(&self)); } } - + Ok(()) } } - impl CircuitBody { - - pub(super) fn from_ast(scope: &Scope, value: &leo_ast::Circuit, circuit: Arc) -> Result { + pub(super) fn from_ast( + scope: &Scope, + value: &leo_ast::Circuit, + circuit: Arc, + ) -> Result { let mut members = IndexMap::new(); let new_scope = InnerScope::make_subscope(scope); new_scope.borrow_mut().circuit_self = Some(circuit.clone()); @@ -108,8 +127,11 @@ impl CircuitBody { for member in value.members.iter() { match member { leo_ast::CircuitMember::CircuitVariable(name, type_) => { - members.insert(name.name.clone(), CircuitMemberBody::Variable(new_scope.borrow().resolve_ast_type(type_)?)); - }, + members.insert( + name.name.clone(), + CircuitMemberBody::Variable(new_scope.borrow().resolve_ast_type(type_)?), + ); + } leo_ast::CircuitMember::CircuitFunction(function) => { let asg_function = { let circuit_members = circuit.members.borrow(); @@ -121,8 +143,11 @@ impl CircuitBody { let function_body = Arc::new(FunctionBody::from_ast(&new_scope, function, asg_function.clone())?); asg_function.body.replace(Arc::downgrade(&function_body)); - members.insert(function.identifier.name.clone(), CircuitMemberBody::Function(function_body)); - }, + members.insert( + function.identifier.name.clone(), + CircuitMemberBody::Function(function_body), + ); + } } } @@ -138,14 +163,19 @@ impl CircuitBody { impl Into for &Circuit { fn into(self) -> leo_ast::Circuit { let members = match self.body.borrow().upgrade() { - Some(body) => { - body.members.borrow().iter().map(|(name, member)| { - match &member { - CircuitMemberBody::Variable(type_) => leo_ast::CircuitMember::CircuitVariable(Identifier::new(name.clone()), type_.into()), - CircuitMemberBody::Function(func) => leo_ast::CircuitMember::CircuitFunction(func.function.as_ref().into()), + Some(body) => body + .members + .borrow() + .iter() + .map(|(name, member)| match &member { + CircuitMemberBody::Variable(type_) => { + leo_ast::CircuitMember::CircuitVariable(Identifier::new(name.clone()), type_.into()) + } + CircuitMemberBody::Function(func) => { + leo_ast::CircuitMember::CircuitFunction(func.function.as_ref().into()) } - }).collect() - }, + }) + .collect(), None => vec![], }; leo_ast::Circuit { @@ -153,4 +183,4 @@ impl Into for &Circuit { members, } } -} \ No newline at end of file +} diff --git a/asg/src/program/function.rs b/asg/src/program/function.rs index 34a1514cbc..955144d221 100644 --- a/asg/src/program/function.rs +++ b/asg/src/program/function.rs @@ -1,8 +1,41 @@ -use crate::{ Identifier, Type, WeakType, Statement, Span, AsgConvertError, BlockStatement, FromAst, Scope, InnerScope, Circuit, Variable, ReturnPathReducer, MonoidalDirector }; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + BlockStatement, + Circuit, + FromAst, + Identifier, + InnerScope, + MonoidalDirector, + ReturnPathReducer, + Scope, + Span, + Statement, + Type, + Variable, + WeakType, +}; -use std::sync::{ Arc, Weak }; -use std::cell::RefCell; use leo_ast::FunctionInput; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; use uuid::Uuid; #[derive(PartialEq)] @@ -50,7 +83,11 @@ impl Eq for FunctionBody {} impl Function { pub(crate) fn from_ast(scope: &Scope, value: &leo_ast::Function) -> Result { - let output: Type = value.output.as_ref().map(|t| scope.borrow().resolve_ast_type(t)).transpose()? + let output: Type = value + .output + .as_ref() + .map(|t| scope.borrow().resolve_ast_type(t)) + .transpose()? .unwrap_or_else(|| Type::Tuple(vec![])); let mut qualifier = FunctionQualifier::Static; let mut has_input = false; @@ -61,16 +98,16 @@ impl Function { match input { FunctionInput::InputKeyword(_) => { has_input = true; - }, + } FunctionInput::SelfKeyword(_) => { qualifier = FunctionQualifier::SelfRef; - }, + } FunctionInput::MutSelfKeyword(_) => { qualifier = FunctionQualifier::MutSelfRef; - }, + } FunctionInput::Variable(leo_ast::FunctionInputVariable { type_, .. }) => { argument_types.push(scope.borrow().resolve_ast_type(&type_)?.into()); - }, + } } } } @@ -88,12 +125,14 @@ impl Function { qualifier, }) } - } - impl FunctionBody { - pub(super) fn from_ast(scope: &Scope, value: &leo_ast::Function, function: Arc) -> Result { + pub(super) fn from_ast( + scope: &Scope, + value: &leo_ast::Function, + function: Arc, + ) -> Result { let new_scope = InnerScope::make_subscope(scope); let mut arguments = vec![]; { @@ -115,10 +154,15 @@ impl FunctionBody { scope_borrow.function = Some(function.clone()); for input in value.input.iter() { match input { - FunctionInput::InputKeyword(_) => {}, - FunctionInput::SelfKeyword(_) => {}, - FunctionInput::MutSelfKeyword(_) => {}, - FunctionInput::Variable(leo_ast::FunctionInputVariable { identifier, mutable, type_, span: _span }) => { + FunctionInput::InputKeyword(_) => {} + FunctionInput::SelfKeyword(_) => {} + FunctionInput::MutSelfKeyword(_) => {} + FunctionInput::Variable(leo_ast::FunctionInputVariable { + identifier, + mutable, + type_, + span: _span, + }) => { let variable = Arc::new(RefCell::new(crate::InnerVariable { id: Uuid::new_v4(), name: identifier.clone(), @@ -131,17 +175,26 @@ impl FunctionBody { })); arguments.push(variable.clone()); scope_borrow.variables.insert(identifier.name.clone(), variable); - }, + } } } } let main_block = BlockStatement::from_ast(&new_scope, &value.block, None)?; let mut director = MonoidalDirector::new(ReturnPathReducer::new()); if !director.reduce_block(&main_block).0 && !function.output.is_unit() { - return Err(AsgConvertError::function_missing_return(&function.name.borrow().name, &value.span)); + return Err(AsgConvertError::function_missing_return( + &function.name.borrow().name, + &value.span, + )); } + + #[allow(clippy::never_loop)] // TODO @Protryon: How should we return multiple errors? for (span, error) in director.reducer().errors { - return Err(AsgConvertError::function_return_validation(&function.name.borrow().name, &error, &span)); + return Err(AsgConvertError::function_return_validation( + &function.name.borrow().name, + &error, + &span, + )); } Ok(FunctionBody { @@ -157,9 +210,10 @@ impl FunctionBody { impl Into for &Function { fn into(self) -> leo_ast::Function { let (input, body, span) = match self.body.borrow().upgrade() { - Some(body) => { - ( - body.arguments.iter().map(|variable| { + Some(body) => ( + body.arguments + .iter() + .map(|variable| { let variable = variable.borrow(); leo_ast::FunctionInput::Variable(leo_ast::FunctionInputVariable { identifier: variable.name.clone(), @@ -167,24 +221,22 @@ impl Into for &Function { type_: (&variable.type_).into(), span: Span::default(), }) - }).collect(), - match body.body.as_ref() { - Statement::Block(block) => block.into(), - _ => unimplemented!(), - }, - body.span.clone().unwrap_or_default(), - ) - }, - None => { - ( - vec![], - leo_ast::Block { - statements: vec![], - span: Default::default(), - }, - Default::default(), - ) - }, + }) + .collect(), + match body.body.as_ref() { + Statement::Block(block) => block.into(), + _ => unimplemented!(), + }, + body.span.clone().unwrap_or_default(), + ), + None => ( + vec![], + leo_ast::Block { + statements: vec![], + span: Default::default(), + }, + Default::default(), + ), }; let output: Type = self.output.clone().into(); leo_ast::Function { @@ -195,4 +247,4 @@ impl Into for &Function { span, } } -} \ No newline at end of file +} diff --git a/asg/src/program/mod.rs b/asg/src/program/mod.rs index 45079a7d44..b789efaf74 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -1,3 +1,18 @@ +// Copyright (C) 2019-2020 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 . mod circuit; pub use circuit::*; @@ -5,12 +20,11 @@ pub use circuit::*; mod function; pub use function::*; -use indexmap::{ IndexMap, IndexSet }; +use indexmap::{IndexMap, IndexSet}; -use std::sync::{ Arc }; -use crate::{ AsgConvertError, InnerScope, Scope, ImportResolver, Input }; -use std::cell::RefCell; -use leo_ast::{Package, PackageAccess, Span, Identifier}; +use crate::{AsgConvertError, ImportResolver, InnerScope, Input, Scope}; +use leo_ast::{Identifier, Package, PackageAccess, Span}; +use std::{cell::RefCell, sync::Arc}; use uuid::Uuid; #[derive(Clone)] @@ -32,20 +46,28 @@ enum ImportSymbol { All, } -fn resolve_import_package(output: &mut Vec<(Vec, ImportSymbol, Span)>, mut package_segments: Vec, package: &Package) { +fn resolve_import_package( + output: &mut Vec<(Vec, ImportSymbol, Span)>, + mut package_segments: Vec, + package: &Package, +) { package_segments.push(package.name.name.clone()); resolve_import_package_access(output, &package_segments, &package.access); } -fn resolve_import_package_access(output: &mut Vec<(Vec, ImportSymbol, Span)>, package_segments: &Vec, package: &PackageAccess) { +fn resolve_import_package_access( + output: &mut Vec<(Vec, ImportSymbol, Span)>, + package_segments: &Vec, + package: &PackageAccess, +) { match package { PackageAccess::Star(span) => { output.push((package_segments.clone(), ImportSymbol::All, span.clone())); - }, + } PackageAccess::SubPackage(subpackage) => { resolve_import_package(output, package_segments.clone(), &*subpackage); - }, + } PackageAccess::Symbol(symbol) => { let span = symbol.symbol.span.clone(); let symbol = if let Some(alias) = symbol.alias.as_ref() { @@ -54,18 +76,16 @@ fn resolve_import_package_access(output: &mut Vec<(Vec, ImportSymbol, Sp ImportSymbol::Direct(symbol.symbol.name.clone()) }; output.push((package_segments.clone(), symbol, span)); - }, + } PackageAccess::Multiple(subaccesses) => { for subaccess in subaccesses.iter() { resolve_import_package_access(output, &package_segments, &subaccess); } - }, - + } } } impl InnerProgram { - /* stages: 1. resolve imports into super scope @@ -73,7 +93,10 @@ impl InnerProgram { 3. finalize declared functions 4. resolve all asg nodes */ - pub fn new<'a, T: ImportResolver + 'static>(value: &leo_ast::Program, import_resolver: &'a mut T) -> Result { + pub fn new<'a, T: ImportResolver + 'static>( + value: &leo_ast::Program, + import_resolver: &'a mut T, + ) -> Result { // TODO: right now if some program A is imported from programs B and C, it will be copied to both, which is not optimal -- needs fixed // recursively extract our imported symbols @@ -90,14 +113,15 @@ impl InnerProgram { let mut wrapped_resolver = crate::CoreImportResolver(import_resolver); // load imported programs - let mut resolved_packages: IndexMap, Program> = IndexMap::new(); + let mut resolved_packages: IndexMap, Program> = IndexMap::new(); for (package, span) in deduplicated_imports.iter() { let pretty_package = package.join("."); - let resolved_package = match wrapped_resolver.resolve_package(&package.iter().map(|x| &**x).collect::>()[..], span)? { - Some(x) => x, - None => return Err(AsgConvertError::unresolved_import(&*pretty_package, &Span::default())), // todo: better span - }; + let resolved_package = + match wrapped_resolver.resolve_package(&package.iter().map(|x| &**x).collect::>()[..], span)? { + Some(x) => x, + None => return Err(AsgConvertError::unresolved_import(&*pretty_package, &Span::default())), // todo: better span + }; resolved_packages.insert(package.clone(), resolved_package); } @@ -109,31 +133,39 @@ impl InnerProgram { for (package, symbol, span) in imported_symbols.into_iter() { let pretty_package = package.join("."); - let resolved_package = resolved_packages.get(&package).expect("could not find preloaded package"); + let resolved_package = resolved_packages + .get(&package) + .expect("could not find preloaded package"); let resolved_package = resolved_package.borrow(); match symbol { ImportSymbol::All => { imported_functions.extend(resolved_package.functions.clone().into_iter()); imported_circuits.extend(resolved_package.circuits.clone().into_iter()); - }, + } ImportSymbol::Direct(name) => { if let Some(function) = resolved_package.functions.get(&name) { imported_functions.insert(name.clone(), function.clone()); } else if let Some(function) = resolved_package.circuits.get(&name) { imported_circuits.insert(name.clone(), function.clone()); } else { - return Err(AsgConvertError::unresolved_import(&*format!("{}.{}", pretty_package, name), &span)); + return Err(AsgConvertError::unresolved_import( + &*format!("{}.{}", pretty_package, name), + &span, + )); } - }, + } ImportSymbol::Alias(name, alias) => { if let Some(function) = resolved_package.functions.get(&name) { imported_functions.insert(alias.clone(), function.clone()); } else if let Some(function) = resolved_package.circuits.get(&name) { imported_circuits.insert(alias.clone(), function.clone()); } else { - return Err(AsgConvertError::unresolved_import(&*format!("{}.{}", pretty_package, name), &span)); + return Err(AsgConvertError::unresolved_import( + &*format!("{}.{}", pretty_package, name), + &span, + )); } - }, + } } } @@ -142,8 +174,14 @@ impl InnerProgram { parent_scope: None, circuit_self: None, variables: IndexMap::new(), - functions: imported_functions.iter().map(|(name, func)| (name.clone(), func.function.clone())).collect(), - circuits: imported_circuits.iter().map(|(name, circuit)| (name.clone(), circuit.circuit.clone())).collect(), + functions: imported_functions + .iter() + .map(|(name, func)| (name.clone(), func.function.clone())) + .collect(), + circuits: imported_circuits + .iter() + .map(|(name, circuit)| (name.clone(), circuit.circuit.clone())) + .collect(), function: None, input: None, })); @@ -153,7 +191,7 @@ impl InnerProgram { for (name, circuit) in value.circuits.iter() { assert_eq!(name.name, circuit.circuit_name.name); let asg_circuit = Circuit::init(circuit)?; - + proto_circuits.insert(name.name.clone(), asg_circuit); } @@ -164,7 +202,10 @@ impl InnerProgram { circuit_self: None, variables: IndexMap::new(), functions: IndexMap::new(), - circuits: proto_circuits.iter().map(|(name, circuit)| (name.clone(), circuit.clone())).collect(), + circuits: proto_circuits + .iter() + .map(|(name, circuit)| (name.clone(), circuit.clone())) + .collect(), function: None, })); @@ -187,7 +228,10 @@ impl InnerProgram { assert_eq!(name.name, function.identifier.name); let asg_function = Arc::new(Function::from_ast(&scope, function)?); - scope.borrow_mut().functions.insert(name.name.clone(), asg_function.clone()); + scope + .borrow_mut() + .functions + .insert(name.name.clone(), asg_function.clone()); proto_functions.insert(name.name.clone(), asg_function); } @@ -197,7 +241,11 @@ impl InnerProgram { assert_eq!(name.name, test_function.function.identifier.name); let function = proto_test_functions.get(&name.name).unwrap(); - let body = Arc::new(FunctionBody::from_ast(&scope, &test_function.function, function.clone())?); + let body = Arc::new(FunctionBody::from_ast( + &scope, + &test_function.function, + function.clone(), + )?); function.body.replace(Arc::downgrade(&body)); test_functions.insert(name.name.clone(), (body, test_function.input_file.clone())); @@ -228,7 +276,10 @@ impl InnerProgram { test_functions, functions, circuits, - imported_modules: resolved_packages.into_iter().map(|(package, program)| (package.join("."), program)).collect(), + imported_modules: resolved_packages + .into_iter() + .map(|(package, program)| (package.join("."), program)) + .collect(), scope, }))) } @@ -259,7 +310,11 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program { program_stack.extend(program.borrow().imported_modules.clone()); } all_programs.insert("".to_string(), program.clone()); - let core_programs: Vec<_> = all_programs.iter().filter(|(module, _)| module.starts_with("core.")).map(|x| x.clone()).collect(); + let core_programs: Vec<_> = all_programs + .iter() + .filter(|(module, _)| module.starts_with("core.")) + .map(|x| x.clone()) + .collect(); all_programs.retain(|module, _| !module.starts_with("core.")); let mut all_circuits: IndexMap> = IndexMap::new(); @@ -275,7 +330,7 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program { } for (name, function) in program.functions.iter() { let identifier = if name == "main" { - "main".to_string() + "main".to_string() } else { format!("{}{}", identifiers.next().unwrap(), name) }; @@ -288,33 +343,34 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program { all_test_functions.insert(identifier, function.clone()); } } - + //todo: core imports leo_ast::Program { name: "ast_aggregate".to_string(), imports: vec![], expected_input: vec![], - tests: all_test_functions.into_iter().map(|(name, (function, ident))| { - ( - function.function.name.borrow().clone(), - leo_ast::TestFunction { + tests: all_test_functions + .into_iter() + .map(|(name, (function, ident))| { + (function.function.name.borrow().clone(), leo_ast::TestFunction { function: function.function.as_ref().into(), input_file: ident, - } - ) - }).collect(), - functions: all_functions.into_iter().map(|(name, function)| { - ( - function.function.name.borrow().clone(), - function.function.as_ref().into(), - ) - }).collect(), - circuits: all_circuits.into_iter().map(|(name, circuit)| { - ( - circuit.circuit.name.borrow().clone(), - circuit.circuit.as_ref().into(), - ) - }).collect(), + }) + }) + .collect(), + functions: all_functions + .into_iter() + .map(|(name, function)| { + ( + function.function.name.borrow().clone(), + function.function.as_ref().into(), + ) + }) + .collect(), + circuits: all_circuits + .into_iter() + .map(|(name, circuit)| (circuit.circuit.name.borrow().clone(), circuit.circuit.as_ref().into())) + .collect(), } } @@ -324,12 +380,31 @@ impl Into for &InnerProgram { name: self.name.clone(), imports: vec![], expected_input: vec![], - circuits: self.circuits.iter().map(|(_, circuit)| (circuit.circuit.name.borrow().clone(), circuit.circuit.as_ref().into())).collect(), - functions: self.functions.iter().map(|(_, function)| (function.function.name.borrow().clone(), function.function.as_ref().into())).collect(), - tests: self.test_functions.iter().map(|(_, function)| (function.0.function.name.borrow().clone(), leo_ast::TestFunction { - function: function.0.function.as_ref().into(), - input_file: function.1.clone(), - })).collect(), + circuits: self + .circuits + .iter() + .map(|(_, circuit)| (circuit.circuit.name.borrow().clone(), circuit.circuit.as_ref().into())) + .collect(), + functions: self + .functions + .iter() + .map(|(_, function)| { + ( + function.function.name.borrow().clone(), + function.function.as_ref().into(), + ) + }) + .collect(), + tests: self + .test_functions + .iter() + .map(|(_, function)| { + (function.0.function.name.borrow().clone(), leo_ast::TestFunction { + function: function.0.function.as_ref().into(), + input_file: function.1.clone(), + }) + }) + .collect(), } } } diff --git a/asg/src/reducer/mod.rs b/asg/src/reducer/mod.rs index 15420a0f6d..393b1917b2 100644 --- a/asg/src/reducer/mod.rs +++ b/asg/src/reducer/mod.rs @@ -1,3 +1,18 @@ +// Copyright (C) 2019-2020 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 . mod monoid; pub use monoid::*; diff --git a/asg/src/reducer/monoid/bool_and.rs b/asg/src/reducer/monoid/bool_and.rs index 4a07bdfefb..fde095e99d 100644 --- a/asg/src/reducer/monoid/bool_and.rs +++ b/asg/src/reducer/monoid/bool_and.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + use super::*; pub struct BoolAnd(pub bool); @@ -13,10 +29,10 @@ impl Monoid for BoolAnd { BoolAnd(self.0 && other.0) } - fn append_all(self, others: impl Iterator) -> Self { + fn append_all(self, others: impl Iterator) -> Self { for item in others { if !item.0 { - return BoolAnd(false) + return BoolAnd(false); } } BoolAnd(true) diff --git a/asg/src/reducer/monoid/mod.rs b/asg/src/reducer/monoid/mod.rs index bd515e8802..e381f3b4c3 100644 --- a/asg/src/reducer/monoid/mod.rs +++ b/asg/src/reducer/monoid/mod.rs @@ -1,3 +1,18 @@ +// Copyright (C) 2019-2020 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 . mod vec_append; pub use vec_append::*; @@ -11,7 +26,7 @@ pub use bool_and::*; pub trait Monoid: Default { fn append(self, other: Self) -> Self; - fn append_all(self, others: impl Iterator) -> Self { + fn append_all(self, others: impl Iterator) -> Self { let mut current = self; for item in others { current = current.append(item); diff --git a/asg/src/reducer/monoid/set_append.rs b/asg/src/reducer/monoid/set_append.rs index 1ef47dc89c..c58774e6a3 100644 --- a/asg/src/reducer/monoid/set_append.rs +++ b/asg/src/reducer/monoid/set_append.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + use super::*; use indexmap::IndexSet; use std::hash::Hash; @@ -16,7 +32,7 @@ impl Monoid for SetAppend { SetAppend(self.0) } - fn append_all(mut self, others: impl Iterator) -> Self { + fn append_all(mut self, others: impl Iterator) -> Self { let all: Vec> = others.map(|x| x.0).collect(); let total_size = all.iter().fold(0, |acc, v| acc + v.len()); self.0.reserve(total_size); @@ -31,4 +47,4 @@ impl Into> for SetAppend { fn into(self) -> IndexSet { self.0 } -} \ No newline at end of file +} diff --git a/asg/src/reducer/monoid/vec_append.rs b/asg/src/reducer/monoid/vec_append.rs index bd82feffe0..f38dc6d1db 100644 --- a/asg/src/reducer/monoid/vec_append.rs +++ b/asg/src/reducer/monoid/vec_append.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + use super::*; pub struct VecAppend(Vec); @@ -14,7 +30,7 @@ impl Monoid for VecAppend { VecAppend(self.0) } - fn append_all(mut self, others: impl Iterator) -> Self { + fn append_all(mut self, others: impl Iterator) -> Self { let all: Vec> = others.map(|x| x.0).collect(); let total_size = all.iter().fold(0, |acc, v| acc + v.len()); self.0.reserve(total_size); @@ -29,4 +45,4 @@ impl Into> for VecAppend { fn into(self) -> Vec { self.0 } -} \ No newline at end of file +} diff --git a/asg/src/reducer/monoidal_director.rs b/asg/src/reducer/monoidal_director.rs index abc7fa2e68..43a32085d5 100644 --- a/asg/src/reducer/monoidal_director.rs +++ b/asg/src/reducer/monoidal_director.rs @@ -1,7 +1,22 @@ +// Copyright (C) 2019-2020 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 . + use super::*; -use crate::{ expression::*, statement::*, program::* }; -use std::sync::Arc; -use std::marker::PhantomData; +use crate::{expression::*, program::*, statement::*}; +use std::{marker::PhantomData, sync::Arc}; pub struct MonoidalDirector> { reducer: R, @@ -97,11 +112,11 @@ impl> MonoidalDirector { let if_true = self.reduce_expression(&input.if_true); let if_false = self.reduce_expression(&input.if_false); - self.reducer.reduce_conditional_expression(input, condition, if_true, if_false) + self.reducer + .reduce_conditional_expression(input, condition, if_true, if_false) } pub fn reduce_constant(&mut self, input: &Constant) -> T { - self.reducer.reduce_constant(input) } @@ -124,14 +139,11 @@ impl> MonoidalDirector { } pub fn reduce_variable_ref(&mut self, input: &VariableRef) -> T { - self.reducer.reduce_variable_ref(input) } } - impl> MonoidalDirector { - pub fn reduce_statement(&mut self, input: &Arc) -> T { match &**input { Statement::Assign(s) => self.reduce_assign(s), @@ -147,12 +159,10 @@ impl> MonoidalDirector { pub fn reduce_assign_access(&mut self, input: &AssignAccess) -> T { let (left, right) = match input { - AssignAccess::ArrayRange(left, right) => { - ( - left.as_ref().map(|e| self.reduce_expression(e)), - right.as_ref().map(|e| self.reduce_expression(e)), - ) - }, + AssignAccess::ArrayRange(left, right) => ( + left.as_ref().map(|e| self.reduce_expression(e)), + right.as_ref().map(|e| self.reduce_expression(e)), + ), AssignAccess::ArrayIndex(index) => (Some(self.reduce_expression(index)), None), _ => (None, None), }; @@ -161,7 +171,11 @@ impl> MonoidalDirector { } pub fn reduce_assign(&mut self, input: &AssignStatement) -> T { - let accesses = input.target_accesses.iter().map(|x| self.reduce_assign_access(x)).collect(); + let accesses = input + .target_accesses + .iter() + .map(|x| self.reduce_assign_access(x)) + .collect(); let value = self.reduce_expression(&input.value); self.reducer.reduce_assign(input, accesses, value) @@ -178,7 +192,8 @@ impl> MonoidalDirector { let if_true = self.reduce_statement(&input.result); let if_false = input.next.as_ref().map(|s| self.reduce_statement(s)); - self.reducer.reduce_conditional_statement(input, condition, if_true, if_false) + self.reducer + .reduce_conditional_statement(input, condition, if_true, if_false) } pub fn reduce_formatted_string(&mut self, input: &FormattedString) -> T { @@ -190,10 +205,9 @@ impl> MonoidalDirector { pub fn reduce_console(&mut self, input: &ConsoleStatement) -> T { let argument = match &input.function { ConsoleFunction::Assert(e) => self.reduce_expression(e), - ConsoleFunction::Debug(f) | - ConsoleFunction::Error(f) | - ConsoleFunction::Log(f) => + ConsoleFunction::Debug(f) | ConsoleFunction::Error(f) | ConsoleFunction::Log(f) => { self.reduce_formatted_string(f) + } }; self.reducer.reduce_console(input, argument) @@ -244,18 +258,32 @@ impl> MonoidalDirector { } fn reduce_circuit(&mut self, input: &Arc) -> T { - let members = input.members.borrow().iter().map(|(_, member)| self.reduce_circuit_member(member)).collect(); + let members = input + .members + .borrow() + .iter() + .map(|(_, member)| self.reduce_circuit_member(member)) + .collect(); self.reducer.reduce_circuit(input, members) } fn reduce_program(&mut self, input: &Program) -> T { let input = input.borrow(); - let imported_modules = input.imported_modules.iter().map(|(_, import)| self.reduce_program(import)).collect(); - let test_functions = input.test_functions.iter().map(|(_, (f, _))| self.reduce_function(f)).collect(); + let imported_modules = input + .imported_modules + .iter() + .map(|(_, import)| self.reduce_program(import)) + .collect(); + let test_functions = input + .test_functions + .iter() + .map(|(_, (f, _))| self.reduce_function(f)) + .collect(); let functions = input.functions.iter().map(|(_, f)| self.reduce_function(f)).collect(); let circuits = input.circuits.iter().map(|(_, c)| self.reduce_circuit(c)).collect(); - self.reducer.reduce_program(&input, imported_modules, test_functions, functions, circuits) + self.reducer + .reduce_program(&input, imported_modules, test_functions, functions, circuits) } } diff --git a/asg/src/reducer/monoidal_reducer.rs b/asg/src/reducer/monoidal_reducer.rs index 0191ef9ac8..b3e2b2a674 100644 --- a/asg/src/reducer/monoidal_reducer.rs +++ b/asg/src/reducer/monoidal_reducer.rs @@ -1,4 +1,20 @@ -use crate::{ Monoid, expression::*, statement::*, program::* }; +// Copyright (C) 2019-2020 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 . + +use crate::{expression::*, program::*, statement::*, Monoid}; use std::sync::Arc; #[allow(unused_variables)] @@ -19,7 +35,13 @@ pub trait MonoidalReducerExpression { T::default().append_all(elements.into_iter()) } - fn reduce_array_range_access(&mut self, input: &ArrayRangeAccessExpression, array: T, left: Option, right: Option) -> T { + fn reduce_array_range_access( + &mut self, + input: &ArrayRangeAccessExpression, + array: T, + left: Option, + right: Option, + ) -> T { array.append_option(left).append_option(right) } @@ -28,8 +50,7 @@ pub trait MonoidalReducerExpression { } fn reduce_call(&mut self, input: &CallExpression, target: Option, arguments: Vec) -> T { - target.unwrap_or_default() - .append_all(arguments.into_iter()) + target.unwrap_or_default().append_all(arguments.into_iter()) } fn reduce_circuit_access(&mut self, input: &CircuitAccessExpression, target: Option) -> T { @@ -40,7 +61,13 @@ pub trait MonoidalReducerExpression { T::default().append_all(values.into_iter()) } - fn reduce_conditional_expression(&mut self, input: &ConditionalExpression, condition: T, if_true: T, if_false: T) -> T { + fn reduce_conditional_expression( + &mut self, + input: &ConditionalExpression, + condition: T, + if_true: T, + if_false: T, + ) -> T { condition.append(if_true).append(if_false) } @@ -77,17 +104,20 @@ pub trait MonoidalReducerStatement: MonoidalReducerExpression { } fn reduce_assign(&mut self, input: &AssignStatement, accesses: Vec, value: T) -> T { - T::default() - .append_all(accesses.into_iter()) - .append(value) + T::default().append_all(accesses.into_iter()).append(value) } fn reduce_block(&mut self, input: &BlockStatement, statements: Vec) -> T { - T::default() - .append_all(statements.into_iter()) + T::default().append_all(statements.into_iter()) } - fn reduce_conditional_statement(&mut self, input: &ConditionalStatement, condition: T, if_true: T, if_false: Option) -> T { + fn reduce_conditional_statement( + &mut self, + input: &ConditionalStatement, + condition: T, + if_true: T, + if_false: Option, + ) -> T { condition.append(if_true).append_option(if_false) } @@ -130,12 +160,18 @@ pub trait MonoidalReducerProgram: MonoidalReducerStatement { T::default().append_all(members.into_iter()) } - fn reduce_program(&mut self, input: &InnerProgram, imported_modules: Vec, test_functions: Vec, functions: Vec, circuits: Vec) -> T { + fn reduce_program( + &mut self, + input: &InnerProgram, + imported_modules: Vec, + test_functions: Vec, + functions: Vec, + circuits: Vec, + ) -> T { T::default() .append_all(imported_modules.into_iter()) .append_all(test_functions.into_iter()) .append_all(functions.into_iter()) .append_all(circuits.into_iter()) } - -} \ No newline at end of file +} diff --git a/asg/src/scope.rs b/asg/src/scope.rs index 13dcabd775..3fdb7e0042 100644 --- a/asg/src/scope.rs +++ b/asg/src/scope.rs @@ -1,7 +1,22 @@ -use std::sync::{ Arc }; -use crate::{ Variable, Circuit, Function, Type, AsgConvertError, Input }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, Circuit, Function, Input, Type, Variable}; use indexmap::IndexMap; +use std::{cell::RefCell, sync::Arc}; use uuid::Uuid; pub struct InnerScope { @@ -128,16 +143,38 @@ impl InnerScope { Array(sub_type, dimensions) => { let mut item = Box::new(self.resolve_ast_type(&*sub_type)?); for dimension in dimensions.0.iter().rev() { - let dimension = dimension.value.parse::().map_err(|_| AsgConvertError::parse_index_error())?; + let dimension = dimension + .value + .parse::() + .map_err(|_| AsgConvertError::parse_index_error())?; item = Box::new(Type::Array(item, dimension)); } *item //Type::Array(item, dimensions.0.get(0).map(|x| x.value.parse::().ok()).flatten().ok_or_else(|| AsgConvertError::parse_index_error())?) - }, - Tuple(sub_types) => Type::Tuple(sub_types.iter().map(|x| self.resolve_ast_type(x)).collect::, AsgConvertError>>()?), - Circuit(name) if name.name == "Self" => Type::Circuit(self.circuit_self.clone().ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?.clone()), - Circuit(name) => Type::Circuit(self.circuits.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?.clone()), - SelfType => Type::Circuit(self.circuit_self.clone().ok_or_else(|| AsgConvertError::reference_self_outside_circuit())?), + } + Tuple(sub_types) => Type::Tuple( + sub_types + .iter() + .map(|x| self.resolve_ast_type(x)) + .collect::, AsgConvertError>>()?, + ), + Circuit(name) if name.name == "Self" => Type::Circuit( + self.circuit_self + .clone() + .ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))? + .clone(), + ), + Circuit(name) => Type::Circuit( + self.circuits + .get(&name.name) + .ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))? + .clone(), + ), + SelfType => Type::Circuit( + self.circuit_self + .clone() + .ok_or_else(|| AsgConvertError::reference_self_outside_circuit())?, + ), }) } -} \ No newline at end of file +} diff --git a/asg/src/statement/assign.rs b/asg/src/statement/assign.rs index e4bd1d08c7..91eaf2919e 100644 --- a/asg/src/statement/assign.rs +++ b/asg/src/statement/assign.rs @@ -1,8 +1,40 @@ -use crate::Span; -use crate::{ Statement, Expression, Variable, Identifier, FromAst, Scope, AsgConvertError, Type, PartialType, CircuitMember, IntegerType, ExpressionNode, ConstValue, ConstInt, Node }; -use std::sync::{ Weak, Arc }; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + CircuitMember, + ConstInt, + ConstValue, + Expression, + ExpressionNode, + FromAst, + Identifier, + IntegerType, + Node, + PartialType, + Scope, + Span, + Statement, + Type, + Variable, +}; pub use leo_ast::AssignOperation; use leo_ast::AssigneeAccess as AstAssigneeAccess; +use std::sync::{Arc, Weak}; pub enum AssignAccess { ArrayRange(Option>, Option>), @@ -27,7 +59,11 @@ impl Node for AssignStatement { } impl FromAst for Arc { - fn from_ast(scope: &Scope, statement: &leo_ast::AssignStatement, _expected_type: Option) -> Result, AsgConvertError> { + fn from_ast( + scope: &Scope, + statement: &leo_ast::AssignStatement, + _expected_type: Option, + ) -> Result, AsgConvertError> { let (name, span) = (&statement.assignee.identifier.name, &statement.assignee.identifier.span); let variable = if name == "input" { @@ -41,12 +77,17 @@ impl FromAst for Arc { if let Some(input) = scope.borrow().resolve_input() { input.container } else { - return Err(AsgConvertError::InternalError("attempted to reference input when none is in scope".to_string())) + return Err(AsgConvertError::InternalError( + "attempted to reference input when none is in scope".to_string(), + )); } } else { - scope.borrow().resolve_variable(&name).ok_or_else(|| AsgConvertError::unresolved_reference(name, span))? + scope + .borrow() + .resolve_variable(&name) + .ok_or_else(|| AsgConvertError::unresolved_reference(name, span))? }; - + if !variable.borrow().mutable { return Err(AsgConvertError::immutable_assignment(&name, &statement.span)); } @@ -57,14 +98,33 @@ impl FromAst for Arc { target_accesses.push(match access { AstAssigneeAccess::ArrayRange(left, right) => { let index_type = Some(Type::Integer(IntegerType::U32).into()); - let left = left.as_ref().map(|left: &leo_ast::Expression| -> Result, AsgConvertError> { Ok(Arc::::from_ast(scope, left, index_type.clone())?) }).transpose()?; - let right = right.as_ref().map(|right: &leo_ast::Expression| -> Result, AsgConvertError> { Ok(Arc::::from_ast(scope, right, index_type)?) }).transpose()?; + let left = left + .as_ref() + .map( + |left: &leo_ast::Expression| -> Result, AsgConvertError> { + Ok(Arc::::from_ast(scope, left, index_type.clone())?) + }, + ) + .transpose()?; + let right = right + .as_ref() + .map( + |right: &leo_ast::Expression| -> Result, AsgConvertError> { + Ok(Arc::::from_ast(scope, right, index_type)?) + }, + ) + .transpose()?; match &target_type { Some(PartialType::Array(item, len)) => { if let (Some(left), Some(right)) = ( - left.as_ref().map(|x| x.const_value()).unwrap_or_else(|| Some(ConstValue::Int(ConstInt::U32(0)))), - right.as_ref().map(|x| x.const_value()).unwrap_or_else(|| Some(ConstValue::Int(ConstInt::U32(len.map(|x| x as u32)?)))), + left.as_ref() + .map(|x| x.const_value()) + .unwrap_or_else(|| Some(ConstValue::Int(ConstInt::U32(0)))), + right + .as_ref() + .map(|x| x.const_value()) + .unwrap_or_else(|| Some(ConstValue::Int(ConstInt::U32(len.map(|x| x as u32)?)))), ) { let left = match left { ConstValue::Int(ConstInt::U32(x)) => x, @@ -78,55 +138,68 @@ impl FromAst for Arc { target_type = Some(PartialType::Array(item.clone(), Some((right - left) as usize))) } } - }, + } _ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)), } - - AssignAccess::ArrayRange( - left, - right, - ) - }, + + AssignAccess::ArrayRange(left, right) + } AstAssigneeAccess::ArrayIndex(index) => { target_type = match target_type.clone() { - Some(PartialType::Array(item, _)) => { - item.map(|x| *x) - }, + Some(PartialType::Array(item, _)) => item.map(|x| *x), _ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)), }; - AssignAccess::ArrayIndex( - Arc::::from_ast(scope, index, Some(Type::Integer(IntegerType::U32).into()))? - ) - }, + AssignAccess::ArrayIndex(Arc::::from_ast( + scope, + index, + Some(Type::Integer(IntegerType::U32).into()), + )?) + } AstAssigneeAccess::Tuple(index, _) => { - let index = index.value.parse::().map_err(|_| AsgConvertError::parse_index_error())?; + let index = index + .value + .parse::() + .map_err(|_| AsgConvertError::parse_index_error())?; target_type = match target_type { - Some(PartialType::Tuple(types)) => { - types.get(index).cloned().ok_or_else(|| AsgConvertError::tuple_index_out_of_bounds(index, &statement.span))? - }, + Some(PartialType::Tuple(types)) => types + .get(index) + .cloned() + .ok_or_else(|| AsgConvertError::tuple_index_out_of_bounds(index, &statement.span))?, _ => return Err(AsgConvertError::index_into_non_tuple(&name, &statement.span)), }; AssignAccess::Tuple(index) - }, + } AstAssigneeAccess::Member(name) => { target_type = match target_type { Some(PartialType::Type(Type::Circuit(circuit))) => { let circuit = circuit; let members = circuit.members.borrow(); - let member = members.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit.name.borrow().name, &name.name, &statement.span))?; + let member = members.get(&name.name).ok_or_else(|| { + AsgConvertError::unresolved_circuit_member( + &circuit.name.borrow().name, + &name.name, + &statement.span, + ) + })?; let x = match &member { CircuitMember::Variable(type_) => type_.clone(), - CircuitMember::Function(_) => return Err(AsgConvertError::illegal_function_assign(&name.name, &statement.span)), + CircuitMember::Function(_) => { + return Err(AsgConvertError::illegal_function_assign(&name.name, &statement.span)); + } }; Some(x.strong().partial()) - }, - _ => return Err(AsgConvertError::index_into_non_tuple(&statement.assignee.identifier.name, &statement.span)), + } + _ => { + return Err(AsgConvertError::index_into_non_tuple( + &statement.assignee.identifier.name, + &statement.span, + )); + } }; AssignAccess::Member(name.clone()) - }, - + } }); } let value = Arc::::from_ast(scope, &statement.value, target_type)?; @@ -155,21 +228,28 @@ impl Into for &AssignStatement { operation: self.operation.clone(), assignee: leo_ast::Assignee { identifier: self.target_variable.borrow().name.clone(), - accesses: self.target_accesses.iter().map(|access| match access { - AssignAccess::ArrayRange(left, right) => - AstAssigneeAccess::ArrayRange( + accesses: self + .target_accesses + .iter() + .map(|access| match access { + AssignAccess::ArrayRange(left, right) => AstAssigneeAccess::ArrayRange( left.as_ref().map(|e| e.as_ref().into()), right.as_ref().map(|e| e.as_ref().into()), ), - AssignAccess::ArrayIndex(index) => - AstAssigneeAccess::ArrayIndex(index.as_ref().into()), - AssignAccess::Tuple(index) => AstAssigneeAccess::Tuple(leo_ast::PositiveNumber { value: index.to_string() }, self.span.clone().unwrap_or_default()), - AssignAccess::Member(name) => AstAssigneeAccess::Member(name.clone()), - }).collect(), + AssignAccess::ArrayIndex(index) => AstAssigneeAccess::ArrayIndex(index.as_ref().into()), + AssignAccess::Tuple(index) => AstAssigneeAccess::Tuple( + leo_ast::PositiveNumber { + value: index.to_string(), + }, + self.span.clone().unwrap_or_default(), + ), + AssignAccess::Member(name) => AstAssigneeAccess::Member(name.clone()), + }) + .collect(), span: self.span.clone().unwrap_or_default(), }, value: self.value.as_ref().into(), span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/block.rs b/asg/src/statement/block.rs index ff00009d4f..0b92a9ff53 100644 --- a/asg/src/statement/block.rs +++ b/asg/src/statement/block.rs @@ -1,6 +1,21 @@ -use crate::Span; -use crate::{ Statement, FromAst, Scope, AsgConvertError, InnerScope, PartialType, Node }; -use std::sync::{ Weak, Arc }; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, FromAst, InnerScope, Node, PartialType, Scope, Span, Statement}; +use std::sync::{Arc, Weak}; pub struct BlockStatement { pub parent: Option>, @@ -16,7 +31,11 @@ impl Node for BlockStatement { } impl FromAst for BlockStatement { - fn from_ast(scope: &Scope, statement: &leo_ast::Block, _expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + statement: &leo_ast::Block, + _expected_type: Option, + ) -> Result { let new_scope = InnerScope::make_subscope(scope); let mut output = vec![]; @@ -35,8 +54,12 @@ impl FromAst for BlockStatement { impl Into for &BlockStatement { fn into(self) -> leo_ast::Block { leo_ast::Block { - statements: self.statements.iter().map(|statement| statement.as_ref().into()).collect(), + statements: self + .statements + .iter() + .map(|statement| statement.as_ref().into()) + .collect(), span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/conditional.rs b/asg/src/statement/conditional.rs index b67c1dd6c8..fdba1a9dc3 100644 --- a/asg/src/statement/conditional.rs +++ b/asg/src/statement/conditional.rs @@ -1,6 +1,21 @@ -use crate::Span; -use crate::{ Statement, Expression, BlockStatement, FromAst, Scope, AsgConvertError, Type, PartialType, Node }; -use std::sync::{ Weak, Arc }; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, BlockStatement, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type}; +use std::sync::{Arc, Weak}; pub struct ConditionalStatement { pub parent: Option>, @@ -17,11 +32,25 @@ impl Node for ConditionalStatement { } impl FromAst for ConditionalStatement { - fn from_ast(scope: &Scope, statement: &leo_ast::ConditionalStatement, _expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + statement: &leo_ast::ConditionalStatement, + _expected_type: Option, + ) -> Result { let condition = Arc::::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?; - let result = Arc::new(Statement::Block(BlockStatement::from_ast(scope, &statement.block, None)?)); - let next = statement.next.as_deref().map(|next| -> Result, AsgConvertError> { Ok(Arc::::from_ast(scope, next, None)?) }).transpose()?; - + let result = Arc::new(Statement::Block(BlockStatement::from_ast( + scope, + &statement.block, + None, + )?)); + let next = statement + .next + .as_deref() + .map(|next| -> Result, AsgConvertError> { + Ok(Arc::::from_ast(scope, next, None)?) + }) + .transpose()?; + Ok(ConditionalStatement { parent: None, span: Some(statement.span.clone()), @@ -44,4 +73,4 @@ impl Into for &ConditionalStatement { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/console.rs b/asg/src/statement/console.rs index b529a48030..d3ef10b8cc 100644 --- a/asg/src/statement/console.rs +++ b/asg/src/statement/console.rs @@ -1,7 +1,22 @@ -use crate::Span; -use crate::{ Statement, Expression, FromAst, Scope, AsgConvertError, Type, PartialType, Node }; -use std::sync::{ Weak, Arc }; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type}; use leo_ast::ConsoleFunction as AstConsoleFunction; +use std::sync::{Arc, Weak}; //todo: refactor to not require/depend on span pub struct FormattedString { @@ -31,10 +46,18 @@ impl Node for ConsoleStatement { } impl FromAst for FormattedString { - fn from_ast(scope: &Scope, value: &leo_ast::FormattedString, _expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + value: &leo_ast::FormattedString, + _expected_type: Option, + ) -> Result { if value.parameters.len() != value.containers.len() { // + 1 for formatting string as to not confuse user - return Err(AsgConvertError::unexpected_call_argument_count(value.containers.len() + 1, value.parameters.len() + 1, &value.span)); + return Err(AsgConvertError::unexpected_call_argument_count( + value.containers.len() + 1, + value.parameters.len() + 1, + &value.span, + )); } let mut parameters = vec![]; for parameter in value.parameters.iter() { @@ -53,9 +76,11 @@ impl Into for &FormattedString { fn into(self) -> leo_ast::FormattedString { leo_ast::FormattedString { string: self.string.clone(), - containers: self.containers.iter().map(|span| leo_ast::FormattedContainer { - span: span.clone(), - }).collect(), + containers: self + .containers + .iter() + .map(|span| leo_ast::FormattedContainer { span: span.clone() }) + .collect(), parameters: self.parameters.iter().map(|e| e.as_ref().into()).collect(), span: self.span.clone(), } @@ -63,16 +88,30 @@ impl Into for &FormattedString { } impl FromAst for ConsoleStatement { - fn from_ast(scope: &Scope, statement: &leo_ast::ConsoleStatement, _expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + statement: &leo_ast::ConsoleStatement, + _expected_type: Option, + ) -> Result { Ok(ConsoleStatement { parent: None, span: Some(statement.span.clone()), function: match &statement.function { - AstConsoleFunction::Assert(expression) => ConsoleFunction::Assert(Arc::::from_ast(scope, expression, Some(Type::Boolean.into()))?), - AstConsoleFunction::Debug(formatted_string) => ConsoleFunction::Debug(FormattedString::from_ast(scope, formatted_string, None)?), - AstConsoleFunction::Error(formatted_string) => ConsoleFunction::Error(FormattedString::from_ast(scope, formatted_string, None)?), - AstConsoleFunction::Log(formatted_string) => ConsoleFunction::Log(FormattedString::from_ast(scope, formatted_string, None)?), - } + AstConsoleFunction::Assert(expression) => ConsoleFunction::Assert(Arc::::from_ast( + scope, + expression, + Some(Type::Boolean.into()), + )?), + AstConsoleFunction::Debug(formatted_string) => { + ConsoleFunction::Debug(FormattedString::from_ast(scope, formatted_string, None)?) + } + AstConsoleFunction::Error(formatted_string) => { + ConsoleFunction::Error(FormattedString::from_ast(scope, formatted_string, None)?) + } + AstConsoleFunction::Log(formatted_string) => { + ConsoleFunction::Log(FormattedString::from_ast(scope, formatted_string, None)?) + } + }, }) } } @@ -90,4 +129,4 @@ impl Into for &ConsoleStatement { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/definition.rs b/asg/src/statement/definition.rs index f6526f16ac..591afd5682 100644 --- a/asg/src/statement/definition.rs +++ b/asg/src/statement/definition.rs @@ -1,7 +1,38 @@ -use crate::Span; -use crate::{ Statement, Variable, InnerVariable, Expression, FromAst, AsgConvertError, Scope, ExpressionNode, Type, ConstValue, PartialType, Node }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + ConstValue, + Expression, + ExpressionNode, + FromAst, + InnerVariable, + Node, + PartialType, + Scope, + Span, + Statement, + Type, + Variable, +}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct DefinitionStatement { pub parent: Option>, @@ -17,9 +48,17 @@ impl Node for DefinitionStatement { } impl FromAst for DefinitionStatement { - fn from_ast(scope: &Scope, statement: &leo_ast::DefinitionStatement, _expected_type: Option) -> Result { - let type_ = statement.type_.as_ref().map(|x| scope.borrow().resolve_ast_type(&x)).transpose()?; - + fn from_ast( + scope: &Scope, + statement: &leo_ast::DefinitionStatement, + _expected_type: Option, + ) -> Result { + let type_ = statement + .type_ + .as_ref() + .map(|x| scope.borrow().resolve_ast_type(&x)) + .transpose()?; + //todo: tuple partially expected types let value = Arc::::from_ast(scope, &statement.value, type_.clone().map(Into::into))?; @@ -29,16 +68,26 @@ impl FromAst for DefinitionStatement { let mut variables = vec![]; if statement.variable_names.len() == 0 { - return Err(AsgConvertError::illegal_ast_structure("cannot have 0 variable names in destructuring tuple")); - } if statement.variable_names.len() == 1 { + return Err(AsgConvertError::illegal_ast_structure( + "cannot have 0 variable names in destructuring tuple", + )); + } + if statement.variable_names.len() == 1 { // any return type is fine output_types.push(type_); - } else { // tuple destructure + } else { + // tuple destructure match type_.as_ref() { Some(Type::Tuple(sub_types)) if sub_types.len() == statement.variable_names.len() => { output_types.extend(sub_types.clone().into_iter().map(Some).collect::>()); - }, - type_ => return Err(AsgConvertError::unexpected_type(&*format!("{}-ary tuple", statement.variable_names.len()), type_.map(|x| x.to_string()).as_deref(), &statement.span)), + } + type_ => { + return Err(AsgConvertError::unexpected_type( + &*format!("{}-ary tuple", statement.variable_names.len()), + type_.map(|x| x.to_string()).as_deref(), + &statement.span, + )); + } } } @@ -54,7 +103,12 @@ impl FromAst for DefinitionStatement { } }; - for (i, (variable, type_)) in statement.variable_names.iter().zip(output_types.into_iter()).enumerate() { + for (i, (variable, type_)) in statement + .variable_names + .iter() + .zip(output_types.into_iter()) + .enumerate() + { if statement.declaration_type == leo_ast::Declare::Const && variable.mutable { return Err(AsgConvertError::illegal_ast_structure("cannot have const mut")); } @@ -67,7 +121,9 @@ impl FromAst for DefinitionStatement { declaration: crate::VariableDeclaration::Definition, const_value: if !variable.mutable { const_values.as_ref().map(|x| x.get(i).cloned()).flatten() - } else { None }, + } else { + None + }, references: vec![], assignments: vec![], }))); @@ -76,7 +132,9 @@ impl FromAst for DefinitionStatement { { let mut scope_borrow = scope.borrow_mut(); for variable in variables.iter() { - scope_borrow.variables.insert(variable.borrow().name.name.clone(), variable.clone()); + scope_borrow + .variables + .insert(variable.borrow().name.name.clone(), variable.clone()); } } @@ -115,4 +173,4 @@ impl Into for &DefinitionStatement { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/expression.rs b/asg/src/statement/expression.rs index f6e8cde05f..a2570caf55 100644 --- a/asg/src/statement/expression.rs +++ b/asg/src/statement/expression.rs @@ -1,6 +1,21 @@ -use crate::Span; -use crate::{ Statement, Expression, FromAst, Scope, AsgConvertError, PartialType, Node }; -use std::sync::{ Weak, Arc }; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement}; +use std::sync::{Arc, Weak}; pub struct ExpressionStatement { pub parent: Option>, @@ -15,9 +30,13 @@ impl Node for ExpressionStatement { } impl FromAst for ExpressionStatement { - fn from_ast(scope: &Scope, statement: &leo_ast::ExpressionStatement, _expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + statement: &leo_ast::ExpressionStatement, + _expected_type: Option, + ) -> Result { let expression = Arc::::from_ast(scope, &statement.expression, None)?; - + Ok(ExpressionStatement { parent: None, span: Some(statement.span.clone()), @@ -33,4 +52,4 @@ impl Into for &ExpressionStatement { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/iteration.rs b/asg/src/statement/iteration.rs index 3920998833..6bfc478aea 100644 --- a/asg/src/statement/iteration.rs +++ b/asg/src/statement/iteration.rs @@ -1,7 +1,37 @@ -use crate::Span; -use crate::{ Statement, Expression, Variable, InnerVariable, Scope, AsgConvertError, FromAst, ExpressionNode, Type, PartialType, Node }; -use std::sync::{ Weak, Arc }; -use std::cell::RefCell; +// Copyright (C) 2019-2020 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 . + +use crate::{ + AsgConvertError, + Expression, + ExpressionNode, + FromAst, + InnerVariable, + Node, + PartialType, + Scope, + Span, + Statement, + Type, + Variable, +}; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; pub struct IterationStatement { pub parent: Option>, @@ -19,7 +49,11 @@ impl Node for IterationStatement { } impl FromAst for IterationStatement { - fn from_ast(scope: &Scope, statement: &leo_ast::IterationStatement, _expected_type: Option) -> Result { + fn from_ast( + scope: &Scope, + statement: &leo_ast::IterationStatement, + _expected_type: Option, + ) -> Result { // todo: is u32 the right type to enforce let expected_index_type = Some(Type::Integer(leo_ast::IntegerType::U32).into()); let start = Arc::::from_ast(scope, &statement.start, expected_index_type.clone())?; @@ -27,14 +61,19 @@ impl FromAst for IterationStatement { let variable = Arc::new(RefCell::new(InnerVariable { id: uuid::Uuid::new_v4(), name: statement.variable.clone(), - type_: start.get_type().ok_or_else(|| AsgConvertError::unresolved_type(&statement.variable.name, &statement.span))?, + type_: start + .get_type() + .ok_or_else(|| AsgConvertError::unresolved_type(&statement.variable.name, &statement.span))?, mutable: false, declaration: crate::VariableDeclaration::IterationDefinition, const_value: None, references: vec![], assignments: vec![], })); - scope.borrow_mut().variables.insert(statement.variable.name.clone(), variable.clone()); + scope + .borrow_mut() + .variables + .insert(statement.variable.name.clone(), variable.clone()); Ok(IterationStatement { parent: None, @@ -42,7 +81,11 @@ impl FromAst for IterationStatement { variable, stop, start, - body: Arc::new(Statement::Block(crate::BlockStatement::from_ast(scope, &statement.block, None)?)), + body: Arc::new(Statement::Block(crate::BlockStatement::from_ast( + scope, + &statement.block, + None, + )?)), }) } } @@ -60,4 +103,4 @@ impl Into for &IterationStatement { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/mod.rs b/asg/src/statement/mod.rs index 7f5fbf4550..2b6801fb1c 100644 --- a/asg/src/statement/mod.rs +++ b/asg/src/statement/mod.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + mod return_; pub use return_::*; mod definition; @@ -15,8 +31,8 @@ pub use expression::*; mod block; pub use block::*; +use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span}; use std::sync::Arc; -use crate::{ AsgConvertError, Scope, FromAst, PartialType, Node, Span }; pub enum Statement { Return(ReturnStatement), @@ -46,33 +62,29 @@ impl Node for Statement { } impl FromAst for Arc { - fn from_ast(scope: &Scope, value: &leo_ast::Statement, _expected_type: Option) -> Result, AsgConvertError> { + fn from_ast( + scope: &Scope, + value: &leo_ast::Statement, + _expected_type: Option, + ) -> Result, AsgConvertError> { use leo_ast::Statement::*; Ok(match value { - Return(statement) => { - Arc::new(Statement::Return(ReturnStatement::from_ast(scope, statement, None)?)) - }, - Definition(statement) => { - Arc::new(Statement::Definition(DefinitionStatement::from_ast(scope, statement, None)?)) - }, - Assign(statement) => { - Arc::::from_ast(scope, statement, None)? - }, - Conditional(statement) => { - Arc::new(Statement::Conditional(ConditionalStatement::from_ast(scope, statement, None)?)) - }, - Iteration(statement) => { - Arc::new(Statement::Iteration(IterationStatement::from_ast(scope, statement, None)?)) - }, - Console(statement) => { - Arc::new(Statement::Console(ConsoleStatement::from_ast(scope, statement, None)?)) - }, - Expression(statement) => { - Arc::new(Statement::Expression(ExpressionStatement::from_ast(scope, statement, None)?)) - }, - Block(statement) => { - Arc::new(Statement::Block(BlockStatement::from_ast(scope, statement, None)?)) - }, + Return(statement) => Arc::new(Statement::Return(ReturnStatement::from_ast(scope, statement, None)?)), + Definition(statement) => Arc::new(Statement::Definition(DefinitionStatement::from_ast( + scope, statement, None, + )?)), + Assign(statement) => Arc::::from_ast(scope, statement, None)?, + Conditional(statement) => Arc::new(Statement::Conditional(ConditionalStatement::from_ast( + scope, statement, None, + )?)), + Iteration(statement) => Arc::new(Statement::Iteration(IterationStatement::from_ast( + scope, statement, None, + )?)), + Console(statement) => Arc::new(Statement::Console(ConsoleStatement::from_ast(scope, statement, None)?)), + Expression(statement) => Arc::new(Statement::Expression(ExpressionStatement::from_ast( + scope, statement, None, + )?)), + Block(statement) => Arc::new(Statement::Block(BlockStatement::from_ast(scope, statement, None)?)), }) } } @@ -91,4 +103,4 @@ impl Into for &Statement { Block(statement) => leo_ast::Statement::Block(statement.into()), } } -} \ No newline at end of file +} diff --git a/asg/src/statement/return_.rs b/asg/src/statement/return_.rs index b37f63c90b..f19b2ef47c 100644 --- a/asg/src/statement/return_.rs +++ b/asg/src/statement/return_.rs @@ -1,6 +1,21 @@ -use crate::Span; -use crate::{ Statement, Expression, Scope, AsgConvertError, FromAst, Type, PartialType, Node }; -use std::sync::{ Weak, Arc }; +// Copyright (C) 2019-2020 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 . + +use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type}; +use std::sync::{Arc, Weak}; pub struct ReturnStatement { pub parent: Option>, @@ -15,8 +30,16 @@ impl Node for ReturnStatement { } impl FromAst for ReturnStatement { - fn from_ast(scope: &Scope, statement: &leo_ast::ReturnStatement, _expected_type: Option) -> Result { - let return_type: Option = scope.borrow().resolve_current_function().map(|x| x.output.clone()).map(Into::into); + fn from_ast( + scope: &Scope, + statement: &leo_ast::ReturnStatement, + _expected_type: Option, + ) -> Result { + let return_type: Option = scope + .borrow() + .resolve_current_function() + .map(|x| x.output.clone()) + .map(Into::into); Ok(ReturnStatement { parent: None, span: Some(statement.span.clone()), @@ -32,4 +55,4 @@ impl Into for &ReturnStatement { span: self.span.clone().unwrap_or_default(), } } -} \ No newline at end of file +} diff --git a/asg/src/type_.rs b/asg/src/type_.rs index 209498afd6..a35b31a855 100644 --- a/asg/src/type_.rs +++ b/asg/src/type_.rs @@ -1,8 +1,25 @@ +// Copyright (C) 2019-2020 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 . -pub use leo_ast::IntegerType; -use std::sync::{ Arc, Weak }; use crate::Circuit; -use std::fmt; +pub use leo_ast::IntegerType; +use std::{ + fmt, + sync::{Arc, Weak}, +}; #[derive(Clone, PartialEq)] pub enum Type { @@ -19,7 +36,6 @@ pub enum Type { Circuit(Arc), } - #[derive(Clone)] pub enum WeakType { Type(Type), // circuit not allowed @@ -69,7 +85,12 @@ impl Into> for PartialType { match self { PartialType::Type(t) => Some(t), PartialType::Array(element, len) => Some(Type::Array(Box::new((*element?).full()?), len?)), - PartialType::Tuple(sub_types) => Some(Type::Tuple(sub_types.into_iter().map(|x| x.map(|x| x.full()).flatten()).collect::>>()?)), + PartialType::Tuple(sub_types) => Some(Type::Tuple( + sub_types + .into_iter() + .map(|x| x.map(|x| x.full()).flatten()) + .collect::>>()?, + )), } } } @@ -92,7 +113,7 @@ impl PartialType { return len == other_len; } true - }, + } (PartialType::Tuple(sub_types), Type::Tuple(other_sub_types)) => { // we dont enforce exact length for tuples here (relying on prior type checking) to allow for full-context-free tuple indexing if sub_types.len() > other_sub_types.len() { @@ -106,9 +127,9 @@ impl PartialType { } } true - }, + } _ => false, - } + } } } @@ -157,7 +178,7 @@ impl fmt::Display for Type { } } write!(f, ")") - }, + } Type::Circuit(circuit) => write!(f, "{}", &circuit.name.borrow().name), } } @@ -180,7 +201,7 @@ impl fmt::Display for PartialType { write!(f, "?")?; } write!(f, "]") - }, + } PartialType::Tuple(sub_types) => { write!(f, "(")?; for (i, sub_type) in sub_types.iter().enumerate() { @@ -194,7 +215,7 @@ impl fmt::Display for PartialType { } } write!(f, ")") - }, + } } } } @@ -208,9 +229,12 @@ impl Into for &Type { Field => leo_ast::Type::Field, Group => leo_ast::Type::Group, Integer(int_type) => leo_ast::Type::IntegerType(int_type.clone()), - Array(type_, len) => leo_ast::Type::Array(Box::new(type_.as_ref().into()), leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber { value: len.to_string() }])), + Array(type_, len) => leo_ast::Type::Array( + Box::new(type_.as_ref().into()), + leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber { value: len.to_string() }]), + ), Tuple(subtypes) => leo_ast::Type::Tuple(subtypes.iter().map(Into::into).collect()), Circuit(circuit) => leo_ast::Type::Circuit(circuit.name.borrow().clone()), } } -} \ No newline at end of file +} diff --git a/asg/src/variable.rs b/asg/src/variable.rs index 89b180f4cf..5611883350 100644 --- a/asg/src/variable.rs +++ b/asg/src/variable.rs @@ -1,7 +1,25 @@ +// Copyright (C) 2019-2020 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 . + +use crate::{ConstValue, Expression, Statement, Type}; use leo_ast::Identifier; -use crate::{ Type, Expression, ConstValue, Statement }; -use std::sync::{ Arc, Weak }; -use std::cell::RefCell; +use std::{ + cell::RefCell, + sync::{Arc, Weak}, +}; use uuid::Uuid; //todo: fill out diff --git a/asg/tests/fail/address/mod.rs b/asg/tests/fail/address/mod.rs index 563a8d5137..b5b4cff3ea 100644 --- a/asg/tests/fail/address/mod.rs +++ b/asg/tests/fail/address/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_implicit_invalid() { diff --git a/asg/tests/fail/array/mod.rs b/asg/tests/fail/array/mod.rs index 6cc23ce924..6f7b2fd19e 100644 --- a/asg/tests/fail/array/mod.rs +++ b/asg/tests/fail/array/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; // Expressions diff --git a/asg/tests/fail/boolean/mod.rs b/asg/tests/fail/boolean/mod.rs index 88ca76dde5..780ed3aa08 100644 --- a/asg/tests/fail/boolean/mod.rs +++ b/asg/tests/fail/boolean/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_not_u32() { diff --git a/asg/tests/fail/circuits/mod.rs b/asg/tests/fail/circuits/mod.rs index 18c096e17b..a9add5a7e1 100644 --- a/asg/tests/fail/circuits/mod.rs +++ b/asg/tests/fail/circuits/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use crate::load_asg; // Expressions diff --git a/asg/tests/fail/console/mod.rs b/asg/tests/fail/console/mod.rs index 965c81b4dd..f6dc483fe1 100644 --- a/asg/tests/fail/console/mod.rs +++ b/asg/tests/fail/console/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_log_fail() { diff --git a/asg/tests/fail/core/mod.rs b/asg/tests/fail/core/mod.rs index 0d37432bb2..9ab7bb52bc 100644 --- a/asg/tests/fail/core/mod.rs +++ b/asg/tests/fail/core/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use crate::load_asg; #[test] diff --git a/asg/tests/fail/definition/mod.rs b/asg/tests/fail/definition/mod.rs index 600f61b496..e946846b69 100644 --- a/asg/tests/fail/definition/mod.rs +++ b/asg/tests/fail/definition/mod.rs @@ -13,3 +13,19 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . + +// Copyright (C) 2019-2020 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 . diff --git a/asg/tests/fail/field/mod.rs b/asg/tests/fail/field/mod.rs index 600f61b496..e946846b69 100644 --- a/asg/tests/fail/field/mod.rs +++ b/asg/tests/fail/field/mod.rs @@ -13,3 +13,19 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . + +// Copyright (C) 2019-2020 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 . diff --git a/asg/tests/fail/function/mod.rs b/asg/tests/fail/function/mod.rs index d279c90fe7..3156e3b580 100644 --- a/asg/tests/fail/function/mod.rs +++ b/asg/tests/fail/function/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_multiple_returns_fail() { diff --git a/asg/tests/fail/group/mod.rs b/asg/tests/fail/group/mod.rs index bb2e89ec89..f8e29728ce 100644 --- a/asg/tests/fail/group/mod.rs +++ b/asg/tests/fail/group/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use crate::load_asg; #[test] diff --git a/asg/tests/fail/import/mod.rs b/asg/tests/fail/import/mod.rs index 600f61b496..e946846b69 100644 --- a/asg/tests/fail/import/mod.rs +++ b/asg/tests/fail/import/mod.rs @@ -13,3 +13,19 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . + +// Copyright (C) 2019-2020 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 . diff --git a/asg/tests/fail/integers/i128/mod.rs b/asg/tests/fail/integers/i128/mod.rs index 37ed2e3da9..2062617fdf 100644 --- a/asg/tests/fail/integers/i128/mod.rs +++ b/asg/tests/fail/integers/i128/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI128 -); +test_int!(TestI128); #[test] fn test_i128_min_fail() { diff --git a/asg/tests/fail/integers/i16/mod.rs b/asg/tests/fail/integers/i16/mod.rs index 5a8a899037..a60934189f 100644 --- a/asg/tests/fail/integers/i16/mod.rs +++ b/asg/tests/fail/integers/i16/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI16 -); +test_int!(TestI16); #[test] fn test_i16_min_fail() { diff --git a/asg/tests/fail/integers/i32/mod.rs b/asg/tests/fail/integers/i32/mod.rs index 60593c4b67..fcae16f93e 100644 --- a/asg/tests/fail/integers/i32/mod.rs +++ b/asg/tests/fail/integers/i32/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI32 -); +test_int!(TestI32); #[test] fn test_i32_min_fail() { diff --git a/asg/tests/fail/integers/i64/mod.rs b/asg/tests/fail/integers/i64/mod.rs index 23d3f2379a..6d1aff51ea 100644 --- a/asg/tests/fail/integers/i64/mod.rs +++ b/asg/tests/fail/integers/i64/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI64 -); +test_int!(TestI64); #[test] fn test_i64_min_fail() { diff --git a/asg/tests/fail/integers/i8/mod.rs b/asg/tests/fail/integers/i8/mod.rs index fa7893bcdf..c4820fcea5 100644 --- a/asg/tests/fail/integers/i8/mod.rs +++ b/asg/tests/fail/integers/i8/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI8 -); +test_int!(TestI8); #[test] fn test_i8_min_fail() { diff --git a/asg/tests/fail/integers/int_macro.rs b/asg/tests/fail/integers/int_macro.rs index d6bce3c0b6..697a18998d 100644 --- a/asg/tests/fail/integers/int_macro.rs +++ b/asg/tests/fail/integers/int_macro.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + macro_rules! test_int { ($name: ident) => { pub struct $name {} diff --git a/asg/tests/fail/integers/integer_tester.rs b/asg/tests/fail/integers/integer_tester.rs index e0281c007d..0c30b88dd1 100644 --- a/asg/tests/fail/integers/integer_tester.rs +++ b/asg/tests/fail/integers/integer_tester.rs @@ -14,11 +14,26 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + pub trait IntegerTester { /// Tests defining the smallest value - 1 fn test_min_fail(); /// Tests defining the largest value + 1 fn test_max_fail(); - } diff --git a/asg/tests/fail/integers/mod.rs b/asg/tests/fail/integers/mod.rs index 3aa0c691c8..d9fc896497 100644 --- a/asg/tests/fail/integers/mod.rs +++ b/asg/tests/fail/integers/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + #[macro_use] pub mod int_macro; diff --git a/asg/tests/fail/integers/u128/mod.rs b/asg/tests/fail/integers/u128/mod.rs index b2ce0840d4..45785327f2 100644 --- a/asg/tests/fail/integers/u128/mod.rs +++ b/asg/tests/fail/integers/u128/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU128 -); +test_uint!(TestU128); #[test] fn test_u128_min_fail() { diff --git a/asg/tests/fail/integers/u16/mod.rs b/asg/tests/fail/integers/u16/mod.rs index c1013d263c..10f8c85612 100644 --- a/asg/tests/fail/integers/u16/mod.rs +++ b/asg/tests/fail/integers/u16/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU16 -); +test_uint!(TestU16); #[test] fn test_u16_min_fail() { diff --git a/asg/tests/fail/integers/u32/mod.rs b/asg/tests/fail/integers/u32/mod.rs index 64a41a29bd..65b17b801d 100644 --- a/asg/tests/fail/integers/u32/mod.rs +++ b/asg/tests/fail/integers/u32/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU32 -); +test_uint!(TestU32); #[test] fn test_u32_min_fail() { diff --git a/asg/tests/fail/integers/u64/mod.rs b/asg/tests/fail/integers/u64/mod.rs index b2f6a268b8..416ebdc3b6 100644 --- a/asg/tests/fail/integers/u64/mod.rs +++ b/asg/tests/fail/integers/u64/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU64 -); +test_uint!(TestU64); #[test] fn test_u64_min_fail() { diff --git a/asg/tests/fail/integers/u8/mod.rs b/asg/tests/fail/integers/u8/mod.rs index 657ab6d735..3eb6ca944c 100644 --- a/asg/tests/fail/integers/u8/mod.rs +++ b/asg/tests/fail/integers/u8/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU8 -); +test_uint!(TestU8); #[test] fn test_u8_min_fail() { diff --git a/asg/tests/fail/integers/uint_macro.rs b/asg/tests/fail/integers/uint_macro.rs index f7ac6f4013..ac81632566 100644 --- a/asg/tests/fail/integers/uint_macro.rs +++ b/asg/tests/fail/integers/uint_macro.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + macro_rules! test_uint { ($name: ident) => { pub struct $name {} diff --git a/asg/tests/fail/mod.rs b/asg/tests/fail/mod.rs index e2f7e63ce1..718731f2c3 100644 --- a/asg/tests/fail/mod.rs +++ b/asg/tests/fail/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + pub mod address; pub mod array; pub mod boolean; diff --git a/asg/tests/fail/mutability/mod.rs b/asg/tests/fail/mutability/mod.rs index f0e49c0459..642369feb6 100644 --- a/asg/tests/fail/mutability/mod.rs +++ b/asg/tests/fail/mutability/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_let() { diff --git a/asg/tests/fail/statements/mod.rs b/asg/tests/fail/statements/mod.rs index 3fb84afc12..99ad1332df 100644 --- a/asg/tests/fail/statements/mod.rs +++ b/asg/tests/fail/statements/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_num_returns_fail() { diff --git a/asg/tests/fail/tuples/mod.rs b/asg/tests/fail/tuples/mod.rs index 600f61b496..e946846b69 100644 --- a/asg/tests/fail/tuples/mod.rs +++ b/asg/tests/fail/tuples/mod.rs @@ -13,3 +13,19 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . + +// Copyright (C) 2019-2020 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 . diff --git a/asg/tests/mod.rs b/asg/tests/mod.rs index 4cc6f289a7..631ade6e5b 100644 --- a/asg/tests/mod.rs +++ b/asg/tests/mod.rs @@ -1,7 +1,23 @@ +// Copyright (C) 2019-2020 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 . + use leo_asg::*; -mod pass; mod fail; +mod pass; fn load_asg(content: &str) -> Result { leo_asg::load_asg(content, &mut NullImportResolver) @@ -13,7 +29,5 @@ fn load_asg_imports(content: &str, imports: &mut T) fn mocked_resolver() -> MockedImportResolver { let packages = indexmap::IndexMap::new(); - MockedImportResolver { - packages, - } -} \ No newline at end of file + MockedImportResolver { packages } +} diff --git a/asg/tests/pass/address/mod.rs b/asg/tests/pass/address/mod.rs index 2b405b3f82..1237a2fd06 100644 --- a/asg/tests/pass/address/mod.rs +++ b/asg/tests/pass/address/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_valid() { diff --git a/asg/tests/pass/array/mod.rs b/asg/tests/pass/array/mod.rs index d92d18a3e0..4087723ae5 100644 --- a/asg/tests/pass/array/mod.rs +++ b/asg/tests/pass/array/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; // Registers diff --git a/asg/tests/pass/boolean/mod.rs b/asg/tests/pass/boolean/mod.rs index 44598f8565..e23749836b 100644 --- a/asg/tests/pass/boolean/mod.rs +++ b/asg/tests/pass/boolean/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_input_pass() { diff --git a/asg/tests/pass/circuits/mod.rs b/asg/tests/pass/circuits/mod.rs index 24adea367c..00d3aa0270 100644 --- a/asg/tests/pass/circuits/mod.rs +++ b/asg/tests/pass/circuits/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use crate::load_asg; // Expressions diff --git a/asg/tests/pass/console/mod.rs b/asg/tests/pass/console/mod.rs index 60ce1eb58c..b32fc56ff2 100644 --- a/asg/tests/pass/console/mod.rs +++ b/asg/tests/pass/console/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_log() { diff --git a/asg/tests/pass/core/mod.rs b/asg/tests/pass/core/mod.rs index bb75667216..edceb6112e 100644 --- a/asg/tests/pass/core/mod.rs +++ b/asg/tests/pass/core/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use crate::load_asg; #[test] diff --git a/asg/tests/pass/definition/mod.rs b/asg/tests/pass/definition/mod.rs index e6ee45b3d2..ebf69c6a66 100644 --- a/asg/tests/pass/definition/mod.rs +++ b/asg/tests/pass/definition/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_out_of_order() { diff --git a/asg/tests/pass/field/mod.rs b/asg/tests/pass/field/mod.rs index ea63249189..3401e24e14 100644 --- a/asg/tests/pass/field/mod.rs +++ b/asg/tests/pass/field/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_negate() { diff --git a/asg/tests/pass/form_ast.rs b/asg/tests/pass/form_ast.rs index 3d5f054656..354c8cfbff 100644 --- a/asg/tests/pass/form_ast.rs +++ b/asg/tests/pass/form_ast.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + use crate::load_asg; #[test] @@ -34,7 +50,6 @@ fn test_function_rename() { panic!(); } - #[test] fn test_imports() { let mut imports = crate::mocked_resolver(); @@ -48,7 +63,9 @@ fn test_imports() { return 1u32 } "#; - imports.packages.insert("test-import".to_string(), load_asg(test_import).unwrap()); + imports + .packages + .insert("test-import".to_string(), load_asg(test_import).unwrap()); let program_string = r#" import test-import.foo; @@ -56,11 +73,16 @@ fn test_imports() { console.assert(foo() == 1u32); } "#; - println!("{}", serde_json::to_string(&crate::load_ast("test-import.leo", test_import).unwrap()).unwrap()); - println!("{}", serde_json::to_string(&crate::load_ast("test.leo", program_string).unwrap()).unwrap()); + println!( + "{}", + serde_json::to_string(&crate::load_ast("test-import.leo", test_import).unwrap()).unwrap() + ); + println!( + "{}", + serde_json::to_string(&crate::load_ast("test.leo", program_string).unwrap()).unwrap() + ); let asg = crate::load_asg_imports(program_string, &mut imports).unwrap(); let reformed_ast = leo_asg::reform_ast(&asg); println!("{}", serde_json::to_string(&reformed_ast).unwrap()); panic!(); - -} \ No newline at end of file +} diff --git a/asg/tests/pass/function/mod.rs b/asg/tests/pass/function/mod.rs index 2d3241d9c7..456d5e7dd1 100644 --- a/asg/tests/pass/function/mod.rs +++ b/asg/tests/pass/function/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_empty() { diff --git a/asg/tests/pass/group/mod.rs b/asg/tests/pass/group/mod.rs index 874d676f2a..c800104bc9 100644 --- a/asg/tests/pass/group/mod.rs +++ b/asg/tests/pass/group/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use crate::load_asg; #[test] diff --git a/asg/tests/pass/import/mod.rs b/asg/tests/pass/import/mod.rs index 05fba7868c..41a84f8a3b 100644 --- a/asg/tests/pass/import/mod.rs +++ b/asg/tests/pass/import/mod.rs @@ -14,12 +14,31 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg_imports, load_asg, mocked_resolver}; +// Copyright (C) 2019-2020 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 . + +use crate::{load_asg, load_asg_imports, mocked_resolver}; #[test] fn test_basic() { let mut imports = mocked_resolver(); - imports.packages.insert("test-import".to_string(), load_asg(include_str!("src/test-import.leo")).unwrap()); + imports.packages.insert( + "test-import".to_string(), + load_asg(include_str!("src/test-import.leo")).unwrap(), + ); let program_string = include_str!("basic.leo"); load_asg_imports(program_string, &mut imports).unwrap(); } @@ -27,7 +46,10 @@ fn test_basic() { #[test] fn test_multiple() { let mut imports = mocked_resolver(); - imports.packages.insert("test-import".to_string(), load_asg(include_str!("src/test-import.leo")).unwrap()); + imports.packages.insert( + "test-import".to_string(), + load_asg(include_str!("src/test-import.leo")).unwrap(), + ); let program_string = include_str!("multiple.leo"); load_asg_imports(program_string, &mut imports).unwrap(); } @@ -35,7 +57,10 @@ fn test_multiple() { #[test] fn test_star() { let mut imports = mocked_resolver(); - imports.packages.insert("test-import".to_string(), load_asg(include_str!("src/test-import.leo")).unwrap()); + imports.packages.insert( + "test-import".to_string(), + load_asg(include_str!("src/test-import.leo")).unwrap(), + ); let program_string = include_str!("star.leo"); load_asg_imports(program_string, &mut imports).unwrap(); @@ -44,7 +69,10 @@ fn test_star() { #[test] fn test_alias() { let mut imports = mocked_resolver(); - imports.packages.insert("test-import".to_string(), load_asg(include_str!("src/test-import.leo")).unwrap()); + imports.packages.insert( + "test-import".to_string(), + load_asg(include_str!("src/test-import.leo")).unwrap(), + ); let program_string = include_str!("alias.leo"); load_asg_imports(program_string, &mut imports).unwrap(); @@ -54,9 +82,16 @@ fn test_alias() { #[test] fn test_name() { let mut imports = mocked_resolver(); - imports.packages.insert("hello-world".to_string(), load_asg(include_str!("src/hello-world.leo")).unwrap()); - imports.packages.insert("a0-f".to_string(), load_asg(include_str!("src/a0-f.leo")).unwrap()); - imports.packages.insert("a-9".to_string(), load_asg(include_str!("src/a-9.leo")).unwrap()); + imports.packages.insert( + "hello-world".to_string(), + load_asg(include_str!("src/hello-world.leo")).unwrap(), + ); + imports + .packages + .insert("a0-f".to_string(), load_asg(include_str!("src/a0-f.leo")).unwrap()); + imports + .packages + .insert("a-9".to_string(), load_asg(include_str!("src/a-9.leo")).unwrap()); let program_string = include_str!("names.leo"); load_asg_imports(program_string, &mut imports).unwrap(); @@ -66,12 +101,30 @@ fn test_name() { #[test] fn test_many_import() { let mut imports = mocked_resolver(); - imports.packages.insert("test-import".to_string(), load_asg(include_str!("src/test-import.leo")).unwrap()); - imports.packages.insert("bar".to_string(), load_asg(include_str!("imports/bar/src/lib.leo")).unwrap()); - imports.packages.insert("bar.baz".to_string(), load_asg(include_str!("imports/bar/src/baz.leo")).unwrap()); - imports.packages.insert("bar.baz".to_string(), load_asg(include_str!("imports/bar/src/baz.leo")).unwrap()); - imports.packages.insert("bar.bat.bat".to_string(), load_asg(include_str!("imports/bar/src/bat/bat.leo")).unwrap()); - imports.packages.insert("car".to_string(), load_asg(include_str!("imports/car/src/lib.leo")).unwrap()); + imports.packages.insert( + "test-import".to_string(), + load_asg(include_str!("src/test-import.leo")).unwrap(), + ); + imports.packages.insert( + "bar".to_string(), + load_asg(include_str!("imports/bar/src/lib.leo")).unwrap(), + ); + imports.packages.insert( + "bar.baz".to_string(), + load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(), + ); + imports.packages.insert( + "bar.baz".to_string(), + load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(), + ); + imports.packages.insert( + "bar.bat.bat".to_string(), + load_asg(include_str!("imports/bar/src/bat/bat.leo")).unwrap(), + ); + imports.packages.insert( + "car".to_string(), + load_asg(include_str!("imports/car/src/lib.leo")).unwrap(), + ); let program_string = include_str!("many_import.leo"); load_asg_imports(program_string, &mut imports).unwrap(); @@ -80,12 +133,30 @@ fn test_many_import() { #[test] fn test_many_import_star() { let mut imports = mocked_resolver(); - imports.packages.insert("test-import".to_string(), load_asg(include_str!("src/test-import.leo")).unwrap()); - imports.packages.insert("bar".to_string(), load_asg(include_str!("imports/bar/src/lib.leo")).unwrap()); - imports.packages.insert("bar.baz".to_string(), load_asg(include_str!("imports/bar/src/baz.leo")).unwrap()); - imports.packages.insert("bar.baz".to_string(), load_asg(include_str!("imports/bar/src/baz.leo")).unwrap()); - imports.packages.insert("bar.bat.bat".to_string(), load_asg(include_str!("imports/bar/src/bat/bat.leo")).unwrap()); - imports.packages.insert("car".to_string(), load_asg(include_str!("imports/car/src/lib.leo")).unwrap()); + imports.packages.insert( + "test-import".to_string(), + load_asg(include_str!("src/test-import.leo")).unwrap(), + ); + imports.packages.insert( + "bar".to_string(), + load_asg(include_str!("imports/bar/src/lib.leo")).unwrap(), + ); + imports.packages.insert( + "bar.baz".to_string(), + load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(), + ); + imports.packages.insert( + "bar.baz".to_string(), + load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(), + ); + imports.packages.insert( + "bar.bat.bat".to_string(), + load_asg(include_str!("imports/bar/src/bat/bat.leo")).unwrap(), + ); + imports.packages.insert( + "car".to_string(), + load_asg(include_str!("imports/car/src/lib.leo")).unwrap(), + ); let program_string = include_str!("many_import_star.leo"); load_asg_imports(program_string, &mut imports).unwrap(); diff --git a/asg/tests/pass/input_files/mod.rs b/asg/tests/pass/input_files/mod.rs index ddfd9aa108..71ce8415d9 100644 --- a/asg/tests/pass/input_files/mod.rs +++ b/asg/tests/pass/input_files/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + mod program_input; mod program_input_and_program_state; mod program_state; diff --git a/asg/tests/pass/input_files/program_input/mod.rs b/asg/tests/pass/input_files/program_input/mod.rs index 5b2e99927f..378b635fa7 100644 --- a/asg/tests/pass/input_files/program_input/mod.rs +++ b/asg/tests/pass/input_files/program_input/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_input_pass() { diff --git a/asg/tests/pass/input_files/program_input_and_program_state/mod.rs b/asg/tests/pass/input_files/program_input_and_program_state/mod.rs index 84e2cb12e0..195013cc7a 100644 --- a/asg/tests/pass/input_files/program_input_and_program_state/mod.rs +++ b/asg/tests/pass/input_files/program_input_and_program_state/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_access() { diff --git a/asg/tests/pass/input_files/program_state/mod.rs b/asg/tests/pass/input_files/program_state/mod.rs index b126a445c1..598a513f15 100644 --- a/asg/tests/pass/input_files/program_state/mod.rs +++ b/asg/tests/pass/input_files/program_state/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_access_state() { @@ -26,4 +42,4 @@ fn test_access_state() { fn test_access_all() { let program_string = include_str!("access_all.leo"); load_asg(program_string).unwrap(); -} \ No newline at end of file +} diff --git a/asg/tests/pass/integers/i128/mod.rs b/asg/tests/pass/integers/i128/mod.rs index c8a30f6398..522e9f2f10 100644 --- a/asg/tests/pass/integers/i128/mod.rs +++ b/asg/tests/pass/integers/i128/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI128 -); +test_int!(TestI128); #[test] fn test_i128_min() { diff --git a/asg/tests/pass/integers/i16/mod.rs b/asg/tests/pass/integers/i16/mod.rs index c5c454af3c..e5a329d18a 100644 --- a/asg/tests/pass/integers/i16/mod.rs +++ b/asg/tests/pass/integers/i16/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI16 -); +test_int!(TestI16); #[test] fn test_i16_min() { diff --git a/asg/tests/pass/integers/i32/mod.rs b/asg/tests/pass/integers/i32/mod.rs index 2568183146..a2f1012941 100644 --- a/asg/tests/pass/integers/i32/mod.rs +++ b/asg/tests/pass/integers/i32/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI32 -); +test_int!(TestI32); #[test] fn test_i32_min() { diff --git a/asg/tests/pass/integers/i64/mod.rs b/asg/tests/pass/integers/i64/mod.rs index 68a83371ff..e4cce13da3 100644 --- a/asg/tests/pass/integers/i64/mod.rs +++ b/asg/tests/pass/integers/i64/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI64 -); +test_int!(TestI64); #[test] fn test_i64_min() { diff --git a/asg/tests/pass/integers/i8/mod.rs b/asg/tests/pass/integers/i8/mod.rs index d9fddd3c89..225e7a3c55 100644 --- a/asg/tests/pass/integers/i8/mod.rs +++ b/asg/tests/pass/integers/i8/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_int!( - TestI8 -); +test_int!(TestI8); #[test] fn test_i8_min() { diff --git a/asg/tests/pass/integers/int_macro.rs b/asg/tests/pass/integers/int_macro.rs index bc3649ede5..734b1773e5 100644 --- a/asg/tests/pass/integers/int_macro.rs +++ b/asg/tests/pass/integers/int_macro.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + macro_rules! test_int { ($name: ident) => { pub struct $name {} diff --git a/asg/tests/pass/integers/integer_tester.rs b/asg/tests/pass/integers/integer_tester.rs index 62d0712de6..f72930b8d0 100644 --- a/asg/tests/pass/integers/integer_tester.rs +++ b/asg/tests/pass/integers/integer_tester.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + pub trait IntegerTester { /// Tests defining the smalled value that can be represented by the integer type fn test_min(); diff --git a/asg/tests/pass/integers/mod.rs b/asg/tests/pass/integers/mod.rs index 3aa0c691c8..d9fc896497 100644 --- a/asg/tests/pass/integers/mod.rs +++ b/asg/tests/pass/integers/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + #[macro_use] pub mod int_macro; diff --git a/asg/tests/pass/integers/u128/mod.rs b/asg/tests/pass/integers/u128/mod.rs index 9ce1743c27..f025e85ba5 100644 --- a/asg/tests/pass/integers/u128/mod.rs +++ b/asg/tests/pass/integers/u128/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU128 -); +test_uint!(TestU128); #[test] fn test_u128_min() { diff --git a/asg/tests/pass/integers/u16/mod.rs b/asg/tests/pass/integers/u16/mod.rs index 3d45380a00..da4af3c797 100644 --- a/asg/tests/pass/integers/u16/mod.rs +++ b/asg/tests/pass/integers/u16/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU16 -); +test_uint!(TestU16); #[test] fn test_u16_min() { diff --git a/asg/tests/pass/integers/u32/mod.rs b/asg/tests/pass/integers/u32/mod.rs index b860e5f865..64668acff5 100644 --- a/asg/tests/pass/integers/u32/mod.rs +++ b/asg/tests/pass/integers/u32/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU32 -); +test_uint!(TestU32); #[test] fn test_u32_min() { diff --git a/asg/tests/pass/integers/u64/mod.rs b/asg/tests/pass/integers/u64/mod.rs index fbb57a74cc..81ef097ce7 100644 --- a/asg/tests/pass/integers/u64/mod.rs +++ b/asg/tests/pass/integers/u64/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU64 -); +test_uint!(TestU64); #[test] fn test_u64_min() { diff --git a/asg/tests/pass/integers/u8/mod.rs b/asg/tests/pass/integers/u8/mod.rs index 417c993789..ee298c8f42 100644 --- a/asg/tests/pass/integers/u8/mod.rs +++ b/asg/tests/pass/integers/u8/mod.rs @@ -14,11 +14,25 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + use super::IntegerTester; -test_uint!( - TestU8 -); +test_uint!(TestU8); #[test] fn test_u8_min() { diff --git a/asg/tests/pass/integers/uint_macro.rs b/asg/tests/pass/integers/uint_macro.rs index 064323874a..7ca8ba3325 100644 --- a/asg/tests/pass/integers/uint_macro.rs +++ b/asg/tests/pass/integers/uint_macro.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + macro_rules! test_uint { ($name: ident) => { pub struct $name {} diff --git a/asg/tests/pass/mod.rs b/asg/tests/pass/mod.rs index 5c032d920d..fa986b91c0 100644 --- a/asg/tests/pass/mod.rs +++ b/asg/tests/pass/mod.rs @@ -14,6 +14,22 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +// Copyright (C) 2019-2020 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 . + pub mod address; pub mod array; pub mod boolean; @@ -22,6 +38,7 @@ pub mod console; pub mod core; pub mod definition; pub mod field; +pub mod form_ast; pub mod function; pub mod group; pub mod import; @@ -30,4 +47,3 @@ pub mod integers; pub mod mutability; pub mod statements; pub mod tuples; -pub mod form_ast; diff --git a/asg/tests/pass/mutability/mod.rs b/asg/tests/pass/mutability/mod.rs index 77065ec537..46d53de5ca 100644 --- a/asg/tests/pass/mutability/mod.rs +++ b/asg/tests/pass/mutability/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_let_mut() { diff --git a/asg/tests/pass/statements/conditional/mod.rs b/asg/tests/pass/statements/conditional/mod.rs index 80406598bd..2d43bda21a 100644 --- a/asg/tests/pass/statements/conditional/mod.rs +++ b/asg/tests/pass/statements/conditional/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_assert() { diff --git a/asg/tests/pass/statements/mod.rs b/asg/tests/pass/statements/mod.rs index 93f8d2e20d..bd6ed29563 100644 --- a/asg/tests/pass/statements/mod.rs +++ b/asg/tests/pass/statements/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; pub mod conditional; diff --git a/asg/tests/pass/tuples/mod.rs b/asg/tests/pass/tuples/mod.rs index 8a4fafeaec..00fad01a43 100644 --- a/asg/tests/pass/tuples/mod.rs +++ b/asg/tests/pass/tuples/mod.rs @@ -14,7 +14,23 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{load_asg}; +// Copyright (C) 2019-2020 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 . + +use crate::load_asg; #[test] fn test_tuple_basic() { diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 8decf7bf29..136b54527f 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -29,13 +29,13 @@ use leo_input::LeoInputParser; use leo_package::inputs::InputPairs; use leo_state::verify_local_data_commitment; +use leo_asg::Program as AsgProgram; use snarkvm_dpc::{base_dpc::instantiated::Components, SystemParameters}; use snarkvm_errors::gadgets::SynthesisError; use snarkvm_models::{ curves::{Field, PrimeField}, gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem}, }; -use leo_asg::Program as AsgProgram; use sha2::{Digest, Sha256}; use std::{ @@ -274,13 +274,11 @@ impl> Compiler { pub fn compile_constraints>(self, cs: &mut CS) -> Result { let path = self.main_file_path; - generate_constraints::(cs, self.asg.as_ref().unwrap(), &self.program_input).map_err( - |mut error| { - error.set_path(&path); + generate_constraints::(cs, self.asg.as_ref().unwrap(), &self.program_input).map_err(|mut error| { + error.set_path(&path); - error - }, - ) + error + }) } /// @@ -303,12 +301,10 @@ impl> Compiler { cs: &mut CS, ) -> Result { let path = &self.main_file_path; - generate_constraints::<_, G, _>(cs, self.asg.as_ref().unwrap(), &self.program_input).map_err( - |mut error| { - error.set_path(&path); - error - }, - ) + generate_constraints::<_, G, _>(cs, self.asg.as_ref().unwrap(), &self.program_input).map_err(|mut error| { + error.set_path(&path); + error + }) } } diff --git a/compiler/src/console/console.rs b/compiler/src/console/console.rs index cde0c2f21d..01ad288256 100644 --- a/compiler/src/console/console.rs +++ b/compiler/src/console/console.rs @@ -35,7 +35,14 @@ impl> ConstrainedProgram { ) -> Result<(), ConsoleError> { match &console.function { ConsoleFunction::Assert(expression) => { - self.evaluate_console_assert(cs, file_scope, function_scope, indicator, expression, &console.span.clone().unwrap_or_default())?; + self.evaluate_console_assert( + cs, + file_scope, + function_scope, + indicator, + expression, + &console.span.clone().unwrap_or_default(), + )?; } ConsoleFunction::Debug(string) => { let string = self.format(cs, file_scope, function_scope, string)?; diff --git a/compiler/src/constraints/constraints.rs b/compiler/src/constraints/constraints.rs index 9f2e4060ac..67ef11f462 100644 --- a/compiler/src/constraints/constraints.rs +++ b/compiler/src/constraints/constraints.rs @@ -16,15 +16,9 @@ //! Generates R1CS constraints for a compiled Leo program. -use crate::{ - errors::CompilerError, - ConstrainedProgram, - GroupType, - OutputBytes, - OutputFile, -}; -use leo_ast::{Input}; +use crate::{errors::CompilerError, ConstrainedProgram, GroupType, OutputBytes, OutputFile}; use leo_asg::Program; +use leo_ast::Input; use leo_input::LeoInputParser; use leo_package::inputs::InputPairs; diff --git a/compiler/src/definition/definition.rs b/compiler/src/definition/definition.rs index 71a98cca08..8bf6bd7077 100644 --- a/compiler/src/definition/definition.rs +++ b/compiler/src/definition/definition.rs @@ -16,22 +16,13 @@ //! Stores all defined names in a compiled Leo program. -use crate::{ - program::{ConstrainedProgram}, - value::ConstrainedValue, - GroupType, -}; +use crate::{program::ConstrainedProgram, value::ConstrainedValue, GroupType}; use leo_asg::Variable; use snarkvm_models::curves::{Field, PrimeField}; impl> ConstrainedProgram { - pub fn store_definition( - &mut self, - function_scope: &str, - variable: &Variable, - mut value: ConstrainedValue, - ) { + pub fn store_definition(&mut self, function_scope: &str, variable: &Variable, mut value: ConstrainedValue) { let variable = variable.borrow(); // Store with given mutability if variable.mutable { diff --git a/compiler/src/errors/statement.rs b/compiler/src/errors/statement.rs index a6e5238a10..26844973b0 100644 --- a/compiler/src/errors/statement.rs +++ b/compiler/src/errors/statement.rs @@ -15,8 +15,8 @@ // along with the Leo library. If not, see . use crate::errors::{AddressError, BooleanError, ConsoleError, ExpressionError, IntegerError, ValueError}; -use leo_ast::{Error as FormattedError, Span}; use leo_asg::Type; +use leo_ast::{Error as FormattedError, Span}; use std::path::Path; diff --git a/compiler/src/expression/array/array.rs b/compiler/src/expression/array/array.rs index a89b394963..aa00eb19a8 100644 --- a/compiler/src/expression/array/array.rs +++ b/compiler/src/expression/array/array.rs @@ -16,12 +16,7 @@ //! Enforces an array expression in a compiled Leo program. -use crate::{ - errors::ExpressionError, - program::{ConstrainedProgram}, - value::ConstrainedValue, - GroupType, -}; +use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; use leo_asg::{Expression, Span}; use std::sync::Arc; @@ -44,12 +39,7 @@ impl> ConstrainedProgram { let mut result = vec![]; for (element, is_spread) in array.into_iter() { - let element_value = self.enforce_expression( - cs, - file_scope, - function_scope, - element, - )?; + let element_value = self.enforce_expression(cs, file_scope, function_scope, element)?; if *is_spread { match element_value { ConstrainedValue::Array(array) => result.extend(array), @@ -84,8 +74,7 @@ impl> ConstrainedProgram { actual_size: usize, span: Span, ) -> Result, ExpressionError> { - let mut value = - self.enforce_expression(cs, file_scope, function_scope, element_expression)?; + let mut value = self.enforce_expression(cs, file_scope, function_scope, element_expression)?; // Allocate the array. let array = vec![value; actual_size]; diff --git a/compiler/src/expression/binary/binary.rs b/compiler/src/expression/binary/binary.rs index 5c0662ab59..25bf78b015 100644 --- a/compiler/src/expression/binary/binary.rs +++ b/compiler/src/expression/binary/binary.rs @@ -39,10 +39,8 @@ impl> ConstrainedProgram { span: &Span, ) -> Result, ExpressionError> { //todo: what are these expected typeS??? - let mut resolved_left = - self.enforce_operand(cs, file_scope, function_scope, left)?; - let mut resolved_right = - self.enforce_operand(cs, file_scope, function_scope, right)?; + let mut resolved_left = self.enforce_operand(cs, file_scope, function_scope, left)?; + let mut resolved_right = self.enforce_operand(cs, file_scope, function_scope, right)?; Ok((resolved_left, resolved_right)) } diff --git a/compiler/src/expression/binary/operand.rs b/compiler/src/expression/binary/operand.rs index 8fb3a20f2c..4956cdb62b 100644 --- a/compiler/src/expression/binary/operand.rs +++ b/compiler/src/expression/binary/operand.rs @@ -17,7 +17,7 @@ //! Enforces one operand in a binary expression in a compiled Leo program. use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; -use leo_asg::{Expression}; +use leo_asg::Expression; use std::sync::Arc; use snarkvm_models::{ diff --git a/compiler/src/expression/circuit/access.rs b/compiler/src/expression/circuit/access.rs index 59a2b720f3..2fc812f57d 100644 --- a/compiler/src/expression/circuit/access.rs +++ b/compiler/src/expression/circuit/access.rs @@ -16,13 +16,8 @@ //! Enforces a circuit access expression in a compiled Leo program. -use crate::{ - errors::ExpressionError, - program::{ConstrainedProgram}, - value::ConstrainedValue, - GroupType, -}; -use leo_asg::{CircuitAccessExpression, Span, Node}; +use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; +use leo_asg::{CircuitAccessExpression, Node, Span}; use snarkvm_models::{ curves::{Field, PrimeField}, @@ -54,11 +49,17 @@ impl> ConstrainedProgram { expr.member.span.clone(), )) } - }, - value => Err(ExpressionError::undefined_circuit(value.to_string(), target.span().cloned().unwrap_or_default())), + } + value => Err(ExpressionError::undefined_circuit( + value.to_string(), + target.span().cloned().unwrap_or_default(), + )), } } else { - Err(ExpressionError::invalid_static_access(expr.member.to_string(), expr.member.span.clone())) + Err(ExpressionError::invalid_static_access( + expr.member.to_string(), + expr.member.span.clone(), + )) } } } diff --git a/compiler/src/expression/circuit/circuit.rs b/compiler/src/expression/circuit/circuit.rs index 1195f15aae..c6abf3f09e 100644 --- a/compiler/src/expression/circuit/circuit.rs +++ b/compiler/src/expression/circuit/circuit.rs @@ -18,7 +18,7 @@ use crate::{ errors::ExpressionError, - program::{ConstrainedProgram}, + program::ConstrainedProgram, value::{ConstrainedCircuitMember, ConstrainedValue}, GroupType, }; @@ -41,7 +41,12 @@ impl> ConstrainedProgram { // Circuit definitions are located at the minimum file scope let minimum_scope = file_scope.split('_').next().unwrap(); - let circuit = expr.circuit.body.borrow().upgrade().expect("circuit init stale circuit ref"); + let circuit = expr + .circuit + .body + .borrow() + .upgrade() + .expect("circuit init stale circuit ref"); let members = circuit.members.borrow(); let circuit_identifier = expr.circuit.name.borrow().clone(); @@ -49,25 +54,19 @@ impl> ConstrainedProgram { // type checking is already done in asg for (name, inner) in expr.values.iter() { - let target = members.get(&name.name).expect("illegal name in asg circuit init expression"); + let target = members + .get(&name.name) + .expect("illegal name in asg circuit init expression"); match target { CircuitMemberBody::Variable(_type_) => { - let variable_value = self.enforce_expression( - cs, - file_scope, - function_scope, - inner, - )?; + let variable_value = self.enforce_expression(cs, file_scope, function_scope, inner)?; resolved_members.push(ConstrainedCircuitMember(name.clone(), variable_value)); } - _ => return Err(ExpressionError::expected_circuit_member(name.to_string(), span.clone())) + _ => return Err(ExpressionError::expected_circuit_member(name.to_string(), span.clone())), } } - let value = ConstrainedValue::CircuitExpression( - circuit.clone(), - resolved_members, - ); + let value = ConstrainedValue::CircuitExpression(circuit.clone(), resolved_members); Ok(value) } } diff --git a/compiler/src/expression/conditional/conditional.rs b/compiler/src/expression/conditional/conditional.rs index 039680c1ac..84d7fa164c 100644 --- a/compiler/src/expression/conditional/conditional.rs +++ b/compiler/src/expression/conditional/conditional.rs @@ -38,11 +38,10 @@ impl> ConstrainedProgram { second: &Arc, span: &Span, ) -> Result, ExpressionError> { - let conditional_value = - match self.enforce_expression(cs, file_scope, function_scope, conditional)? { - ConstrainedValue::Boolean(resolved) => resolved, - value => return Err(ExpressionError::conditional_boolean(value.to_string(), span.to_owned())), - }; + let conditional_value = match self.enforce_expression(cs, file_scope, function_scope, conditional)? { + ConstrainedValue::Boolean(resolved) => resolved, + value => return Err(ExpressionError::conditional_boolean(value.to_string(), span.to_owned())), + }; let first_value = self.enforce_operand(cs, file_scope, function_scope, first)?; diff --git a/compiler/src/expression/expression.rs b/compiler/src/expression/expression.rs index 614a205012..0caa7b7c8b 100644 --- a/compiler/src/expression/expression.rs +++ b/compiler/src/expression/expression.rs @@ -22,17 +22,16 @@ use crate::{ logical::*, program::ConstrainedProgram, relational::*, + value::{Address, ConstrainedValue, Integer}, FieldType, - value::{ConstrainedValue, Address, Integer}, GroupType, }; -use leo_asg::{expression::*, Expression, ConstValue, Node}; +use leo_asg::{expression::*, ConstValue, Expression, Node}; use std::sync::Arc; use snarkvm_models::{ curves::{Field, PrimeField}, - gadgets::r1cs::ConstraintSystem, - gadgets::utilities::boolean::Boolean, + gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean}, }; impl> ConstrainedProgram { @@ -46,9 +45,7 @@ impl> ConstrainedProgram { let span = expression.span().cloned().unwrap_or_default(); match &**expression { // Variables - Expression::VariableRef(variable_ref) => { - self.evaluate_ref(file_scope, function_scope, variable_ref) - } + Expression::VariableRef(variable_ref) => self.evaluate_ref(file_scope, function_scope, variable_ref), // Values Expression::Constant(Constant { value, .. }) => { @@ -63,15 +60,11 @@ impl> ConstrainedProgram { } // Binary operations - Expression::Binary(BinaryExpression { left, right, operation, .. }) => { - let (resolved_left, resolved_right) = self.enforce_binary_expression( - cs, - file_scope, - function_scope, - left, - right, - &span, - )?; + Expression::Binary(BinaryExpression { + left, right, operation, .. + }) => { + let (resolved_left, resolved_right) = + self.enforce_binary_expression(cs, file_scope, function_scope, left, right, &span)?; match operation { BinaryOperation::Add => enforce_add(cs, resolved_left, resolved_right, &span), @@ -98,8 +91,7 @@ impl> ConstrainedProgram { // Unary operations Expression::Unary(UnaryExpression { inner, operation, .. }) => match operation { UnaryOperation::Negate => { - let resolved_inner = - self.enforce_expression(cs, file_scope, function_scope, inner)?; + let resolved_inner = self.enforce_expression(cs, file_scope, function_scope, inner)?; enforce_negate(cs, resolved_inner, &span) } UnaryOperation::Not => Ok(evaluate_not( @@ -113,49 +105,30 @@ impl> ConstrainedProgram { if_true, if_false, .. - }) => self.enforce_conditional_expression( - cs, - file_scope, - function_scope, - condition, - if_true, - if_false, - &span, - ), + }) => { + self.enforce_conditional_expression(cs, file_scope, function_scope, condition, if_true, if_false, &span) + } // Arrays Expression::ArrayInline(ArrayInlineExpression { elements, .. }) => { self.enforce_array(cs, file_scope, function_scope, elements, span) } - Expression::ArrayInit(ArrayInitExpression { - element, - len, - .. - }) => self.enforce_array_initializer( - cs, - file_scope, - function_scope, - element, - *len, - span, - ), + Expression::ArrayInit(ArrayInitExpression { element, len, .. }) => { + self.enforce_array_initializer(cs, file_scope, function_scope, element, *len, span) + } Expression::ArrayAccess(ArrayAccessExpression { array, index, .. }) => { self.enforce_array_access(cs, file_scope, function_scope, array, index, &span) } - Expression::ArrayRangeAccess(ArrayRangeAccessExpression { - array, - left, - right, - .. - }) => self.enforce_array_range_access( - cs, - file_scope, - function_scope, - array, - left.as_ref(), - right.as_ref(), - &span, - ), + Expression::ArrayRangeAccess(ArrayRangeAccessExpression { array, left, right, .. }) => self + .enforce_array_range_access( + cs, + file_scope, + function_scope, + array, + left.as_ref(), + right.as_ref(), + &span, + ), // Tuples Expression::TupleInit(TupleInitExpression { elements, .. }) => { @@ -166,12 +139,8 @@ impl> ConstrainedProgram { } // Circuits - Expression::CircuitInit(expr) => { - self.enforce_circuit(cs, file_scope, function_scope, expr, &span) - } - Expression::CircuitAccess(expr) => { - self.enforce_circuit_access(cs, file_scope, function_scope, expr, &span) - } + Expression::CircuitInit(expr) => self.enforce_circuit(cs, file_scope, function_scope, expr, &span), + Expression::CircuitAccess(expr) => self.enforce_circuit_access(cs, file_scope, function_scope, expr, &span), // Functions Expression::Call(CallExpression { diff --git a/compiler/src/expression/function/core_circuit.rs b/compiler/src/expression/function/core_circuit.rs index 5dcc97cc17..716f23bbaf 100644 --- a/compiler/src/expression/function/core_circuit.rs +++ b/compiler/src/expression/function/core_circuit.rs @@ -15,7 +15,7 @@ // along with the Leo library. If not, see . use crate::{program::ConstrainedProgram, value::ConstrainedValue, GroupType}; -use crate::errors::{ExpressionError}; +use crate::errors::ExpressionError; use leo_asg::{Expression, Span}; use leo_core::call_core_circuit; use snarkvm_models::{ diff --git a/compiler/src/expression/function/function.rs b/compiler/src/expression/function/function.rs index fff8c1036c..fc9b3fb3c9 100644 --- a/compiler/src/expression/function/function.rs +++ b/compiler/src/expression/function/function.rs @@ -17,7 +17,7 @@ //! Enforce a function call expression in a compiled Leo program. use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; -use leo_asg::{Expression, Span, Function}; +use leo_asg::{Expression, Function, Span}; use std::sync::Arc; use snarkvm_models::{ @@ -37,7 +37,6 @@ impl> ConstrainedProgram { arguments: &Vec>, span: &Span, ) -> Result, ExpressionError> { - let name_unique = || { format!( "function call {} {}:{}", @@ -46,7 +45,11 @@ impl> ConstrainedProgram { span.start, ) }; - let function = function.body.borrow().upgrade().expect("stale function in call expression"); + let function = function + .body + .borrow() + .upgrade() + .expect("stale function in call expression"); let target = if let Some(target) = target { Some(self.enforce_expression(cs, file_scope, function_scope, target)?) @@ -55,9 +58,13 @@ impl> ConstrainedProgram { }; let old_self_alias = self.self_alias.take(); - // self.self_alias = + // self.self_alias = if let Some(target) = &target { - let self_var = function.scope.borrow().resolve_variable("self").expect("attempted to call static function from non-static context"); + let self_var = function + .scope + .borrow() + .resolve_variable("self") + .expect("attempted to call static function from non-static context"); self.store(self_var.borrow().id.clone(), target.clone()); // match target { // ConstrainedValue::CircuitExpression(circuit, values) => { @@ -70,19 +77,19 @@ impl> ConstrainedProgram { // } else { // None // }; - - //todo: mut self + //todo: mut self - let return_value = self.enforce_function( - &mut cs.ns(name_unique), - file_scope, - function_scope, - &function, - target, - arguments, - ) - .map_err(|error| ExpressionError::from(Box::new(error)))?; + let return_value = self + .enforce_function( + &mut cs.ns(name_unique), + file_scope, + function_scope, + &function, + target, + arguments, + ) + .map_err(|error| ExpressionError::from(Box::new(error)))?; self.self_alias = old_self_alias; Ok(return_value) diff --git a/compiler/src/expression/tuple/access.rs b/compiler/src/expression/tuple/access.rs index c1022778c7..4c635ee9e0 100644 --- a/compiler/src/expression/tuple/access.rs +++ b/compiler/src/expression/tuple/access.rs @@ -43,7 +43,8 @@ impl> ConstrainedProgram { }; // Check for out of bounds access. - if index > tuple.len() - 1 { // probably safe to be a panic here + if index > tuple.len() - 1 { + // probably safe to be a panic here return Err(ExpressionError::index_out_of_bounds(index, span.to_owned())); } diff --git a/compiler/src/expression/tuple/tuple.rs b/compiler/src/expression/tuple/tuple.rs index 73b79499ab..3cf4f49ad7 100644 --- a/compiler/src/expression/tuple/tuple.rs +++ b/compiler/src/expression/tuple/tuple.rs @@ -37,7 +37,6 @@ impl> ConstrainedProgram { ) -> Result, ExpressionError> { let mut result = Vec::with_capacity(tuple.len()); for (i, expression) in tuple.iter().enumerate() { - result.push(self.enforce_expression(cs, file_scope, function_scope, expression)?); } diff --git a/compiler/src/expression/variable_ref/variable_ref.rs b/compiler/src/expression/variable_ref/variable_ref.rs index 412b155802..cbcce7660c 100644 --- a/compiler/src/expression/variable_ref/variable_ref.rs +++ b/compiler/src/expression/variable_ref/variable_ref.rs @@ -16,13 +16,8 @@ //! Enforces an identifier expression in a compiled Leo program. -use crate::{ - errors::ExpressionError, - program::{ConstrainedProgram}, - value::ConstrainedValue, - GroupType, -}; -use leo_asg::{VariableRef}; +use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; +use leo_asg::VariableRef; use snarkvm_models::curves::{Field, PrimeField}; diff --git a/compiler/src/function/function.rs b/compiler/src/function/function.rs index 12ccfe28a0..d6551fa9c9 100644 --- a/compiler/src/function/function.rs +++ b/compiler/src/function/function.rs @@ -73,19 +73,16 @@ impl> ConstrainedProgram { // FunctionQualifier::Static => (), // } if function.arguments.len() != arguments.len() { - return Err(FunctionError::input_not_found("arguments length invalid".to_string(), function.span.clone().unwrap_or_default())); + return Err(FunctionError::input_not_found( + "arguments length invalid".to_string(), + function.span.clone().unwrap_or_default(), + )); } // Store input values as new variables in resolved program for (variable, input_expression) in function.arguments.iter().zip(arguments.into_iter()) { - - let mut input_value = self.enforce_function_input( - cs, - scope, - caller_scope, - &function_name, - input_expression, - )?; + let mut input_value = + self.enforce_function_input(cs, scope, caller_scope, &function_name, input_expression)?; let variable = variable.borrow(); if variable.mutable { @@ -101,14 +98,7 @@ impl> ConstrainedProgram { let output = function.function.output.clone().strong(); - let mut result = self.enforce_statement( - cs, - scope, - &function_name, - &indicator, - &function.body, - mut_self, - )?; + let mut result = self.enforce_statement(cs, scope, &function_name, &indicator, &function.body, mut_self)?; results.append(&mut result); diff --git a/compiler/src/function/input/array.rs b/compiler/src/function/input/array.rs index 7f5aec6fee..af9761abae 100644 --- a/compiler/src/function/input/array.rs +++ b/compiler/src/function/input/array.rs @@ -23,8 +23,8 @@ use crate::{ GroupType, }; -use leo_ast::{InputValue, Span}; use leo_asg::Type; +use leo_ast::{InputValue, Span}; use snarkvm_models::{ curves::{Field, PrimeField}, @@ -41,7 +41,6 @@ impl> ConstrainedProgram { input_value: Option, span: &Span, ) -> Result, FunctionError> { - // Build the array value using the expected types. let mut array_value = vec![]; @@ -65,13 +64,7 @@ impl> ConstrainedProgram { for i in 0..array_len { let value_name = new_scope(&name, &i.to_string()); - array_value.push(self.allocate_main_function_input( - cs, - array_type, - &value_name, - None, - span, - )?); + array_value.push(self.allocate_main_function_input(cs, array_type, &value_name, None, span)?); } } _ => { diff --git a/compiler/src/function/input/function_input.rs b/compiler/src/function/input/function_input.rs index 96cedf05a2..1a4ae9ab62 100644 --- a/compiler/src/function/input/function_input.rs +++ b/compiler/src/function/input/function_input.rs @@ -18,7 +18,7 @@ use crate::{errors::FunctionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; -use leo_asg::{Expression}; +use leo_asg::Expression; use std::sync::Arc; use snarkvm_models::{ diff --git a/compiler/src/function/input/input_keyword.rs b/compiler/src/function/input/input_keyword.rs index 1b72f07726..93c6d78aa2 100644 --- a/compiler/src/function/input/input_keyword.rs +++ b/compiler/src/function/input/input_keyword.rs @@ -15,8 +15,8 @@ // along with the Leo library. If not, see . use crate::{errors::FunctionError, ConstrainedCircuitMember, ConstrainedProgram, ConstrainedValue, GroupType}; -use leo_ast::{Identifier, Input, Span}; use leo_asg::{CircuitBody, CircuitMemberBody, Type}; +use leo_ast::{Identifier, Input, Span}; use std::sync::Arc; use snarkvm_models::{ @@ -76,12 +76,14 @@ impl> ConstrainedProgram { for (name, values) in sections { let sub_circuit = match expected_type.members.borrow().get(&name.name) { - Some(CircuitMemberBody::Variable(Type::Circuit(circuit))) => { - circuit.body.borrow().upgrade().expect("stale circuit body for input subtype") - }, + Some(CircuitMemberBody::Variable(Type::Circuit(circuit))) => circuit + .body + .borrow() + .upgrade() + .expect("stale circuit body for input subtype"), _ => panic!("illegal input type definition from asg"), }; - + let member_name = name.clone(); let member_value = self.allocate_input_section(cs, name, sub_circuit, values)?; diff --git a/compiler/src/function/input/input_section.rs b/compiler/src/function/input/input_section.rs index 79f423f305..6b57d54062 100644 --- a/compiler/src/function/input/input_section.rs +++ b/compiler/src/function/input/input_section.rs @@ -14,8 +14,8 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ConstrainedCircuitMember, ConstrainedProgram, ConstrainedValue, GroupType, errors::{FunctionError}}; -use leo_asg::{CircuitBody, CircuitMemberBody, AsgConvertError}; +use crate::{errors::FunctionError, ConstrainedCircuitMember, ConstrainedProgram, ConstrainedValue, GroupType}; +use leo_asg::{AsgConvertError, CircuitBody, CircuitMemberBody}; use leo_ast::{Identifier, InputValue, Parameter}; use std::sync::Arc; @@ -46,7 +46,11 @@ impl> ConstrainedProgram { }; let declared_type = self.asg.borrow().scope.borrow().resolve_ast_type(¶meter.type_)?; if !expected_type.is_assignable_from(&declared_type) { - Err(AsgConvertError::unexpected_type(&expected_type.to_string(), Some(&declared_type.to_string()), &identifier.span))?; + Err(AsgConvertError::unexpected_type( + &expected_type.to_string(), + Some(&declared_type.to_string()), + &identifier.span, + ))?; } let member_name = parameter.variable.clone(); let member_value = self.allocate_main_function_input( diff --git a/compiler/src/function/input/main_function_input.rs b/compiler/src/function/input/main_function_input.rs index 1bd95bd8ec..cf30aed515 100644 --- a/compiler/src/function/input/main_function_input.rs +++ b/compiler/src/function/input/main_function_input.rs @@ -30,8 +30,8 @@ use crate::{ Integer, }; -use leo_ast::{InputValue, Span}; use leo_asg::Type; +use leo_ast::{InputValue, Span}; use snarkvm_models::{ curves::{Field, PrimeField}, gadgets::r1cs::ConstraintSystem, diff --git a/compiler/src/function/input/tuple.rs b/compiler/src/function/input/tuple.rs index 575c69dea8..e4b1c9abeb 100644 --- a/compiler/src/function/input/tuple.rs +++ b/compiler/src/function/input/tuple.rs @@ -23,8 +23,8 @@ use crate::{ GroupType, }; -use leo_ast::{InputValue, Span}; use leo_asg::Type; +use leo_ast::{InputValue, Span}; use snarkvm_models::{ curves::{Field, PrimeField}, diff --git a/compiler/src/function/main_function.rs b/compiler/src/function/main_function.rs index 17bb019c35..b3b092b925 100644 --- a/compiler/src/function/main_function.rs +++ b/compiler/src/function/main_function.rs @@ -46,30 +46,45 @@ impl> ConstrainedProgram { // Iterate over main function input variables and allocate new values if function.function.has_input { // let input_var = function.scope. - let asg_input = function.scope.borrow().resolve_input().expect("no input variable in scope when function is qualified"); - - let value = self.allocate_input_keyword(cs, function.function.name.borrow().span.clone(), &asg_input.container_circuit, input)?; + let asg_input = function + .scope + .borrow() + .resolve_input() + .expect("no input variable in scope when function is qualified"); + + let value = self.allocate_input_keyword( + cs, + function.function.name.borrow().span.clone(), + &asg_input.container_circuit, + input, + )?; self.store(asg_input.container.borrow().id.clone(), value); } - match function.function.qualifier { - FunctionQualifier::SelfRef | FunctionQualifier::MutSelfRef => unimplemented!("cannot access self variable in main function"), + FunctionQualifier::SelfRef | FunctionQualifier::MutSelfRef => { + unimplemented!("cannot access self variable in main function") + } FunctionQualifier::Static => (), } let mut arguments = vec![]; - + for input_variable in function.arguments.iter() { { let input_variable = input_variable.borrow(); let name = input_variable.name.name.clone(); - let input_option = input - .get(&name) - .ok_or_else(|| FunctionError::input_not_found(name.clone(), function.span.clone().unwrap_or_default()))?; - let input_value = - self.allocate_main_function_input(cs, &input_variable.type_, &name, input_option, &function.span.clone().unwrap_or_default())?; + let input_option = input.get(&name).ok_or_else(|| { + FunctionError::input_not_found(name.clone(), function.span.clone().unwrap_or_default()) + })?; + let input_value = self.allocate_main_function_input( + cs, + &input_variable.type_, + &name, + input_option, + &function.span.clone().unwrap_or_default(), + )?; // Store a new variable for every allocated main function input self.store(input_variable.id.clone(), input_value); diff --git a/compiler/src/function/result/result.rs b/compiler/src/function/result/result.rs index 7e5c21e05f..0fb2a77cd1 100644 --- a/compiler/src/function/result/result.rs +++ b/compiler/src/function/result/result.rs @@ -66,7 +66,11 @@ impl> ConstrainedProgram { // Error if a statement returned a result with an incorrect type let result_type = result.to_type(span)?; if !expected_return.is_assignable_from(&result_type) { - panic!("failed type resolution for function return: expected '{}', got '{}'", expected_return.to_string(), result_type.to_string()); + panic!( + "failed type resolution for function return: expected '{}', got '{}'", + expected_return.to_string(), + result_type.to_string() + ); } if get_indicator_value(&indicator) { diff --git a/compiler/src/program/program.rs b/compiler/src/program/program.rs index 37733070d3..234c631758 100644 --- a/compiler/src/program/program.rs +++ b/compiler/src/program/program.rs @@ -18,8 +18,8 @@ use crate::{value::ConstrainedValue, GroupType}; -use snarkvm_models::curves::{Field, PrimeField}; use leo_asg::Program; +use snarkvm_models::curves::{Field, PrimeField}; use indexmap::IndexMap; use uuid::Uuid; @@ -59,7 +59,7 @@ impl> ConstrainedProgram { pub(crate) fn get(&self, name: &Uuid) -> Option<&ConstrainedValue> { if let Some((from, to)) = &self.self_alias { if name == from { - return self.identifiers.get(to) + return self.identifiers.get(to); } } self.identifiers.get(name) diff --git a/compiler/src/stage.rs b/compiler/src/stage.rs index bf956666c7..8278085e38 100644 --- a/compiler/src/stage.rs +++ b/compiler/src/stage.rs @@ -2,4 +2,4 @@ use leo_asg::Program; pub trait ASGStage { fn apply(asg: &mut Program); -} \ No newline at end of file +} diff --git a/compiler/src/statement/assign/assign.rs b/compiler/src/statement/assign/assign.rs index dbb2b66f94..11a3ef9e33 100644 --- a/compiler/src/statement/assign/assign.rs +++ b/compiler/src/statement/assign/assign.rs @@ -16,13 +16,7 @@ //! Enforces an assign statement in a compiled Leo program. -use crate::{ - arithmetic::*, - errors::StatementError, - program::ConstrainedProgram, - value::ConstrainedValue, - GroupType, -}; +use crate::{arithmetic::*, errors::StatementError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; use leo_asg::{AssignOperation, AssignStatement, Span}; use snarkvm_models::{ @@ -46,13 +40,7 @@ impl> ConstrainedProgram { ) -> Result<(), StatementError> { // Get the name of the variable we are assigning to let mut new_value = self.enforce_expression(cs, file_scope, function_scope, &statement.value)?; - let mut resolved_assignee = self.resolve_assign( - cs, - file_scope, - function_scope, - mut_self, - statement, - )?; + let mut resolved_assignee = self.resolve_assign(cs, file_scope, function_scope, mut_self, statement)?; if resolved_assignee.len() == 1 { let span = statement.span.clone().unwrap_or_default(); @@ -85,7 +73,11 @@ impl> ConstrainedProgram { )?; } } - _ => return Err(StatementError::array_assign_range(statement.span.clone().unwrap_or_default())), + _ => { + return Err(StatementError::array_assign_range( + statement.span.clone().unwrap_or_default(), + )); + } }; } diff --git a/compiler/src/statement/assign/assignee.rs b/compiler/src/statement/assign/assignee.rs index 00c2f5868c..1751afca72 100644 --- a/compiler/src/statement/assign/assignee.rs +++ b/compiler/src/statement/assign/assignee.rs @@ -16,12 +16,7 @@ //! Resolves assignees in a compiled Leo program. -use crate::{ - errors::StatementError, - program::ConstrainedProgram, - value::ConstrainedValue, - GroupType, -}; +use crate::{errors::StatementError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; use leo_asg::{AssignAccess, AssignStatement, Identifier, Span}; use snarkvm_models::{ @@ -45,7 +40,6 @@ impl> ConstrainedProgram { mut_self: bool, assignee: &AssignStatement, ) -> Result>, StatementError> { - let span = assignee.span.clone().unwrap_or_default(); let resolved_accesses = assignee @@ -167,7 +161,6 @@ impl> ConstrainedProgram { } } ResolvedAssigneeAccess::Tuple(index, span) => { - if value.len() != 1 { return Err(StatementError::array_assign_interior_index(span)); } diff --git a/compiler/src/statement/block/block.rs b/compiler/src/statement/block/block.rs index 2b0740c0d6..8d59463b48 100644 --- a/compiler/src/statement/block/block.rs +++ b/compiler/src/statement/block/block.rs @@ -17,7 +17,7 @@ //! Enforces a branch of a conditional or iteration statement in a compiled Leo program. use crate::{program::ConstrainedProgram, GroupType, IndicatorAndConstrainedValue, StatementResult}; -use leo_asg::{BlockStatement}; +use leo_asg::BlockStatement; use snarkvm_models::{ curves::{Field, PrimeField}, @@ -40,14 +40,7 @@ impl> ConstrainedProgram { let mut results = Vec::with_capacity(block.statements.len()); // Evaluate statements. Only allow a single return argument to be returned. for statement in block.statements.iter() { - let value = self.enforce_statement( - cs, - file_scope, - function_scope, - indicator, - statement, - mut_self, - )?; + let value = self.enforce_statement(cs, file_scope, function_scope, indicator, statement, mut_self)?; results.extend(value); } diff --git a/compiler/src/statement/conditional/conditional.rs b/compiler/src/statement/conditional/conditional.rs index 242b0c648e..eba00ebd50 100644 --- a/compiler/src/statement/conditional/conditional.rs +++ b/compiler/src/statement/conditional/conditional.rs @@ -24,7 +24,7 @@ use crate::{ IndicatorAndConstrainedValue, StatementResult, }; -use leo_asg::{ConditionalStatement}; +use leo_asg::ConditionalStatement; use snarkvm_models::{ curves::{Field, PrimeField}, @@ -53,24 +53,15 @@ impl> ConstrainedProgram { mut_self: bool, statement: &ConditionalStatement, ) -> StatementResult>> { - let span = statement.span.clone().unwrap_or_default(); // Inherit an indicator from a previous statement. let outer_indicator = indicator; // Evaluate the conditional boolean as the inner indicator - let inner_indicator = match self.enforce_expression( - cs, - file_scope, - function_scope, - &statement.condition, - )? { + let inner_indicator = match self.enforce_expression(cs, file_scope, function_scope, &statement.condition)? { ConstrainedValue::Boolean(resolved) => resolved, value => { - return Err(StatementError::conditional_boolean( - value.to_string(), - span, - )); + return Err(StatementError::conditional_boolean(value.to_string(), span)); } }; @@ -82,12 +73,7 @@ impl> ConstrainedProgram { outer_indicator_string, inner_indicator_string ); let branch_1_indicator = Boolean::and( - &mut cs.ns(|| { - format!( - "branch 1 {} {}:{}", - span.text, &span.line, &span.start - ) - }), + &mut cs.ns(|| format!("branch 1 {} {}:{}", span.text, &span.line, &span.start)), outer_indicator, &inner_indicator, ) @@ -123,14 +109,9 @@ impl> ConstrainedProgram { // Evaluate branch 2 let mut branch_2_result = match &statement.next { - Some(next) => self.enforce_statement( - cs, - file_scope, - function_scope, - &branch_2_indicator, - next, - mut_self, - )?, + Some(next) => { + self.enforce_statement(cs, file_scope, function_scope, &branch_2_indicator, next, mut_self)? + } None => vec![], }; diff --git a/compiler/src/statement/definition/definition.rs b/compiler/src/statement/definition/definition.rs index dd08f6681c..d721a3d128 100644 --- a/compiler/src/statement/definition/definition.rs +++ b/compiler/src/statement/definition/definition.rs @@ -33,7 +33,6 @@ impl> ConstrainedProgram { mut value: ConstrainedValue, span: &Span, ) -> Result<(), StatementError> { - self.store_definition(function_scope, variable, value); Ok(()) @@ -71,13 +70,18 @@ impl> ConstrainedProgram { statement: &DefinitionStatement, ) -> Result<(), StatementError> { let num_variables = statement.variables.len(); - let expression = - self.enforce_expression(cs, file_scope, function_scope, &statement.value)?; + let expression = self.enforce_expression(cs, file_scope, function_scope, &statement.value)?; let span = statement.span.clone().unwrap_or_default(); if num_variables == 1 { // Define a single variable with a single value - self.enforce_single_definition(cs, function_scope, statement.variables.get(0).unwrap(), expression, &span) + self.enforce_single_definition( + cs, + function_scope, + statement.variables.get(0).unwrap(), + expression, + &span, + ) } else { // Define multiple variables for an expression that returns multiple results (multiple definition) let values = match expression { @@ -86,13 +90,7 @@ impl> ConstrainedProgram { value => return Err(StatementError::multiple_definition(value.to_string(), span.clone())), }; - self.enforce_multiple_definition( - cs, - function_scope, - &statement.variables, - values, - &span, - ) + self.enforce_multiple_definition(cs, function_scope, &statement.variables, values, &span) } } } diff --git a/compiler/src/statement/iteration/iteration.rs b/compiler/src/statement/iteration/iteration.rs index 9fb3d1c9d9..706fe1c99b 100644 --- a/compiler/src/statement/iteration/iteration.rs +++ b/compiler/src/statement/iteration/iteration.rs @@ -24,7 +24,7 @@ use crate::{ Integer, StatementResult, }; -use leo_asg::{IterationStatement}; +use leo_asg::IterationStatement; use snarkvm_models::{ curves::{Field, PrimeField}, diff --git a/compiler/src/statement/return_/return_.rs b/compiler/src/statement/return_/return_.rs index 81e54b234a..edc486726c 100644 --- a/compiler/src/statement/return_/return_.rs +++ b/compiler/src/statement/return_/return_.rs @@ -17,7 +17,7 @@ //! Enforces a return statement in a compiled Leo program. use crate::{errors::StatementError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; -use leo_asg::{ReturnStatement}; +use leo_asg::ReturnStatement; use snarkvm_models::{ curves::{Field, PrimeField}, @@ -32,12 +32,7 @@ impl> ConstrainedProgram { function_scope: &str, statement: &ReturnStatement, ) -> Result, StatementError> { - let result = self.enforce_operand( - cs, - file_scope, - function_scope, - &statement.expression, - )?; + let result = self.enforce_operand(cs, file_scope, function_scope, &statement.expression)?; Ok(result) } } diff --git a/compiler/src/statement/statement.rs b/compiler/src/statement/statement.rs index 5c88de7a8f..35063638de 100644 --- a/compiler/src/statement/statement.rs +++ b/compiler/src/statement/statement.rs @@ -17,7 +17,7 @@ //! Enforces a statement in a compiled Leo program. use crate::{errors::StatementError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; -use leo_asg::{Statement}; +use leo_asg::Statement; use std::sync::Arc; use snarkvm_models::{ @@ -61,36 +61,17 @@ impl> ConstrainedProgram { self.enforce_definition_statement(cs, file_scope, function_scope, statement)?; } Statement::Assign(statement) => { - self.enforce_assign_statement( - cs, - file_scope, - function_scope, - indicator, - mut_self, - statement, - )?; + self.enforce_assign_statement(cs, file_scope, function_scope, indicator, mut_self, statement)?; } Statement::Conditional(statement) => { - let result = self.enforce_conditional_statement( - cs, - file_scope, - function_scope, - indicator, - mut_self, - statement, - )?; + let result = + self.enforce_conditional_statement(cs, file_scope, function_scope, indicator, mut_self, statement)?; results.extend(result); } Statement::Iteration(statement) => { - let result = self.enforce_iteration_statement( - cs, - file_scope, - function_scope, - indicator, - mut_self, - statement, - )?; + let result = + self.enforce_iteration_statement(cs, file_scope, function_scope, indicator, mut_self, statement)?; results.extend(result); } @@ -106,7 +87,12 @@ impl> ConstrainedProgram { results.push((*indicator, value)); } } - _ => return Err(StatementError::unassigned(statement.span.as_ref().map(|x| x.text.clone()).unwrap_or_default(), statement.span.clone().unwrap_or_default())), + _ => { + return Err(StatementError::unassigned( + statement.span.as_ref().map(|x| x.text.clone()).unwrap_or_default(), + statement.span.clone().unwrap_or_default(), + )); + } } } Statement::Block(statement) => { diff --git a/compiler/src/value/group/input.rs b/compiler/src/value/group/input.rs index e47e13b87c..c492cec1f9 100644 --- a/compiler/src/value/group/input.rs +++ b/compiler/src/value/group/input.rs @@ -57,10 +57,15 @@ pub(crate) fn group_from_input, CS: Const None => None, }; - let group = allocate_group(cs, name, option.map(|x| match x { - leo_ast::GroupValue::Single(s, _) => GroupValue::Single(s), - leo_ast::GroupValue::Tuple(leo_ast::GroupTuple { x, y, .. }) => GroupValue::Tuple((&x).into(), (&y).into()), - }), span)?; + let group = allocate_group( + cs, + name, + option.map(|x| match x { + leo_ast::GroupValue::Single(s, _) => GroupValue::Single(s), + leo_ast::GroupValue::Tuple(leo_ast::GroupTuple { x, y, .. }) => GroupValue::Tuple((&x).into(), (&y).into()), + }), + span, + )?; Ok(ConstrainedValue::Group(group)) } diff --git a/compiler/src/value/group/targets/edwards_bls12.rs b/compiler/src/value/group/targets/edwards_bls12.rs index 154d06249a..bbb65606c3 100644 --- a/compiler/src/value/group/targets/edwards_bls12.rs +++ b/compiler/src/value/group/targets/edwards_bls12.rs @@ -146,14 +146,19 @@ impl EdwardsGroupType { Ok(EdwardsAffine::zero()) } else { let one = edwards_affine_one(); - let number_value = Fp256::from_str(&number).map_err(|_| GroupError::n_group(number.clone(), span.clone()))?; + let number_value = + Fp256::from_str(&number).map_err(|_| GroupError::n_group(number.clone(), span.clone()))?; let result: EdwardsAffine = one.mul(&number_value); Ok(result) } } - pub fn edwards_affine_from_tuple(x: &GroupCoordinate, y: &GroupCoordinate, span: &Span) -> Result { + pub fn edwards_affine_from_tuple( + x: &GroupCoordinate, + y: &GroupCoordinate, + span: &Span, + ) -> Result { let x = x.clone(); let y = y.clone(); diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index 7b57ce42fc..4dd38aad99 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -16,8 +16,8 @@ //! Conversion of integer declarations to constraints in Leo. use crate::{errors::IntegerError, IntegerTrait}; -use leo_ast::{InputValue}; use leo_asg::{ConstInt, IntegerType, Span}; +use leo_ast::InputValue; use leo_gadgets::{ arithmetic::*, bits::comparator::{ComparatorGadget, EvaluateLtGadget}, @@ -73,10 +73,7 @@ impl Integer { /// /// Checks that the expression is equal to the expected type if given. /// - pub fn new( - value: &ConstInt, - span: &Span, - ) -> Self { + pub fn new(value: &ConstInt, span: &Span) -> Self { match value { ConstInt::U8(i) => Integer::U8(UInt8::constant(*i)), ConstInt::U16(i) => Integer::U16(UInt16::constant(*i)), diff --git a/compiler/src/value/value.rs b/compiler/src/value/value.rs index 8b6da3a0b6..ca2262a115 100644 --- a/compiler/src/value/value.rs +++ b/compiler/src/value/value.rs @@ -17,7 +17,7 @@ //! The in memory stored value for a defined name in a compiled Leo program. use crate::{ - boolean::input::{allocate_bool}, + boolean::input::allocate_bool, errors::{FieldError, ValueError}, Address, FieldType, @@ -35,8 +35,7 @@ use snarkvm_models::{ utilities::{boolean::Boolean, eq::ConditionalEqGadget, select::CondSelectGadget}, }, }; -use std::fmt; -use std::sync::Arc; +use std::{fmt, sync::Arc}; #[derive(Clone, PartialEq, Eq)] pub struct ConstrainedCircuitMember>(pub Identifier, pub ConstrainedValue); @@ -64,7 +63,6 @@ pub enum ConstrainedValue> { } impl> ConstrainedValue { - pub(crate) fn to_type(&self, span: &Span) -> Result { Ok(match self { // Data types diff --git a/compiler/tests/address/mod.rs b/compiler/tests/address/mod.rs index eb93b660f9..4e25869771 100644 --- a/compiler/tests/address/mod.rs +++ b/compiler/tests/address/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{assert_satisfied, expect_compiler_error, generate_main_input, parse_program}; +use crate::{assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input, parse_program}; use leo_ast::InputValue; static TEST_ADDRESS_1: &str = "aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8"; @@ -63,9 +63,9 @@ fn test_implicit_valid() { #[test] fn test_implicit_invalid() { let program_string = include_str!("implicit_invalid.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _output = expect_compiler_error(program); + expect_asg_error(error); } #[test] diff --git a/compiler/tests/array/mod.rs b/compiler/tests/array/mod.rs index ff9b86029c..0f227d0e31 100644 --- a/compiler/tests/array/mod.rs +++ b/compiler/tests/array/mod.rs @@ -16,6 +16,7 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, get_output, parse_program, @@ -150,17 +151,17 @@ fn test_input_tuple_3x2_fail() { #[test] fn test_multi_fail_initializer() { let program_string = include_str!("multi_fail_initializer.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] fn test_multi_inline_fail() { let program_string = include_str!("multi_fail_inline.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -174,9 +175,9 @@ fn test_multi_initializer() { #[test] fn test_multi_initializer_fail() { let program_string = include_str!("multi_initializer_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -190,9 +191,9 @@ fn test_nested_3x2_value() { #[test] fn test_nested_3x2_value_fail() { let program_string = include_str!("nested_3x2_value_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -206,9 +207,9 @@ fn test_tuple_3x2_value() { #[test] fn test_tuple_3x2_value_fail() { let program_string = include_str!("tuple_3x2_value_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -258,9 +259,9 @@ fn test_type_nested_value_nested_3x2() { #[test] fn test_type_nested_value_nested_3x2_fail() { let program_string = include_str!("type_nested_value_nested_3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -274,9 +275,9 @@ fn test_type_nested_value_nested_4x3x2() { #[test] fn test_type_nested_value_nested_4x3x2_fail() { let program_string = include_str!("type_nested_value_nested_4x3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -290,9 +291,9 @@ fn test_type_nested_value_tuple_3x2() { #[test] fn test_type_nested_value_tuple_3x2_fail() { let program_string = include_str!("type_nested_value_tuple_3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -306,9 +307,9 @@ fn test_type_nested_value_tuple_4x3x2() { #[test] fn test_type_nested_value_tuple_4x3x2_fail() { let program_string = include_str!("type_nested_value_tuple_4x3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -322,9 +323,9 @@ fn test_type_tuple_value_nested_3x2() { #[test] fn test_type_tuple_value_nested_3x2_fail() { let program_string = include_str!("type_tuple_value_nested_3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -338,9 +339,9 @@ fn test_type_tuple_value_nested_4x3x2() { #[test] fn test_type_tuple_value_nested_4x3x2_fail() { let program_string = include_str!("type_tuple_value_nested_4x3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -354,9 +355,9 @@ fn test_type_tuple_value_tuple_3x2() { #[test] fn test_type_tuple_value_tuple_3x2_fail() { let program_string = include_str!("type_tuple_value_tuple_3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -370,9 +371,9 @@ fn test_type_tuple_value_tuple_4x3x2() { #[test] fn test_type_tuple_value_tuple_4x3x2_fail() { let program_string = include_str!("type_tuple_value_tuple_4x3x2_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } // Tests for nested multi-dimensional arrays as input to the program @@ -526,7 +527,7 @@ fn test_input_type_tuple_value_tuple_4x3x2_fail() { #[test] fn test_variable_slice_fail() { let program_string = include_str!("variable_slice_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - assert_satisfied(program); + expect_asg_error(error); } diff --git a/compiler/tests/array/type_tuple_value_nested_3x2.leo b/compiler/tests/array/type_tuple_value_nested_3x2.leo index 0b960c6b44..81195e03a1 100644 --- a/compiler/tests/array/type_tuple_value_nested_3x2.leo +++ b/compiler/tests/array/type_tuple_value_nested_3x2.leo @@ -1,7 +1,7 @@ function main() { const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline - const b: [u8; (2, 3)] = [[0; 3]; 2]; // initializer + const b: [u8; (3, 2)] = [[0; 2]; 3]; // initializer console.assert(a == b); } \ No newline at end of file diff --git a/compiler/tests/array/type_tuple_value_tuple_3x2.leo b/compiler/tests/array/type_tuple_value_tuple_3x2.leo index d742a544a7..d451a9c1a8 100644 --- a/compiler/tests/array/type_tuple_value_tuple_3x2.leo +++ b/compiler/tests/array/type_tuple_value_tuple_3x2.leo @@ -1,7 +1,7 @@ function main() { const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline - const b: [u8; (2, 3)] = [0; (2, 3)]; // initializer + const b: [u8; (3, 2)] = [0; (3, 2)]; // initializer console.assert(a == b); } \ No newline at end of file diff --git a/compiler/tests/boolean/mod.rs b/compiler/tests/boolean/mod.rs index 75f94b3f86..d192d06541 100644 --- a/compiler/tests/boolean/mod.rs +++ b/compiler/tests/boolean/mod.rs @@ -16,8 +16,8 @@ use crate::{ assert_satisfied, - expect_compiler_error, expect_asg_error, + expect_compiler_error, get_output, parse_program, parse_program_with_input, @@ -104,9 +104,9 @@ fn test_not_mutable() { #[test] fn test_not_u32() { let program_string = include_str!("not_u32.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } // Boolean or || diff --git a/compiler/tests/circuits/member_function.leo b/compiler/tests/circuits/member_function.leo index 024b0d7e02..e40603dffb 100644 --- a/compiler/tests/circuits/member_function.leo +++ b/compiler/tests/circuits/member_function.leo @@ -1,11 +1,13 @@ circuit Foo { - function echo(x: u32) -> u32 { - return x + x: u32, + + function echo(self) -> u32 { + return self.x } } function main() { - let a = Foo { }; + let a = Foo { x: 1u32 }; - console.assert(a.echo(1u32) == 1u32); + console.assert(a.echo() == 1u32); } \ No newline at end of file diff --git a/compiler/tests/circuits/mod.rs b/compiler/tests/circuits/mod.rs index be753acf30..995ac3080a 100644 --- a/compiler/tests/circuits/mod.rs +++ b/compiler/tests/circuits/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{assert_satisfied, expect_compiler_error, expect_asg_error, parse_program}; +use crate::{assert_satisfied, expect_asg_error, expect_compiler_error, parse_program}; // Expressions @@ -29,9 +29,9 @@ fn test_inline() { #[test] fn test_inline_fail() { let program_string = include_str!("inline_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -161,9 +161,9 @@ fn test_mutate_self_variable_conditional() { #[test] fn test_mutate_self_variable_fail() { let program_string = include_str!("mut_self_variable_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -201,9 +201,9 @@ fn test_mutate_variable() { #[test] fn test_mutate_variable_fail() { let program_string = include_str!("mut_variable_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } // Self diff --git a/compiler/tests/console/mod.rs b/compiler/tests/console/mod.rs index 7741c9034c..1f321f8f8b 100644 --- a/compiler/tests/console/mod.rs +++ b/compiler/tests/console/mod.rs @@ -14,7 +14,14 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{assert_satisfied, expect_compiler_error, generate_main_input, parse_program, parse_program_with_input}; +use crate::{ + assert_satisfied, + expect_asg_error, + expect_compiler_error, + generate_main_input, + parse_program, + parse_program_with_input, +}; use leo_ast::InputValue; #[test] @@ -49,27 +56,27 @@ fn test_log_parameter_many() { } #[test] -fn test_log_parameter_fail_unknown() { - let program_string = include_str!("log_parameter_fail_unknown.leo"); - let program = parse_program(program_string).unwrap(); +fn test_log_parameter_fail_empty() { + let program_string = include_str!("log_parameter_fail_empty.leo"); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } #[test] -fn test_log_parameter_fail_empty() { +fn test_log_parameter_fail_none() { let program_string = include_str!("log_parameter_fail_empty.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } #[test] -fn test_log_parameter_fail_none() { - let program_string = include_str!("log_parameter_fail_empty.leo"); - let program = parse_program(program_string).unwrap(); +fn test_log_parameter_fail_unknown() { + let program_string = include_str!("log_parameter_fail_unknown.leo"); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } #[test] diff --git a/compiler/tests/function/mod.rs b/compiler/tests/function/mod.rs index 35fc09a11d..5092126b73 100644 --- a/compiler/tests/function/mod.rs +++ b/compiler/tests/function/mod.rs @@ -16,8 +16,8 @@ use crate::{ assert_satisfied, - expect_compiler_error, expect_asg_error, + expect_compiler_error, get_output, parse_program, parse_program_with_input, @@ -71,17 +71,17 @@ fn test_multiple_returns() { #[test] fn test_multiple_returns_fail() { let program_string = include_str!("multiple_returns_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } #[test] fn test_multiple_returns_fail_conditional() { let program_string = include_str!("multiple_returns_fail_conditional.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -151,9 +151,9 @@ fn test_array_input() { #[test] fn test_return_array_nested_fail() { let program_string = include_str!("return_array_nested_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] @@ -167,9 +167,9 @@ fn test_return_array_nested_pass() { #[test] fn test_return_array_tuple_fail() { let program_string = include_str!("return_array_tuple_fail.leo"); - let program = parse_program(program_string).unwrap(); + let error = parse_program(program_string).err().unwrap(); - let _err = expect_compiler_error(program); + expect_asg_error(error); } #[test] diff --git a/compiler/tests/integers/i128/mod.rs b/compiler/tests/integers/i128/mod.rs index d671fc494e..04f2583c77 100644 --- a/compiler/tests/integers/i128/mod.rs +++ b/compiler/tests/integers/i128/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, integers::{expect_computation_error, IntegerTester}, - expect_asg_error, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/i16/mod.rs b/compiler/tests/integers/i16/mod.rs index 92620d7468..945932008a 100644 --- a/compiler/tests/integers/i16/mod.rs +++ b/compiler/tests/integers/i16/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, integers::{expect_computation_error, IntegerTester}, - expect_asg_error, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/i32/mod.rs b/compiler/tests/integers/i32/mod.rs index 37f3d161fe..4ebede74e2 100644 --- a/compiler/tests/integers/i32/mod.rs +++ b/compiler/tests/integers/i32/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, integers::{expect_computation_error, IntegerTester}, - expect_asg_error, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/i64/mod.rs b/compiler/tests/integers/i64/mod.rs index bded8ffd07..ee8382aab3 100644 --- a/compiler/tests/integers/i64/mod.rs +++ b/compiler/tests/integers/i64/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, integers::{expect_computation_error, IntegerTester}, - expect_asg_error, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/i8/mod.rs b/compiler/tests/integers/i8/mod.rs index a1c07df5bd..9fede660e8 100644 --- a/compiler/tests/integers/i8/mod.rs +++ b/compiler/tests/integers/i8/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, integers::{expect_computation_error, IntegerTester}, - expect_asg_error, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/u128/mod.rs b/compiler/tests/integers/u128/mod.rs index 60e3df5d47..9764352850 100644 --- a/compiler/tests/integers/u128/mod.rs +++ b/compiler/tests/integers/u128/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, - integers::{IntegerTester}, - expect_asg_error, + integers::IntegerTester, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/u16/mod.rs b/compiler/tests/integers/u16/mod.rs index bb07fba4fe..0971e17537 100644 --- a/compiler/tests/integers/u16/mod.rs +++ b/compiler/tests/integers/u16/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, - integers::{IntegerTester}, - expect_asg_error, + integers::IntegerTester, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/u32/mod.rs b/compiler/tests/integers/u32/mod.rs index c475d967d9..9491623ad7 100644 --- a/compiler/tests/integers/u32/mod.rs +++ b/compiler/tests/integers/u32/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, - integers::{IntegerTester}, - expect_asg_error, + integers::IntegerTester, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/u64/mod.rs b/compiler/tests/integers/u64/mod.rs index 9bc2186fa6..12163a017f 100644 --- a/compiler/tests/integers/u64/mod.rs +++ b/compiler/tests/integers/u64/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, - integers::{IntegerTester}, - expect_asg_error, + integers::IntegerTester, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/integers/u8/mod.rs b/compiler/tests/integers/u8/mod.rs index 5a7632f365..6f3e394163 100644 --- a/compiler/tests/integers/u8/mod.rs +++ b/compiler/tests/integers/u8/mod.rs @@ -16,10 +16,10 @@ use crate::{ assert_satisfied, + expect_asg_error, expect_compiler_error, generate_main_input, - integers::{IntegerTester}, - expect_asg_error, + integers::IntegerTester, parse_program, }; use leo_ast::InputValue; diff --git a/compiler/tests/syntax/mod.rs b/compiler/tests/syntax/mod.rs index 1f9c271c47..81478013f6 100644 --- a/compiler/tests/syntax/mod.rs +++ b/compiler/tests/syntax/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{expect_compiler_error, parse_input, parse_program}; +use crate::{expect_asg_error, expect_compiler_error, parse_input, parse_program}; use leo_compiler::errors::{CompilerError, ExpressionError, FunctionError, StatementError}; use leo_grammar::ParserError; use leo_input::InputParserError; @@ -36,29 +36,9 @@ fn test_semicolon() { #[test] fn test_undefined() { let program_string = include_str!("undefined.leo"); - let program = parse_program(program_string).unwrap(); - - let error = expect_compiler_error(program); + let error = parse_program(program_string).err().unwrap(); - match error { - CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError( - ExpressionError::Error(error), - ))) => { - assert_eq!( - error.to_string(), - vec![ - " --> \"/test/src/main.leo\": 2:12", - " |", - " 2 | return a", - " | ^", - " |", - " = Cannot find value `a` in this scope", - ] - .join("\n") - ); - } - _ => panic!("expected an undefined identifier error"), - } + expect_asg_error(error); } #[test] diff --git a/imports/src/errors/import_parser.rs b/imports/src/errors/import_parser.rs index a45de1a5d9..5a223e09be 100644 --- a/imports/src/errors/import_parser.rs +++ b/imports/src/errors/import_parser.rs @@ -13,9 +13,9 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +use leo_asg::AsgConvertError; use leo_ast::{Error as FormattedError, Identifier, Span}; use leo_grammar::ParserError; -use leo_asg::AsgConvertError; use std::{io, path::Path}; diff --git a/imports/src/parser/import_parser.rs b/imports/src/parser/import_parser.rs index e342b15b69..c1133f7222 100644 --- a/imports/src/parser/import_parser.rs +++ b/imports/src/parser/import_parser.rs @@ -15,7 +15,7 @@ // along with the Leo library. If not, see . use crate::errors::ImportParserError; -use leo_asg::{Program, ImportResolver, AsgConvertError, Span}; +use leo_asg::{AsgConvertError, ImportResolver, Program, Span}; use indexmap::{IndexMap, IndexSet}; use std::env::current_dir; @@ -35,18 +35,21 @@ impl ImportResolver for ImportParser { fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result, AsgConvertError> { let full_path = package_segments.join("."); if self.partial_imports.contains(&full_path) { - return Err(ImportParserError::recursive_imports(&full_path, span).into()) + return Err(ImportParserError::recursive_imports(&full_path, span).into()); } if let Some(program) = self.imports.get(&full_path) { return Ok(Some(program.clone())); } let mut imports = Self::default(); - let path = current_dir().map_err(|x| -> AsgConvertError { ImportParserError::current_directory_error(x).into() })?; + let path = + current_dir().map_err(|x| -> AsgConvertError { ImportParserError::current_directory_error(x).into() })?; self.partial_imports.insert(full_path.clone()); - let program = imports.parse_package(path.clone(), package_segments, span).map_err(|x| -> AsgConvertError { x.into() })?; + let program = imports + .parse_package(path.clone(), package_segments, span) + .map_err(|x| -> AsgConvertError { x.into() })?; self.partial_imports.remove(&full_path); self.imports.insert(full_path, program.clone()); Ok(Some(program)) } -} \ No newline at end of file +} diff --git a/imports/src/parser/mod.rs b/imports/src/parser/mod.rs index 917eb7dfd1..a3d7851112 100644 --- a/imports/src/parser/mod.rs +++ b/imports/src/parser/mod.rs @@ -15,7 +15,6 @@ // along with the Leo library. If not, see . /// The import parser creates a hashmap of import program names -> import program structs - pub mod parse_symbol; pub use self::parse_symbol::*; diff --git a/imports/src/parser/parse_package.rs b/imports/src/parser/parse_package.rs index 6cb16b8328..798949edf8 100644 --- a/imports/src/parser/parse_package.rs +++ b/imports/src/parser/parse_package.rs @@ -17,15 +17,19 @@ use crate::{errors::ImportParserError, ImportParser}; use leo_asg::{Identifier, Program, Span}; -use std::{fs::DirEntry, fs, path::PathBuf}; +use std::{fs, fs::DirEntry, path::PathBuf}; static SOURCE_FILE_EXTENSION: &str = ".leo"; static SOURCE_DIRECTORY_NAME: &str = "src/"; static IMPORTS_DIRECTORY_NAME: &str = "imports/"; impl ImportParser { - - fn parse_package_access(&mut self, package: &DirEntry, remaining_segments: &[&str], span: &Span) -> Result { + fn parse_package_access( + &mut self, + package: &DirEntry, + remaining_segments: &[&str], + span: &Span, + ) -> Result { if remaining_segments.len() > 0 { return self.parse_package(package.path(), remaining_segments, span); } @@ -40,7 +44,12 @@ impl ImportParser { /// /// Inserts the Leo syntax tree into the `ImportParser`. /// - pub(crate) fn parse_package(&mut self, mut path: PathBuf, segments: &[&str], span: &Span) -> Result { + pub(crate) fn parse_package( + &mut self, + mut path: PathBuf, + segments: &[&str], + span: &Span, + ) -> Result { let error_path = path.clone(); let package_name = segments[0]; @@ -98,16 +107,25 @@ impl ImportParser { // Check if the package name was found in both the source and imports directory. match (matched_source_entry, matched_import_entry) { - (Some(_), Some(_)) => Err(ImportParserError::conflicting_imports(Identifier::new_with_span(package_name, span))), + (Some(_), Some(_)) => Err(ImportParserError::conflicting_imports(Identifier::new_with_span( + package_name, + span, + ))), (Some(source_entry), None) => self.parse_package_access(&source_entry, &segments[1..], span), (None, Some(import_entry)) => self.parse_package_access(&import_entry, &segments[1..], span), - (None, None) => Err(ImportParserError::unknown_package(Identifier::new_with_span(package_name, span))), + (None, None) => Err(ImportParserError::unknown_package(Identifier::new_with_span( + package_name, + span, + ))), } } else { // Enforce local package access with no found imports directory match matched_source_entry { Some(source_entry) => self.parse_package_access(&source_entry, &segments[1..], span), - None => Err(ImportParserError::unknown_package(Identifier::new_with_span(package_name, span))), + None => Err(ImportParserError::unknown_package(Identifier::new_with_span( + package_name, + span, + ))), } } } diff --git a/imports/src/parser/parse_symbol.rs b/imports/src/parser/parse_symbol.rs index 2a2ee14d9d..7caf97772c 100644 --- a/imports/src/parser/parse_symbol.rs +++ b/imports/src/parser/parse_symbol.rs @@ -18,7 +18,7 @@ use crate::{errors::ImportParserError, ImportParser}; use leo_ast::{Program, Span}; use leo_grammar::Grammar; -use std::{fs::DirEntry}; +use std::fs::DirEntry; static LIBRARY_FILE: &str = "src/lib.leo"; @@ -37,11 +37,11 @@ impl ImportParser { .file_name() .into_string() .map_err(|_| ImportParserError::convert_os_string(span.clone()))?; - + let mut file_path = package.path(); if file_type.is_dir() { file_path.push(LIBRARY_FILE); - + if !file_path.exists() { return Err(ImportParserError::expected_lib_file( format!("{:?}", file_path.as_path()), @@ -49,11 +49,11 @@ impl ImportParser { )); } } - + // Build the package abstract syntax tree. let program_string = &Grammar::load_file(&file_path)?; let ast = &Grammar::new(&file_path, &program_string)?; - + // Build the package Leo syntax tree from the package abstract syntax tree. Ok(Program::from(&file_name, ast.as_repr())) } From 999f4572baad70792dcb63fecbedd28632a36302 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 21 Jan 2021 15:40:21 -0500 Subject: [PATCH 2/2] use expect_asg_error + add license headers --- compiler/src/stage.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compiler/src/stage.rs b/compiler/src/stage.rs index 8278085e38..be06c464ec 100644 --- a/compiler/src/stage.rs +++ b/compiler/src/stage.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2020 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 . + use leo_asg::Program; pub trait ASGStage {