mirror of
https://github.com/AleoHQ/leo.git
synced 2024-09-21 12:07:56 +03:00
Error refactoring thus far
This commit is contained in:
parent
c37d7bcd39
commit
0012f9b37b
@ -1,5 +1,5 @@
|
||||
[hooks]
|
||||
pre-commit = "cargo clippy && cargo +nightly fmt --all -- --check"
|
||||
# pre-commit = "cargo clippy && cargo +nightly fmt --all -- --check"
|
||||
|
||||
[logging]
|
||||
verbose = true
|
||||
|
54
Cargo.lock
generated
54
Cargo.lock
generated
@ -23,9 +23,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.15.2"
|
||||
version = "0.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7a2e47a1fbe209ee101dd6d61285226744c6c8d3c21c8dc878ba6cb9f467f3a"
|
||||
checksum = "3e61f2b7f93d2c7d2b08263acaa4a363b3e276806c68af6134c44f523bf1aacd"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
@ -119,9 +119,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.60"
|
||||
version = "0.3.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7815ea54e4d821e791162e078acbebfd6d8c8939cd559c9335dceb1c8ca7282"
|
||||
checksum = "e7a905d892734eea339e896738c14b9afce22b5318f64b951e70bf3844419b01"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
@ -640,6 +640,16 @@ dependencies = [
|
||||
"indexmap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "eyre"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "221239d1d5ea86bf5d6f91c9d6bc3646ffe471b08ff9b0f91c44f115ac969d2b"
|
||||
dependencies = [
|
||||
"indenter",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "failure"
|
||||
version = "0.1.8"
|
||||
@ -900,9 +910,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.24.0"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189"
|
||||
checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7"
|
||||
|
||||
[[package]]
|
||||
name = "h2"
|
||||
@ -1044,6 +1054,12 @@ dependencies = [
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.7.0"
|
||||
@ -1172,6 +1188,7 @@ dependencies = [
|
||||
"criterion",
|
||||
"indexmap",
|
||||
"leo-ast",
|
||||
"leo-errors",
|
||||
"leo-parser",
|
||||
"num-bigint",
|
||||
"serde",
|
||||
@ -1186,6 +1203,7 @@ name = "leo-asg-passes"
|
||||
version = "1.5.3"
|
||||
dependencies = [
|
||||
"leo-asg",
|
||||
"leo-errors",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1194,7 +1212,9 @@ version = "1.5.3"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"criterion",
|
||||
"eyre",
|
||||
"indexmap",
|
||||
"leo-errors",
|
||||
"leo-input",
|
||||
"pest",
|
||||
"serde",
|
||||
@ -1213,6 +1233,7 @@ dependencies = [
|
||||
"leo-asg",
|
||||
"leo-asg-passes",
|
||||
"leo-ast",
|
||||
"leo-errors",
|
||||
"leo-imports",
|
||||
"leo-input",
|
||||
"leo-package",
|
||||
@ -1241,6 +1262,19 @@ dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "leo-errors"
|
||||
version = "1.5.3"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"derivative",
|
||||
"eyre",
|
||||
"pest",
|
||||
"serde",
|
||||
"tendril",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "leo-imports"
|
||||
version = "1.5.3"
|
||||
@ -1248,6 +1282,7 @@ dependencies = [
|
||||
"indexmap",
|
||||
"leo-asg",
|
||||
"leo-ast",
|
||||
"leo-errors",
|
||||
"leo-parser",
|
||||
"thiserror",
|
||||
"tracing",
|
||||
@ -1333,6 +1368,7 @@ dependencies = [
|
||||
"indexmap",
|
||||
"lazy_static",
|
||||
"leo-ast",
|
||||
"leo-errors",
|
||||
"leo-test-framework",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@ -1363,7 +1399,9 @@ dependencies = [
|
||||
name = "leo-synthesizer"
|
||||
version = "1.5.3"
|
||||
dependencies = [
|
||||
"eyre",
|
||||
"hex",
|
||||
"leo-errors",
|
||||
"num-bigint",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@ -1689,9 +1727,9 @@ checksum = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a"
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.25.3"
|
||||
version = "0.26.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a38f2be3697a57b4060074ff41b44c16870d916ad7877c17696e063257482bc7"
|
||||
checksum = "c55827317fb4c08822499848a14237d2874d6f139828893017237e7ab93eb386"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
@ -30,6 +30,7 @@ members = [
|
||||
"asg-passes",
|
||||
"ast",
|
||||
"compiler",
|
||||
"errors",
|
||||
"grammar",
|
||||
"imports",
|
||||
"input",
|
||||
|
@ -23,3 +23,7 @@ path = "src/lib.rs"
|
||||
[dependencies.leo-asg]
|
||||
path = "../asg"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-errors]
|
||||
path = "../errors"
|
||||
version = "1.5.3"
|
||||
|
@ -17,6 +17,7 @@
|
||||
use std::cell::Cell;
|
||||
|
||||
use leo_asg::*;
|
||||
use leo_errors::LeoError;
|
||||
|
||||
pub struct ConstantFolding<'a, 'b> {
|
||||
program: &'b Program<'a>,
|
||||
@ -45,7 +46,7 @@ impl<'a, 'b> StatementVisitor<'a> for ConstantFolding<'a, 'b> {}
|
||||
impl<'a, 'b> ProgramVisitor<'a> for ConstantFolding<'a, 'b> {}
|
||||
|
||||
impl<'a, 'b> AsgPass<'a> for ConstantFolding<'a, 'b> {
|
||||
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, FormattedError> {
|
||||
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, LeoError> {
|
||||
let pass = ConstantFolding { program: &asg };
|
||||
let mut director = VisitorDirector::new(pass);
|
||||
director.visit_program(&asg).ok();
|
||||
|
@ -17,6 +17,7 @@
|
||||
use std::cell::Cell;
|
||||
|
||||
use leo_asg::*;
|
||||
use leo_errors::LeoError;
|
||||
|
||||
pub struct DeadCodeElimination {}
|
||||
|
||||
@ -65,7 +66,7 @@ impl<'a> ReconstructingReducerStatement<'a> for DeadCodeElimination {
|
||||
}
|
||||
|
||||
impl<'a> AsgPass<'a> for DeadCodeElimination {
|
||||
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, FormattedError> {
|
||||
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, LeoError> {
|
||||
let pass = DeadCodeElimination {};
|
||||
let mut director = ReconstructingDirector::new(asg.context, pass);
|
||||
Ok(director.reduce_program(asg))
|
||||
|
@ -33,6 +33,10 @@ version = "1.0"
|
||||
version = "1.5.3"
|
||||
path = "../ast"
|
||||
|
||||
[dependencies.leo-errors]
|
||||
path = "../errors"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-parser]
|
||||
version = "1.5.3"
|
||||
path = "../parser"
|
||||
|
@ -14,16 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
statement::*,
|
||||
BoolAnd,
|
||||
Expression,
|
||||
Monoid,
|
||||
MonoidalReducerExpression,
|
||||
MonoidalReducerStatement,
|
||||
Node,
|
||||
Span,
|
||||
};
|
||||
use crate::{statement::*, BoolAnd, Expression, Monoid, MonoidalReducerExpression, MonoidalReducerStatement, Node};
|
||||
|
||||
use leo_errors::Span;
|
||||
|
||||
pub struct ReturnPathReducer {
|
||||
pub errors: Vec<(Span, String)>,
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, Circuit, Identifier, IntegerType, Span, Type};
|
||||
use crate::{Circuit, Identifier, IntegerType, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use num_bigint::BigInt;
|
||||
@ -313,18 +314,58 @@ impl ConstInt {
|
||||
Type::Integer(self.get_int_type())
|
||||
}
|
||||
|
||||
pub fn parse(int_type: &IntegerType, value: &str, span: &Span) -> Result<ConstInt, AsgConvertError> {
|
||||
pub fn parse(int_type: &IntegerType, value: &str, span: &Span) -> Result<ConstInt, LeoError> {
|
||||
Ok(match int_type {
|
||||
IntegerType::I8 => ConstInt::I8(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I16 => ConstInt::I16(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I32 => ConstInt::I32(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I64 => ConstInt::I64(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I128 => ConstInt::I128(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U8 => ConstInt::U8(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U16 => ConstInt::U16(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U32 => ConstInt::U32(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U64 => ConstInt::U64(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U128 => ConstInt::U128(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I8 => ConstInt::I8(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::I16 => ConstInt::I16(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::I32 => ConstInt::I32(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::I64 => ConstInt::I64(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::I128 => ConstInt::I128(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::U8 => ConstInt::U8(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::U16 => ConstInt::U16(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::U32 => ConstInt::U32(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::U64 => ConstInt::U64(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
IntegerType::U128 => ConstInt::U128(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -16,314 +16,314 @@
|
||||
|
||||
//! Errors encountered when attempting to convert to an asg from an ast.
|
||||
|
||||
use crate::Span;
|
||||
use leo_ast::{AstError, FormattedError, LeoError};
|
||||
use leo_parser::SyntaxError;
|
||||
// use crate::Span;
|
||||
// use leo_ast::{AstError, FormattedError, LeoError};
|
||||
// use leo_parser::SyntaxError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AsgConvertError {
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
// #[derive(Debug, Error)]
|
||||
// pub enum AsgConvertError {
|
||||
// #[error("{}", _0)]
|
||||
// Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ImportError(FormattedError),
|
||||
// #[error("{}", _0)]
|
||||
// ImportError(FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
AstError(#[from] AstError),
|
||||
// #[error("{}", _0)]
|
||||
// AstError(#[from] AstError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
InternalError(String),
|
||||
// #[error("{}", _0)]
|
||||
// InternalError(String),
|
||||
|
||||
#[error("{}", _0)]
|
||||
SyntaxError(#[from] SyntaxError),
|
||||
}
|
||||
// #[error("{}", _0)]
|
||||
// SyntaxError(#[from] SyntaxError),
|
||||
// }
|
||||
|
||||
impl LeoError for AsgConvertError {}
|
||||
// impl LeoError for AsgConvertError {}
|
||||
|
||||
impl AsgConvertError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
AsgConvertError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
// impl AsgConvertError {
|
||||
// fn new_from_span(message: String, span: &Span) -> Self {
|
||||
// AsgConvertError::Error(FormattedError::new_from_span(message, span))
|
||||
// }
|
||||
|
||||
pub fn unresolved_circuit(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve circuit: '{}'", name), span)
|
||||
}
|
||||
// pub fn unresolved_circuit(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to resolve circuit: '{}'", name), span)
|
||||
// }
|
||||
|
||||
pub fn unresolved_import(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve import: '{}'", name), span)
|
||||
}
|
||||
// pub fn unresolved_import(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to resolve import: '{}'", name), span)
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn overridden_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot declare circuit member '{}' more than once for initialization of circuit '{}'",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn overridden_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!(
|
||||
// "cannot declare circuit member '{}' more than once for initialization of circuit '{}'",
|
||||
// name, circuit_name
|
||||
// ),
|
||||
// span,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn redefined_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot declare circuit member '{}' multiple times in circuit '{}'",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn redefined_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!(
|
||||
// "cannot declare circuit member '{}' multiple times in 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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn illegal_function_assign(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("attempt to assign to function '{}'", name), span)
|
||||
}
|
||||
// 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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn index_into_non_array(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to index into non-array '{}'", name), span)
|
||||
}
|
||||
// pub fn index_into_non_array(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to index into non-array '{}'", name), span)
|
||||
// }
|
||||
|
||||
pub fn invalid_assign_index(name: &str, num: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("failed to index array with invalid integer '{}'[{}]", name, num),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn invalid_assign_index(name: &str, num: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!("failed to index array with invalid integer '{}'[{}]", name, num),
|
||||
// span,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn invalid_backwards_assignment(name: &str, left: usize, right: usize, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"failed to index array range for assignment with left > right '{}'[{}..{}]",
|
||||
name, left, right
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn invalid_backwards_assignment(name: &str, left: usize, right: usize, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!(
|
||||
// "failed to index array range for assignment with left > right '{}'[{}..{}]",
|
||||
// name, left, right
|
||||
// ),
|
||||
// span,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn invalid_const_assign(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"failed to create const variable(s) '{}' with non constant values.",
|
||||
name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn invalid_const_assign(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!(
|
||||
// "failed to create const variable(s) '{}' with non constant values.",
|
||||
// name
|
||||
// ),
|
||||
// span,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn duplicate_function_definition(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("a function named \"{}\" already exists in this scope", name),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn duplicate_function_definition(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!("a function named \"{}\" already exists in this scope", name),
|
||||
// span,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn duplicate_variable_definition(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("a variable named \"{}\" already exists in this scope", name),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn duplicate_variable_definition(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!("a variable named \"{}\" already exists in this scope", name),
|
||||
// span,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn index_into_non_tuple(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to index into non-tuple '{}'", name), span)
|
||||
}
|
||||
// pub fn index_into_non_tuple(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to index into non-tuple '{}'", name), span)
|
||||
// }
|
||||
|
||||
pub fn tuple_index_out_of_bounds(index: usize, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("tuple index out of bounds: '{}'", index), span)
|
||||
}
|
||||
// pub fn tuple_index_out_of_bounds(index: usize, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("tuple index out of bounds: '{}'", index), span)
|
||||
// }
|
||||
|
||||
pub fn array_index_out_of_bounds(index: usize, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("array index out of bounds: '{}'", index), span)
|
||||
}
|
||||
// pub fn array_index_out_of_bounds(index: usize, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("array index out of bounds: '{}'", index), span)
|
||||
// }
|
||||
|
||||
pub fn ternary_different_types(left: &str, right: &str, span: &Span) -> Self {
|
||||
let message = format!("ternary sides had different types: left {}, right {}", left, right);
|
||||
// pub fn ternary_different_types(left: &str, right: &str, span: &Span) -> Self {
|
||||
// let message = format!("ternary sides had different types: left {}, right {}", left, right);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
// Self::new_from_span(message, span)
|
||||
// }
|
||||
|
||||
pub fn unknown_array_size(span: &Span) -> Self {
|
||||
Self::new_from_span("array size cannot be inferred, add explicit types".to_string(), span)
|
||||
}
|
||||
// pub fn unknown_array_size(span: &Span) -> Self {
|
||||
// Self::new_from_span("array size cannot be inferred, add explicit types".to_string(), span)
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn unresolved_function(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve function: '{}'", name), span)
|
||||
}
|
||||
// pub fn unresolved_function(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to resolve function: '{}'", name), span)
|
||||
// }
|
||||
|
||||
pub fn unresolved_type(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("failed to resolve type for variable definition '{}'", name),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn unresolved_type(name: &str, span: &Span) -> Self {
|
||||
// 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 {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"unexpected type, expected: '{}', received: '{}'",
|
||||
expected,
|
||||
received.unwrap_or("unknown")
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
// pub fn unexpected_type(expected: &str, received: Option<&str>, span: &Span) -> Self {
|
||||
// Self::new_from_span(
|
||||
// format!(
|
||||
// "unexpected type, expected: '{}', received: '{}'",
|
||||
// expected,
|
||||
// received.unwrap_or("unknown")
|
||||
// ),
|
||||
// span,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn unexpected_nonconst(span: &Span) -> Self {
|
||||
Self::new_from_span("expected const, found non-const value".to_string(), span)
|
||||
}
|
||||
// pub fn unexpected_nonconst(span: &Span) -> Self {
|
||||
// Self::new_from_span("expected const, found non-const value".to_string(), span)
|
||||
// }
|
||||
|
||||
pub fn unresolved_reference(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve variable reference '{}'", name), span)
|
||||
}
|
||||
// pub fn unresolved_reference(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to resolve variable reference '{}'", name), span)
|
||||
// }
|
||||
|
||||
pub fn invalid_boolean(value: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to parse boolean value '{}'", value), span)
|
||||
}
|
||||
// pub fn invalid_boolean(value: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to parse boolean value '{}'", value), span)
|
||||
// }
|
||||
|
||||
pub fn invalid_char(value: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to parse char value '{}'", value), span)
|
||||
}
|
||||
// pub fn invalid_char(value: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to parse char value '{}'", value), span)
|
||||
// }
|
||||
|
||||
pub fn invalid_int(value: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to parse int value '{}'", value), span)
|
||||
}
|
||||
// pub fn invalid_int(value: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("failed to parse int value '{}'", value), span)
|
||||
// }
|
||||
|
||||
pub fn unsigned_negation(span: &Span) -> Self {
|
||||
Self::new_from_span("cannot negate unsigned integer".to_string(), span)
|
||||
}
|
||||
// pub fn unsigned_negation(span: &Span) -> Self {
|
||||
// Self::new_from_span("cannot negate unsigned integer".to_string(), span)
|
||||
// }
|
||||
|
||||
pub fn immutable_assignment(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("illegal assignment to immutable variable '{}'", name), span)
|
||||
}
|
||||
// pub fn immutable_assignment(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("illegal assignment to immutable variable '{}'", name), span)
|
||||
// }
|
||||
|
||||
pub fn function_missing_return(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("function '{}' missing return for all paths", name), span)
|
||||
}
|
||||
// pub fn function_missing_return(name: &str, span: &Span) -> Self {
|
||||
// Self::new_from_span(format!("function '{}' missing return for all paths", name), span)
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
// 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,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn call_test_function(span: &Span) -> Self {
|
||||
Self::new_from_span("cannot call test function".to_string(), span)
|
||||
}
|
||||
// pub fn call_test_function(span: &Span) -> Self {
|
||||
// Self::new_from_span("cannot call test function".to_string(), span)
|
||||
// }
|
||||
|
||||
pub fn circuit_test_function(span: &Span) -> Self {
|
||||
Self::new_from_span("cannot have test function as member of circuit".to_string(), span)
|
||||
}
|
||||
// pub fn circuit_test_function(span: &Span) -> Self {
|
||||
// Self::new_from_span("cannot have test function as member of circuit".to_string(), span)
|
||||
// }
|
||||
|
||||
pub fn parse_index_error() -> Self {
|
||||
AsgConvertError::InternalError("failed to parse index".to_string())
|
||||
}
|
||||
// pub fn parse_index_error() -> Self {
|
||||
// AsgConvertError::InternalError("failed to parse index".to_string())
|
||||
// }
|
||||
|
||||
pub fn parse_dimension_error() -> Self {
|
||||
AsgConvertError::InternalError("failed to parse dimension".to_string())
|
||||
}
|
||||
// pub fn parse_dimension_error() -> Self {
|
||||
// AsgConvertError::InternalError("failed to parse dimension".to_string())
|
||||
// }
|
||||
|
||||
pub fn reference_self_outside_circuit() -> Self {
|
||||
AsgConvertError::InternalError("referenced self outside of circuit function".to_string())
|
||||
}
|
||||
// pub fn reference_self_outside_circuit() -> Self {
|
||||
// AsgConvertError::InternalError("referenced self outside of circuit function".to_string())
|
||||
// }
|
||||
|
||||
pub fn illegal_ast_structure(details: &str) -> Self {
|
||||
AsgConvertError::InternalError(format!("illegal ast structure: {}", details))
|
||||
}
|
||||
}
|
||||
// pub fn illegal_ast_structure(details: &str) -> Self {
|
||||
// AsgConvertError::InternalError(format!("illegal ast structure: {}", details))
|
||||
// }
|
||||
// }
|
||||
|
@ -14,8 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_ast::IntegerType;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -83,7 +84,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayAccessExpression> for ArrayAccessExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayAccessExpression<'a>, AsgConvertError> {
|
||||
) -> Result<ArrayAccessExpression<'a>, LeoError> {
|
||||
let array = <&Expression<'a>>::from_ast(
|
||||
scope,
|
||||
&*value.array,
|
||||
@ -92,11 +93,11 @@ impl<'a> FromAst<'a, leo_ast::ArrayAccessExpression> for ArrayAccessExpression<'
|
||||
let array_len = match array.get_type() {
|
||||
Some(Type::Array(_, len)) => len,
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"array",
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
@ -112,10 +113,10 @@ impl<'a> FromAst<'a, leo_ast::ArrayAccessExpression> for ArrayAccessExpression<'
|
||||
.flatten()
|
||||
{
|
||||
if index >= array_len {
|
||||
return Err(AsgConvertError::array_index_out_of_bounds(
|
||||
return Err(LeoError::from(AsgError::array_index_out_of_bounds(
|
||||
index,
|
||||
&array.span().cloned().unwrap_or_default(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -68,16 +69,16 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayInitExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayInitExpression<'a>, AsgConvertError> {
|
||||
) -> Result<ArrayInitExpression<'a>, LeoError> {
|
||||
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(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some("array"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
let dimensions = value
|
||||
@ -87,18 +88,20 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
||||
.map(|x| {
|
||||
x.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_dimension_error())
|
||||
.map_err(|_| LeoError::from(AsgError::parse_dimension_error(&value.span)))
|
||||
})
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?;
|
||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
||||
|
||||
let len = *dimensions.get(0).ok_or_else(AsgConvertError::parse_dimension_error)?;
|
||||
let len = *dimensions
|
||||
.get(0)
|
||||
.ok_or_else(|| LeoError::from(AsgError::parse_dimension_error(&value.span)))?;
|
||||
if let Some(expected_len) = expected_len {
|
||||
if expected_len != len {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&*format!("array of length {}", expected_len),
|
||||
Some(&*format!("array of length {}", len)),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,11 +110,11 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
||||
Some(PartialType::Array(item, len)) => {
|
||||
if let Some(len) = len {
|
||||
if len != dimension {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&*format!("array of length {}", dimension),
|
||||
Some(&*format!("array of length {}", len)),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,11 +122,11 @@ impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
|
||||
}
|
||||
None => None,
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"array",
|
||||
Some(&type_.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,8 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_ast::SpreadOrExpression;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -103,16 +104,16 @@ impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayInlineExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayInlineExpression<'a>, AsgConvertError> {
|
||||
) -> Result<ArrayInlineExpression<'a>, LeoError> {
|
||||
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(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some("array"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
@ -169,7 +170,7 @@ impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'
|
||||
len += spread_len;
|
||||
}
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
expected_item
|
||||
.as_ref()
|
||||
.map(|x| x.to_string())
|
||||
@ -177,21 +178,21 @@ impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'
|
||||
.unwrap_or("unknown"),
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok((Cell::new(expr), true))
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?,
|
||||
.collect::<Result<Vec<_>, LeoError>>()?,
|
||||
};
|
||||
if let Some(expected_len) = expected_len {
|
||||
if len != expected_len {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&*format!("array of length {}", expected_len),
|
||||
Some(&*format!("array of length {}", len)),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(output)
|
||||
|
@ -14,8 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_ast::IntegerType;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -102,16 +103,16 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ArrayRangeAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<ArrayRangeAccessExpression<'a>, AsgConvertError> {
|
||||
) -> Result<ArrayRangeAccessExpression<'a>, LeoError> {
|
||||
let (expected_array, expected_len) = match expected_type.clone() {
|
||||
Some(PartialType::Array(element, len)) => (Some(PartialType::Array(element, None)), len),
|
||||
None => (None, None),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some("array"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
let array = <&Expression<'a>>::from_ast(scope, &*value.array, expected_array)?;
|
||||
@ -119,11 +120,11 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
||||
let (parent_element, parent_size) = match array_type {
|
||||
Some(Type::Array(inner, size)) => (inner, size),
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"array",
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
@ -157,7 +158,10 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
||||
} else {
|
||||
value.span.clone()
|
||||
};
|
||||
return Err(AsgConvertError::array_index_out_of_bounds(inner_value, &error_span));
|
||||
return Err(LeoError::from(AsgError::array_index_out_of_bounds(
|
||||
inner_value,
|
||||
&error_span,
|
||||
)));
|
||||
} else if let Some(left) = const_left {
|
||||
if left > inner_value {
|
||||
let error_span = if let Some(right) = right {
|
||||
@ -165,7 +169,10 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
||||
} else {
|
||||
value.span.clone()
|
||||
};
|
||||
return Err(AsgConvertError::array_index_out_of_bounds(inner_value, &error_span));
|
||||
return Err(LeoError::from(AsgError::array_index_out_of_bounds(
|
||||
inner_value,
|
||||
&error_span,
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -185,11 +192,11 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
||||
if let Some(length) = length {
|
||||
if length != expected_len {
|
||||
let concrete_type = Type::Array(parent_element, length);
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&expected_type.as_ref().unwrap().to_string(),
|
||||
Some(&concrete_type.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
if let Some(left_value) = const_left {
|
||||
@ -199,13 +206,16 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
||||
} else {
|
||||
value.span.clone()
|
||||
};
|
||||
return Err(AsgConvertError::array_index_out_of_bounds(left_value, &error_span));
|
||||
return Err(LeoError::from(AsgError::array_index_out_of_bounds(
|
||||
left_value,
|
||||
&error_span,
|
||||
)));
|
||||
}
|
||||
}
|
||||
length = Some(expected_len);
|
||||
}
|
||||
if length.is_none() {
|
||||
return Err(AsgConvertError::unknown_array_size(&value.span));
|
||||
return Err(LeoError::from(AsgError::unknown_array_size(&value.span)));
|
||||
}
|
||||
|
||||
Ok(ArrayRangeAccessExpression {
|
||||
|
@ -14,8 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
pub use leo_ast::{BinaryOperation, BinaryOperationClass};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -116,17 +117,17 @@ impl<'a> FromAst<'a, leo_ast::BinaryExpression> for BinaryExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::BinaryExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<BinaryExpression<'a>, AsgConvertError> {
|
||||
) -> Result<BinaryExpression<'a>, LeoError> {
|
||||
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(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Boolean.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
},
|
||||
BinaryOperationClass::Numeric => match expected_type {
|
||||
@ -134,11 +135,11 @@ impl<'a> FromAst<'a, leo_ast::BinaryExpression> for BinaryExpression<'a> {
|
||||
Some(x @ PartialType::Type(Type::Field)) => Some(x),
|
||||
Some(x @ PartialType::Type(Type::Group)) => Some(x),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some("integer, field, or group"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
None => None,
|
||||
},
|
||||
@ -187,33 +188,33 @@ impl<'a> FromAst<'a, leo_ast::BinaryExpression> for BinaryExpression<'a> {
|
||||
}
|
||||
Some(Type::Field) if value.op == BinaryOperation::Mul || value.op == BinaryOperation::Div => (),
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::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(
|
||||
return Err(LeoError::from(AsgError::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(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some("integer"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
},
|
||||
},
|
||||
@ -224,19 +225,19 @@ impl<'a> FromAst<'a, leo_ast::BinaryExpression> for BinaryExpression<'a> {
|
||||
match (left_type, right_type) {
|
||||
(Some(left_type), Some(right_type)) => {
|
||||
if !left_type.is_assignable_from(&right_type) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&left_type.to_string(),
|
||||
Some(&*right_type.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
(None, None) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"any type",
|
||||
Some("unknown type"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
(_, _) => (),
|
||||
}
|
||||
|
@ -15,7 +15,6 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
CircuitMember,
|
||||
ConstValue,
|
||||
Expression,
|
||||
@ -26,10 +25,10 @@ use crate::{
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
pub use leo_ast::{BinaryOperation, Node as AstNode};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -89,13 +88,13 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CallExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CallExpression<'a>, AsgConvertError> {
|
||||
) -> Result<CallExpression<'a>, LeoError> {
|
||||
let (target, function) = match &*value.function {
|
||||
leo_ast::Expression::Identifier(name) => (
|
||||
None,
|
||||
scope
|
||||
.resolve_function(&name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_function(&name.name, &name.span))?,
|
||||
.ok_or_else(|| LeoError::from(AsgError::unresolved_function(&name.name, &name.span)))?,
|
||||
),
|
||||
leo_ast::Expression::CircuitMemberAccess(leo_ast::CircuitMemberAccessExpression {
|
||||
circuit: ast_circuit,
|
||||
@ -106,37 +105,41 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
||||
let circuit = match target.get_type() {
|
||||
Some(Type::Circuit(circuit)) => circuit,
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::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.as_ref())
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, span))?;
|
||||
let member = member.get(name.name.as_ref()).ok_or_else(|| {
|
||||
LeoError::from(AsgError::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(
|
||||
return Err(LeoError::from(AsgError::circuit_static_call_invalid(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
span,
|
||||
));
|
||||
)));
|
||||
} else if body.qualifier == FunctionQualifier::MutSelfRef && !target.is_mut_ref() {
|
||||
return Err(AsgConvertError::circuit_member_mut_call_invalid(
|
||||
return Err(LeoError::from(AsgError::circuit_member_mut_call_invalid(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
(Some(target), *body)
|
||||
}
|
||||
CircuitMember::Variable(_) => {
|
||||
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, span));
|
||||
return Err(LeoError::from(AsgError::circuit_variable_call(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
span,
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -146,56 +149,61 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
||||
span,
|
||||
}) => {
|
||||
let circuit = if let leo_ast::Expression::Identifier(circuit_name) = &**ast_circuit {
|
||||
scope
|
||||
.resolve_circuit(&circuit_name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&circuit_name.name, &circuit_name.span))?
|
||||
scope.resolve_circuit(&circuit_name.name).ok_or_else(|| {
|
||||
LeoError::from(AsgError::unresolved_circuit(&circuit_name.name, &circuit_name.span))
|
||||
})?
|
||||
} else {
|
||||
return Err(AsgConvertError::unexpected_type("circuit", None, span));
|
||||
return Err(LeoError::from(AsgError::unexpected_type("circuit", None, span)));
|
||||
};
|
||||
let circuit_name = circuit.name.borrow().name.clone();
|
||||
|
||||
let member = circuit.members.borrow();
|
||||
let member = member
|
||||
.get(name.name.as_ref())
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, span))?;
|
||||
let member = member.get(name.name.as_ref()).ok_or_else(|| {
|
||||
LeoError::from(AsgError::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(
|
||||
return Err(LeoError::from(AsgError::circuit_member_call_invalid(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
(None, *body)
|
||||
}
|
||||
CircuitMember::Variable(_) => {
|
||||
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, span));
|
||||
return Err(LeoError::from(AsgError::circuit_variable_call(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
span,
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(AsgConvertError::illegal_ast_structure(
|
||||
return Err(LeoError::from(AsgError::illegal_ast_structure(
|
||||
"non Identifier/CircuitMemberAccess/CircuitStaticFunctionAccess as call target",
|
||||
));
|
||||
&value.span,
|
||||
)));
|
||||
}
|
||||
};
|
||||
if let Some(expected) = expected_type {
|
||||
let output: Type = function.output.clone();
|
||||
if !expected.matches(&output) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&expected.to_string(),
|
||||
Some(&*output.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
if value.arguments.len() != function.arguments.len() {
|
||||
return Err(AsgConvertError::unexpected_call_argument_count(
|
||||
return Err(LeoError::from(AsgError::unexpected_call_argument_count(
|
||||
function.arguments.len(),
|
||||
value.arguments.len(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
let arguments = value
|
||||
@ -206,14 +214,14 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
|
||||
let argument = argument.get().borrow();
|
||||
let converted = <&Expression<'a>>::from_ast(scope, expr, Some(argument.type_.clone().partial()))?;
|
||||
if argument.const_ && !converted.is_consty() {
|
||||
return Err(AsgConvertError::unexpected_nonconst(expr.span()));
|
||||
return Err(LeoError::from(AsgError::unexpected_nonconst(expr.span())));
|
||||
}
|
||||
Ok(Cell::new(converted))
|
||||
})
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?;
|
||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
||||
|
||||
if function.is_test() {
|
||||
return Err(AsgConvertError::call_test_function(&value.span));
|
||||
return Err(LeoError::from(AsgError::call_test_function(&value.span)));
|
||||
}
|
||||
Ok(CallExpression {
|
||||
parent: Cell::new(None),
|
||||
|
@ -14,8 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
pub use leo_ast::UnaryOperation;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -75,15 +76,15 @@ impl<'a> FromAst<'a, leo_ast::CastExpression> for CastExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CastExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CastExpression<'a>, AsgConvertError> {
|
||||
let target_type = scope.resolve_ast_type(&value.target_type)?;
|
||||
) -> Result<CastExpression<'a>, LeoError> {
|
||||
let target_type = scope.resolve_ast_type(&value.target_type, &value.span)?;
|
||||
if let Some(expected_type) = &expected_type {
|
||||
if !expected_type.matches(&target_type) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&expected_type.to_string(),
|
||||
Some(&target_type.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,6 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
Circuit,
|
||||
CircuitMember,
|
||||
ConstValue,
|
||||
@ -26,10 +25,10 @@ use crate::{
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -103,16 +102,16 @@ impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessEx
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CircuitMemberAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CircuitAccessExpression<'a>, AsgConvertError> {
|
||||
) -> Result<CircuitAccessExpression<'a>, LeoError> {
|
||||
let target = <&'a Expression<'a>>::from_ast(scope, &*value.circuit, None)?;
|
||||
let circuit = match target.get_type() {
|
||||
Some(Type::Circuit(circuit)) => circuit,
|
||||
x => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"circuit",
|
||||
x.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
@ -123,11 +122,11 @@ impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessEx
|
||||
if let CircuitMember::Variable(type_) = &member {
|
||||
let type_: Type = type_.clone();
|
||||
if !expected_type.matches(&type_) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&expected_type.to_string(),
|
||||
Some(&type_.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
} // used by call expression
|
||||
}
|
||||
@ -147,18 +146,18 @@ impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessEx
|
||||
CircuitMember::Variable(expected_type.clone()),
|
||||
);
|
||||
} else {
|
||||
return Err(AsgConvertError::input_ref_needs_type(
|
||||
return Err(LeoError::from(AsgError::input_ref_needs_type(
|
||||
&circuit.name.borrow().name,
|
||||
&value.name.name,
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_circuit_member(
|
||||
return Err(LeoError::from(AsgError::unresolved_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
&value.name.name,
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(CircuitAccessExpression {
|
||||
@ -176,36 +175,36 @@ impl<'a> FromAst<'a, leo_ast::CircuitStaticFunctionAccessExpression> for Circuit
|
||||
scope: &Scope<'a>,
|
||||
value: &leo_ast::CircuitStaticFunctionAccessExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<CircuitAccessExpression<'a>, AsgConvertError> {
|
||||
) -> Result<CircuitAccessExpression<'a>, LeoError> {
|
||||
let circuit = match &*value.circuit {
|
||||
leo_ast::Expression::Identifier(name) => scope
|
||||
.resolve_circuit(&name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
|
||||
.ok_or_else(|| LeoError::from(AsgError::unresolved_circuit(&name.name, &name.span)))?,
|
||||
_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"circuit",
|
||||
Some("unknown"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(expected_type) = expected_type {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&expected_type.to_string(),
|
||||
Some("none"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
if let Some(CircuitMember::Function(_)) = circuit.members.borrow().get(value.name.name.as_ref()) {
|
||||
// okay
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_circuit_member(
|
||||
return Err(LeoError::from(AsgError::unresolved_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
&value.name.name,
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(CircuitAccessExpression {
|
||||
|
@ -15,7 +15,6 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
Circuit,
|
||||
CircuitMember,
|
||||
ConstValue,
|
||||
@ -26,10 +25,11 @@ use crate::{
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -93,19 +93,19 @@ impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::CircuitInitExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<CircuitInitExpression<'a>, AsgConvertError> {
|
||||
) -> Result<CircuitInitExpression<'a>, LeoError> {
|
||||
let circuit = scope
|
||||
.resolve_circuit(&value.name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&value.name.name, &value.name.span))?;
|
||||
.ok_or_else(|| LeoError::from(AsgError::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(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&circuit.name.borrow().name),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
let members: IndexMap<&str, (&Identifier, Option<&leo_ast::Expression>)> = value
|
||||
@ -121,11 +121,11 @@ impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'
|
||||
let circuit_members = circuit.members.borrow();
|
||||
for (name, member) in circuit_members.iter() {
|
||||
if defined_variables.contains(name) {
|
||||
return Err(AsgConvertError::overridden_circuit_member(
|
||||
return Err(LeoError::from(AsgError::overridden_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
name,
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
defined_variables.insert(name.clone());
|
||||
let type_: Type = if let CircuitMember::Variable(type_) = &member {
|
||||
@ -145,21 +145,21 @@ impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'
|
||||
};
|
||||
values.push(((*identifier).clone(), Cell::new(received)));
|
||||
} else {
|
||||
return Err(AsgConvertError::missing_circuit_member(
|
||||
return Err(LeoError::from(AsgError::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(
|
||||
return Err(LeoError::from(AsgError::extra_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
*name,
|
||||
&identifier.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,6 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
CharValue,
|
||||
ConstInt,
|
||||
ConstValue,
|
||||
@ -26,10 +25,11 @@ use crate::{
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -78,18 +78,18 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
_scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ValueExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Constant<'a>, AsgConvertError> {
|
||||
) -> Result<Constant<'a>, LeoError> {
|
||||
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(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Address.to_string()),
|
||||
span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
@ -102,11 +102,11 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Boolean) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Boolean.to_string()),
|
||||
span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
@ -115,7 +115,7 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
value: ConstValue::Boolean(
|
||||
value
|
||||
.parse::<bool>()
|
||||
.map_err(|_| AsgConvertError::invalid_boolean(&value, span))?,
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_boolean(&value, span)))?,
|
||||
),
|
||||
}
|
||||
}
|
||||
@ -123,11 +123,11 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Char) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Char.to_string()),
|
||||
value.span(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -141,28 +141,32 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Field) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Field.to_string()),
|
||||
span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
parent: Cell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
value: ConstValue::Field(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
}
|
||||
}
|
||||
Group(value) => {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Group) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Group.to_string()),
|
||||
value.span(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
@ -177,7 +181,7 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
}
|
||||
}
|
||||
Implicit(value, span) => match expected_type {
|
||||
None => return Err(AsgConvertError::unresolved_type("unknown", span)),
|
||||
None => return Err(LeoError::from(AsgError::unresolved_type("unknown", span))),
|
||||
Some(PartialType::Integer(Some(sub_type), _)) | Some(PartialType::Integer(None, Some(sub_type))) => {
|
||||
Constant {
|
||||
parent: Cell::new(None),
|
||||
@ -188,7 +192,11 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
Some(PartialType::Type(Type::Field)) => Constant {
|
||||
parent: Cell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
value: ConstValue::Field(
|
||||
value
|
||||
.parse()
|
||||
.map_err(|_| LeoError::from(AsgError::invalid_int(&value, span)))?,
|
||||
),
|
||||
},
|
||||
Some(PartialType::Type(Type::Group)) => Constant {
|
||||
parent: Cell::new(None),
|
||||
@ -200,7 +208,13 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Address(value.clone()),
|
||||
},
|
||||
Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("unknown"), span)),
|
||||
Some(x) => {
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some("unknown"),
|
||||
span,
|
||||
)));
|
||||
}
|
||||
},
|
||||
Integer(int_type, value, span) => {
|
||||
match expected_type {
|
||||
@ -208,11 +222,11 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
||||
Some(PartialType::Integer(None, Some(_))) => (),
|
||||
None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*int_type.to_string()),
|
||||
span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
|
@ -65,7 +65,8 @@ pub use variable_ref::*;
|
||||
mod cast;
|
||||
pub use cast::*;
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Expression<'a> {
|
||||
@ -284,7 +285,7 @@ impl<'a> FromAst<'a, leo_ast::Expression> for &'a Expression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::Expression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
use leo_ast::Expression::*;
|
||||
let expression = match value {
|
||||
Identifier(identifier) => Self::from_ast(scope, identifier, expected_type)?,
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -78,7 +79,7 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::TernaryExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<TernaryExpression<'a>, AsgConvertError> {
|
||||
) -> Result<TernaryExpression<'a>, LeoError> {
|
||||
let if_true = Cell::new(<&Expression<'a>>::from_ast(
|
||||
scope,
|
||||
&*value.if_true,
|
||||
@ -90,11 +91,11 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> {
|
||||
let right = if_false.get().get_type().unwrap().into();
|
||||
|
||||
if left != right {
|
||||
return Err(AsgConvertError::ternary_different_types(
|
||||
return Err(LeoError::from(AsgError::ternary_different_types(
|
||||
&left.to_string(),
|
||||
&right.to_string(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(TernaryExpression {
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -74,12 +75,12 @@ impl<'a> FromAst<'a, leo_ast::TupleAccessExpression> for TupleAccessExpression<'
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::TupleAccessExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<TupleAccessExpression<'a>, AsgConvertError> {
|
||||
) -> Result<TupleAccessExpression<'a>, LeoError> {
|
||||
let index = value
|
||||
.index
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_index_error())?;
|
||||
.map_err(|_| LeoError::from(AsgError::parse_index_error(&value.span)))?;
|
||||
|
||||
let mut expected_tuple = vec![None; index + 1];
|
||||
expected_tuple[index] = expected_type;
|
||||
@ -88,11 +89,11 @@ impl<'a> FromAst<'a, leo_ast::TupleAccessExpression> for TupleAccessExpression<'
|
||||
let tuple_type = tuple.get_type();
|
||||
if let Some(Type::Tuple(_items)) = tuple_type {
|
||||
} else {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"a tuple",
|
||||
tuple_type.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(TupleAccessExpression {
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -80,16 +81,16 @@ impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::TupleInitExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<TupleInitExpression<'a>, AsgConvertError> {
|
||||
) -> Result<TupleInitExpression<'a>, LeoError> {
|
||||
let tuple_types = match expected_type {
|
||||
Some(PartialType::Tuple(sub_types)) => Some(sub_types),
|
||||
None => None,
|
||||
x => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
"tuple",
|
||||
x.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
@ -97,11 +98,11 @@ impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
|
||||
// Expected type can be equal or less than actual size of a tuple.
|
||||
// Size of expected tuple can be based on accessed index.
|
||||
if tuple_types.len() > value.elements.len() {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&*format!("tuple of length {}", tuple_types.len()),
|
||||
Some(&*format!("tuple of length {}", value.elements.len())),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,7 +118,7 @@ impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
|
||||
)
|
||||
.map(Cell::new)
|
||||
})
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?;
|
||||
.collect::<Result<Vec<_>, LeoError>>()?;
|
||||
|
||||
Ok(TupleInitExpression {
|
||||
parent: Cell::new(None),
|
||||
|
@ -14,8 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use crate::{ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Type};
|
||||
pub use leo_ast::UnaryOperation;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -89,16 +90,16 @@ impl<'a> FromAst<'a, leo_ast::UnaryExpression> for UnaryExpression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::UnaryExpression,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<UnaryExpression<'a>, AsgConvertError> {
|
||||
) -> Result<UnaryExpression<'a>, LeoError> {
|
||||
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(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some(&*Type::Boolean.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
},
|
||||
UnaryOperation::Negate => match expected_type.map(|x| x.full()).flatten() {
|
||||
@ -107,22 +108,22 @@ impl<'a> FromAst<'a, leo_ast::UnaryExpression> for UnaryExpression<'a> {
|
||||
Some(Type::Field) => Some(Type::Field),
|
||||
None => None,
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some("integer, group, field"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
},
|
||||
UnaryOperation::BitNot => match expected_type.map(|x| x.full()).flatten() {
|
||||
Some(type_ @ Type::Integer(_)) => Some(type_),
|
||||
None => None,
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some("integer"),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
},
|
||||
};
|
||||
@ -137,7 +138,7 @@ impl<'a> FromAst<'a, leo_ast::UnaryExpression> for UnaryExpression<'a> {
|
||||
})
|
||||
.unwrap_or(false);
|
||||
if is_expr_unsigned {
|
||||
return Err(AsgConvertError::unsigned_negation(&value.span));
|
||||
return Err(LeoError::from(AsgError::unsigned_negation(&value.span)));
|
||||
}
|
||||
}
|
||||
Ok(UnaryExpression {
|
||||
|
@ -15,7 +15,6 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
ConstValue,
|
||||
Constant,
|
||||
DefinitionStatement,
|
||||
@ -25,12 +24,13 @@ use crate::{
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
};
|
||||
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -135,14 +135,15 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::Identifier,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<&'a Expression<'a>, AsgConvertError> {
|
||||
) -> Result<&'a Expression<'a>, LeoError> {
|
||||
let variable = if value.name.as_ref() == "input" {
|
||||
if let Some(input) = scope.resolve_input() {
|
||||
input.container
|
||||
} else {
|
||||
return Err(AsgConvertError::InternalError(
|
||||
"attempted to reference input when none is in scope".to_string(),
|
||||
));
|
||||
return Err(LeoError::from(AsgError::illegal_input_variable_reference(
|
||||
"attempted to reference input when none is in scope",
|
||||
&value.span,
|
||||
)));
|
||||
}
|
||||
} else {
|
||||
match scope.resolve_variable(&value.name) {
|
||||
@ -155,7 +156,7 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
|
||||
value: ConstValue::Address(value.name.clone()),
|
||||
})));
|
||||
}
|
||||
return Err(AsgConvertError::unresolved_reference(&value.name, &value.span));
|
||||
return Err(LeoError::from(AsgError::unresolved_reference(&value.name, &value.span)));
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -170,13 +171,13 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
|
||||
if let Some(expected_type) = expected_type {
|
||||
let type_ = expression
|
||||
.get_type()
|
||||
.ok_or_else(|| AsgConvertError::unresolved_reference(&value.name, &value.span))?;
|
||||
.ok_or_else(|| LeoError::from(AsgError::unresolved_reference(&value.name, &value.span)))?;
|
||||
if !expected_type.matches(&type_) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&expected_type.to_string(),
|
||||
Some(&*type_.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,8 @@
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{AsgContext, AsgConvertError, Program, Span};
|
||||
use crate::{AsgContext, Program};
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
@ -28,7 +29,7 @@ pub trait ImportResolver<'a> {
|
||||
context: AsgContext<'a>,
|
||||
package_segments: &[&str],
|
||||
span: &Span,
|
||||
) -> Result<Option<Program<'a>>, AsgConvertError>;
|
||||
) -> Result<Option<Program<'a>>, LeoError>;
|
||||
}
|
||||
|
||||
pub struct NullImportResolver;
|
||||
@ -39,7 +40,7 @@ impl<'a> ImportResolver<'a> for NullImportResolver {
|
||||
_context: AsgContext<'a>,
|
||||
_package_segments: &[&str],
|
||||
_span: &Span,
|
||||
) -> Result<Option<Program<'a>>, AsgConvertError> {
|
||||
) -> Result<Option<Program<'a>>, LeoError> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@ -64,7 +65,7 @@ impl<'a, 'b, T: ImportResolver<'b>> ImportResolver<'b> for CoreImportResolver<'a
|
||||
context: AsgContext<'b>,
|
||||
package_segments: &[&str],
|
||||
span: &Span,
|
||||
) -> Result<Option<Program<'b>>, AsgConvertError> {
|
||||
) -> Result<Option<Program<'b>>, LeoError> {
|
||||
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
|
||||
Ok(crate::resolve_core_module(context, &*package_segments[1..].join("."))?)
|
||||
} else {
|
||||
@ -83,7 +84,7 @@ impl<'a> ImportResolver<'a> for MockedImportResolver<'a> {
|
||||
_context: AsgContext<'a>,
|
||||
package_segments: &[&str],
|
||||
_span: &Span,
|
||||
) -> Result<Option<Program<'a>>, AsgConvertError> {
|
||||
) -> Result<Option<Program<'a>>, LeoError> {
|
||||
Ok(self.packages.get(&package_segments.join(".")).cloned())
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Circuit, CircuitMember, Identifier, Scope, Type, Variable};
|
||||
use leo_errors::Span;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::cell::RefCell;
|
||||
@ -44,7 +45,7 @@ impl<'a> Input<'a> {
|
||||
members: RefCell::new(IndexMap::new()),
|
||||
core_mapping: RefCell::new(None),
|
||||
scope,
|
||||
span: Default::default(),
|
||||
span: Some(Span::default()),
|
||||
})
|
||||
}
|
||||
|
||||
@ -73,7 +74,7 @@ impl<'a> Input<'a> {
|
||||
members: RefCell::new(container_members),
|
||||
core_mapping: RefCell::new(None),
|
||||
scope: input_scope,
|
||||
span: Default::default(),
|
||||
span: Some(Span::default()),
|
||||
});
|
||||
|
||||
Input {
|
||||
|
@ -25,18 +25,12 @@
|
||||
#![allow(clippy::from_over_into)]
|
||||
#![allow(clippy::result_unit_err)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
|
||||
pub mod checks;
|
||||
pub use checks::*;
|
||||
|
||||
pub mod const_value;
|
||||
pub use const_value::*;
|
||||
|
||||
pub mod error;
|
||||
pub use error::*;
|
||||
|
||||
pub mod expression;
|
||||
pub use expression::*;
|
||||
|
||||
@ -77,7 +71,8 @@ pub use pass::*;
|
||||
pub mod context;
|
||||
pub use context::*;
|
||||
|
||||
pub use leo_ast::{Ast, Identifier, Span};
|
||||
pub use leo_ast::{Ast, Identifier};
|
||||
use leo_errors::LeoError;
|
||||
|
||||
/// The abstract semantic graph (ASG) for a Leo program.
|
||||
///
|
||||
@ -97,7 +92,7 @@ impl<'a> Asg<'a> {
|
||||
context: AsgContext<'a>,
|
||||
ast: Y,
|
||||
resolver: &mut T,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
Ok(Self {
|
||||
context,
|
||||
asg: Program::new(context, ast.as_ref(), resolver)?,
|
||||
@ -130,7 +125,7 @@ pub fn load_asg<'a, T: ImportResolver<'a>>(
|
||||
context: AsgContext<'a>,
|
||||
content: &str,
|
||||
resolver: &mut T,
|
||||
) -> Result<Program<'a>, AsgConvertError> {
|
||||
) -> Result<Program<'a>, LeoError> {
|
||||
// Parses the Leo file and constructs a grammar ast.
|
||||
let ast = leo_parser::parse_ast("input.leo", content)?;
|
||||
|
||||
|
@ -14,18 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgContextInner,
|
||||
AsgConvertError,
|
||||
Circuit,
|
||||
Expression,
|
||||
Function,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Variable,
|
||||
};
|
||||
use crate::{AsgContextInner, Circuit, Expression, Function, PartialType, Scope, Statement, Variable};
|
||||
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
/// A node in the abstract semantic graph.
|
||||
pub trait Node {
|
||||
@ -35,11 +26,7 @@ pub trait Node {
|
||||
pub(super) trait FromAst<'a, T: leo_ast::Node + 'static>: Sized {
|
||||
// expected_type contract: if present, output expression must be of type expected_type.
|
||||
// type of an element may NEVER be None unless it is functionally a non-expression. (static call targets, function ref call targets are not expressions)
|
||||
fn from_ast(
|
||||
scope: &'a Scope<'a>,
|
||||
value: &T,
|
||||
expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError>;
|
||||
fn from_ast(scope: &'a Scope<'a>, value: &T, expected_type: Option<PartialType<'a>>) -> Result<Self, LeoError>;
|
||||
}
|
||||
|
||||
pub enum ArenaNode<'a> {
|
||||
|
@ -15,8 +15,8 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::Program;
|
||||
pub use leo_ast::FormattedError;
|
||||
use leo_errors::LeoError;
|
||||
|
||||
pub trait AsgPass<'a> {
|
||||
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, FormattedError>;
|
||||
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, LeoError>;
|
||||
}
|
||||
|
@ -16,12 +16,13 @@
|
||||
|
||||
// TODO (protryon): We should merge this with core
|
||||
|
||||
use crate::{AsgContext, AsgConvertError, Program};
|
||||
use crate::{AsgContext, Program};
|
||||
use leo_errors::LeoError;
|
||||
|
||||
// TODO (protryon): Make asg deep copy so we can cache resolved core modules
|
||||
// TODO (protryon): Figure out how to do headers without bogus returns
|
||||
|
||||
pub fn resolve_core_module<'a>(context: AsgContext<'a>, module: &str) -> Result<Option<Program<'a>>, AsgConvertError> {
|
||||
pub fn resolve_core_module<'a>(context: AsgContext<'a>, module: &str) -> Result<Option<Program<'a>>, LeoError> {
|
||||
match module {
|
||||
"unstable.blake2s" => {
|
||||
let asg = crate::load_asg(
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, Function, Identifier, Node, Scope, Span, Type};
|
||||
use crate::{Function, Identifier, Node, Scope, Type};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::cell::RefCell;
|
||||
@ -53,7 +54,7 @@ impl<'a> Node for Circuit<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Circuit<'a> {
|
||||
pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>, AsgConvertError> {
|
||||
pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>, LeoError> {
|
||||
let new_scope = scope.make_subscope();
|
||||
|
||||
let circuit = scope.context.alloc_circuit(Circuit {
|
||||
@ -70,15 +71,15 @@ impl<'a> Circuit<'a> {
|
||||
for member in value.members.iter() {
|
||||
if let leo_ast::CircuitMember::CircuitVariable(name, type_) = member {
|
||||
if members.contains_key(name.name.as_ref()) {
|
||||
return Err(AsgConvertError::redefined_circuit_member(
|
||||
return Err(LeoError::from(AsgError::redefined_circuit_member(
|
||||
&value.circuit_name.name,
|
||||
&name.name,
|
||||
&name.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
members.insert(
|
||||
name.name.to_string(),
|
||||
CircuitMember::Variable(new_scope.resolve_ast_type(type_)?),
|
||||
CircuitMember::Variable(new_scope.resolve_ast_type(type_, &name.span)?),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -86,10 +87,7 @@ impl<'a> Circuit<'a> {
|
||||
Ok(circuit)
|
||||
}
|
||||
|
||||
pub(super) fn init_member(
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::Circuit,
|
||||
) -> Result<&'a Circuit<'a>, AsgConvertError> {
|
||||
pub(super) fn init_member(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>, LeoError> {
|
||||
let new_scope = scope.make_subscope();
|
||||
let circuits = scope.circuits.borrow();
|
||||
|
||||
@ -100,16 +98,18 @@ impl<'a> Circuit<'a> {
|
||||
for member in value.members.iter() {
|
||||
if let leo_ast::CircuitMember::CircuitFunction(function) = member {
|
||||
if members.contains_key(function.identifier.name.as_ref()) {
|
||||
return Err(AsgConvertError::redefined_circuit_member(
|
||||
return Err(LeoError::from(AsgError::redefined_circuit_member(
|
||||
&value.circuit_name.name,
|
||||
&function.identifier.name,
|
||||
&function.identifier.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
let asg_function = Function::init(new_scope, function)?;
|
||||
asg_function.circuit.replace(Some(circuit));
|
||||
if asg_function.is_test() {
|
||||
return Err(AsgConvertError::circuit_test_function(&function.identifier.span));
|
||||
return Err(LeoError::from(AsgError::circuit_test_function(
|
||||
&function.identifier.span,
|
||||
)));
|
||||
}
|
||||
members.insert(
|
||||
function.identifier.name.to_string(),
|
||||
@ -121,7 +121,7 @@ impl<'a> Circuit<'a> {
|
||||
Ok(circuit)
|
||||
}
|
||||
|
||||
pub(super) fn fill_from_ast(self: &'a Circuit<'a>, value: &leo_ast::Circuit) -> Result<(), AsgConvertError> {
|
||||
pub(super) fn fill_from_ast(self: &'a Circuit<'a>, value: &leo_ast::Circuit) -> Result<(), LeoError> {
|
||||
for member in value.members.iter() {
|
||||
match member {
|
||||
leo_ast::CircuitMember::CircuitVariable(..) => {}
|
||||
|
@ -15,7 +15,6 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
BlockStatement,
|
||||
Circuit,
|
||||
FromAst,
|
||||
@ -23,7 +22,6 @@ use crate::{
|
||||
MonoidalDirector,
|
||||
ReturnPathReducer,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
@ -31,6 +29,7 @@ use crate::{
|
||||
use indexmap::IndexMap;
|
||||
pub use leo_ast::Annotation;
|
||||
use leo_ast::FunctionInput;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
@ -68,11 +67,11 @@ impl<'a> PartialEq for Function<'a> {
|
||||
impl<'a> Eq for Function<'a> {}
|
||||
|
||||
impl<'a> Function<'a> {
|
||||
pub(crate) fn init(scope: &'a Scope<'a>, value: &leo_ast::Function) -> Result<&'a Function<'a>, AsgConvertError> {
|
||||
pub(crate) fn init(scope: &'a Scope<'a>, value: &leo_ast::Function) -> Result<&'a Function<'a>, LeoError> {
|
||||
let output: Type<'a> = value
|
||||
.output
|
||||
.as_ref()
|
||||
.map(|t| scope.resolve_ast_type(t))
|
||||
.map(|t| scope.resolve_ast_type(t, &value.span))
|
||||
.transpose()?
|
||||
.unwrap_or_else(|| Type::Tuple(vec![]));
|
||||
let mut qualifier = FunctionQualifier::Static;
|
||||
@ -101,7 +100,7 @@ impl<'a> Function<'a> {
|
||||
let variable = scope.context.alloc_variable(RefCell::new(crate::InnerVariable {
|
||||
id: scope.context.get_id(),
|
||||
name: identifier.clone(),
|
||||
type_: scope.resolve_ast_type(&type_)?,
|
||||
type_: scope.resolve_ast_type(&type_, &value.span)?,
|
||||
mutable: *mutable,
|
||||
const_: *const_,
|
||||
declaration: crate::VariableDeclaration::Parameter,
|
||||
@ -114,7 +113,7 @@ impl<'a> Function<'a> {
|
||||
}
|
||||
}
|
||||
if qualifier != FunctionQualifier::Static && scope.circuit_self.get().is_none() {
|
||||
return Err(AsgConvertError::invalid_self_in_global(&value.span));
|
||||
return Err(LeoError::from(AsgError::invalid_self_in_global(&value.span)));
|
||||
}
|
||||
let function = scope.context.alloc_function(Function {
|
||||
id: scope.context.get_id(),
|
||||
@ -133,7 +132,7 @@ impl<'a> Function<'a> {
|
||||
Ok(function)
|
||||
}
|
||||
|
||||
pub(super) fn fill_from_ast(self: &'a Function<'a>, value: &leo_ast::Function) -> Result<(), AsgConvertError> {
|
||||
pub(super) fn fill_from_ast(self: &'a Function<'a>, value: &leo_ast::Function) -> Result<(), LeoError> {
|
||||
if self.qualifier != FunctionQualifier::Static {
|
||||
let circuit = self.circuit.get();
|
||||
let self_variable = self.scope.context.alloc_variable(RefCell::new(crate::InnerVariable {
|
||||
@ -158,19 +157,19 @@ impl<'a> Function<'a> {
|
||||
let main_block = BlockStatement::from_ast(self.scope, &value.block, None)?;
|
||||
let mut director = MonoidalDirector::new(ReturnPathReducer::new());
|
||||
if !director.reduce_block(&main_block).0 && !self.output.is_unit() {
|
||||
return Err(AsgConvertError::function_missing_return(
|
||||
return Err(LeoError::from(AsgError::function_missing_return(
|
||||
&self.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(
|
||||
return Err(LeoError::from(AsgError::function_return_validation(
|
||||
&self.name.borrow().name,
|
||||
&error,
|
||||
&span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
self.body
|
||||
|
@ -24,18 +24,9 @@ pub use circuit::*;
|
||||
mod function;
|
||||
pub use function::*;
|
||||
|
||||
use crate::{
|
||||
node::FromAst,
|
||||
ArenaNode,
|
||||
AsgContext,
|
||||
AsgConvertError,
|
||||
DefinitionStatement,
|
||||
ImportResolver,
|
||||
Input,
|
||||
Scope,
|
||||
Statement,
|
||||
};
|
||||
use leo_ast::{Identifier, PackageAccess, PackageOrPackages, Span};
|
||||
use crate::{node::FromAst, ArenaNode, AsgContext, DefinitionStatement, ImportResolver, Input, Scope, Statement};
|
||||
use leo_ast::{Identifier, PackageAccess, PackageOrPackages};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::cell::{Cell, RefCell};
|
||||
@ -146,7 +137,7 @@ impl<'a> Program<'a> {
|
||||
context: AsgContext<'a>,
|
||||
program: &leo_ast::Program,
|
||||
import_resolver: &mut T,
|
||||
) -> Result<Program<'a>, AsgConvertError> {
|
||||
) -> Result<Program<'a>, LeoError> {
|
||||
// Recursively extract imported symbols.
|
||||
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
|
||||
for import in program.imports.iter() {
|
||||
@ -172,7 +163,12 @@ impl<'a> Program<'a> {
|
||||
span,
|
||||
)? {
|
||||
Some(x) => x,
|
||||
None => return Err(AsgConvertError::unresolved_import(&*pretty_package, &Span::default())),
|
||||
None => {
|
||||
return Err(LeoError::from(AsgError::unresolved_import(
|
||||
&*pretty_package,
|
||||
&Span::default(),
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
resolved_packages.insert(package.clone(), resolved_package);
|
||||
@ -203,10 +199,10 @@ impl<'a> Program<'a> {
|
||||
} else if let Some(global_const) = resolved_package.global_consts.get(&name) {
|
||||
imported_global_consts.insert(name.clone(), *global_const);
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_import(
|
||||
return Err(LeoError::from(AsgError::unresolved_import(
|
||||
&*format!("{}.{}", pretty_package, name),
|
||||
&span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
ImportSymbol::Alias(name, alias) => {
|
||||
@ -217,10 +213,10 @@ impl<'a> Program<'a> {
|
||||
} else if let Some(global_const) = resolved_package.global_consts.get(&name) {
|
||||
imported_global_consts.insert(alias.clone(), *global_const);
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_import(
|
||||
return Err(LeoError::from(AsgError::unresolved_import(
|
||||
&*format!("{}.{}", pretty_package, name),
|
||||
&span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -285,7 +281,7 @@ impl<'a> Program<'a> {
|
||||
.for_each(|variable_name| assert!(name.contains(&variable_name.identifier.name.to_string())));
|
||||
let gc = <&Statement<'a>>::from_ast(scope, global_const, None)?;
|
||||
if let Statement::Definition(gc) = gc {
|
||||
scope.global_consts.borrow_mut().insert(name.clone(), gc);
|
||||
scope.global_consts.borrow_mut().insert(name.clone(), &gc);
|
||||
}
|
||||
}
|
||||
|
||||
@ -311,7 +307,10 @@ impl<'a> Program<'a> {
|
||||
let name = name.name.to_string();
|
||||
|
||||
if functions.contains_key(&name) {
|
||||
return Err(AsgConvertError::duplicate_function_definition(&name, &function.span));
|
||||
return Err(LeoError::from(AsgError::duplicate_function_definition(
|
||||
&name,
|
||||
&function.span,
|
||||
)));
|
||||
}
|
||||
|
||||
functions.insert(name, asg_function);
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgContext, AsgConvertError, Circuit, DefinitionStatement, Function, Input, Type, Variable};
|
||||
use crate::{AsgContext, Circuit, DefinitionStatement, Function, Input, Type, Variable};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::cell::{Cell, RefCell};
|
||||
@ -173,7 +174,7 @@ impl<'a> Scope<'a> {
|
||||
///
|
||||
/// Returns the type returned by the current scope.
|
||||
///
|
||||
pub fn resolve_ast_type(&self, type_: &leo_ast::Type) -> Result<Type<'a>, AsgConvertError> {
|
||||
pub fn resolve_ast_type(&self, type_: &leo_ast::Type, span: &Span) -> Result<Type<'a>, LeoError> {
|
||||
use leo_ast::Type::*;
|
||||
Ok(match type_ {
|
||||
Address => Type::Address,
|
||||
@ -183,12 +184,12 @@ impl<'a> Scope<'a> {
|
||||
Group => Type::Group,
|
||||
IntegerType(int_type) => Type::Integer(int_type.clone()),
|
||||
Array(sub_type, dimensions) => {
|
||||
let mut item = Box::new(self.resolve_ast_type(&*sub_type)?);
|
||||
let mut item = Box::new(self.resolve_ast_type(&*sub_type, span)?);
|
||||
for dimension in dimensions.0.iter().rev() {
|
||||
let dimension = dimension
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_index_error())?;
|
||||
.map_err(|_| LeoError::from(AsgError::parse_index_error(span)))?;
|
||||
item = Box::new(Type::Array(item, dimension));
|
||||
}
|
||||
*item
|
||||
@ -196,20 +197,20 @@ impl<'a> Scope<'a> {
|
||||
Tuple(sub_types) => Type::Tuple(
|
||||
sub_types
|
||||
.iter()
|
||||
.map(|x| self.resolve_ast_type(x))
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?,
|
||||
.map(|x| self.resolve_ast_type(x, span))
|
||||
.collect::<Result<Vec<_>, LeoError>>()?,
|
||||
),
|
||||
Circuit(name) if name.name.as_ref() == "Self" => Type::Circuit(
|
||||
self.resolve_circuit_self()
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
|
||||
.ok_or_else(|| LeoError::from(AsgError::unresolved_circuit(&name.name, &name.span)))?,
|
||||
),
|
||||
SelfType => Type::Circuit(
|
||||
self.resolve_circuit_self()
|
||||
.ok_or_else(AsgConvertError::reference_self_outside_circuit)?,
|
||||
.ok_or_else(|| LeoError::from(AsgError::reference_self_outside_circuit(span)))?,
|
||||
),
|
||||
Circuit(name) => Type::Circuit(
|
||||
self.resolve_circuit(&name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
|
||||
.ok_or_else(|| LeoError::from(AsgError::unresolved_circuit(&name.name, &name.span)))?,
|
||||
),
|
||||
})
|
||||
}
|
||||
|
@ -15,7 +15,6 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
CircuitMember,
|
||||
ConstInt,
|
||||
ConstValue,
|
||||
@ -27,13 +26,13 @@ use crate::{
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
};
|
||||
pub use leo_ast::AssignOperation;
|
||||
use leo_ast::AssigneeAccess as AstAssigneeAccess;
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -66,25 +65,26 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::AssignStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let (name, span) = (&statement.assignee.identifier.name, &statement.assignee.identifier.span);
|
||||
|
||||
let variable = if name.as_ref() == "input" {
|
||||
if let Some(input) = scope.resolve_input() {
|
||||
input.container
|
||||
} else {
|
||||
return Err(AsgConvertError::InternalError(
|
||||
"attempted to reference input when none is in scope".to_string(),
|
||||
));
|
||||
return Err(LeoError::from(AsgError::illegal_input_variable_reference(
|
||||
"attempted to reference input when none is in scope",
|
||||
&statement.span,
|
||||
)));
|
||||
}
|
||||
} else {
|
||||
scope
|
||||
.resolve_variable(&name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_reference(name, &span))?
|
||||
.ok_or_else(|| LeoError::from(AsgError::unresolved_reference(name, &span)))?
|
||||
};
|
||||
|
||||
if !variable.borrow().mutable {
|
||||
return Err(AsgConvertError::immutable_assignment(&name, &statement.span));
|
||||
return Err(LeoError::from(AsgError::immutable_assignment(&name, &statement.span)));
|
||||
}
|
||||
let mut target_type: Option<PartialType> = Some(variable.borrow().type_.clone().into());
|
||||
|
||||
@ -95,19 +95,15 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
let index_type = Some(PartialType::Integer(None, Some(IntegerType::U32)));
|
||||
let left = left
|
||||
.as_ref()
|
||||
.map(
|
||||
|left: &leo_ast::Expression| -> Result<&'a Expression<'a>, AsgConvertError> {
|
||||
.map(|left: &leo_ast::Expression| -> Result<&'a Expression<'a>, LeoError> {
|
||||
<&Expression<'a>>::from_ast(scope, left, index_type.clone())
|
||||
},
|
||||
)
|
||||
})
|
||||
.transpose()?;
|
||||
let right = right
|
||||
.as_ref()
|
||||
.map(
|
||||
|right: &leo_ast::Expression| -> Result<&'a Expression<'a>, AsgConvertError> {
|
||||
.map(|right: &leo_ast::Expression| -> Result<&'a Expression<'a>, LeoError> {
|
||||
<&Expression<'a>>::from_ast(scope, right, index_type)
|
||||
},
|
||||
)
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
match &target_type {
|
||||
@ -123,29 +119,37 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
) {
|
||||
let left = match left {
|
||||
ConstValue::Int(x) => x.to_usize().ok_or_else(|| {
|
||||
AsgConvertError::invalid_assign_index(&name, &x.to_string(), &statement.span)
|
||||
LeoError::from(AsgError::invalid_assign_index(
|
||||
&name,
|
||||
&x.to_string(),
|
||||
&statement.span,
|
||||
))
|
||||
})?,
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
let right = match right {
|
||||
ConstValue::Int(x) => x.to_usize().ok_or_else(|| {
|
||||
AsgConvertError::invalid_assign_index(&name, &x.to_string(), &statement.span)
|
||||
LeoError::from(AsgError::invalid_assign_index(
|
||||
&name,
|
||||
&x.to_string(),
|
||||
&statement.span,
|
||||
))
|
||||
})?,
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
if right >= left {
|
||||
target_type = Some(PartialType::Array(item.clone(), Some((right - left) as usize)))
|
||||
} else {
|
||||
return Err(AsgConvertError::invalid_backwards_assignment(
|
||||
return Err(LeoError::from(AsgError::invalid_backwards_assignment(
|
||||
&name,
|
||||
left,
|
||||
right,
|
||||
&statement.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)),
|
||||
_ => return Err(LeoError::from(AsgError::index_into_non_array(&name, &statement.span))),
|
||||
}
|
||||
|
||||
AssignAccess::ArrayRange(Cell::new(left), Cell::new(right))
|
||||
@ -153,7 +157,7 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
AstAssigneeAccess::ArrayIndex(index) => {
|
||||
target_type = match target_type.clone() {
|
||||
Some(PartialType::Array(item, _)) => item.map(|x| *x),
|
||||
_ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)),
|
||||
_ => return Err(LeoError::from(AsgError::index_into_non_array(&name, &statement.span))),
|
||||
};
|
||||
AssignAccess::ArrayIndex(Cell::new(<&Expression<'a>>::from_ast(
|
||||
scope,
|
||||
@ -161,17 +165,16 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
Some(PartialType::Integer(None, Some(IntegerType::U32))),
|
||||
)?))
|
||||
}
|
||||
AstAssigneeAccess::Tuple(index, _) => {
|
||||
AstAssigneeAccess::Tuple(index, span) => {
|
||||
let index = index
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_index_error())?;
|
||||
.map_err(|_| LeoError::from(AsgError::parse_index_error(&span)))?;
|
||||
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))?,
|
||||
_ => return Err(AsgConvertError::index_into_non_tuple(&name, &statement.span)),
|
||||
Some(PartialType::Tuple(types)) => types.get(index).cloned().ok_or_else(|| {
|
||||
LeoError::from(AsgError::tuple_index_out_of_bounds(index, &statement.span))
|
||||
})?,
|
||||
_ => return Err(LeoError::from(AsgError::index_into_non_tuple(&name, &statement.span))),
|
||||
};
|
||||
AssignAccess::Tuple(index)
|
||||
}
|
||||
@ -182,26 +185,29 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
|
||||
let members = circuit.members.borrow();
|
||||
let member = members.get(name.name.as_ref()).ok_or_else(|| {
|
||||
AsgConvertError::unresolved_circuit_member(
|
||||
LeoError::from(AsgError::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));
|
||||
return Err(LeoError::from(AsgError::illegal_function_assign(
|
||||
&name.name,
|
||||
&statement.span,
|
||||
)));
|
||||
}
|
||||
};
|
||||
Some(x.partial())
|
||||
}
|
||||
_ => {
|
||||
return Err(AsgConvertError::index_into_non_tuple(
|
||||
return Err(LeoError::from(AsgError::index_into_non_tuple(
|
||||
&statement.assignee.identifier.name,
|
||||
&statement.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
};
|
||||
AssignAccess::Member(name.clone())
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span, Statement};
|
||||
use crate::{FromAst, Node, PartialType, Scope, Statement};
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -37,7 +38,7 @@ impl<'a> FromAst<'a, leo_ast::Block> for BlockStatement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::Block,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let new_scope = scope.make_subscope();
|
||||
|
||||
let mut output = vec![];
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, BlockStatement, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
|
||||
use crate::{BlockStatement, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -38,7 +39,7 @@ impl<'a> FromAst<'a, leo_ast::ConditionalStatement> for ConditionalStatement<'a>
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::ConditionalStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let condition = <&Expression<'a>>::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?;
|
||||
let result = scope.context.alloc_statement(Statement::Block(BlockStatement::from_ast(
|
||||
scope,
|
||||
@ -48,9 +49,7 @@ impl<'a> FromAst<'a, leo_ast::ConditionalStatement> for ConditionalStatement<'a>
|
||||
let next = statement
|
||||
.next
|
||||
.as_deref()
|
||||
.map(|next| -> Result<&'a Statement<'a>, AsgConvertError> {
|
||||
<&'a Statement<'a>>::from_ast(scope, next, None)
|
||||
})
|
||||
.map(|next| -> Result<&'a Statement<'a>, LeoError> { <&'a Statement<'a>>::from_ast(scope, next, None) })
|
||||
.transpose()?;
|
||||
|
||||
Ok(ConditionalStatement {
|
||||
|
@ -14,8 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, CharValue, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
|
||||
use crate::{CharValue, Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||
use leo_ast::ConsoleFunction as AstConsoleFunction;
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -52,7 +53,7 @@ impl<'a> FromAst<'a, leo_ast::ConsoleArgs> for ConsoleArgs<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::ConsoleArgs,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let mut parameters = vec![];
|
||||
for parameter in value.parameters.iter() {
|
||||
parameters.push(Cell::new(<&Expression<'a>>::from_ast(scope, parameter, None)?));
|
||||
@ -80,7 +81,7 @@ impl<'a> FromAst<'a, leo_ast::ConsoleStatement> for ConsoleStatement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::ConsoleStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
Ok(ConsoleStatement {
|
||||
parent: Cell::new(None),
|
||||
span: Some(statement.span.clone()),
|
||||
|
@ -14,20 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
InnerVariable,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
};
|
||||
use crate::{Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Statement, Type, Variable};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
@ -66,11 +54,11 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::DefinitionStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let type_ = statement
|
||||
.type_
|
||||
.as_ref()
|
||||
.map(|x| scope.resolve_ast_type(&x))
|
||||
.map(|x| scope.resolve_ast_type(&x, &statement.span))
|
||||
.transpose()?;
|
||||
|
||||
let value = <&Expression<'a>>::from_ast(scope, &statement.value, type_.clone().map(Into::into))?;
|
||||
@ -83,7 +71,10 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
||||
.collect::<Vec<String>>()
|
||||
.join(" ,");
|
||||
|
||||
return Err(AsgConvertError::invalid_const_assign(&var_names, &statement.span));
|
||||
return Err(LeoError::from(AsgError::invalid_const_assign(
|
||||
&var_names,
|
||||
&statement.span,
|
||||
)));
|
||||
}
|
||||
|
||||
let type_ = type_.or_else(|| value.get_type());
|
||||
@ -92,9 +83,10 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
||||
|
||||
let mut variables = vec![];
|
||||
if statement.variable_names.is_empty() {
|
||||
return Err(AsgConvertError::illegal_ast_structure(
|
||||
return Err(LeoError::from(AsgError::illegal_ast_structure(
|
||||
"cannot have 0 variable names in destructuring tuple",
|
||||
));
|
||||
&statement.span,
|
||||
)));
|
||||
}
|
||||
if statement.variable_names.len() == 1 {
|
||||
// any return type is fine
|
||||
@ -106,11 +98,11 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
||||
output_types.extend(sub_types.clone().into_iter().map(Some).collect::<Vec<_>>());
|
||||
}
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
return Err(LeoError::from(AsgError::unexpected_type(
|
||||
&*format!("{}-ary tuple", statement.variable_names.len()),
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&statement.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -119,8 +111,9 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
||||
variables.push(&*scope.context.alloc_variable(RefCell::new(InnerVariable {
|
||||
id: scope.context.get_id(),
|
||||
name: variable.identifier.clone(),
|
||||
type_:
|
||||
type_.ok_or_else(|| AsgConvertError::unresolved_type(&variable.identifier.name, &statement.span))?,
|
||||
type_: type_.ok_or_else(|| {
|
||||
LeoError::from(AsgError::unresolved_type(&variable.identifier.name, &statement.span))
|
||||
})?,
|
||||
mutable: variable.mutable,
|
||||
const_: false,
|
||||
declaration: crate::VariableDeclaration::Definition,
|
||||
@ -133,10 +126,10 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
|
||||
let mut variables = scope.variables.borrow_mut();
|
||||
let var_name = variable.borrow().name.name.to_string();
|
||||
if variables.contains_key(&var_name) {
|
||||
return Err(AsgConvertError::duplicate_variable_definition(
|
||||
return Err(LeoError::from(AsgError::duplicate_variable_definition(
|
||||
&var_name,
|
||||
&statement.span,
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
variables.insert(var_name, *variable);
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement};
|
||||
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement};
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
@ -36,7 +37,7 @@ impl<'a> FromAst<'a, leo_ast::ExpressionStatement> for ExpressionStatement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::ExpressionStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let expression = <&Expression<'a>>::from_ast(scope, &statement.expression, None)?;
|
||||
|
||||
Ok(ExpressionStatement {
|
||||
|
@ -16,19 +16,8 @@
|
||||
|
||||
use leo_ast::IntegerType;
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
InnerVariable,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Variable,
|
||||
};
|
||||
use crate::{Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Statement, Variable};
|
||||
use leo_errors::{AsgError, LeoError, Span};
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
@ -54,21 +43,21 @@ impl<'a> FromAst<'a, leo_ast::IterationStatement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::IterationStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let expected_index_type = Some(PartialType::Integer(Some(IntegerType::U32), None));
|
||||
let start = <&Expression<'a>>::from_ast(scope, &statement.start, expected_index_type.clone())?;
|
||||
let stop = <&Expression<'a>>::from_ast(scope, &statement.stop, expected_index_type)?;
|
||||
|
||||
// Return an error if start or stop is not constant.
|
||||
if !start.is_consty() {
|
||||
return Err(AsgConvertError::unexpected_nonconst(
|
||||
return Err(LeoError::from(AsgError::unexpected_nonconst(
|
||||
&start.span().cloned().unwrap_or_default(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if !stop.is_consty() {
|
||||
return Err(AsgConvertError::unexpected_nonconst(
|
||||
return Err(LeoError::from(AsgError::unexpected_nonconst(
|
||||
&stop.span().cloned().unwrap_or_default(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
let variable = scope.context.alloc_variable(RefCell::new(InnerVariable {
|
||||
@ -76,7 +65,7 @@ impl<'a> FromAst<'a, leo_ast::IterationStatement> for &'a Statement<'a> {
|
||||
name: statement.variable.clone(),
|
||||
type_: start
|
||||
.get_type()
|
||||
.ok_or_else(|| AsgConvertError::unresolved_type(&statement.variable.name, &statement.span))?,
|
||||
.ok_or_else(|| LeoError::from(AsgError::unresolved_type(&statement.variable.name, &statement.span)))?,
|
||||
mutable: false,
|
||||
const_: true,
|
||||
declaration: crate::VariableDeclaration::IterationDefinition,
|
||||
|
@ -42,7 +42,8 @@ pub use iteration::*;
|
||||
mod return_;
|
||||
pub use return_::*;
|
||||
|
||||
use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span};
|
||||
use crate::{FromAst, Node, PartialType, Scope};
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Statement<'a> {
|
||||
@ -79,7 +80,7 @@ impl<'a> FromAst<'a, leo_ast::Statement> for &'a Statement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
value: &leo_ast::Statement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<&'a Statement<'a>, AsgConvertError> {
|
||||
) -> Result<&'a Statement<'a>, LeoError> {
|
||||
use leo_ast::Statement::*;
|
||||
Ok(match value {
|
||||
Return(statement) => scope
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
|
||||
use crate::{Expression, FromAst, Node, PartialType, Scope, Statement, Type};
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
use std::cell::Cell;
|
||||
#[derive(Clone)]
|
||||
@ -35,7 +36,7 @@ impl<'a> FromAst<'a, leo_ast::ReturnStatement> for ReturnStatement<'a> {
|
||||
scope: &'a Scope<'a>,
|
||||
statement: &leo_ast::ReturnStatement,
|
||||
_expected_type: Option<PartialType<'a>>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let return_type: Option<Type> = scope
|
||||
.resolve_current_function()
|
||||
.map(|x| x.output.clone())
|
||||
|
@ -21,6 +21,14 @@ edition = "2018"
|
||||
path = "../input"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-errors]
|
||||
path = "../errors"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.eyre]
|
||||
version = "0.6.5"
|
||||
default-features = false
|
||||
|
||||
[dependencies.indexmap]
|
||||
version = "1.7.0"
|
||||
features = [ "serde-1" ]
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Span};
|
||||
use crate::Identifier;
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tendril::StrTendril;
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::common::span::Span;
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use serde::de::{Deserialize as SerDeserialize, Deserializer};
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Node, Span};
|
||||
use crate::{Identifier, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::Span;
|
||||
use leo_errors::Span;
|
||||
use leo_input::common::Identifier as InputIdentifier;
|
||||
use tendril::StrTendril;
|
||||
|
||||
|
@ -32,9 +32,6 @@ pub use positive_number::*;
|
||||
pub mod self_keyword;
|
||||
pub use self_keyword::*;
|
||||
|
||||
pub mod span;
|
||||
pub use span::*;
|
||||
|
||||
pub mod spread_or_expression;
|
||||
pub use spread_or_expression::*;
|
||||
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Node, Span};
|
||||
use crate::{Identifier, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Node, Span};
|
||||
use crate::{Identifier, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, Node, Span};
|
||||
use crate::{Expression, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -13,20 +13,3 @@
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
pub mod ast;
|
||||
pub use ast::*;
|
||||
|
||||
pub mod canonicalization;
|
||||
pub use canonicalization::*;
|
||||
|
||||
pub mod combiner;
|
||||
pub use combiner::*;
|
||||
|
||||
pub mod error;
|
||||
pub use error::*;
|
||||
|
||||
pub mod reducer;
|
||||
pub use reducer::*;
|
||||
|
||||
pub trait LeoError {}
|
||||
|
@ -20,16 +20,16 @@ use crate::{
|
||||
GroupValue,
|
||||
Identifier,
|
||||
IntegerType,
|
||||
Node,
|
||||
PositiveNumber,
|
||||
Span,
|
||||
SpreadOrExpression,
|
||||
};
|
||||
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
use crate::Node;
|
||||
|
||||
mod binary;
|
||||
pub use binary::*;
|
||||
mod unary;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Annotation, Block, FunctionInput, Identifier, Node, Span, Type};
|
||||
use crate::{Annotation, Block, FunctionInput, Identifier, Node, Type};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Node, Span, Type};
|
||||
use crate::{Identifier, Node, Type};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{ConstSelfKeyword, FunctionInputVariable, MutSelfKeyword, Node, SelfKeyword, Span};
|
||||
use crate::{ConstSelfKeyword, FunctionInputVariable, MutSelfKeyword, Node, SelfKeyword};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::common::span::Span;
|
||||
use leo_errors::Span;
|
||||
use leo_input::values::{
|
||||
GroupCoordinate as InputGroupCoordinate,
|
||||
Inferred as InputInferred,
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{common::span::Span, groups::GroupCoordinate};
|
||||
use crate::groups::GroupCoordinate;
|
||||
use leo_errors::Span;
|
||||
use leo_input::values::{
|
||||
GroupRepresentation as InputGroupRepresentation,
|
||||
GroupTuple as InputGroupTuple,
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{PackageOrPackages, Span};
|
||||
use crate::PackageOrPackages;
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Span};
|
||||
use crate::Identifier;
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{common::Identifier, PackageAccess, Span};
|
||||
use crate::{common::Identifier, PackageAccess};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{ImportSymbol, Node, Package, Packages, Span};
|
||||
use crate::{ImportSymbol, Node, Package, Packages};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Node, Package, Packages, Span};
|
||||
use crate::{Node, Package, Packages};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{common::Identifier, PackageAccess, Span};
|
||||
use crate::{common::Identifier, PackageAccess};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{ArrayDimensions, Char, CharValue, GroupValue, Span as AstSpan};
|
||||
use crate::{ArrayDimensions, Char, CharValue, GroupValue};
|
||||
use leo_errors::Span as AstSpan;
|
||||
use leo_input::{
|
||||
errors::InputParserError,
|
||||
expressions::{ArrayInitializerExpression, ArrayInlineExpression, Expression, StringExpression, TupleExpression},
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Span, Type};
|
||||
use crate::{Identifier, Type};
|
||||
use leo_errors::Span;
|
||||
use leo_input::parameters::Parameter as GrammarParameter;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
|
@ -20,9 +20,6 @@
|
||||
//! The [`Ast`] type is intended to be parsed and modified by different passes
|
||||
//! of the Leo compiler. The Leo compiler can generate a set of R1CS constraints from any [`Ast`].
|
||||
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
|
||||
pub mod annotation;
|
||||
pub use self::annotation::*;
|
||||
|
||||
@ -35,9 +32,6 @@ pub use self::chars::*;
|
||||
pub mod common;
|
||||
pub use self::common::*;
|
||||
|
||||
pub mod errors;
|
||||
pub use self::errors::*;
|
||||
|
||||
pub mod expression;
|
||||
pub use self::expression::*;
|
||||
|
||||
@ -68,6 +62,10 @@ pub use self::types::*;
|
||||
mod node;
|
||||
pub use node::*;
|
||||
|
||||
use leo_errors::LeoError;
|
||||
|
||||
use eyre::eyre;
|
||||
|
||||
/// The abstract syntax tree (AST) for a Leo program.
|
||||
///
|
||||
/// The [`Ast`] type represents a Leo program as a series of recursive data types.
|
||||
@ -86,7 +84,7 @@ impl Ast {
|
||||
}
|
||||
|
||||
/// Mutates the program ast by preforming canonicalization on it.
|
||||
pub fn canonicalize(&mut self) -> Result<(), AstError> {
|
||||
pub fn canonicalize(&mut self) -> Result<(), LeoError> {
|
||||
self.ast = ReconstructingDirector::new(Canonicalizer::default()).reduce_program(self.as_repr())?;
|
||||
Ok(())
|
||||
}
|
||||
@ -101,26 +99,26 @@ impl Ast {
|
||||
}
|
||||
|
||||
/// Serializes the ast into a JSON string.
|
||||
pub fn to_json_string(&self) -> Result<String, AstError> {
|
||||
Ok(serde_json::to_string_pretty(&self.ast)?)
|
||||
pub fn to_json_string(&self) -> Result<String, LeoError> {
|
||||
Ok(serde_json::to_string_pretty(&self.ast).map_err(|e| eyre!(e))?)
|
||||
}
|
||||
|
||||
pub fn to_json_file(&self, mut path: std::path::PathBuf, file_name: &str) -> Result<(), AstError> {
|
||||
pub fn to_json_file(&self, mut path: std::path::PathBuf, file_name: &str) -> Result<(), LeoError> {
|
||||
path.push(file_name);
|
||||
let file = std::fs::File::create(path)?;
|
||||
let file = std::fs::File::create(path).map_err(|e| eyre!(e))?;
|
||||
let writer = std::io::BufWriter::new(file);
|
||||
serde_json::to_writer_pretty(writer, &self.ast)?;
|
||||
serde_json::to_writer_pretty(writer, &self.ast).map_err(|e| eyre!(e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Deserializes the JSON string into a ast.
|
||||
pub fn from_json_string(json: &str) -> Result<Self, AstError> {
|
||||
let ast: Program = serde_json::from_str(json)?;
|
||||
pub fn from_json_string(json: &str) -> Result<Self, LeoError> {
|
||||
let ast: Program = serde_json::from_str(json).map_err(|e| eyre!(e))?;
|
||||
Ok(Self { ast })
|
||||
}
|
||||
|
||||
pub fn from_json_file(path: std::path::PathBuf) -> Result<Self, AstError> {
|
||||
let data = std::fs::read_to_string(path)?;
|
||||
pub fn from_json_file(path: std::path::PathBuf) -> Result<Self, LeoError> {
|
||||
let data = std::fs::read_to_string(path).map_err(|e| eyre!(e))?;
|
||||
Self::from_json_string(&data)
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::Span;
|
||||
use leo_errors::Span;
|
||||
|
||||
pub trait Node:
|
||||
std::fmt::Debug + std::fmt::Display + Clone + PartialEq + Eq + serde::Serialize + serde::de::DeserializeOwned
|
||||
|
@ -15,6 +15,7 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::*;
|
||||
use leo_errors::{AstError, LeoError, Span};
|
||||
|
||||
/// Replace Self when it is in a enclosing circuit type.
|
||||
/// Error when Self is outside an enclosing circuit type.
|
||||
@ -43,7 +44,7 @@ impl Canonicalizer {
|
||||
start: Expression,
|
||||
accesses: &[AssigneeAccess],
|
||||
span: &Span,
|
||||
) -> Result<Box<Expression>, ReducerError> {
|
||||
) -> Result<Box<Expression>, LeoError> {
|
||||
let mut left = Box::new(start);
|
||||
|
||||
for access in accesses.iter() {
|
||||
@ -83,10 +84,7 @@ impl Canonicalizer {
|
||||
Ok(left)
|
||||
}
|
||||
|
||||
pub fn compound_operation_converstion(
|
||||
&mut self,
|
||||
operation: &AssignOperation,
|
||||
) -> Result<BinaryOperation, ReducerError> {
|
||||
pub fn compound_operation_converstion(&mut self, operation: &AssignOperation) -> Result<BinaryOperation, LeoError> {
|
||||
match operation {
|
||||
AssignOperation::Assign => unreachable!(),
|
||||
AssignOperation::Add => Ok(BinaryOperation::Add),
|
||||
@ -462,13 +460,11 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
self.in_circuit = !self.in_circuit;
|
||||
}
|
||||
|
||||
fn reduce_type(&mut self, _type_: &Type, new: Type, span: &Span) -> Result<Type, ReducerError> {
|
||||
fn reduce_type(&mut self, _type_: &Type, new: Type, span: &Span) -> Result<Type, LeoError> {
|
||||
match new {
|
||||
Type::Array(type_, mut dimensions) => {
|
||||
if dimensions.is_zero() {
|
||||
return Err(ReducerError::from(CanonicalizeError::invalid_array_dimension_size(
|
||||
span,
|
||||
)));
|
||||
return Err(LeoError::from(AstError::invalid_array_dimension_size(span)));
|
||||
}
|
||||
|
||||
let mut next = Type::Array(type_, ArrayDimensions(vec![dimensions.remove_last().unwrap()]));
|
||||
@ -485,16 +481,14 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
|
||||
Ok(array)
|
||||
}
|
||||
Type::SelfType if !self.in_circuit => {
|
||||
Err(ReducerError::from(CanonicalizeError::big_self_outside_of_circuit(span)))
|
||||
}
|
||||
Type::SelfType if !self.in_circuit => Err(LeoError::from(AstError::big_self_outside_of_circuit(span))),
|
||||
_ => Ok(new.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, ReducerError> {
|
||||
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, LeoError> {
|
||||
if string.is_empty() {
|
||||
return Err(ReducerError::empty_string(span));
|
||||
return Err(LeoError::from(AstError::empty_string(span)));
|
||||
}
|
||||
|
||||
let mut elements = Vec::new();
|
||||
@ -529,14 +523,14 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
elements.push(SpreadOrExpression::Expression(Expression::Value(
|
||||
ValueExpression::Char(CharValue {
|
||||
character: character.clone(),
|
||||
span: Span {
|
||||
line_start: span.line_start,
|
||||
line_stop: span.line_stop,
|
||||
span: Span::new(
|
||||
span.line_start,
|
||||
span.line_stop,
|
||||
col_start,
|
||||
col_stop,
|
||||
path: span.path.clone(),
|
||||
content: span.content.clone(),
|
||||
},
|
||||
span.path.clone(),
|
||||
span.content.clone(),
|
||||
),
|
||||
}),
|
||||
)));
|
||||
}
|
||||
@ -551,11 +545,9 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
&mut self,
|
||||
array_init: &ArrayInitExpression,
|
||||
element: Expression,
|
||||
) -> Result<ArrayInitExpression, ReducerError> {
|
||||
) -> Result<ArrayInitExpression, LeoError> {
|
||||
if array_init.dimensions.is_zero() {
|
||||
return Err(ReducerError::from(CanonicalizeError::invalid_array_dimension_size(
|
||||
&array_init.span,
|
||||
)));
|
||||
return Err(LeoError::from(AstError::invalid_array_dimension_size(&array_init.span)));
|
||||
}
|
||||
|
||||
let element = Box::new(element);
|
||||
@ -602,7 +594,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
assign: &AssignStatement,
|
||||
assignee: Assignee,
|
||||
value: Expression,
|
||||
) -> Result<AssignStatement, ReducerError> {
|
||||
) -> Result<AssignStatement, LeoError> {
|
||||
match value {
|
||||
value if assign.operation != AssignOperation::Assign => {
|
||||
let left = self.canonicalize_accesses(
|
||||
@ -644,7 +636,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
input: Vec<FunctionInput>,
|
||||
output: Option<Type>,
|
||||
block: Block,
|
||||
) -> Result<Function, ReducerError> {
|
||||
) -> Result<Function, LeoError> {
|
||||
let new_output = match output {
|
||||
None => Some(Type::Tuple(vec![])),
|
||||
_ => output,
|
||||
@ -665,7 +657,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
_circuit: &Circuit,
|
||||
circuit_name: Identifier,
|
||||
members: Vec<CircuitMember>,
|
||||
) -> Result<Circuit, ReducerError> {
|
||||
) -> Result<Circuit, LeoError> {
|
||||
self.circuit_name = Some(circuit_name.clone());
|
||||
let circ = Circuit {
|
||||
circuit_name,
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
use crate::*;
|
||||
use indexmap::IndexMap;
|
||||
use leo_errors::{AstError, LeoError, Span};
|
||||
|
||||
pub struct ReconstructingDirector<R: ReconstructingReducer> {
|
||||
reducer: R,
|
||||
@ -29,7 +30,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
Self { reducer }
|
||||
}
|
||||
|
||||
pub fn reduce_type(&mut self, type_: &Type, span: &Span) -> Result<Type, ReducerError> {
|
||||
pub fn reduce_type(&mut self, type_: &Type, span: &Span) -> Result<Type, LeoError> {
|
||||
let new = match type_ {
|
||||
Type::Array(type_, dimensions) => Type::Array(Box::new(self.reduce_type(type_, span)?), dimensions.clone()),
|
||||
Type::Tuple(types) => {
|
||||
@ -48,7 +49,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
}
|
||||
|
||||
// Expressions
|
||||
pub fn reduce_expression(&mut self, expression: &Expression) -> Result<Expression, ReducerError> {
|
||||
pub fn reduce_expression(&mut self, expression: &Expression) -> Result<Expression, LeoError> {
|
||||
let new = match expression {
|
||||
Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(&identifier)?),
|
||||
Expression::Value(value) => self.reduce_value(&value)?,
|
||||
@ -83,15 +84,15 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_expression(expression, new)
|
||||
}
|
||||
|
||||
pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, ReducerError> {
|
||||
pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, LeoError> {
|
||||
self.reducer.reduce_identifier(identifier)
|
||||
}
|
||||
|
||||
pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, ReducerError> {
|
||||
pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, LeoError> {
|
||||
self.reducer.reduce_group_tuple(group_tuple)
|
||||
}
|
||||
|
||||
pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> Result<GroupValue, ReducerError> {
|
||||
pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> Result<GroupValue, LeoError> {
|
||||
let new = match group_value {
|
||||
GroupValue::Tuple(group_tuple) => GroupValue::Tuple(self.reduce_group_tuple(&group_tuple)?),
|
||||
_ => group_value.clone(),
|
||||
@ -100,11 +101,11 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_group_value(group_value, new)
|
||||
}
|
||||
|
||||
pub fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, ReducerError> {
|
||||
pub fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, LeoError> {
|
||||
self.reducer.reduce_string(string, span)
|
||||
}
|
||||
|
||||
pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<Expression, ReducerError> {
|
||||
pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<Expression, LeoError> {
|
||||
let new = match value {
|
||||
ValueExpression::Group(group_value) => {
|
||||
Expression::Value(ValueExpression::Group(Box::new(self.reduce_group_value(&group_value)?)))
|
||||
@ -116,20 +117,20 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_value(value, new)
|
||||
}
|
||||
|
||||
pub fn reduce_binary(&mut self, binary: &BinaryExpression) -> Result<BinaryExpression, ReducerError> {
|
||||
pub fn reduce_binary(&mut self, binary: &BinaryExpression) -> Result<BinaryExpression, LeoError> {
|
||||
let left = self.reduce_expression(&binary.left)?;
|
||||
let right = self.reduce_expression(&binary.right)?;
|
||||
|
||||
self.reducer.reduce_binary(binary, left, right, binary.op.clone())
|
||||
}
|
||||
|
||||
pub fn reduce_unary(&mut self, unary: &UnaryExpression) -> Result<UnaryExpression, ReducerError> {
|
||||
pub fn reduce_unary(&mut self, unary: &UnaryExpression) -> Result<UnaryExpression, LeoError> {
|
||||
let inner = self.reduce_expression(&unary.inner)?;
|
||||
|
||||
self.reducer.reduce_unary(unary, inner, unary.op.clone())
|
||||
}
|
||||
|
||||
pub fn reduce_ternary(&mut self, ternary: &TernaryExpression) -> Result<TernaryExpression, ReducerError> {
|
||||
pub fn reduce_ternary(&mut self, ternary: &TernaryExpression) -> Result<TernaryExpression, LeoError> {
|
||||
let condition = self.reduce_expression(&ternary.condition)?;
|
||||
let if_true = self.reduce_expression(&ternary.if_true)?;
|
||||
let if_false = self.reduce_expression(&ternary.if_false)?;
|
||||
@ -137,7 +138,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_ternary(ternary, condition, if_true, if_false)
|
||||
}
|
||||
|
||||
pub fn reduce_cast(&mut self, cast: &CastExpression) -> Result<CastExpression, ReducerError> {
|
||||
pub fn reduce_cast(&mut self, cast: &CastExpression) -> Result<CastExpression, LeoError> {
|
||||
let inner = self.reduce_expression(&cast.inner)?;
|
||||
let target_type = self.reduce_type(&cast.target_type, &cast.span)?;
|
||||
|
||||
@ -147,7 +148,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_array_inline(
|
||||
&mut self,
|
||||
array_inline: &ArrayInlineExpression,
|
||||
) -> Result<ArrayInlineExpression, ReducerError> {
|
||||
) -> Result<ArrayInlineExpression, LeoError> {
|
||||
let mut elements = vec![];
|
||||
for element in array_inline.elements.iter() {
|
||||
let reduced_element = match element {
|
||||
@ -165,7 +166,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_array_inline(array_inline, elements)
|
||||
}
|
||||
|
||||
pub fn reduce_array_init(&mut self, array_init: &ArrayInitExpression) -> Result<ArrayInitExpression, ReducerError> {
|
||||
pub fn reduce_array_init(&mut self, array_init: &ArrayInitExpression) -> Result<ArrayInitExpression, LeoError> {
|
||||
let element = self.reduce_expression(&array_init.element)?;
|
||||
|
||||
self.reducer.reduce_array_init(array_init, element)
|
||||
@ -174,7 +175,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_array_access(
|
||||
&mut self,
|
||||
array_access: &ArrayAccessExpression,
|
||||
) -> Result<ArrayAccessExpression, ReducerError> {
|
||||
) -> Result<ArrayAccessExpression, LeoError> {
|
||||
let array = self.reduce_expression(&array_access.array)?;
|
||||
let index = self.reduce_expression(&array_access.index)?;
|
||||
|
||||
@ -184,7 +185,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_array_range_access(
|
||||
&mut self,
|
||||
array_range_access: &ArrayRangeAccessExpression,
|
||||
) -> Result<ArrayRangeAccessExpression, ReducerError> {
|
||||
) -> Result<ArrayRangeAccessExpression, LeoError> {
|
||||
let array = self.reduce_expression(&array_range_access.array)?;
|
||||
let left = array_range_access
|
||||
.left
|
||||
@ -201,7 +202,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
.reduce_array_range_access(array_range_access, array, left, right)
|
||||
}
|
||||
|
||||
pub fn reduce_tuple_init(&mut self, tuple_init: &TupleInitExpression) -> Result<TupleInitExpression, ReducerError> {
|
||||
pub fn reduce_tuple_init(&mut self, tuple_init: &TupleInitExpression) -> Result<TupleInitExpression, LeoError> {
|
||||
let mut elements = vec![];
|
||||
for element in tuple_init.elements.iter() {
|
||||
elements.push(self.reduce_expression(element)?);
|
||||
@ -213,7 +214,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_tuple_access(
|
||||
&mut self,
|
||||
tuple_access: &TupleAccessExpression,
|
||||
) -> Result<TupleAccessExpression, ReducerError> {
|
||||
) -> Result<TupleAccessExpression, LeoError> {
|
||||
let tuple = self.reduce_expression(&tuple_access.tuple)?;
|
||||
|
||||
self.reducer.reduce_tuple_access(tuple_access, tuple)
|
||||
@ -222,7 +223,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_circuit_implied_variable_definition(
|
||||
&mut self,
|
||||
variable: &CircuitImpliedVariableDefinition,
|
||||
) -> Result<CircuitImpliedVariableDefinition, ReducerError> {
|
||||
) -> Result<CircuitImpliedVariableDefinition, LeoError> {
|
||||
let identifier = self.reduce_identifier(&variable.identifier)?;
|
||||
let expression = variable
|
||||
.expression
|
||||
@ -237,7 +238,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_circuit_init(
|
||||
&mut self,
|
||||
circuit_init: &CircuitInitExpression,
|
||||
) -> Result<CircuitInitExpression, ReducerError> {
|
||||
) -> Result<CircuitInitExpression, LeoError> {
|
||||
let name = self.reduce_identifier(&circuit_init.name)?;
|
||||
|
||||
let mut members = vec![];
|
||||
@ -251,7 +252,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_circuit_member_access(
|
||||
&mut self,
|
||||
circuit_member_access: &CircuitMemberAccessExpression,
|
||||
) -> Result<CircuitMemberAccessExpression, ReducerError> {
|
||||
) -> Result<CircuitMemberAccessExpression, LeoError> {
|
||||
let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
|
||||
let name = self.reduce_identifier(&circuit_member_access.name)?;
|
||||
|
||||
@ -262,7 +263,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_circuit_static_fn_access(
|
||||
&mut self,
|
||||
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
||||
) -> Result<CircuitStaticFunctionAccessExpression, ReducerError> {
|
||||
) -> Result<CircuitStaticFunctionAccessExpression, LeoError> {
|
||||
let circuit = self.reduce_expression(&circuit_static_fn_access.circuit)?;
|
||||
let name = self.reduce_identifier(&circuit_static_fn_access.name)?;
|
||||
|
||||
@ -270,7 +271,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
.reduce_circuit_static_fn_access(circuit_static_fn_access, circuit, name)
|
||||
}
|
||||
|
||||
pub fn reduce_call(&mut self, call: &CallExpression) -> Result<CallExpression, ReducerError> {
|
||||
pub fn reduce_call(&mut self, call: &CallExpression) -> Result<CallExpression, LeoError> {
|
||||
let function = self.reduce_expression(&call.function)?;
|
||||
|
||||
let mut arguments = vec![];
|
||||
@ -282,7 +283,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
}
|
||||
|
||||
// Statements
|
||||
pub fn reduce_statement(&mut self, statement: &Statement) -> Result<Statement, ReducerError> {
|
||||
pub fn reduce_statement(&mut self, statement: &Statement) -> Result<Statement, LeoError> {
|
||||
let new = match statement {
|
||||
Statement::Return(return_statement) => Statement::Return(self.reduce_return(&return_statement)?),
|
||||
Statement::Definition(definition) => Statement::Definition(self.reduce_definition(&definition)?),
|
||||
@ -297,19 +298,19 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_statement(statement, new)
|
||||
}
|
||||
|
||||
pub fn reduce_return(&mut self, return_statement: &ReturnStatement) -> Result<ReturnStatement, ReducerError> {
|
||||
pub fn reduce_return(&mut self, return_statement: &ReturnStatement) -> Result<ReturnStatement, LeoError> {
|
||||
let expression = self.reduce_expression(&return_statement.expression)?;
|
||||
|
||||
self.reducer.reduce_return(return_statement, expression)
|
||||
}
|
||||
|
||||
pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> Result<VariableName, ReducerError> {
|
||||
pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> Result<VariableName, LeoError> {
|
||||
let identifier = self.reduce_identifier(&variable_name.identifier)?;
|
||||
|
||||
self.reducer.reduce_variable_name(variable_name, identifier)
|
||||
}
|
||||
|
||||
pub fn reduce_definition(&mut self, definition: &DefinitionStatement) -> Result<DefinitionStatement, ReducerError> {
|
||||
pub fn reduce_definition(&mut self, definition: &DefinitionStatement) -> Result<DefinitionStatement, LeoError> {
|
||||
let mut variable_names = vec![];
|
||||
for variable_name in definition.variable_names.iter() {
|
||||
variable_names.push(self.reduce_variable_name(variable_name)?);
|
||||
@ -326,7 +327,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_definition(definition, variable_names, type_, value)
|
||||
}
|
||||
|
||||
pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess) -> Result<AssigneeAccess, ReducerError> {
|
||||
pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess) -> Result<AssigneeAccess, LeoError> {
|
||||
let new = match access {
|
||||
AssigneeAccess::ArrayRange(left, right) => {
|
||||
let left = left.as_ref().map(|left| self.reduce_expression(left)).transpose()?;
|
||||
@ -342,7 +343,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_assignee_access(access, new)
|
||||
}
|
||||
|
||||
pub fn reduce_assignee(&mut self, assignee: &Assignee) -> Result<Assignee, ReducerError> {
|
||||
pub fn reduce_assignee(&mut self, assignee: &Assignee) -> Result<Assignee, LeoError> {
|
||||
let identifier = self.reduce_identifier(&assignee.identifier)?;
|
||||
|
||||
let mut accesses = vec![];
|
||||
@ -353,17 +354,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_assignee(assignee, identifier, accesses)
|
||||
}
|
||||
|
||||
pub fn reduce_assign(&mut self, assign: &AssignStatement) -> Result<AssignStatement, ReducerError> {
|
||||
pub fn reduce_assign(&mut self, assign: &AssignStatement) -> Result<AssignStatement, LeoError> {
|
||||
let assignee = self.reduce_assignee(&assign.assignee)?;
|
||||
let value = self.reduce_expression(&assign.value)?;
|
||||
|
||||
self.reducer.reduce_assign(assign, assignee, value)
|
||||
}
|
||||
|
||||
pub fn reduce_conditional(
|
||||
&mut self,
|
||||
conditional: &ConditionalStatement,
|
||||
) -> Result<ConditionalStatement, ReducerError> {
|
||||
pub fn reduce_conditional(&mut self, conditional: &ConditionalStatement) -> Result<ConditionalStatement, LeoError> {
|
||||
let condition = self.reduce_expression(&conditional.condition)?;
|
||||
let block = self.reduce_block(&conditional.block)?;
|
||||
let next = conditional
|
||||
@ -375,7 +373,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_conditional(conditional, condition, block, next)
|
||||
}
|
||||
|
||||
pub fn reduce_iteration(&mut self, iteration: &IterationStatement) -> Result<IterationStatement, ReducerError> {
|
||||
pub fn reduce_iteration(&mut self, iteration: &IterationStatement) -> Result<IterationStatement, LeoError> {
|
||||
let variable = self.reduce_identifier(&iteration.variable)?;
|
||||
let start = self.reduce_expression(&iteration.start)?;
|
||||
let stop = self.reduce_expression(&iteration.stop)?;
|
||||
@ -384,10 +382,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_iteration(iteration, variable, start, stop, block)
|
||||
}
|
||||
|
||||
pub fn reduce_console(
|
||||
&mut self,
|
||||
console_function_call: &ConsoleStatement,
|
||||
) -> Result<ConsoleStatement, ReducerError> {
|
||||
pub fn reduce_console(&mut self, console_function_call: &ConsoleStatement) -> Result<ConsoleStatement, LeoError> {
|
||||
let function = match &console_function_call.function {
|
||||
ConsoleFunction::Assert(expression) => ConsoleFunction::Assert(self.reduce_expression(expression)?),
|
||||
ConsoleFunction::Error(args) | ConsoleFunction::Log(args) => {
|
||||
@ -405,7 +400,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
match &console_function_call.function {
|
||||
ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted),
|
||||
ConsoleFunction::Log(_) => ConsoleFunction::Log(formatted),
|
||||
_ => return Err(ReducerError::impossible_console_assert_call(&args.span)),
|
||||
_ => return Err(LeoError::from(AstError::impossible_console_assert_call(&args.span))),
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -416,12 +411,12 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_expression_statement(
|
||||
&mut self,
|
||||
expression: &ExpressionStatement,
|
||||
) -> Result<ExpressionStatement, ReducerError> {
|
||||
) -> Result<ExpressionStatement, LeoError> {
|
||||
let inner_expression = self.reduce_expression(&expression.expression)?;
|
||||
self.reducer.reduce_expression_statement(expression, inner_expression)
|
||||
}
|
||||
|
||||
pub fn reduce_block(&mut self, block: &Block) -> Result<Block, ReducerError> {
|
||||
pub fn reduce_block(&mut self, block: &Block) -> Result<Block, LeoError> {
|
||||
let mut statements = vec![];
|
||||
for statement in block.statements.iter() {
|
||||
statements.push(self.reduce_statement(statement)?);
|
||||
@ -431,7 +426,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
}
|
||||
|
||||
// Program
|
||||
pub fn reduce_program(&mut self, program: &Program) -> Result<Program, ReducerError> {
|
||||
pub fn reduce_program(&mut self, program: &Program) -> Result<Program, LeoError> {
|
||||
let mut inputs = vec![];
|
||||
for input in program.expected_input.iter() {
|
||||
inputs.push(self.reduce_function_input(input)?);
|
||||
@ -466,14 +461,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_function_input_variable(
|
||||
&mut self,
|
||||
variable: &FunctionInputVariable,
|
||||
) -> Result<FunctionInputVariable, ReducerError> {
|
||||
) -> Result<FunctionInputVariable, LeoError> {
|
||||
let identifier = self.reduce_identifier(&variable.identifier)?;
|
||||
let type_ = self.reduce_type(&variable.type_, &variable.span)?;
|
||||
|
||||
self.reducer.reduce_function_input_variable(variable, identifier, type_)
|
||||
}
|
||||
|
||||
pub fn reduce_function_input(&mut self, input: &FunctionInput) -> Result<FunctionInput, ReducerError> {
|
||||
pub fn reduce_function_input(&mut self, input: &FunctionInput) -> Result<FunctionInput, LeoError> {
|
||||
let new = match input {
|
||||
FunctionInput::Variable(function_input_variable) => {
|
||||
FunctionInput::Variable(self.reduce_function_input_variable(function_input_variable)?)
|
||||
@ -487,7 +482,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
pub fn reduce_package_or_packages(
|
||||
&mut self,
|
||||
package_or_packages: &PackageOrPackages,
|
||||
) -> Result<PackageOrPackages, ReducerError> {
|
||||
) -> Result<PackageOrPackages, LeoError> {
|
||||
let new = match package_or_packages {
|
||||
PackageOrPackages::Package(package) => PackageOrPackages::Package(Package {
|
||||
name: self.reduce_identifier(&package.name)?,
|
||||
@ -504,13 +499,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_package_or_packages(package_or_packages, new)
|
||||
}
|
||||
|
||||
pub fn reduce_import(&mut self, import: &ImportStatement) -> Result<ImportStatement, ReducerError> {
|
||||
pub fn reduce_import(&mut self, import: &ImportStatement) -> Result<ImportStatement, LeoError> {
|
||||
let package_or_packages = self.reduce_package_or_packages(&import.package_or_packages)?;
|
||||
|
||||
self.reducer.reduce_import(import, package_or_packages)
|
||||
}
|
||||
|
||||
pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember, ReducerError> {
|
||||
pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember, LeoError> {
|
||||
let new = match circuit_member {
|
||||
CircuitMember::CircuitVariable(identifier, type_) => CircuitMember::CircuitVariable(
|
||||
self.reduce_identifier(&identifier)?,
|
||||
@ -524,7 +519,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_circuit_member(circuit_member, new)
|
||||
}
|
||||
|
||||
pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Result<Circuit, ReducerError> {
|
||||
pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Result<Circuit, LeoError> {
|
||||
let circuit_name = self.reduce_identifier(&circuit.circuit_name)?;
|
||||
|
||||
let mut members = vec![];
|
||||
@ -535,13 +530,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
self.reducer.reduce_circuit(circuit, circuit_name, members)
|
||||
}
|
||||
|
||||
fn reduce_annotation(&mut self, annotation: &Annotation) -> Result<Annotation, ReducerError> {
|
||||
fn reduce_annotation(&mut self, annotation: &Annotation) -> Result<Annotation, LeoError> {
|
||||
let name = self.reduce_identifier(&annotation.name)?;
|
||||
|
||||
self.reducer.reduce_annotation(annotation, name)
|
||||
}
|
||||
|
||||
pub fn reduce_function(&mut self, function: &Function) -> Result<Function, ReducerError> {
|
||||
pub fn reduce_function(&mut self, function: &Function) -> Result<Function, LeoError> {
|
||||
let identifier = self.reduce_identifier(&function.identifier)?;
|
||||
|
||||
let mut annotations = vec![];
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
use crate::*;
|
||||
use indexmap::IndexMap;
|
||||
use leo_errors::{LeoError, Span};
|
||||
|
||||
// Needed to fix clippy bug.
|
||||
#[allow(clippy::redundant_closure)]
|
||||
@ -23,23 +24,23 @@ pub trait ReconstructingReducer {
|
||||
fn in_circuit(&self) -> bool;
|
||||
fn swap_in_circuit(&mut self);
|
||||
|
||||
fn reduce_type(&mut self, _type_: &Type, new: Type, _span: &Span) -> Result<Type, ReducerError> {
|
||||
fn reduce_type(&mut self, _type_: &Type, new: Type, _span: &Span) -> Result<Type, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
// Expressions
|
||||
fn reduce_expression(&mut self, _expression: &Expression, new: Expression) -> Result<Expression, ReducerError> {
|
||||
fn reduce_expression(&mut self, _expression: &Expression, new: Expression) -> Result<Expression, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, ReducerError> {
|
||||
fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, LeoError> {
|
||||
Ok(Identifier {
|
||||
name: identifier.name.clone(),
|
||||
span: identifier.span.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, ReducerError> {
|
||||
fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, LeoError> {
|
||||
Ok(GroupTuple {
|
||||
x: group_tuple.x.clone(),
|
||||
y: group_tuple.y.clone(),
|
||||
@ -47,18 +48,18 @@ pub trait ReconstructingReducer {
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_group_value(&mut self, _group_value: &GroupValue, new: GroupValue) -> Result<GroupValue, ReducerError> {
|
||||
fn reduce_group_value(&mut self, _group_value: &GroupValue, new: GroupValue) -> Result<GroupValue, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, ReducerError> {
|
||||
fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result<Expression, LeoError> {
|
||||
Ok(Expression::Value(ValueExpression::String(
|
||||
string.to_vec(),
|
||||
span.clone(),
|
||||
)))
|
||||
}
|
||||
|
||||
fn reduce_value(&mut self, _value: &ValueExpression, new: Expression) -> Result<Expression, ReducerError> {
|
||||
fn reduce_value(&mut self, _value: &ValueExpression, new: Expression) -> Result<Expression, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -68,7 +69,7 @@ pub trait ReconstructingReducer {
|
||||
left: Expression,
|
||||
right: Expression,
|
||||
op: BinaryOperation,
|
||||
) -> Result<BinaryExpression, ReducerError> {
|
||||
) -> Result<BinaryExpression, LeoError> {
|
||||
Ok(BinaryExpression {
|
||||
left: Box::new(left),
|
||||
right: Box::new(right),
|
||||
@ -82,7 +83,7 @@ pub trait ReconstructingReducer {
|
||||
unary: &UnaryExpression,
|
||||
inner: Expression,
|
||||
op: UnaryOperation,
|
||||
) -> Result<UnaryExpression, ReducerError> {
|
||||
) -> Result<UnaryExpression, LeoError> {
|
||||
Ok(UnaryExpression {
|
||||
inner: Box::new(inner),
|
||||
op,
|
||||
@ -96,7 +97,7 @@ pub trait ReconstructingReducer {
|
||||
condition: Expression,
|
||||
if_true: Expression,
|
||||
if_false: Expression,
|
||||
) -> Result<TernaryExpression, ReducerError> {
|
||||
) -> Result<TernaryExpression, LeoError> {
|
||||
Ok(TernaryExpression {
|
||||
condition: Box::new(condition),
|
||||
if_true: Box::new(if_true),
|
||||
@ -110,7 +111,7 @@ pub trait ReconstructingReducer {
|
||||
cast: &CastExpression,
|
||||
inner: Expression,
|
||||
target_type: Type,
|
||||
) -> Result<CastExpression, ReducerError> {
|
||||
) -> Result<CastExpression, LeoError> {
|
||||
Ok(CastExpression {
|
||||
inner: Box::new(inner),
|
||||
target_type,
|
||||
@ -122,7 +123,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
array_inline: &ArrayInlineExpression,
|
||||
elements: Vec<SpreadOrExpression>,
|
||||
) -> Result<ArrayInlineExpression, ReducerError> {
|
||||
) -> Result<ArrayInlineExpression, LeoError> {
|
||||
Ok(ArrayInlineExpression {
|
||||
elements,
|
||||
span: array_inline.span.clone(),
|
||||
@ -133,7 +134,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
array_init: &ArrayInitExpression,
|
||||
element: Expression,
|
||||
) -> Result<ArrayInitExpression, ReducerError> {
|
||||
) -> Result<ArrayInitExpression, LeoError> {
|
||||
Ok(ArrayInitExpression {
|
||||
element: Box::new(element),
|
||||
dimensions: array_init.dimensions.clone(),
|
||||
@ -146,7 +147,7 @@ pub trait ReconstructingReducer {
|
||||
array_access: &ArrayAccessExpression,
|
||||
array: Expression,
|
||||
index: Expression,
|
||||
) -> Result<ArrayAccessExpression, ReducerError> {
|
||||
) -> Result<ArrayAccessExpression, LeoError> {
|
||||
Ok(ArrayAccessExpression {
|
||||
array: Box::new(array),
|
||||
index: Box::new(index),
|
||||
@ -160,7 +161,7 @@ pub trait ReconstructingReducer {
|
||||
array: Expression,
|
||||
left: Option<Expression>,
|
||||
right: Option<Expression>,
|
||||
) -> Result<ArrayRangeAccessExpression, ReducerError> {
|
||||
) -> Result<ArrayRangeAccessExpression, LeoError> {
|
||||
Ok(ArrayRangeAccessExpression {
|
||||
array: Box::new(array),
|
||||
left: left.map(|expr| Box::new(expr)),
|
||||
@ -173,7 +174,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
tuple_init: &TupleInitExpression,
|
||||
elements: Vec<Expression>,
|
||||
) -> Result<TupleInitExpression, ReducerError> {
|
||||
) -> Result<TupleInitExpression, LeoError> {
|
||||
Ok(TupleInitExpression {
|
||||
elements,
|
||||
span: tuple_init.span.clone(),
|
||||
@ -184,7 +185,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
tuple_access: &TupleAccessExpression,
|
||||
tuple: Expression,
|
||||
) -> Result<TupleAccessExpression, ReducerError> {
|
||||
) -> Result<TupleAccessExpression, LeoError> {
|
||||
Ok(TupleAccessExpression {
|
||||
tuple: Box::new(tuple),
|
||||
index: tuple_access.index.clone(),
|
||||
@ -197,7 +198,7 @@ pub trait ReconstructingReducer {
|
||||
_variable: &CircuitImpliedVariableDefinition,
|
||||
identifier: Identifier,
|
||||
expression: Option<Expression>,
|
||||
) -> Result<CircuitImpliedVariableDefinition, ReducerError> {
|
||||
) -> Result<CircuitImpliedVariableDefinition, LeoError> {
|
||||
Ok(CircuitImpliedVariableDefinition { identifier, expression })
|
||||
}
|
||||
|
||||
@ -206,7 +207,7 @@ pub trait ReconstructingReducer {
|
||||
circuit_init: &CircuitInitExpression,
|
||||
name: Identifier,
|
||||
members: Vec<CircuitImpliedVariableDefinition>,
|
||||
) -> Result<CircuitInitExpression, ReducerError> {
|
||||
) -> Result<CircuitInitExpression, LeoError> {
|
||||
Ok(CircuitInitExpression {
|
||||
name,
|
||||
members,
|
||||
@ -219,7 +220,7 @@ pub trait ReconstructingReducer {
|
||||
circuit_member_access: &CircuitMemberAccessExpression,
|
||||
circuit: Expression,
|
||||
name: Identifier,
|
||||
) -> Result<CircuitMemberAccessExpression, ReducerError> {
|
||||
) -> Result<CircuitMemberAccessExpression, LeoError> {
|
||||
Ok(CircuitMemberAccessExpression {
|
||||
circuit: Box::new(circuit),
|
||||
name,
|
||||
@ -232,7 +233,7 @@ pub trait ReconstructingReducer {
|
||||
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
|
||||
circuit: Expression,
|
||||
name: Identifier,
|
||||
) -> Result<CircuitStaticFunctionAccessExpression, ReducerError> {
|
||||
) -> Result<CircuitStaticFunctionAccessExpression, LeoError> {
|
||||
Ok(CircuitStaticFunctionAccessExpression {
|
||||
circuit: Box::new(circuit),
|
||||
name,
|
||||
@ -245,7 +246,7 @@ pub trait ReconstructingReducer {
|
||||
call: &CallExpression,
|
||||
function: Expression,
|
||||
arguments: Vec<Expression>,
|
||||
) -> Result<CallExpression, ReducerError> {
|
||||
) -> Result<CallExpression, LeoError> {
|
||||
Ok(CallExpression {
|
||||
function: Box::new(function),
|
||||
arguments,
|
||||
@ -254,7 +255,7 @@ pub trait ReconstructingReducer {
|
||||
}
|
||||
|
||||
// Statements
|
||||
fn reduce_statement(&mut self, _statement: &Statement, new: Statement) -> Result<Statement, ReducerError> {
|
||||
fn reduce_statement(&mut self, _statement: &Statement, new: Statement) -> Result<Statement, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -262,7 +263,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
return_statement: &ReturnStatement,
|
||||
expression: Expression,
|
||||
) -> Result<ReturnStatement, ReducerError> {
|
||||
) -> Result<ReturnStatement, LeoError> {
|
||||
Ok(ReturnStatement {
|
||||
expression,
|
||||
span: return_statement.span.clone(),
|
||||
@ -273,7 +274,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
variable_name: &VariableName,
|
||||
identifier: Identifier,
|
||||
) -> Result<VariableName, ReducerError> {
|
||||
) -> Result<VariableName, LeoError> {
|
||||
Ok(VariableName {
|
||||
mutable: variable_name.mutable,
|
||||
identifier,
|
||||
@ -287,7 +288,7 @@ pub trait ReconstructingReducer {
|
||||
variable_names: Vec<VariableName>,
|
||||
type_: Option<Type>,
|
||||
value: Expression,
|
||||
) -> Result<DefinitionStatement, ReducerError> {
|
||||
) -> Result<DefinitionStatement, LeoError> {
|
||||
Ok(DefinitionStatement {
|
||||
declaration_type: definition.declaration_type.clone(),
|
||||
variable_names,
|
||||
@ -301,7 +302,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
_access: &AssigneeAccess,
|
||||
new: AssigneeAccess,
|
||||
) -> Result<AssigneeAccess, ReducerError> {
|
||||
) -> Result<AssigneeAccess, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -310,7 +311,7 @@ pub trait ReconstructingReducer {
|
||||
assignee: &Assignee,
|
||||
identifier: Identifier,
|
||||
accesses: Vec<AssigneeAccess>,
|
||||
) -> Result<Assignee, ReducerError> {
|
||||
) -> Result<Assignee, LeoError> {
|
||||
Ok(Assignee {
|
||||
identifier,
|
||||
accesses,
|
||||
@ -323,7 +324,7 @@ pub trait ReconstructingReducer {
|
||||
assign: &AssignStatement,
|
||||
assignee: Assignee,
|
||||
value: Expression,
|
||||
) -> Result<AssignStatement, ReducerError> {
|
||||
) -> Result<AssignStatement, LeoError> {
|
||||
Ok(AssignStatement {
|
||||
operation: assign.operation,
|
||||
assignee,
|
||||
@ -338,7 +339,7 @@ pub trait ReconstructingReducer {
|
||||
condition: Expression,
|
||||
block: Block,
|
||||
statement: Option<Statement>,
|
||||
) -> Result<ConditionalStatement, ReducerError> {
|
||||
) -> Result<ConditionalStatement, LeoError> {
|
||||
Ok(ConditionalStatement {
|
||||
condition,
|
||||
block,
|
||||
@ -354,7 +355,7 @@ pub trait ReconstructingReducer {
|
||||
start: Expression,
|
||||
stop: Expression,
|
||||
block: Block,
|
||||
) -> Result<IterationStatement, ReducerError> {
|
||||
) -> Result<IterationStatement, LeoError> {
|
||||
Ok(IterationStatement {
|
||||
variable,
|
||||
start,
|
||||
@ -369,7 +370,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
console: &ConsoleStatement,
|
||||
function: ConsoleFunction,
|
||||
) -> Result<ConsoleStatement, ReducerError> {
|
||||
) -> Result<ConsoleStatement, LeoError> {
|
||||
Ok(ConsoleStatement {
|
||||
function,
|
||||
span: console.span.clone(),
|
||||
@ -380,14 +381,14 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
expression_statement: &ExpressionStatement,
|
||||
expression: Expression,
|
||||
) -> Result<ExpressionStatement, ReducerError> {
|
||||
) -> Result<ExpressionStatement, LeoError> {
|
||||
Ok(ExpressionStatement {
|
||||
expression,
|
||||
span: expression_statement.span.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_block(&mut self, block: &Block, statements: Vec<Statement>) -> Result<Block, ReducerError> {
|
||||
fn reduce_block(&mut self, block: &Block, statements: Vec<Statement>) -> Result<Block, LeoError> {
|
||||
Ok(Block {
|
||||
statements,
|
||||
span: block.span.clone(),
|
||||
@ -403,7 +404,7 @@ pub trait ReconstructingReducer {
|
||||
circuits: IndexMap<Identifier, Circuit>,
|
||||
functions: IndexMap<Identifier, Function>,
|
||||
global_consts: IndexMap<String, DefinitionStatement>,
|
||||
) -> Result<Program, ReducerError> {
|
||||
) -> Result<Program, LeoError> {
|
||||
Ok(Program {
|
||||
name: program.name.clone(),
|
||||
expected_input,
|
||||
@ -419,7 +420,7 @@ pub trait ReconstructingReducer {
|
||||
variable: &FunctionInputVariable,
|
||||
identifier: Identifier,
|
||||
type_: Type,
|
||||
) -> Result<FunctionInputVariable, ReducerError> {
|
||||
) -> Result<FunctionInputVariable, LeoError> {
|
||||
Ok(FunctionInputVariable {
|
||||
identifier,
|
||||
const_: variable.const_,
|
||||
@ -429,11 +430,7 @@ pub trait ReconstructingReducer {
|
||||
})
|
||||
}
|
||||
|
||||
fn reduce_function_input(
|
||||
&mut self,
|
||||
_input: &FunctionInput,
|
||||
new: FunctionInput,
|
||||
) -> Result<FunctionInput, ReducerError> {
|
||||
fn reduce_function_input(&mut self, _input: &FunctionInput, new: FunctionInput) -> Result<FunctionInput, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -441,7 +438,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
_package_or_packages: &PackageOrPackages,
|
||||
new: PackageOrPackages,
|
||||
) -> Result<PackageOrPackages, ReducerError> {
|
||||
) -> Result<PackageOrPackages, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -449,7 +446,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
import: &ImportStatement,
|
||||
package_or_packages: PackageOrPackages,
|
||||
) -> Result<ImportStatement, ReducerError> {
|
||||
) -> Result<ImportStatement, LeoError> {
|
||||
Ok(ImportStatement {
|
||||
package_or_packages,
|
||||
span: import.span.clone(),
|
||||
@ -460,7 +457,7 @@ pub trait ReconstructingReducer {
|
||||
&mut self,
|
||||
_circuit_member: &CircuitMember,
|
||||
new: CircuitMember,
|
||||
) -> Result<CircuitMember, ReducerError> {
|
||||
) -> Result<CircuitMember, LeoError> {
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
@ -469,11 +466,11 @@ pub trait ReconstructingReducer {
|
||||
_circuit: &Circuit,
|
||||
circuit_name: Identifier,
|
||||
members: Vec<CircuitMember>,
|
||||
) -> Result<Circuit, ReducerError> {
|
||||
) -> Result<Circuit, LeoError> {
|
||||
Ok(Circuit { circuit_name, members })
|
||||
}
|
||||
|
||||
fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Result<Annotation, ReducerError> {
|
||||
fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Result<Annotation, LeoError> {
|
||||
Ok(Annotation {
|
||||
span: annotation.span.clone(),
|
||||
name,
|
||||
@ -490,7 +487,7 @@ pub trait ReconstructingReducer {
|
||||
input: Vec<FunctionInput>,
|
||||
output: Option<Type>,
|
||||
block: Block,
|
||||
) -> Result<Function, ReducerError> {
|
||||
) -> Result<Function, LeoError> {
|
||||
Ok(Function {
|
||||
identifier,
|
||||
annotations,
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, Identifier, PositiveNumber, Span};
|
||||
use crate::{Expression, Identifier, PositiveNumber};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, Node, Span};
|
||||
use crate::{Expression, Node};
|
||||
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Node, Span, Statement};
|
||||
use crate::{Node, Statement};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Block, Expression, Node, Span, Statement};
|
||||
use crate::{Block, Expression, Node, Statement};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Char, Expression, Node, Span};
|
||||
use crate::{Char, Expression, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{ConsoleArgs, Expression, Node, Span};
|
||||
use crate::{ConsoleArgs, Expression, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{ConsoleFunction, Node, Span};
|
||||
use crate::{ConsoleFunction, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, Node, Span, Type};
|
||||
use crate::{Expression, Node, Type};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Identifier, Node, Span};
|
||||
use crate::{Identifier, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, Node, Span};
|
||||
use crate::{Expression, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Block, Expression, Identifier, Node, Span};
|
||||
use crate::{Block, Expression, Identifier, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, Node, Span};
|
||||
use crate::{Expression, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{ConditionalStatement, Node, Span};
|
||||
use crate::{ConditionalStatement, Node};
|
||||
use leo_errors::Span;
|
||||
|
||||
use super::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -21,6 +21,10 @@ edition = "2018"
|
||||
path = "../ast"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-errors]
|
||||
path = "../errors"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-imports]
|
||||
path = "../imports"
|
||||
version = "1.5.3"
|
||||
|
@ -17,7 +17,6 @@
|
||||
//! Compiles a Leo program from a file path.
|
||||
use crate::{
|
||||
constraints::{generate_constraints, generate_test_constraints},
|
||||
errors::CompilerError,
|
||||
AstSnapshotOptions,
|
||||
CompilerOptions,
|
||||
GroupType,
|
||||
@ -26,8 +25,9 @@ use crate::{
|
||||
TypeInferencePhase,
|
||||
};
|
||||
pub use leo_asg::{new_context, AsgContext as Context, AsgContext};
|
||||
use leo_asg::{Asg, AsgPass, FormattedError, Program as AsgProgram};
|
||||
use leo_asg::{Asg, AsgPass, Program as AsgProgram};
|
||||
use leo_ast::{Input, MainInput, Program as AstProgram};
|
||||
use leo_errors::{CompilerError, LeoError, Span};
|
||||
use leo_input::LeoInputParser;
|
||||
use leo_package::inputs::InputPairs;
|
||||
use leo_parser::parse_ast;
|
||||
@ -113,7 +113,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
context: AsgContext<'a>,
|
||||
options: Option<CompilerOptions>,
|
||||
ast_snapshot_options: Option<AstSnapshotOptions>,
|
||||
) -> Result<Self, CompilerError> {
|
||||
) -> Result<Self, LeoError> {
|
||||
let mut compiler = Self::new(
|
||||
package_name,
|
||||
main_file_path,
|
||||
@ -152,7 +152,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
context: AsgContext<'a>,
|
||||
options: Option<CompilerOptions>,
|
||||
ast_snapshot_options: Option<AstSnapshotOptions>,
|
||||
) -> Result<Self, CompilerError> {
|
||||
) -> Result<Self, LeoError {
|
||||
let mut compiler = Self::new(
|
||||
package_name,
|
||||
main_file_path,
|
||||
@ -180,7 +180,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
input_path: &Path,
|
||||
state_string: &str,
|
||||
state_path: &Path,
|
||||
) -> Result<(), CompilerError> {
|
||||
) -> Result<(), LeoError {
|
||||
let input_syntax_tree = LeoInputParser::parse_file(&input_string).map_err(|mut e| {
|
||||
e.set_path(
|
||||
input_path.to_str().unwrap_or_default(),
|
||||
@ -223,10 +223,10 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
///
|
||||
/// Parses and stores all programs imported by the main program file.
|
||||
///
|
||||
pub fn parse_program(&mut self) -> Result<(), CompilerError> {
|
||||
pub fn parse_program(&mut self) -> Result<(), LeoError {
|
||||
// Load the program file.
|
||||
let content = fs::read_to_string(&self.main_file_path)
|
||||
.map_err(|e| CompilerError::FileReadError(self.main_file_path.clone(), e))?;
|
||||
.map_err(|e| LeoError::from(CompilerError::FileReadError(self.main_file_path.clone(), e)))?;
|
||||
|
||||
self.parse_program_from_string(&content)
|
||||
}
|
||||
@ -235,7 +235,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
/// Equivalent to parse_and_check_program but uses the given program_string instead of a main
|
||||
/// file path.
|
||||
///
|
||||
pub fn parse_program_from_string(&mut self, program_string: &str) -> Result<(), CompilerError> {
|
||||
pub fn parse_program_from_string(&mut self, program_string: &str) -> Result<(), LeoError {
|
||||
// Use the parser to construct the abstract syntax tree (ast).
|
||||
|
||||
let mut ast: leo_ast::Ast = parse_ast(self.main_file_path.to_str().unwrap_or_default(), program_string)?;
|
||||
@ -278,7 +278,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
// Store the ASG.
|
||||
self.asg = Some(asg.into_repr());
|
||||
|
||||
self.do_asg_passes().map_err(CompilerError::AsgPassError)?;
|
||||
self.do_asg_passes().map_err(LeoError::from(CompilerError::AsgPassError))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -307,24 +307,24 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
///
|
||||
/// Synthesizes the circuit with program input to verify correctness.
|
||||
///
|
||||
pub fn compile_constraints<CS: ConstraintSystem<F>>(&self, cs: &mut CS) -> Result<Output, CompilerError> {
|
||||
pub fn compile_constraints<CS: ConstraintSystem<F>>(&self, cs: &mut CS) -> Result<Output, LeoError {
|
||||
generate_constraints::<F, G, CS>(cs, &self.asg.as_ref().unwrap(), &self.program_input)
|
||||
}
|
||||
|
||||
///
|
||||
/// Synthesizes the circuit for test functions with program input.
|
||||
///
|
||||
pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(u32, u32), CompilerError> {
|
||||
pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(u32, u32), LeoError {
|
||||
generate_test_constraints::<F, G>(&self.asg.as_ref().unwrap(), input_pairs, &self.output_directory)
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a SHA256 checksum of the program file.
|
||||
///
|
||||
pub fn checksum(&self) -> Result<String, CompilerError> {
|
||||
pub fn checksum(&self) -> Result<String, LeoError {
|
||||
// Read in the main file as string
|
||||
let unparsed_file = fs::read_to_string(&self.main_file_path)
|
||||
.map_err(|e| CompilerError::FileReadError(self.main_file_path.clone(), e))?;
|
||||
.map_err(|e| LeoError::from(CompilerError::FileReadError(self.main_file_path.clone(), e)))?;
|
||||
|
||||
// Hash the file contents
|
||||
let mut hasher = Sha256::new();
|
||||
@ -342,7 +342,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
pub fn verify_local_data_commitment(
|
||||
&self,
|
||||
system_parameters: &SystemParameters<Components>,
|
||||
) -> Result<bool, CompilerError> {
|
||||
) -> Result<bool, LeoError {
|
||||
let result = verify_local_data_commitment(system_parameters, &self.program_input)?;
|
||||
|
||||
Ok(result)
|
||||
@ -362,7 +362,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstraintSynthesizer<F> for Compiler<'
|
||||
///
|
||||
/// Synthesizes the circuit with program input.
|
||||
///
|
||||
fn generate_constraints<CS: ConstraintSystem<F>>(&self, cs: &mut CS) -> Result<(), SynthesisError> {
|
||||
fn generate_constraints<CS: ConstraintSystem<F>>(&self, cs: &mut CS) -> Result<(), LeoError> {
|
||||
let output_directory = self.output_directory.clone();
|
||||
let package_name = self.program_name.clone();
|
||||
let result = self.compile_constraints(cs).map_err(|e| {
|
||||
|
@ -17,13 +17,13 @@
|
||||
//! Enforces an assert equals statement in a compiled Leo program.
|
||||
|
||||
use crate::{
|
||||
errors::ConsoleError,
|
||||
get_indicator_value,
|
||||
program::ConstrainedProgram,
|
||||
value::ConstrainedValue,
|
||||
GroupType,
|
||||
};
|
||||
use leo_asg::{Expression, Span};
|
||||
use leo_asg::Expression;
|
||||
use leo_errors::{CompilerError, LeoError, Span};
|
||||
|
||||
use snarkvm_fields::PrimeField;
|
||||
use snarkvm_gadgets::boolean::Boolean;
|
||||
@ -36,7 +36,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
indicator: &Boolean,
|
||||
expression: &'a Expression<'a>,
|
||||
span: &Span,
|
||||
) -> Result<(), ConsoleError> {
|
||||
) -> Result<(), LeoError> {
|
||||
// Evaluate assert expression
|
||||
let assert_expression = self.enforce_expression(cs, expression)?;
|
||||
|
||||
@ -50,13 +50,13 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
let result_option = match assert_expression {
|
||||
ConstrainedValue::Boolean(boolean) => boolean.get_value(),
|
||||
_ => {
|
||||
return Err(ConsoleError::assertion_must_be_boolean(span));
|
||||
return Err(LeoError::from(CompilerError::assertion_must_be_boolean(span)));
|
||||
}
|
||||
};
|
||||
let result_bool = result_option.ok_or_else(|| ConsoleError::assertion_depends_on_input(span))?;
|
||||
let result_bool = result_option.ok_or_else(|| LeoError::from(CompilerError::assertion_depends_on_input(span)))?;
|
||||
|
||||
if !result_bool {
|
||||
return Err(ConsoleError::assertion_failed(span));
|
||||
return Err(LeoError::from(CompilerError::assertion_failed(span)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -16,8 +16,9 @@
|
||||
|
||||
//! Evaluates a macro in a compiled Leo program.
|
||||
|
||||
use crate::{errors::ConsoleError, program::ConstrainedProgram, statement::get_indicator_value, GroupType};
|
||||
use crate::{program::ConstrainedProgram, statement::get_indicator_value, GroupType};
|
||||
use leo_asg::{ConsoleFunction, ConsoleStatement};
|
||||
use leo_errors::LeoError;
|
||||
|
||||
use snarkvm_fields::PrimeField;
|
||||
use snarkvm_gadgets::boolean::Boolean;
|
||||
@ -29,7 +30,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
cs: &mut CS,
|
||||
indicator: &Boolean,
|
||||
console: &ConsoleStatement<'a>,
|
||||
) -> Result<(), ConsoleError> {
|
||||
) -> Result<(), LeoError> {
|
||||
match &console.function {
|
||||
ConsoleFunction::Assert(expression) => {
|
||||
self.evaluate_console_assert(
|
||||
|
@ -16,8 +16,10 @@
|
||||
|
||||
//! Evaluates a formatted string in a compiled Leo program.
|
||||
|
||||
use crate::{errors::ConsoleError, program::ConstrainedProgram, GroupType};
|
||||
use crate::{program::ConstrainedProgram, GroupType};
|
||||
use leo_asg::{CharValue, ConsoleArgs};
|
||||
use leo_errors::{CompilerError, LeoError};
|
||||
|
||||
use snarkvm_fields::PrimeField;
|
||||
use snarkvm_r1cs::ConstraintSystem;
|
||||
|
||||
@ -26,7 +28,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
args: &ConsoleArgs<'a>,
|
||||
) -> Result<String, ConsoleError> {
|
||||
) -> Result<String, LeoError> {
|
||||
let mut out = Vec::new();
|
||||
let mut in_container = false;
|
||||
let mut substring = String::new();
|
||||
@ -52,7 +54,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
in_container = false;
|
||||
let parameter = match args.parameters.get(arg_index) {
|
||||
Some(index) => index,
|
||||
None => return Err(ConsoleError::length(arg_index + 1, args.parameters.len(), &args.span)),
|
||||
None => return Err(LeoError::from(CompilerError::from(CompilerError::length(arg_index + 1, args.parameters.len(), &args.span)))),
|
||||
};
|
||||
out.push(self.enforce_expression(cs, parameter.get())?.to_string());
|
||||
arg_index += 1;
|
||||
@ -63,12 +65,12 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
substring.push('}');
|
||||
escape_right_bracket = true;
|
||||
} else {
|
||||
return Err(ConsoleError::expected_escaped_right_brace(&args.span));
|
||||
return Err(LeoError::from(CompilerError::from(CompilerError::expected_escaped_right_brace(&args.span))));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ if in_container => {
|
||||
return Err(ConsoleError::expected_left_or_right_brace(&args.span));
|
||||
return Err(LeoError::from(CompilerError::from(CompilerError::expected_left_or_right_brace(&args.span))));
|
||||
}
|
||||
_ => substring.push(*scalar),
|
||||
},
|
||||
@ -82,7 +84,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
||||
|
||||
// Check that containers and parameters match
|
||||
if arg_index != args.parameters.len() {
|
||||
return Err(ConsoleError::length(arg_index, args.parameters.len(), &args.span));
|
||||
return Err(LeoError::from(CompilerError::from(CompilerError::length(arg_index, args.parameters.len(), &args.span))));
|
||||
}
|
||||
|
||||
Ok(out.join(""))
|
||||
|
@ -16,9 +16,10 @@
|
||||
|
||||
//! Generates R1CS constraints for a compiled Leo program.
|
||||
|
||||
use crate::{errors::CompilerError, ConstrainedProgram, GroupType, Output, OutputFile};
|
||||
use crate::{ConstrainedProgram, GroupType, Output, OutputFile};
|
||||
use leo_asg::Program;
|
||||
use leo_ast::Input;
|
||||
use leo_errors::{CompilerError, LeoError};
|
||||
use leo_input::LeoInputParser;
|
||||
use leo_package::inputs::InputPairs;
|
||||
|
||||
@ -30,7 +31,7 @@ pub fn generate_constraints<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSy
|
||||
cs: &mut CS,
|
||||
program: &Program<'a>,
|
||||
input: &Input,
|
||||
) -> Result<Output, CompilerError> {
|
||||
) -> Result<Output, LeoError> {
|
||||
let mut resolved_program = ConstrainedProgram::<F, G>::new(program.clone());
|
||||
|
||||
for (_, global_const) in program.global_consts.iter() {
|
||||
@ -47,7 +48,7 @@ pub fn generate_constraints<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSy
|
||||
let result = resolved_program.enforce_main_function(cs, &function, input)?;
|
||||
Ok(result)
|
||||
}
|
||||
_ => Err(CompilerError::NoMainFunction),
|
||||
_ => Err(LeoError::from(CompilerError::NoMainFunction)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,7 +56,7 @@ pub fn generate_test_constraints<'a, F: PrimeField, G: GroupType<F>>(
|
||||
program: &Program<'a>,
|
||||
input: InputPairs,
|
||||
output_directory: &Path,
|
||||
) -> Result<(u32, u32), CompilerError> {
|
||||
) -> Result<(u32, u32), LeoError> {
|
||||
let mut resolved_program = ConstrainedProgram::<F, G>::new(program.clone());
|
||||
let program_name = program.name.clone();
|
||||
|
||||
@ -101,10 +102,10 @@ pub fn generate_test_constraints<'a, F: PrimeField, G: GroupType<F>>(
|
||||
.or_else(|| input.pairs.get(&file_name_kebab))
|
||||
{
|
||||
Some(pair) => pair.to_owned(),
|
||||
None => return Err(CompilerError::InvalidTestContext(file_name.to_string())),
|
||||
None => return Err(LeoError::from(CompilerError::InvalidTestContext(file_name.to_string()))),
|
||||
}
|
||||
}
|
||||
None => default.ok_or(CompilerError::NoTestInput)?,
|
||||
None => default.ok_or(LeoError::from(CompilerError::NoTestInput))?,
|
||||
};
|
||||
|
||||
// parse input files to abstract syntax trees
|
||||
|
@ -1,71 +0,0 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::errors::{ExpressionError, FunctionError, ImportError, StatementError};
|
||||
use leo_asg::{AsgConvertError, FormattedError};
|
||||
use leo_ast::{AstError, LeoError};
|
||||
use leo_input::InputParserError;
|
||||
use leo_parser::SyntaxError;
|
||||
use leo_state::LocalDataVerificationError;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum CompilerError {
|
||||
#[error("{}", _0)]
|
||||
SyntaxError(#[from] SyntaxError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
AsgPassError(FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ExpressionError(#[from] ExpressionError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ImportError(#[from] ImportError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
InputParserError(#[from] InputParserError),
|
||||
|
||||
#[error("Cannot find input files with context name `{}`", _0)]
|
||||
InvalidTestContext(String),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FunctionError(#[from] FunctionError),
|
||||
|
||||
#[error("Cannot read from the provided file path '{:?}': {}", _0, _1)]
|
||||
FileReadError(PathBuf, std::io::Error),
|
||||
|
||||
#[error("{}", _0)]
|
||||
LocalDataVerificationError(#[from] LocalDataVerificationError),
|
||||
|
||||
#[error("`main` must be a function")]
|
||||
NoMainFunction,
|
||||
|
||||
#[error("Failed to find input files for the current test")]
|
||||
NoTestInput,
|
||||
|
||||
#[error("{}", _0)]
|
||||
AsgConvertError(#[from] AsgConvertError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
AstError(#[from] AstError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
StatementError(#[from] StatementError),
|
||||
}
|
||||
|
||||
impl LeoError for CompilerError {}
|
@ -1,76 +0,0 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::errors::ExpressionError;
|
||||
use leo_ast::{FormattedError, LeoError, Span};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConsoleError {
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
Expression(#[from] ExpressionError),
|
||||
}
|
||||
|
||||
impl LeoError for ConsoleError {}
|
||||
|
||||
impl ConsoleError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
ConsoleError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
pub fn expected_left_or_right_brace(span: &Span) -> Self {
|
||||
let message = "Formatter given a {. Expected a { or } after".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn expected_escaped_right_brace(span: &Span) -> Self {
|
||||
let message = "Formatter given a }. Expected a container {} or }}".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn length(containers: usize, parameters: usize, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Formatter given {} containers and found {} parameters",
|
||||
containers, parameters
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn assertion_depends_on_input(span: &Span) -> Self {
|
||||
let message = "console.assert() does not produce constraints and cannot use inputs. \
|
||||
Assertions should only be used in @test functions"
|
||||
.to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn assertion_failed(span: &Span) -> Self {
|
||||
let message = "Assertion failed".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn assertion_must_be_boolean(span: &Span) -> Self {
|
||||
let message = "Assertion expression must evaluate to a boolean value".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
}
|
@ -1,174 +0,0 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::errors::{
|
||||
AddressError,
|
||||
BooleanError,
|
||||
CharError,
|
||||
FieldError,
|
||||
FunctionError,
|
||||
GroupError,
|
||||
IntegerError,
|
||||
ValueError,
|
||||
};
|
||||
use leo_ast::{FormattedError, Identifier, LeoError, Span};
|
||||
use snarkvm_r1cs::SynthesisError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ExpressionError {
|
||||
#[error("{}", _0)]
|
||||
AddressError(#[from] AddressError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
BooleanError(#[from] BooleanError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
CharError(#[from] CharError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FieldError(#[from] FieldError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FunctionError(#[from] Box<FunctionError>),
|
||||
|
||||
#[error("{}", _0)]
|
||||
GroupError(#[from] GroupError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
IntegerError(#[from] IntegerError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ValueError(#[from] ValueError),
|
||||
}
|
||||
|
||||
impl LeoError for ExpressionError {}
|
||||
|
||||
impl ExpressionError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
ExpressionError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
pub fn cannot_enforce(operation: String, error: SynthesisError, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"the gadget operation `{}` failed due to synthesis error `{:?}`",
|
||||
operation, error,
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn cannot_evaluate(operation: String, span: &Span) -> Self {
|
||||
let message = format!("Mismatched types found for operation `{}`", operation);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_length_out_of_bounds(span: &Span) -> Self {
|
||||
let message = "array length cannot be >= 2^32".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_index_out_of_legal_bounds(span: &Span) -> Self {
|
||||
let message = "array index cannot be >= 2^32".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn conditional_boolean(actual: String, span: &Span) -> Self {
|
||||
let message = format!("if, else conditional must resolve to a boolean, found `{}`", actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn expected_circuit_member(expected: String, span: &Span) -> Self {
|
||||
let message = format!("expected circuit member `{}`, not found", expected);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn incompatible_types(operation: String, span: &Span) -> Self {
|
||||
let message = format!("no implementation for `{}`", operation);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn tuple_index_out_of_bounds(index: usize, span: &Span) -> Self {
|
||||
let message = format!("cannot access index {} of tuple out of bounds", index);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_index_out_of_bounds(index: usize, span: &Span) -> Self {
|
||||
let message = format!("cannot access index {} of array out of bounds", index);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_invalid_slice_length(span: &Span) -> Self {
|
||||
let message = "illegal length of slice".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_index(actual: String, span: &Span) -> Self {
|
||||
let message = format!("index must resolve to an integer, found `{}`", actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_length(expected: usize, actual: usize, span: &Span) -> Self {
|
||||
let message = format!("expected array length {}, found one with length {}", expected, actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_static_access(member: String, span: &Span) -> Self {
|
||||
let message = format!("static member `{}` must be accessed using `::` syntax", member);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn undefined_array(actual: String, span: &Span) -> Self {
|
||||
let message = format!("array `{}` must be declared before it is used in an expression", actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn undefined_circuit(actual: String, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"circuit `{}` must be declared before it is used in an expression",
|
||||
actual
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn undefined_identifier(identifier: Identifier) -> Self {
|
||||
let message = format!("Cannot find value `{}` in this scope", identifier.name);
|
||||
|
||||
Self::new_from_span(message, &identifier.span)
|
||||
}
|
||||
|
||||
pub fn undefined_member_access(circuit: String, member: String, span: &Span) -> Self {
|
||||
let message = format!("Circuit `{}` has no member `{}`", circuit, member);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
}
|
@ -1,146 +0,0 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::errors::{
|
||||
AddressError,
|
||||
BooleanError,
|
||||
CharError,
|
||||
ExpressionError,
|
||||
FieldError,
|
||||
GroupError,
|
||||
IntegerError,
|
||||
OutputBytesError,
|
||||
StatementError,
|
||||
ValueError,
|
||||
};
|
||||
use leo_asg::AsgConvertError;
|
||||
use leo_ast::{FormattedError, LeoError, Span};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FunctionError {
|
||||
#[error("{}", _0)]
|
||||
AddressError(#[from] AddressError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
BooleanError(#[from] BooleanError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
CharError(#[from] CharError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ExpressionError(#[from] ExpressionError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FieldError(#[from] FieldError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
GroupError(#[from] GroupError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
IntegerError(#[from] IntegerError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
OutputStringError(#[from] OutputBytesError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
StatementError(#[from] StatementError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ValueError(#[from] ValueError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ImportASGError(#[from] AsgConvertError),
|
||||
}
|
||||
|
||||
impl LeoError for FunctionError {}
|
||||
|
||||
impl FunctionError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
FunctionError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
pub fn input_type_mismatch(expected: String, actual: String, variable: String, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Expected input variable `{}` to be type `{}`, found type `{}`",
|
||||
variable, expected, actual
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn expected_const_input(variable: String, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Expected input variable `{}` to be constant. Move input variable `{}` to [constants] section of input file",
|
||||
variable, variable
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn expected_non_const_input(variable: String, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Expected input variable `{}` to be non-constant. Move input variable `{}` to [main] section of input file",
|
||||
variable, variable
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_array(actual: String, span: &Span) -> Self {
|
||||
let message = format!("Expected function input array, found `{}`", actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_input_array_dimensions(expected: usize, actual: usize, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Input array dimensions mismatch expected {}, found array dimensions {}",
|
||||
expected, actual
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn tuple_size_mismatch(expected: usize, actual: usize, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Input tuple size mismatch expected {}, found tuple with length {}",
|
||||
expected, actual
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_tuple(actual: String, span: &Span) -> Self {
|
||||
let message = format!("Expected function input tuple, found `{}`", actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn input_not_found(expected: String, span: &Span) -> Self {
|
||||
let message = format!("main function input {} not found", expected);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn double_input_declaration(input_name: String, span: &Span) -> Self {
|
||||
let message = format!("Input variable {} declared twice", input_name);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::errors::ValueError;
|
||||
use leo_asg::{AsgConvertError, Type};
|
||||
use leo_ast::{FormattedError, LeoError, Span};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum OutputBytesError {
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ValueError(#[from] ValueError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
AsgConvertError(#[from] AsgConvertError),
|
||||
}
|
||||
|
||||
impl LeoError for OutputBytesError {}
|
||||
|
||||
impl OutputBytesError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
OutputBytesError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
pub fn not_enough_registers(span: &Span) -> Self {
|
||||
let message = "number of input registers must be greater than or equal to output registers".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn mismatched_output_types(left: &Type, right: &Type, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Mismatched types. Expected register output type `{}`, found type `{}`.",
|
||||
left, right
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
}
|
@ -1,193 +0,0 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::errors::{AddressError, BooleanError, ConsoleError, ExpressionError, IntegerError, ValueError};
|
||||
use leo_asg::Type;
|
||||
use leo_ast::{FormattedError, LeoError, Span};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum StatementError {
|
||||
#[error("{}", _0)]
|
||||
AddressError(#[from] AddressError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
BooleanError(#[from] BooleanError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ExpressionError(#[from] ExpressionError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
IntegerError(#[from] IntegerError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
MacroError(#[from] ConsoleError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ValueError(#[from] ValueError),
|
||||
}
|
||||
|
||||
impl LeoError for StatementError {}
|
||||
|
||||
impl StatementError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
StatementError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
pub fn array_assign_index(span: &Span) -> Self {
|
||||
let message = "Cannot assign single index to array of values".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_assign_index_const(span: &Span) -> Self {
|
||||
let message = "Cannot assign to non-const array index".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_assign_interior_index(span: &Span) -> Self {
|
||||
let message = "Cannot assign single index to interior of array of values".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_assign_range(span: &Span) -> Self {
|
||||
let message = "Cannot assign range of array values to single value".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_assign_index_bounds(index: usize, length: usize, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Array assign index `{}` out of range for array of length `{}`",
|
||||
index, length
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn array_assign_range_order(start: usize, stop: usize, length: usize, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Array assign range `{}`..`{}` out of range for array of length `{}`",
|
||||
start, stop, length
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn conditional_boolean(actual: String, span: &Span) -> Self {
|
||||
let message = format!("If, else conditional must resolve to a boolean, found `{}`", actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn indicator_calculation(name: String, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Constraint system failed to evaluate branch selection indicator `{}`",
|
||||
name
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_number_of_definitions(expected: usize, actual: usize, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Multiple definition statement expected {} return values, found {} values",
|
||||
expected, actual
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn multiple_definition(value: String, span: &Span) -> Self {
|
||||
let message = format!("cannot assign multiple variables to a single value: {}", value,);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn multiple_returns(span: &Span) -> Self {
|
||||
let message = "This function returns multiple times and produces unreachable circuits with undefined behavior."
|
||||
.to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn no_returns(expected: &Type, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"function expected `{}` return type but no valid branches returned a result",
|
||||
expected
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn select_fail(first: String, second: String, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Conditional select gadget failed to select between `{}` or `{}`",
|
||||
first, second
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn tuple_assign_index(span: &Span) -> Self {
|
||||
let message = "Cannot assign single index to tuple of values".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn tuple_assign_index_bounds(index: usize, length: usize, span: &Span) -> Self {
|
||||
let message = format!(
|
||||
"Tuple assign index `{}` out of range for tuple of length `{}`",
|
||||
index, length
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn unassigned(span: &Span) -> Self {
|
||||
let message = "Expected assignment of return values for expression".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn undefined_variable(name: String, span: &Span) -> Self {
|
||||
let message = format!("Attempted to assign to unknown variable `{}`", name);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn undefined_circuit(name: String, span: &Span) -> Self {
|
||||
let message = format!("Attempted to assign to unknown circuit `{}`", name);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn undefined_circuit_variable(name: String, span: &Span) -> Self {
|
||||
let message = format!("Attempted to assign to unknown circuit member variable `{}`", name);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn loop_index_const(span: &Span) -> Self {
|
||||
let message = "iteration range must be const".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_ast::{FormattedError, LeoError, Span};
|
||||
use snarkvm_dpc::AccountError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AddressError {
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
}
|
||||
|
||||
impl LeoError for AddressError {}
|
||||
|
||||
impl AddressError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
AddressError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
pub fn account_error(error: AccountError, span: &Span) -> Self {
|
||||
let message = format!("account creation failed due to `{}`", error);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn invalid_address(actual: String, span: &Span) -> Self {
|
||||
let message = format!("expected address input type, found `{}`", actual);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn missing_address(span: &Span) -> Self {
|
||||
let message = "expected address input not found".to_string();
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user