tendrilize spans

This commit is contained in:
Protryon 2021-03-22 06:48:48 -07:00
parent 9bc53eb594
commit 5985d8da41
122 changed files with 668 additions and 1171 deletions

42
Cargo.lock generated
View File

@ -787,6 +787,16 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]]
name = "futf"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c9c1ce3fa9336301af935ab852c437817d14cd33690446569392e65170aac3b"
dependencies = [
"mac",
"new_debug_unreachable",
]
[[package]]
name = "futures-channel"
version = "0.3.13"
@ -1203,6 +1213,7 @@ dependencies = [
"num-bigint",
"serde",
"serde_json",
"tendril",
"thiserror",
"typed-arena",
]
@ -1225,6 +1236,7 @@ dependencies = [
"pest",
"serde",
"serde_json",
"tendril",
"thiserror",
]
@ -1351,6 +1363,7 @@ dependencies = [
"leo-ast",
"serde",
"serde_json",
"tendril",
"thiserror",
"tracing",
]
@ -1444,6 +1457,12 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "mac"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
[[package]]
name = "maplit"
version = "1.0.2"
@ -1610,6 +1629,12 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "new_debug_unreachable"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
[[package]]
name = "nias"
version = "0.5.0"
@ -2759,6 +2784,17 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "tendril"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9ef557cb397a4f0a5a3a628f06515f78563f2209e64d47055d9dc6052bf5e33"
dependencies = [
"futf",
"mac",
"utf-8",
]
[[package]]
name = "termcolor"
version = "1.1.2"
@ -3077,6 +3113,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "utf-8"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7"
[[package]]
name = "vcpkg"
version = "0.2.11"

View File

@ -43,5 +43,8 @@ version = "0.4"
[dependencies.typed-arena]
version = "2.0"
[dependencies.tendril]
version = "0.4"
[dev-dependencies.criterion]
version = "0.3"

View File

@ -15,14 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
statement::*,
BoolAnd,
Expression,
Monoid,
MonoidalReducerExpression,
MonoidalReducerStatement,
Node,
Span,
statement::*, BoolAnd, Expression, Monoid, MonoidalReducerExpression, MonoidalReducerStatement, Node, Span,
};
pub struct ReturnPathReducer {

View File

@ -18,6 +18,7 @@ use crate::{AsgConvertError, IntegerType, Span, Type};
use num_bigint::BigInt;
use std::{convert::TryInto, fmt};
use tendril::StrTendril;
/// Constant integer values in a program.
#[derive(Clone, Debug, PartialEq)]
@ -38,7 +39,7 @@ pub enum ConstInt {
#[derive(Clone, Debug, PartialEq)]
pub enum GroupCoordinate {
/// Explicit field element number string.
Number(String),
Number(StrTendril),
/// Attempt to recover with a sign high bit.
SignHigh,
@ -87,7 +88,7 @@ impl Into<leo_ast::GroupCoordinate> for &GroupCoordinate {
#[derive(Clone, Debug, PartialEq)]
pub enum GroupValue {
Single(String),
Single(StrTendril),
Tuple(GroupCoordinate, GroupCoordinate),
}
@ -106,7 +107,7 @@ pub enum ConstValue {
Int(ConstInt),
Group(GroupValue),
Field(BigInt),
Address(String),
Address(StrTendril),
Boolean(bool),
// compounds

View File

@ -35,25 +35,7 @@ pub enum AsgConvertError {
SyntaxError(#[from] SyntaxError),
}
impl LeoError for AsgConvertError {
fn get_path(&self) -> Option<&str> {
match self {
AsgConvertError::Error(error) => error.get_path(),
AsgConvertError::SyntaxError(error) => error.get_path(),
AsgConvertError::ImportError(error) => error.get_path(),
AsgConvertError::InternalError(_) => None,
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
AsgConvertError::Error(error) => error.set_path(path, contents),
AsgConvertError::SyntaxError(error) => error.set_path(path, contents),
AsgConvertError::ImportError(error) => error.set_path(path, contents),
AsgConvertError::InternalError(_) => {}
}
}
}
impl LeoError for AsgConvertError {}
impl AsgConvertError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -152,7 +152,7 @@ impl<'a> Into<leo_ast::ArrayInitExpression> for &ArrayInitExpression<'a> {
leo_ast::ArrayInitExpression {
element: Box::new(self.element.get().into()),
dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber {
value: self.len.to_string(),
value: self.len.to_string().into(),
}]),
span: self.span.clone().unwrap_or_default(),
}

View File

@ -15,19 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
AsgConvertError,
CircuitMember,
ConstValue,
Expression,
ExpressionNode,
FromAst,
Function,
FunctionQualifier,
Node,
PartialType,
Scope,
Span,
Type,
AsgConvertError, CircuitMember, ConstValue, Expression, ExpressionNode, FromAst, Function, FunctionQualifier, Node,
PartialType, Scope, Span, Type,
};
pub use leo_ast::{BinaryOperation, Node as AstNode};
@ -116,7 +105,7 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
let circuit_name = circuit.name.borrow().name.clone();
let member = circuit.members.borrow();
let member = member
.get(&name.name)
.get(name.name.as_ref())
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, span))?;
match member {
CircuitMember::Function(body) => {
@ -156,7 +145,7 @@ impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
let member = circuit.members.borrow();
let member = member
.get(&name.name)
.get(name.name.as_ref())
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, span))?;
match member {
CircuitMember::Function(body) => {

View File

@ -15,19 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
AsgConvertError,
Circuit,
CircuitMember,
ConstValue,
Expression,
ExpressionNode,
FromAst,
Identifier,
Node,
PartialType,
Scope,
Span,
Type,
AsgConvertError, Circuit, CircuitMember, ConstValue, Expression, ExpressionNode, FromAst, Identifier, Node,
PartialType, Scope, Span, Type,
};
use std::cell::Cell;
@ -67,7 +56,7 @@ impl<'a> ExpressionNode<'a> for CircuitAccessExpression<'a> {
None // function target only for static
} else {
let members = self.circuit.get().members.borrow();
let member = members.get(&self.member.name)?;
let member = members.get(self.member.name.as_ref())?;
match member {
CircuitMember::Variable(type_) => Some(type_.clone()),
CircuitMember::Function(_) => None,
@ -112,7 +101,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessEx
// scoping refcell reference
let found_member = {
if let Some(member) = circuit.members.borrow().get(&value.name.name) {
if let Some(member) = circuit.members.borrow().get(value.name.name.as_ref()) {
if let Some(expected_type) = &expected_type {
if let CircuitMember::Variable(type_) = &member {
let type_: Type = type_.clone();
@ -136,10 +125,10 @@ impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessEx
} else if circuit.is_input_pseudo_circuit() {
// add new member to implicit input
if let Some(expected_type) = expected_type.map(PartialType::full).flatten() {
circuit
.members
.borrow_mut()
.insert(value.name.name.clone(), CircuitMember::Variable(expected_type.clone()));
circuit.members.borrow_mut().insert(
value.name.name.to_string(),
CircuitMember::Variable(expected_type.clone()),
);
} else {
return Err(AsgConvertError::input_ref_needs_type(
&circuit.name.borrow().name,
@ -192,7 +181,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitStaticFunctionAccessExpression> for Circuit
));
}
if let Some(CircuitMember::Function(_)) = circuit.members.borrow().get(&value.name.name) {
if let Some(CircuitMember::Function(_)) = circuit.members.borrow().get(value.name.name.as_ref()) {
// okay
} else {
return Err(AsgConvertError::unresolved_circuit_member(

View File

@ -15,19 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
AsgConvertError,
Circuit,
CircuitMember,
ConstValue,
Expression,
ExpressionNode,
FromAst,
Identifier,
Node,
PartialType,
Scope,
Span,
Type,
AsgConvertError, Circuit, CircuitMember, ConstValue, Expression, ExpressionNode, FromAst, Identifier, Node,
PartialType, Scope, Span, Type,
};
use indexmap::{IndexMap, IndexSet};
@ -99,10 +88,10 @@ impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'
));
}
}
let members: IndexMap<&String, (&Identifier, Option<&leo_ast::Expression>)> = value
let members: IndexMap<&str, (&Identifier, Option<&leo_ast::Expression>)> = value
.members
.iter()
.map(|x| (&x.identifier.name, (&x.identifier, x.expression.as_ref())))
.map(|x| (x.identifier.name.as_ref(), (&x.identifier, x.expression.as_ref())))
.collect();
let mut values: Vec<(Identifier, Cell<&'a Expression<'a>>)> = vec![];
@ -124,7 +113,7 @@ impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'
} else {
continue;
};
if let Some((identifier, receiver)) = members.get(&name) {
if let Some((identifier, receiver)) = members.get(&**name) {
let received = if let Some(receiver) = *receiver {
<&Expression<'a>>::from_ast(scope, receiver, Some(type_.partial()))?
} else {

View File

@ -15,18 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
AsgConvertError,
ConstInt,
ConstValue,
Expression,
ExpressionNode,
FromAst,
GroupValue,
Node,
PartialType,
Scope,
Span,
Type,
AsgConvertError, ConstInt, ConstValue, Expression, ExpressionNode, FromAst, GroupValue, Node, PartialType, Scope,
Span, Type,
};
use std::cell::Cell;
@ -174,12 +164,12 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
Some(PartialType::Type(Type::Group)) => Constant {
parent: Cell::new(None),
span: Some(span.clone()),
value: ConstValue::Group(GroupValue::Single(value.to_string())),
value: ConstValue::Group(GroupValue::Single(value.clone())),
},
Some(PartialType::Type(Type::Address)) => Constant {
parent: Cell::new(None),
span: Some(span.clone()),
value: ConstValue::Address(value.to_string()),
value: ConstValue::Address(value.clone()),
},
Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("unknown"), span)),
},
@ -213,10 +203,10 @@ impl<'a> Into<leo_ast::ValueExpression> for &Constant<'a> {
leo_ast::ValueExpression::Address(value.clone(), self.span.clone().unwrap_or_default())
}
ConstValue::Boolean(value) => {
leo_ast::ValueExpression::Boolean(value.to_string(), self.span.clone().unwrap_or_default())
leo_ast::ValueExpression::Boolean(value.to_string().into(), self.span.clone().unwrap_or_default())
}
ConstValue::Field(value) => {
leo_ast::ValueExpression::Field(value.to_string(), self.span.clone().unwrap_or_default())
leo_ast::ValueExpression::Field(value.to_string().into(), self.span.clone().unwrap_or_default())
}
ConstValue::Group(value) => leo_ast::ValueExpression::Group(Box::new(match value {
GroupValue::Single(single) => {
@ -230,7 +220,7 @@ impl<'a> Into<leo_ast::ValueExpression> for &Constant<'a> {
})),
ConstValue::Int(int) => leo_ast::ValueExpression::Integer(
int.get_int_type(),
int.raw_value(),
int.raw_value().into(),
self.span.clone().unwrap_or_default(),
),
ConstValue::Tuple(_) => unimplemented!(),

View File

@ -109,7 +109,7 @@ impl<'a> Into<leo_ast::TupleAccessExpression> for &TupleAccessExpression<'a> {
leo_ast::TupleAccessExpression {
tuple: Box::new(self.tuple_ref.get().into()),
index: leo_ast::PositiveNumber {
value: self.index.to_string(),
value: self.index.to_string().into(),
},
span: self.span.clone().unwrap_or_default(),
}

View File

@ -15,20 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
AsgConvertError,
ConstValue,
Constant,
DefinitionStatement,
Expression,
ExpressionNode,
FromAst,
Node,
PartialType,
Scope,
Span,
Statement,
Type,
Variable,
AsgConvertError, ConstValue, Constant, DefinitionStatement, Expression, ExpressionNode, FromAst, Node, PartialType,
Scope, Span, Statement, Type, Variable,
};
use std::cell::Cell;
@ -136,7 +124,7 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
value: &leo_ast::Identifier,
expected_type: Option<PartialType<'a>>,
) -> Result<&'a Expression<'a>, AsgConvertError> {
let variable = if value.name == "input" {
let variable = if value.name.as_ref() == "input" {
if let Some(function) = scope.resolve_current_function() {
if !function.has_input {
return Err(AsgConvertError::unresolved_reference(&value.name, &value.span));

View File

@ -40,7 +40,7 @@ impl<'a> Input<'a> {
fn make_header(scope: &'a Scope<'a>, name: &str) -> &'a Circuit<'a> {
scope.context.alloc_circuit(Circuit {
id: scope.context.get_id(),
name: RefCell::new(Identifier::new(name.to_string())),
name: RefCell::new(Identifier::new(name.into())),
members: RefCell::new(IndexMap::new()),
core_mapping: RefCell::new(None),
scope,
@ -69,7 +69,7 @@ impl<'a> Input<'a> {
let container_circuit = input_scope.context.alloc_circuit(Circuit {
id: scope.context.get_id(),
name: RefCell::new(Identifier::new(CONTAINER_PSEUDO_CIRCUIT.to_string())),
name: RefCell::new(Identifier::new(CONTAINER_PSEUDO_CIRCUIT.into())),
members: RefCell::new(container_members),
core_mapping: RefCell::new(None),
scope: input_scope,
@ -84,7 +84,7 @@ impl<'a> Input<'a> {
container_circuit,
container: input_scope.context.alloc_variable(RefCell::new(crate::InnerVariable {
id: scope.context.get_id(),
name: Identifier::new("input".to_string()),
name: Identifier::new("input".into()),
type_: Type::Circuit(container_circuit),
mutable: false,
const_: false,

View File

@ -15,16 +15,7 @@
// 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,
AsgContextInner, AsgConvertError, Circuit, Expression, Function, PartialType, Scope, Span, Statement, Variable,
};
/// A node in the abstract semantic graph.

View File

@ -69,7 +69,7 @@ impl<'a> Circuit<'a> {
let mut members = circuit.members.borrow_mut();
for member in value.members.iter() {
if let leo_ast::CircuitMember::CircuitVariable(name, type_) = member {
if members.contains_key(&name.name) {
if members.contains_key(name.name.as_ref()) {
return Err(AsgConvertError::redefined_circuit_member(
&value.circuit_name.name,
&name.name,
@ -77,7 +77,7 @@ impl<'a> Circuit<'a> {
));
}
members.insert(
name.name.clone(),
name.name.to_string(),
CircuitMember::Variable(new_scope.resolve_ast_type(type_)?),
);
}
@ -93,13 +93,13 @@ impl<'a> Circuit<'a> {
let new_scope = scope.make_subscope();
let circuits = scope.circuits.borrow();
let circuit = circuits.get(&value.circuit_name.name).unwrap();
let circuit = circuits.get(value.circuit_name.name.as_ref()).unwrap();
new_scope.circuit_self.replace(Some(circuit));
let mut members = circuit.members.borrow_mut();
for member in value.members.iter() {
if let leo_ast::CircuitMember::CircuitFunction(function) = member {
if members.contains_key(&function.identifier.name) {
if members.contains_key(function.identifier.name.as_ref()) {
return Err(AsgConvertError::redefined_circuit_member(
&value.circuit_name.name,
&function.identifier.name,
@ -111,7 +111,7 @@ impl<'a> Circuit<'a> {
if asg_function.is_test() {
return Err(AsgConvertError::circuit_test_function(&function.identifier.span));
}
members.insert(function.identifier.name.clone(), CircuitMember::Function(asg_function));
members.insert(function.identifier.name.to_string(), CircuitMember::Function(asg_function));
}
}
@ -126,7 +126,7 @@ impl<'a> Circuit<'a> {
let asg_function = match *self
.members
.borrow()
.get(&function.identifier.name)
.get(function.identifier.name.as_ref())
.expect("missing header for defined circuit function")
{
CircuitMember::Function(f) => f,
@ -148,7 +148,7 @@ impl<'a> Into<leo_ast::Circuit> for &Circuit<'a> {
.iter()
.map(|(name, member)| match &member {
CircuitMember::Variable(type_) => {
leo_ast::CircuitMember::CircuitVariable(Identifier::new(name.clone()), type_.into())
leo_ast::CircuitMember::CircuitVariable(Identifier::new((&**name).into()), type_.into())
}
CircuitMember::Function(func) => leo_ast::CircuitMember::CircuitFunction((*func).into()),
})

View File

@ -15,18 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
AsgConvertError,
BlockStatement,
Circuit,
FromAst,
Identifier,
MonoidalDirector,
ReturnPathReducer,
Scope,
Span,
Statement,
Type,
Variable,
AsgConvertError, BlockStatement, Circuit, FromAst, Identifier, MonoidalDirector, ReturnPathReducer, Scope, Span,
Statement, Type, Variable,
};
use indexmap::IndexMap;
pub use leo_ast::Annotation;
@ -113,7 +103,7 @@ impl<'a> Function<'a> {
references: vec![],
assignments: vec![],
}));
arguments.insert(identifier.name.clone(), Cell::new(&*variable));
arguments.insert(identifier.name.to_string(), Cell::new(&*variable));
}
}
}
@ -144,7 +134,7 @@ impl<'a> Function<'a> {
let circuit = self.circuit.get();
let self_variable = self.scope.context.alloc_variable(RefCell::new(crate::InnerVariable {
id: self.scope.context.get_id(),
name: Identifier::new("self".to_string()),
name: Identifier::new("self".into()),
type_: Type::Circuit(circuit.as_ref().unwrap()),
mutable: self.qualifier == FunctionQualifier::MutSelfRef,
const_: false,
@ -186,7 +176,7 @@ impl<'a> Function<'a> {
}
pub fn is_test(&self) -> bool {
self.annotations.iter().any(|x| x.name.name == "test")
self.annotations.iter().any(|x| x.name.name.as_ref() == "test")
}
}

View File

@ -75,11 +75,11 @@ fn resolve_import_package(
) {
match package_or_packages {
PackageOrPackages::Package(package) => {
package_segments.push(package.name.name.clone());
package_segments.push(package.name.name.to_string());
resolve_import_package_access(output, package_segments, &package.access);
}
PackageOrPackages::Packages(packages) => {
package_segments.push(packages.name.name.clone());
package_segments.push(packages.name.name.to_string());
for access in packages.accesses.clone() {
resolve_import_package_access(output, package_segments.clone(), &access);
}
@ -106,14 +106,14 @@ fn resolve_import_package_access(
PackageAccess::Symbol(symbol) => {
let span = symbol.symbol.span.clone();
let symbol = if let Some(alias) = symbol.alias.as_ref() {
ImportSymbol::Alias(symbol.symbol.name.clone(), alias.name.clone())
ImportSymbol::Alias(symbol.symbol.name.to_string(), alias.name.to_string())
} else {
ImportSymbol::Direct(symbol.symbol.name.clone())
ImportSymbol::Direct(symbol.symbol.name.to_string())
};
output.push((package_segments, symbol, span));
}
PackageAccess::Multiple(packages) => {
package_segments.push(packages.name.name.clone());
package_segments.push(packages.name.name.to_string());
for subaccess in packages.accesses.iter() {
resolve_import_package_access(output, package_segments.clone(), &subaccess);
}
@ -240,7 +240,7 @@ impl<'a> Program<'a> {
assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = Circuit::init(scope, circuit)?;
scope.circuits.borrow_mut().insert(name.name.clone(), asg_circuit);
scope.circuits.borrow_mut().insert(name.name.to_string(), asg_circuit);
}
// Second pass for circuit members.
@ -248,35 +248,35 @@ impl<'a> Program<'a> {
assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = Circuit::init_member(scope, circuit)?;
scope.circuits.borrow_mut().insert(name.name.clone(), asg_circuit);
scope.circuits.borrow_mut().insert(name.name.to_string(), asg_circuit);
}
for (name, function) in program.functions.iter() {
assert_eq!(name.name, function.identifier.name);
let function = Function::init(scope, function)?;
scope.functions.borrow_mut().insert(name.name.clone(), function);
scope.functions.borrow_mut().insert(name.name.to_string(), function);
}
// Load concrete definitions.
let mut functions = IndexMap::new();
for (name, function) in program.functions.iter() {
assert_eq!(name.name, function.identifier.name);
let asg_function = *scope.functions.borrow().get(&name.name).unwrap();
let asg_function = *scope.functions.borrow().get(name.name.as_ref()).unwrap();
asg_function.fill_from_ast(function)?;
functions.insert(name.name.clone(), asg_function);
functions.insert(name.name.to_string(), asg_function);
}
let mut circuits = IndexMap::new();
for (name, circuit) in program.circuits.iter() {
assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = *scope.circuits.borrow().get(&name.name).unwrap();
let asg_circuit = *scope.circuits.borrow().get(name.name.as_ref()).unwrap();
asg_circuit.fill_from_ast(circuit)?;
circuits.insert(name.name.clone(), asg_circuit);
circuits.insert(name.name.to_string(), asg_circuit);
}
Ok(Program {
@ -338,7 +338,7 @@ pub fn reform_ast<'a>(program: &Program<'a>) -> leo_ast::Program {
for (_, program) in all_programs.into_iter() {
for (name, circuit) in program.circuits.iter() {
let identifier = format!("{}{}", identifiers.next().unwrap(), name);
circuit.name.borrow_mut().name = identifier.clone();
circuit.name.borrow_mut().name = identifier.clone().into();
all_circuits.insert(identifier, *circuit);
}
for (name, function) in program.functions.iter() {
@ -347,7 +347,7 @@ pub fn reform_ast<'a>(program: &Program<'a>) -> leo_ast::Program {
} else {
format!("{}{}", identifiers.next().unwrap(), name)
};
function.name.borrow_mut().name = identifier.clone();
function.name.borrow_mut().name = identifier.clone().into();
all_functions.insert(identifier, *function);
}
}
@ -358,7 +358,7 @@ pub fn reform_ast<'a>(program: &Program<'a>) -> leo_ast::Program {
.iter()
.map(|(module, _)| leo_ast::ImportStatement {
package_or_packages: leo_ast::PackageOrPackages::Package(leo_ast::Package {
name: Identifier::new(module.clone()),
name: Identifier::new(module.clone().into()),
access: leo_ast::PackageAccess::Star(Span::default()),
span: Default::default(),
}),

View File

@ -194,7 +194,7 @@ impl<'a> Scope<'a> {
.map(|x| self.resolve_ast_type(x))
.collect::<Result<Vec<_>, AsgConvertError>>()?,
),
Circuit(name) if name.name == "Self" => Type::Circuit(
Circuit(name) if name.name.as_ref() == "Self" => Type::Circuit(
self.resolve_circuit_self()
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
),

View File

@ -15,22 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
AsgConvertError,
CircuitMember,
ConstInt,
ConstValue,
Expression,
ExpressionNode,
FromAst,
Identifier,
IntegerType,
Node,
PartialType,
Scope,
Span,
Statement,
Type,
Variable,
AsgConvertError, CircuitMember, ConstInt, ConstValue, Expression, ExpressionNode, FromAst, Identifier, IntegerType,
Node, PartialType, Scope, Span, Statement, Type, Variable,
};
pub use leo_ast::AssignOperation;
use leo_ast::AssigneeAccess as AstAssigneeAccess;
@ -69,7 +55,7 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
) -> Result<Self, AsgConvertError> {
let (name, span) = (&statement.assignee.identifier.name, &statement.assignee.identifier.span);
let variable = if name == "input" {
let variable = if name.as_ref() == "input" {
if let Some(function) = scope.resolve_current_function() {
if !function.has_input {
return Err(AsgConvertError::unresolved_reference(name, &span));
@ -188,7 +174,7 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
let circuit = circuit;
let members = circuit.members.borrow();
let member = members.get(&name.name).ok_or_else(|| {
let member = members.get(name.name.as_ref()).ok_or_else(|| {
AsgConvertError::unresolved_circuit_member(
&circuit.name.borrow().name,
&name.name,
@ -251,7 +237,7 @@ impl<'a> Into<leo_ast::AssignStatement> for &AssignStatement<'a> {
AssignAccess::ArrayIndex(index) => AstAssigneeAccess::ArrayIndex(index.get().into()),
AssignAccess::Tuple(index) => AstAssigneeAccess::Tuple(
leo_ast::PositiveNumber {
value: index.to_string(),
value: index.to_string().into(),
},
self.span.clone().unwrap_or_default(),
),

View File

@ -15,18 +15,8 @@
// 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,
AsgConvertError, Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Span, Statement,
Type, Variable,
};
use std::cell::{Cell, RefCell};
@ -106,7 +96,7 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
scope
.variables
.borrow_mut()
.insert(variable.borrow().name.name.clone(), *variable);
.insert(variable.borrow().name.name.to_string(), *variable);
}
let statement = scope

View File

@ -17,16 +17,7 @@
use leo_ast::IntegerType;
use crate::{
AsgConvertError,
Expression,
ExpressionNode,
FromAst,
InnerVariable,
Node,
PartialType,
Scope,
Span,
Statement,
AsgConvertError, Expression, ExpressionNode, FromAst, InnerVariable, Node, PartialType, Scope, Span, Statement,
Variable,
};
@ -85,7 +76,7 @@ impl<'a> FromAst<'a, leo_ast::IterationStatement> for &'a Statement<'a> {
scope
.variables
.borrow_mut()
.insert(statement.variable.name.clone(), variable);
.insert(statement.variable.name.to_string(), variable);
let statement = scope.context.alloc_statement(Statement::Iteration(IterationStatement {
parent: Cell::new(None),

View File

@ -204,7 +204,9 @@ impl<'a> Into<leo_ast::Type> for &Type<'a> {
Integer(int_type) => leo_ast::Type::IntegerType(int_type.clone()),
Array(type_, len) => leo_ast::Type::Array(
Box::new(type_.as_ref().into()),
leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber { value: len.to_string() }]),
leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber {
value: len.to_string().into(),
}]),
),
Tuple(subtypes) => leo_ast::Type::Tuple(subtypes.iter().map(Into::into).collect()),
Circuit(circuit) => leo_ast::Type::Circuit(circuit.name.borrow().clone()),

View File

@ -41,6 +41,9 @@ version = "1.0"
[dependencies.thiserror]
version = "1.0"
[dependencies.tendril]
version = "0.4"
[dev-dependencies.criterion]
version = "0.3"

View File

@ -17,10 +17,12 @@
use crate::{Identifier, Span};
use serde::{Deserialize, Serialize};
use tendril::StrTendril;
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Annotation {
pub span: Span,
pub name: Identifier,
pub arguments: Vec<String>,
#[serde(with = "crate::common::vec_tendril_json")]
pub arguments: Vec<StrTendril>,
}

View File

@ -32,7 +32,7 @@ impl ArrayDimensions {
///
pub fn push_usize(&mut self, number: usize) {
let positive_number = PositiveNumber {
value: number.to_string(),
value: number.to_string().into(),
};
self.0.push(positive_number)

View File

@ -16,17 +16,14 @@
use crate::Span;
use leo_input::common::Identifier as InputIdentifier;
use tendril::StrTendril;
use crate::Node;
use serde::{
de::{
Visitor,
{self},
Visitor, {self},
},
Deserialize,
Deserializer,
Serialize,
Serializer,
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
collections::BTreeMap,
@ -41,7 +38,7 @@ use std::{
/// to reflect the new struct instantiation.
#[derive(Clone)]
pub struct Identifier {
pub name: String,
pub name: StrTendril,
pub span: Span,
}
@ -56,7 +53,7 @@ impl Node for Identifier {
}
impl Identifier {
pub fn new(name: String) -> Self {
pub fn new(name: StrTendril) -> Self {
Self {
name,
span: Span::default(),
@ -65,24 +62,16 @@ impl Identifier {
pub fn new_with_span(name: &str, span: Span) -> Self {
Self {
name: name.to_owned(),
name: name.into(),
span,
}
}
pub fn is_self_type(&self) -> bool {
self.name == "Self"
}
pub fn is_self(&self) -> bool {
self.is_self_type() || self.name == "self"
}
}
impl<'ast> From<InputIdentifier<'ast>> for Identifier {
fn from(identifier: InputIdentifier<'ast>) -> Self {
Self {
name: identifier.value,
name: identifier.value.into(),
span: Span::from(identifier.span),
}
}
@ -123,7 +112,7 @@ impl Serialize for Identifier {
// Load the struct elements into a BTreeMap (to preserve serialized ordering of keys).
let mut key: BTreeMap<String, String> = BTreeMap::new();
key.insert("name".to_string(), self.name.clone());
key.insert("name".to_string(), self.name.to_string());
key.insert("span".to_string(), to_json_string(&self.span)?);
// Convert the serialized object into a string for use as a key.
@ -164,7 +153,10 @@ impl<'de> Deserialize<'de> for Identifier {
None => return Err(E::custom("missing 'span' in serialized Identifier struct")),
};
Ok(Identifier { name, span })
Ok(Identifier {
name: name.into(),
span,
})
}
}

View File

@ -40,3 +40,7 @@ pub use span::*;
pub mod spread_or_expression;
pub use spread_or_expression::*;
pub mod tendril_json;
pub mod vec_tendril_json;

View File

@ -18,11 +18,13 @@ use leo_input::values::PositiveNumber as InputPositiveNumber;
use serde::{Deserialize, Serialize};
use std::fmt;
use tendril::StrTendril;
/// A number string guaranteed to be positive by the pest grammar.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
pub struct PositiveNumber {
pub value: String,
#[serde(with = "crate::common::tendril_json")]
pub value: StrTendril,
}
impl PositiveNumber {
@ -30,14 +32,16 @@ impl PositiveNumber {
/// Returns `true` if this number is zero.
///
pub fn is_zero(&self) -> bool {
self.value.eq("0")
self.value.as_ref().eq("0")
}
}
/// Create a new [`PositiveNumber`] from an [`InputPositiveNumber`] in a Leo input file.
impl<'ast> From<InputPositiveNumber<'ast>> for PositiveNumber {
fn from(array: InputPositiveNumber<'ast>) -> Self {
Self { value: array.value }
Self {
value: array.value.into(),
}
}
}

View File

@ -14,9 +14,10 @@
// 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 std::{fmt, rc::Rc};
use std::{fmt, sync::Arc};
use serde::{Deserialize, Serialize};
use tendril::StrTendril;
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct Span {
@ -24,7 +25,9 @@ pub struct Span {
pub line_stop: usize,
pub col_start: usize,
pub col_stop: usize,
pub path: Rc<String>,
pub path: Arc<String>,
#[serde(with = "crate::common::tendril_json")]
pub content: StrTendril,
}
impl fmt::Display for Span {
@ -51,7 +54,8 @@ impl<'ast> From<pest::Span<'ast>> for Span {
line_stop: end.0,
col_start: start.1,
col_stop: end.1,
path: Rc::new(String::new()),
path: Arc::new(String::new()),
content: span.as_str().into(),
}
}
}
@ -76,22 +80,40 @@ impl std::ops::Add for Span {
col_start: self.col_start.min(other.col_start),
col_stop: self.col_stop.max(other.col_stop),
path: self.path,
}
} else if self.line_start < other.line_stop {
Span {
line_start: self.line_start,
line_stop: other.line_stop,
col_start: self.col_start,
col_stop: other.col_stop,
path: self.path,
content: self.content,
}
} else {
Span {
line_start: other.line_start,
line_stop: self.line_stop,
col_start: other.col_start,
col_stop: self.col_stop,
path: self.path,
let mut new_content = vec![];
let self_lines = self.content.lines().collect::<Vec<_>>();
let other_lines = other.content.lines().collect::<Vec<_>>();
for line in self.line_start.min(other.line_start)..self.line_stop.max(other.line_stop) + 1 {
if line >= self.line_start && line <= self.line_stop {
new_content.push(self_lines.get(line - self.line_start).copied().unwrap_or_default());
} else if line >= other.line_start && line <= other.line_stop {
new_content.push(other_lines.get(line - other.line_start).copied().unwrap_or_default());
} else if new_content.last().map(|x| *x != "...").unwrap_or(true) {
new_content.push("...");
}
}
let new_content = new_content.join("\n").into();
if self.line_start < other.line_stop {
Span {
line_start: self.line_start,
line_stop: other.line_stop,
col_start: self.col_start,
col_stop: other.col_stop,
path: self.path,
content: new_content,
}
} else {
Span {
line_start: other.line_start,
line_stop: self.line_stop,
col_start: other.col_start,
col_stop: self.col_stop,
path: self.path,
content: new_content,
}
}
}
}

View File

@ -0,0 +1,26 @@
// 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 serde::{Deserialize, Deserializer, Serializer};
use tendril::StrTendril;
pub fn serialize<S: Serializer>(tendril: &StrTendril, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&tendril[..])
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<StrTendril, D::Error> {
Ok(String::deserialize(deserializer)?.into())
}

View File

@ -0,0 +1,33 @@
// 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 serde::{Deserialize, Deserializer, Serialize, Serializer};
use tendril::StrTendril;
pub fn serialize<S: Serializer>(tendril: &Vec<StrTendril>, serializer: S) -> Result<S::Ok, S::Error> {
tendril
.iter()
.map(|x| x.as_ref())
.collect::<Vec<_>>()
.serialize(serializer)
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<StrTendril>, D::Error> {
Ok(Vec::<String>::deserialize(deserializer)?
.into_iter()
.map(|x| x.into())
.collect())
}

View File

@ -16,7 +16,7 @@
use crate::{LeoError, Span};
use std::fmt;
use std::{fmt, sync::Arc};
pub const INDENT: &str = " ";
@ -29,60 +29,30 @@ pub const INDENT: &str = " ";
/// = undefined value `x`
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct FormattedError {
/// File path where error occurred
pub path: Option<String>,
/// Line start number
pub line_start: usize,
/// Line end number
pub line_stop: usize,
/// Starting column
pub start: usize,
/// Ending column
pub end: usize,
/// Text of errored lines
pub text: Option<Vec<String>>,
/// Error explanation
pub col_start: usize,
pub col_stop: usize,
pub path: Arc<String>,
pub content: String,
pub message: String,
}
impl FormattedError {
pub fn new_from_span(message: String, span: &Span) -> Self {
Self {
path: None,
line_start: span.line_start,
line_stop: span.line_stop,
start: span.col_start,
end: span.col_stop,
text: None,
col_start: span.col_start,
col_stop: span.col_stop,
path: span.path.clone(),
content: span.content.to_string(),
message,
}
}
}
impl LeoError for FormattedError {
fn set_path(&mut self, path: &str, content: &[String]) {
self.path = Some(path.to_string());
if self.line_stop - 1 > content.len() {
self.text = Some(vec!["corrupt file".to_string()]);
return;
}
assert!(self.line_stop >= self.line_start);
// if self.line_stop == self.line_start {
// self.text = Some(vec![content[self.line_start - 1][self.start - 1..self.end - 1].to_string()]);
// } else {
self.text = Some(
content[self.line_start - 1..self.line_stop]
.iter()
.map(|x| x.to_string())
.collect(),
);
// }
}
fn get_path(&self) -> Option<&str> {
self.path.as_deref()
}
}
impl LeoError for FormattedError {}
fn underline(mut start: usize, mut end: usize) -> String {
if start > end {
@ -105,29 +75,26 @@ fn underline(mut start: usize, mut end: usize) -> String {
impl fmt::Display for FormattedError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let path = self.path.as_ref().map(|path| format!("{}:", path)).unwrap_or_default();
let underline = underline(self.start - 1, self.end - 1);
let underline = underline(self.col_start - 1, self.col_stop - 1);
write!(
f,
"{indent }--> {path} {line_start}:{start}\n\
"{indent }--> {path}: {line_start}:{start}\n\
{indent } |\n",
indent = INDENT,
path = path,
path = &*self.path,
line_start = self.line_start,
start = self.start,
start = self.col_start,
)?;
if let Some(lines) = &self.text {
for (line_no, line) in lines.iter().enumerate() {
writeln!(
f,
"{line_no:width$} | {text}",
width = INDENT.len(),
line_no = self.line_start + line_no,
text = line,
)?;
}
for (line_no, line) in self.content.lines().enumerate() {
writeln!(
f,
"{line_no:width$} | {text}",
width = INDENT.len(),
line_no = self.line_start + line_no,
text = line,
)?;
}
write!(
@ -151,12 +118,12 @@ impl std::error::Error for FormattedError {
#[test]
fn test_error() {
let err = FormattedError {
path: Some("file.leo".to_string()),
path: std::sync::Arc::new("file.leo".to_string()),
line_start: 2,
line_stop: 2,
start: 8,
end: 9,
text: Some(vec!["let a = x;".to_string()]),
col_start: 8,
col_stop: 9,
content: "let a = x;".into(),
message: "undefined value `x`".to_string(),
};

View File

@ -17,8 +17,4 @@
pub mod error;
pub use error::*;
pub trait LeoError {
fn get_path(&self) -> Option<&str>;
fn set_path(&mut self, path: &str, contents: &[String]);
}
pub trait LeoError {}

View File

@ -15,13 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
ArrayDimensions,
CircuitImpliedVariableDefinition,
GroupValue,
Identifier,
IntegerType,
PositiveNumber,
Span,
ArrayDimensions, CircuitImpliedVariableDefinition, GroupValue, Identifier, IntegerType, PositiveNumber, Span,
SpreadOrExpression,
};

View File

@ -14,18 +14,24 @@
// 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 tendril::StrTendril;
use super::*;
use crate::GroupTuple;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ValueExpression {
// todo: deserialize values here
Address(String, Span),
Boolean(String, Span),
Field(String, Span),
Address(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
Boolean(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
Field(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
Group(Box<GroupValue>),
Implicit(String, Span),
Integer(IntegerType, String, Span),
Implicit(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
Integer(
IntegerType,
#[serde(with = "crate::common::tendril_json")] StrTendril,
Span,
),
}
impl fmt::Display for ValueExpression {

View File

@ -16,19 +16,17 @@
use crate::common::span::Span;
use leo_input::values::{
GroupCoordinate as InputGroupCoordinate,
Inferred as InputInferred,
NumberValue as InputNumberValue,
SignHigh as InputSignHigh,
SignLow as InputSignLow,
GroupCoordinate as InputGroupCoordinate, Inferred as InputInferred, NumberValue as InputNumberValue,
SignHigh as InputSignHigh, SignLow as InputSignLow,
};
use serde::{Deserialize, Serialize};
use std::fmt;
use tendril::StrTendril;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GroupCoordinate {
Number(String, Span),
Number(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
SignHigh,
SignLow,
Inferred,
@ -61,7 +59,7 @@ impl<'ast> From<InputNumberValue<'ast>> for GroupCoordinate {
let value = number.to_string();
let span = Span::from(number.span().clone());
GroupCoordinate::Number(value, span)
GroupCoordinate::Number(value.into(), span)
}
}

View File

@ -16,17 +16,16 @@
use crate::{common::span::Span, groups::GroupCoordinate};
use leo_input::values::{
GroupRepresentation as InputGroupRepresentation,
GroupTuple as InputGroupTuple,
GroupValue as InputGroupValue,
GroupRepresentation as InputGroupRepresentation, GroupTuple as InputGroupTuple, GroupValue as InputGroupValue,
};
use serde::{Deserialize, Serialize};
use std::fmt;
use tendril::StrTendril;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GroupValue {
Single(String, Span),
Single(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
Tuple(GroupTuple),
}
@ -51,7 +50,7 @@ impl<'ast> From<InputGroupValue<'ast>> for GroupValue {
let span = Span::from(ast_group.span);
match ast_group.value {
InputGroupRepresentation::Single(number) => GroupValue::Single(number.to_string(), span),
InputGroupRepresentation::Single(number) => GroupValue::Single(number.to_string().into(), span),
InputGroupRepresentation::Tuple(tuple) => GroupValue::Tuple(GroupTuple::from(tuple)),
}
}

View File

@ -51,7 +51,7 @@ impl ImportSymbol {
pub fn star(span: &Span) -> Self {
Self {
symbol: Identifier {
name: "*".to_string(),
name: "*".into(),
span: span.clone(),
},
alias: None,
@ -60,6 +60,6 @@ impl ImportSymbol {
}
pub fn is_star(&self) -> bool {
self.symbol.name.eq("*")
self.symbol.name.as_ref().eq("*")
}
}

View File

@ -96,13 +96,13 @@ impl Input {
/// Returns the main function input value with the given `name`.
#[allow(clippy::ptr_arg)]
pub fn get(&self, name: &String) -> Option<Option<InputValue>> {
pub fn get(&self, name: &str) -> Option<Option<InputValue>> {
self.program_input.get(name)
}
/// Returns the constant input value with the given `name`.
#[allow(clippy::ptr_arg)]
pub fn get_constant(&self, name: &String) -> Option<Option<InputValue>> {
pub fn get_constant(&self, name: &str) -> Option<Option<InputValue>> {
self.program_input.get_constant(name)
}

View File

@ -74,12 +74,12 @@ impl ProgramInput {
/// Returns the main function input value with the given `name`
#[allow(clippy::ptr_arg)]
pub fn get(&self, name: &String) -> Option<Option<InputValue>> {
pub fn get(&self, name: &str) -> Option<Option<InputValue>> {
self.main.get(name)
}
#[allow(clippy::ptr_arg)]
pub fn get_constant(&self, name: &String) -> Option<Option<InputValue>> {
pub fn get_constant(&self, name: &str) -> Option<Option<InputValue>> {
self.constants.get(name)
}

View File

@ -40,7 +40,11 @@ impl PublicState {
}
pub fn len(&self) -> usize {
if self.state.is_present() { 1usize } else { 0usize }
if self.state.is_present() {
1usize
} else {
0usize
}
}
/// Parse all input variables included in a file and store them in `self`.

View File

@ -171,7 +171,7 @@ impl Canonicalizer {
Expression::CircuitInit(circuit_init) => {
let mut name = circuit_init.name.clone();
if name.name == *"Self" {
if name.name.as_ref() == "Self" {
name = self.circuit_name.as_ref().unwrap().clone();
}

View File

@ -18,10 +18,11 @@ use crate::{Expression, Node, Span};
use serde::{Deserialize, Serialize};
use std::fmt;
use tendril::StrTendril;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum FormattedStringPart {
Const(String),
Const(#[serde(with = "crate::common::tendril_json")] StrTendril),
Container,
}

View File

@ -15,8 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_input::types::{
IntegerType as InputIntegerType,
SignedIntegerType as InputSignedIntegerType,
IntegerType as InputIntegerType, SignedIntegerType as InputSignedIntegerType,
UnsignedIntegerType as InputUnsignedIntegerType,
};

View File

@ -16,10 +16,7 @@
use crate::{ArrayDimensions, Identifier, IntegerType};
use leo_input::types::{
ArrayType as InputArrayType,
DataType as InputDataType,
TupleType as InputTupleType,
Type as InputType,
ArrayType as InputArrayType, DataType as InputDataType, TupleType as InputTupleType, Type as InputType,
};
use serde::{Deserialize, Serialize};

View File

@ -19,15 +19,11 @@
use crate::{
constraints::{generate_constraints, generate_test_constraints},
errors::CompilerError,
CompilerOptions,
GroupType,
OutputBytes,
OutputFile,
CompilerOptions, GroupType, OutputBytes, OutputFile,
};
use indexmap::IndexMap;
pub use leo_asg::{new_context, AsgContext as Context, AsgContext};
use leo_asg::{Asg, AsgPass, FormattedError, Program as AsgProgram};
use leo_ast::{Input, LeoError, MainInput, Program as AstProgram};
use leo_ast::{Input, MainInput, Program as AstProgram};
use leo_input::LeoInputParser;
use leo_package::inputs::InputPairs;
use leo_parser::parse_ast;
@ -39,11 +35,9 @@ use snarkvm_r1cs::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
use sha2::{Digest, Sha256};
use std::{
cell::RefCell,
fs,
marker::PhantomData,
path::{Path, PathBuf},
rc::Rc,
};
thread_local! {
@ -68,7 +62,6 @@ pub struct Compiler<'a, F: PrimeField, G: GroupType<F>> {
program_input: Input,
context: AsgContext<'a>,
asg: Option<AsgProgram<'a>>,
file_contents: RefCell<IndexMap<String, Rc<Vec<String>>>>,
options: CompilerOptions,
_engine: PhantomData<F>,
_group: PhantomData<G>,
@ -93,7 +86,6 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
asg: None,
context,
options: CompilerOptions::default(),
file_contents: RefCell::new(IndexMap::new()),
_engine: PhantomData,
_group: PhantomData,
}
@ -200,20 +192,6 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
Ok(())
}
fn resolve_content(&self, path: &str) -> Result<Rc<Vec<String>>, CompilerError> {
let mut file_contents = self.file_contents.borrow_mut();
if file_contents.contains_key(path) {
// using this pattern because of mutable reference in branch below
Ok(file_contents.get(path).unwrap().clone())
} else {
let content = fs::read_to_string(path).map_err(|e| CompilerError::FileReadError(PathBuf::from(path), e))?;
let content = Rc::new(content.lines().map(|x| x.to_string()).collect::<Vec<String>>());
file_contents.insert(path.to_string(), content);
Ok(file_contents.get(path).unwrap().clone())
}
}
///
/// Parses and stores the main program file, constructs a syntax tree, and generates a program.
///
@ -224,31 +202,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
let content = fs::read_to_string(&self.main_file_path)
.map_err(|e| CompilerError::FileReadError(self.main_file_path.clone(), e))?;
self.parse_program_from_string(&content).map_err(|mut error| {
// Return a formatted error with file path and code text.
let path = match error.get_path().map(|x| x.to_string()) {
// Get the file path if it exists
Some(path) => path,
// If a file path does not exist, then insert the main file path.
None => match self.main_file_path.clone().into_os_string().into_string() {
Err(e) => return CompilerError::FileStringError(e),
Ok(path) => path,
},
};
// Resolve the code text using the file path.
let content = match self.resolve_content(&path) {
Err(e) => return e,
Ok(x) => x,
};
// Update the formatted error.
error.set_path(&path, &content[..]);
error
})
self.parse_program_from_string(&content)
}
///
@ -257,11 +211,6 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
///
pub fn parse_program_from_string(&mut self, program_string: &str) -> Result<(), CompilerError> {
// Use the parser to construct the abstract syntax tree (ast).
let lines = program_string.lines().map(|x| x.to_string()).collect();
self.file_contents.borrow_mut().insert(
self.main_file_path.to_str().map(|x| x.to_string()).unwrap_or_default(),
Rc::new(lines),
);
let mut ast = parse_ast(self.main_file_path.to_str().unwrap_or_default(), program_string)?;
// Preform compiler optimization via canonicalizing AST if its enabled.
@ -313,28 +262,14 @@ 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<OutputBytes, CompilerError> {
generate_constraints::<F, G, CS>(cs, &self.asg.as_ref().unwrap(), &self.program_input).map_err(|mut error| {
if let Some(path) = error.get_path().map(|x| x.to_string()) {
let content = match self.resolve_content(&path) {
Err(e) => return e,
Ok(x) => x,
};
error.set_path(&path, &content[..]);
}
error
})
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> {
generate_test_constraints::<F, G>(
&self.asg.as_ref().unwrap(),
input_pairs,
&self.main_file_path,
&self.output_directory,
)
generate_test_constraints::<F, G>(&self.asg.as_ref().unwrap(), input_pairs, &self.output_directory)
}
///

View File

@ -17,11 +17,7 @@
//! Enforces an assert equals statement in a compiled Leo program.
use crate::{
errors::ConsoleError,
get_indicator_value,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
errors::ConsoleError, get_indicator_value, program::ConstrainedProgram, value::ConstrainedValue, GroupType,
};
use leo_asg::{Expression, Span};

View File

@ -18,7 +18,7 @@
use crate::{errors::CompilerError, ConstrainedProgram, GroupType, OutputBytes, OutputFile};
use leo_asg::Program;
use leo_ast::{Input, LeoError};
use leo_ast::Input;
use leo_input::LeoInputParser;
use leo_package::inputs::InputPairs;
@ -50,7 +50,6 @@ pub fn generate_constraints<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSy
pub fn generate_test_constraints<'a, F: PrimeField, G: GroupType<F>>(
program: &Program<'a>,
input: InputPairs,
main_file_path: &Path,
output_directory: &Path,
) -> Result<(u32, u32), CompilerError> {
let mut resolved_program = ConstrainedProgram::<F, G>::new(program.clone());
@ -78,7 +77,7 @@ pub fn generate_test_constraints<'a, F: PrimeField, G: GroupType<F>>(
let input_file = function
.annotations
.iter()
.find(|x| x.name.name == "test")
.find(|x| x.name.name.as_ref() == "test")
.unwrap()
.arguments
.get(0);
@ -87,11 +86,11 @@ pub fn generate_test_constraints<'a, F: PrimeField, G: GroupType<F>>(
Some(file_id) => {
let file_name = file_id.clone();
output_file_name = file_name.clone();
output_file_name = file_name.to_string();
match input.pairs.get(&file_name) {
match input.pairs.get(file_name.as_ref()) {
Some(pair) => pair.to_owned(),
None => return Err(CompilerError::InvalidTestContext(file_name)),
None => return Err(CompilerError::InvalidTestContext(file_name.to_string())),
}
}
None => default.ok_or(CompilerError::NoTestInput)?,
@ -135,8 +134,7 @@ pub fn generate_test_constraints<'a, F: PrimeField, G: GroupType<F>>(
}
(false, _) => {
// Set file location of error
let mut error = result.unwrap_err();
error.set_path(main_file_path.to_str().unwrap_or_default(), &[]);
let error = result.unwrap_err();
tracing::error!("{} failed due to error\n\n{}\n", full_test_name, error);

View File

@ -82,30 +82,4 @@ pub enum CompilerError {
CanonicalizeError(#[from] CanonicalizeError),
}
impl LeoError for CompilerError {
fn get_path(&self) -> Option<&str> {
match self {
CompilerError::SyntaxError(error) => error.get_path(),
CompilerError::ImportError(error) => error.get_path(),
CompilerError::ImportParserError(error) => error.get_path(),
CompilerError::InputParserError(error) => error.get_path(),
CompilerError::FunctionError(error) => error.get_path(),
CompilerError::OutputStringError(error) => error.get_path(),
CompilerError::AsgConvertError(error) => error.get_path(),
_ => None,
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
CompilerError::SyntaxError(error) => error.set_path(path, contents),
CompilerError::ImportError(error) => error.set_path(path, contents),
CompilerError::ImportParserError(error) => error.set_path(path, contents),
CompilerError::InputParserError(error) => error.set_path(path, contents),
CompilerError::FunctionError(error) => error.set_path(path, contents),
CompilerError::OutputStringError(error) => error.set_path(path, contents),
CompilerError::AsgConvertError(error) => error.set_path(path, contents),
_ => {}
}
}
}
impl LeoError for CompilerError {}

View File

@ -26,21 +26,7 @@ pub enum ConsoleError {
Expression(#[from] ExpressionError),
}
impl LeoError for ConsoleError {
fn get_path(&self) -> Option<&str> {
match self {
ConsoleError::Error(error) => error.get_path(),
ConsoleError::Expression(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
ConsoleError::Error(error) => error.set_path(path, contents),
ConsoleError::Expression(error) => error.set_path(path, contents),
}
}
}
impl LeoError for ConsoleError {}
impl ConsoleError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -45,33 +45,7 @@ pub enum ExpressionError {
ValueError(#[from] ValueError),
}
impl LeoError for ExpressionError {
fn get_path(&self) -> Option<&str> {
match self {
ExpressionError::AddressError(error) => error.get_path(),
ExpressionError::BooleanError(error) => error.get_path(),
ExpressionError::Error(error) => error.get_path(),
ExpressionError::FieldError(error) => error.get_path(),
ExpressionError::FunctionError(error) => error.get_path(),
ExpressionError::GroupError(error) => error.get_path(),
ExpressionError::IntegerError(error) => error.get_path(),
ExpressionError::ValueError(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
ExpressionError::AddressError(error) => error.set_path(path, contents),
ExpressionError::BooleanError(error) => error.set_path(path, contents),
ExpressionError::Error(error) => error.set_path(path, contents),
ExpressionError::FieldError(error) => error.set_path(path, contents),
ExpressionError::FunctionError(error) => error.set_path(path, contents),
ExpressionError::GroupError(error) => error.set_path(path, contents),
ExpressionError::IntegerError(error) => error.set_path(path, contents),
ExpressionError::ValueError(error) => error.set_path(path, contents),
}
}
}
impl LeoError for ExpressionError {}
impl ExpressionError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -15,15 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::errors::{
AddressError,
BooleanError,
ExpressionError,
FieldError,
GroupError,
IntegerError,
OutputBytesError,
StatementError,
ValueError,
AddressError, BooleanError, ExpressionError, FieldError, GroupError, IntegerError, OutputBytesError,
StatementError, ValueError,
};
use leo_asg::AsgConvertError;
use leo_ast::{FormattedError, LeoError, Span};
@ -64,39 +57,7 @@ pub enum FunctionError {
ImportASGError(#[from] AsgConvertError),
}
impl LeoError for FunctionError {
fn get_path(&self) -> Option<&str> {
match self {
FunctionError::AddressError(error) => error.get_path(),
FunctionError::BooleanError(error) => error.get_path(),
FunctionError::ExpressionError(error) => error.get_path(),
FunctionError::Error(error) => error.get_path(),
FunctionError::FieldError(error) => error.get_path(),
FunctionError::GroupError(error) => error.get_path(),
FunctionError::IntegerError(error) => error.get_path(),
FunctionError::OutputStringError(error) => error.get_path(),
FunctionError::StatementError(error) => error.get_path(),
FunctionError::ValueError(error) => error.get_path(),
FunctionError::ImportASGError(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
FunctionError::AddressError(error) => error.set_path(path, contents),
FunctionError::BooleanError(error) => error.set_path(path, contents),
FunctionError::ExpressionError(error) => error.set_path(path, contents),
FunctionError::Error(error) => error.set_path(path, contents),
FunctionError::FieldError(error) => error.set_path(path, contents),
FunctionError::GroupError(error) => error.set_path(path, contents),
FunctionError::IntegerError(error) => error.set_path(path, contents),
FunctionError::OutputStringError(error) => error.set_path(path, contents),
FunctionError::StatementError(error) => error.set_path(path, contents),
FunctionError::ValueError(error) => error.set_path(path, contents),
FunctionError::ImportASGError(error) => error.set_path(path, contents),
}
}
}
impl LeoError for FunctionError {}
impl FunctionError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -22,19 +22,7 @@ pub enum ImportError {
Error(#[from] FormattedError),
}
impl LeoError for ImportError {
fn get_path(&self) -> Option<&str> {
match self {
ImportError::Error(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
ImportError::Error(error) => error.set_path(path, contents),
}
}
}
impl LeoError for ImportError {}
impl ImportError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -30,23 +30,7 @@ pub enum OutputBytesError {
AsgConvertError(#[from] AsgConvertError),
}
impl LeoError for OutputBytesError {
fn get_path(&self) -> Option<&str> {
match self {
OutputBytesError::Error(error) => error.get_path(),
OutputBytesError::ValueError(error) => error.get_path(),
OutputBytesError::AsgConvertError(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
OutputBytesError::Error(error) => error.set_path(path, contents),
OutputBytesError::ValueError(error) => error.set_path(path, contents),
OutputBytesError::AsgConvertError(error) => error.set_path(path, contents),
}
}
}
impl LeoError for OutputBytesError {}
impl OutputBytesError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -42,31 +42,7 @@ pub enum StatementError {
ValueError(#[from] ValueError),
}
impl LeoError for StatementError {
fn get_path(&self) -> Option<&str> {
match self {
StatementError::AddressError(error) => error.get_path(),
StatementError::BooleanError(error) => error.get_path(),
StatementError::Error(error) => error.get_path(),
StatementError::ExpressionError(error) => error.get_path(),
StatementError::IntegerError(error) => error.get_path(),
StatementError::MacroError(error) => error.get_path(),
StatementError::ValueError(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
StatementError::AddressError(error) => error.set_path(path, contents),
StatementError::BooleanError(error) => error.set_path(path, contents),
StatementError::Error(error) => error.set_path(path, contents),
StatementError::ExpressionError(error) => error.set_path(path, contents),
StatementError::IntegerError(error) => error.set_path(path, contents),
StatementError::MacroError(error) => error.set_path(path, contents),
StatementError::ValueError(error) => error.set_path(path, contents),
}
}
}
impl LeoError for StatementError {}
impl StatementError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -24,19 +24,7 @@ pub enum AddressError {
Error(#[from] FormattedError),
}
impl LeoError for AddressError {
fn get_path(&self) -> Option<&str> {
match self {
AddressError::Error(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
AddressError::Error(error) => error.set_path(path, contents),
}
}
}
impl LeoError for AddressError {}
impl AddressError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -23,19 +23,7 @@ pub enum BooleanError {
Error(#[from] FormattedError),
}
impl LeoError for BooleanError {
fn get_path(&self) -> Option<&str> {
match self {
BooleanError::Error(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
BooleanError::Error(error) => error.set_path(path, contents),
}
}
}
impl LeoError for BooleanError {}
impl BooleanError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -23,19 +23,7 @@ pub enum FieldError {
Error(#[from] FormattedError),
}
impl LeoError for FieldError {
fn get_path(&self) -> Option<&str> {
match self {
FieldError::Error(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
FieldError::Error(error) => error.set_path(path, contents),
}
}
}
impl LeoError for FieldError {}
impl FieldError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -23,19 +23,7 @@ pub enum GroupError {
Error(#[from] FormattedError),
}
impl LeoError for GroupError {
fn get_path(&self) -> Option<&str> {
match self {
GroupError::Error(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
GroupError::Error(error) => error.set_path(path, contents),
}
}
}
impl LeoError for GroupError {}
impl GroupError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -25,19 +25,7 @@ pub enum IntegerError {
Error(#[from] FormattedError),
}
impl LeoError for IntegerError {
fn get_path(&self) -> Option<&str> {
match self {
IntegerError::Error(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
IntegerError::Error(error) => error.set_path(path, contents),
}
}
}
impl LeoError for IntegerError {}
impl IntegerError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -38,29 +38,7 @@ pub enum ValueError {
IntegerError(#[from] IntegerError),
}
impl LeoError for ValueError {
fn get_path(&self) -> Option<&str> {
match self {
ValueError::AddressError(error) => error.get_path(),
ValueError::BooleanError(error) => error.get_path(),
ValueError::Error(error) => error.get_path(),
ValueError::FieldError(error) => error.get_path(),
ValueError::GroupError(error) => error.get_path(),
ValueError::IntegerError(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
ValueError::AddressError(error) => error.set_path(path, contents),
ValueError::BooleanError(error) => error.set_path(path, contents),
ValueError::Error(error) => error.set_path(path, contents),
ValueError::FieldError(error) => error.set_path(path, contents),
ValueError::GroupError(error) => error.set_path(path, contents),
ValueError::IntegerError(error) => error.set_path(path, contents),
}
}
}
impl LeoError for ValueError {}
impl ValueError {
fn new_from_span(message: String, span: &Span) -> Self {

View File

@ -42,7 +42,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
// type checking is already done in asg
for (name, inner) in expr.values.iter() {
let target = members
.get(&name.name)
.get(name.name.as_ref())
.expect("illegal name in asg circuit init expression");
match target {
CircuitMember::Variable(_type_) => {

View File

@ -24,8 +24,7 @@ use crate::{
relational::*,
resolve_core_circuit,
value::{Address, ConstrainedValue, Integer},
FieldType,
GroupType,
FieldType, GroupType,
};
use leo_asg::{expression::*, ConstValue, Expression, Node, Span};
@ -41,7 +40,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
span: &Span,
) -> Result<ConstrainedValue<'a, F, G>, ExpressionError> {
Ok(match value {
ConstValue::Address(value) => ConstrainedValue::Address(Address::constant(value.clone(), span)?),
ConstValue::Address(value) => ConstrainedValue::Address(Address::constant(value.to_string(), span)?),
ConstValue::Boolean(value) => ConstrainedValue::Boolean(Boolean::Constant(*value)),
ConstValue::Field(value) => ConstrainedValue::Field(FieldType::constant(value.to_string(), span)?),
ConstValue::Group(value) => ConstrainedValue::Group(G::constant(value, span)?),

View File

@ -38,19 +38,19 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
// Create an identifier for each input variable
let registers_name = Identifier {
name: REGISTERS_VARIABLE_NAME.to_string(),
name: REGISTERS_VARIABLE_NAME.into(),
span: span.clone(),
};
let record_name = Identifier {
name: RECORD_VARIABLE_NAME.to_string(),
name: RECORD_VARIABLE_NAME.into(),
span: span.clone(),
};
let state_name = Identifier {
name: STATE_VARIABLE_NAME.to_string(),
name: STATE_VARIABLE_NAME.into(),
span: span.clone(),
};
let state_leaf_name = Identifier {
name: STATE_LEAF_VARIABLE_NAME.to_string(),
name: STATE_LEAF_VARIABLE_NAME.into(),
span: span.clone(),
};
@ -73,7 +73,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
let mut members = Vec::with_capacity(sections.len());
for (name, values) in sections {
let sub_circuit = match expected_type.members.borrow().get(&name.name) {
let sub_circuit = match expected_type.members.borrow().get(name.name.as_ref()) {
Some(CircuitMember::Variable(Type::Circuit(circuit))) => *circuit,
_ => panic!("illegal input type definition from asg"),
};

View File

@ -36,7 +36,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
// Allocate each section definition as a circuit member value
for (parameter, option) in section.into_iter() {
let section_members = expected_type.members.borrow();
let expected_type = match section_members.get(&parameter.variable.name) {
let expected_type = match section_members.get(parameter.variable.name.as_ref()) {
Some(CircuitMember::Variable(inner)) => inner,
_ => continue, // present, but unused
};

View File

@ -21,9 +21,7 @@ use crate::{
errors::FunctionError,
program::ConstrainedProgram,
value::{
boolean::input::bool_from_input,
field::input::field_from_input,
group::input::group_from_input,
boolean::input::bool_from_input, field::input::field_from_input, group::input::group_from_input,
ConstrainedValue,
},
FieldType,

View File

@ -62,11 +62,11 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
let input_variable = input_variable.get().borrow();
let name = input_variable.name.name.clone();
let input_value = match (input_variable.const_, input.get(&name), input.get_constant(&name)) {
let input_value = match (input_variable.const_, input.get(&name), input.get_constant(name.as_ref())) {
// If variable is in both [main] and [constants] sections - error.
(_, Some(_), Some(_)) => {
return Err(FunctionError::double_input_declaration(
name.clone(),
name.to_string(),
&function.span.clone().unwrap_or_default(),
));
}
@ -89,21 +89,21 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
// Function argument is const, input is not.
(true, Some(_), None) => {
return Err(FunctionError::expected_const_input(
name.clone(),
name.to_string(),
&function.span.clone().unwrap_or_default(),
));
}
// Input is const, function argument is not.
(false, None, Some(_)) => {
return Err(FunctionError::expected_non_const_input(
name.clone(),
name.to_string(),
&function.span.clone().unwrap_or_default(),
));
}
// When not found - Error out.
(_, _, _) => {
return Err(FunctionError::input_not_found(
name.clone(),
name.to_string(),
&function.span.clone().unwrap_or_default(),
));
}

View File

@ -17,21 +17,11 @@
//! Resolves assignees in a compiled Leo program.
use crate::{
errors::StatementError,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
ResolvedAssigneeAccess,
errors::StatementError, program::ConstrainedProgram, value::ConstrainedValue, GroupType, ResolvedAssigneeAccess,
};
use leo_asg::{
ArrayAccessExpression,
ArrayRangeAccessExpression,
CircuitAccessExpression,
Expression,
Node,
Span,
TupleAccessExpression,
Variable,
ArrayAccessExpression, ArrayRangeAccessExpression, CircuitAccessExpression, Expression, Node, Span,
TupleAccessExpression, Variable,
};
use snarkvm_fields::PrimeField;

View File

@ -17,11 +17,7 @@
//! Enforces that one return value is produced in a compiled Leo program.
use crate::{
errors::StatementError,
get_indicator_value,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
errors::StatementError, get_indicator_value, program::ConstrainedProgram, value::ConstrainedValue, GroupType,
};
use leo_asg::{Span, Type};

View File

@ -21,8 +21,7 @@ use crate::errors::OutputFileError;
use std::{
borrow::Cow,
fs::{
File,
{self},
File, {self},
},
io::Write,
path::Path,

View File

@ -56,7 +56,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> CoreCircuit<'a, F, G> for Blake2s {
mut arguments: Vec<ConstrainedValue<'a, F, G>>,
) -> Result<ConstrainedValue<'a, F, G>, ExpressionError> {
assert_eq!(arguments.len(), 2); // asg enforced
assert!(function.name.borrow().name == "hash"); // asg enforced
assert!(function.name.borrow().name.as_ref() == "hash"); // asg enforced
assert!(target.is_none()); // asg enforced
let input = unwrap_argument(arguments.remove(1));
let seed = unwrap_argument(arguments.remove(0));

View File

@ -17,12 +17,8 @@
//! Methods to enforce constraints on statements in a compiled Leo program.
use crate::{
errors::StatementError,
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
IndicatorAndConstrainedValue,
StatementResult,
errors::StatementError, program::ConstrainedProgram, value::ConstrainedValue, GroupType,
IndicatorAndConstrainedValue, StatementResult,
};
use leo_asg::ConditionalStatement;

View File

@ -17,11 +17,7 @@
//! Enforces an iteration statement in a compiled Leo program.
use crate::{
program::ConstrainedProgram,
value::ConstrainedValue,
GroupType,
IndicatorAndConstrainedValue,
Integer,
program::ConstrainedProgram, value::ConstrainedValue, GroupType, IndicatorAndConstrainedValue, Integer,
StatementResult,
};
use leo_asg::IterationStatement;

View File

@ -198,7 +198,11 @@ impl<F: PrimeField> ConditionalEqGadget<F> for Address {
}
fn cond_select_helper(first: &Address, second: &Address, cond: bool) -> Address {
if cond { first.clone() } else { second.clone() }
if cond {
first.clone()
} else {
second.clone()
}
}
impl<F: PrimeField> CondSelectGadget<F> for Address {

View File

@ -289,7 +289,11 @@ impl<F: PrimeField> CondSelectGadget<F> for FieldType<F> {
second: &Self,
) -> Result<Self, SynthesisError> {
if let Boolean::Constant(cond) = *cond {
if cond { Ok(first.clone()) } else { Ok(second.clone()) }
if cond {
Ok(first.clone())
} else {
Ok(second.clone())
}
} else {
let first_gadget = first.allocated(&mut cs)?;
let second_gadget = second.allocated(&mut cs)?;

View File

@ -20,8 +20,7 @@ use leo_asg::{GroupCoordinate, GroupValue, Span};
use snarkvm_curves::{
edwards_bls12::{EdwardsAffine, EdwardsParameters, Fq},
templates::twisted_edwards_extended::GroupAffine,
AffineCurve,
TEModelParameters,
AffineCurve, TEModelParameters,
};
use snarkvm_fields::{Fp256, One, Zero};
use snarkvm_gadgets::{
@ -482,7 +481,11 @@ impl CondSelectGadget<Fq> for EdwardsGroupType {
second: &Self,
) -> Result<Self, SynthesisError> {
if let Boolean::Constant(cond) = *cond {
if cond { Ok(first.clone()) } else { Ok(second.clone()) }
if cond {
Ok(first.clone())
} else {
Ok(second.clone())
}
} else {
let first_gadget = first.allocated(cs.ns(|| "first"))?;
let second_gadget = second.allocated(cs.ns(|| "second"))?;

View File

@ -19,11 +19,14 @@ use snarkvm_gadgets::traits::utilities::{
int::{Int128, Int16, Int32, Int64, Int8},
uint::{UInt128, UInt16, UInt32, UInt64, UInt8},
};
use std::convert::TryInto;
use std::fmt::Debug;
pub trait IntegerTrait: Sized + Clone + Debug {
fn get_value(&self) -> Option<String>;
fn get_index(&self) -> Option<usize>;
fn get_bits(&self) -> Vec<Boolean>;
}
@ -34,6 +37,10 @@ macro_rules! integer_trait_impl {
self.value.map(|num| num.to_string())
}
fn get_index(&self) -> Option<usize> {
self.value.map(|num| num.try_into().ok()).flatten()
}
fn get_bits(&self) -> Vec<Boolean> {
self.bits.clone()
}

View File

@ -15,12 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
get_output,
parse_program,
parse_program_with_input,
assert_satisfied, expect_asg_error, expect_compiler_error, get_output, parse_program, parse_program_with_input,
EdwardsTestCompiler,
};

View File

@ -15,12 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
get_output,
parse_program,
parse_program_with_input,
assert_satisfied, expect_asg_error, expect_compiler_error, get_output, parse_program, parse_program_with_input,
EdwardsTestCompiler,
};

View File

@ -15,11 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
parse_program,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input, parse_program,
parse_program_with_input,
};
use leo_ast::InputValue;

View File

@ -15,12 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
generate_main_input,
get_output,
parse_program,
parse_program_with_input,
assert_satisfied, expect_asg_error, generate_main_input, get_output, parse_program, parse_program_with_input,
};
use leo_ast::InputValue;

View File

@ -15,10 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input,
integers::{expect_computation_error, IntegerTester},
parse_program,
};

View File

@ -15,10 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input,
integers::{expect_computation_error, IntegerTester},
parse_program,
};

View File

@ -15,10 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input,
integers::{expect_computation_error, IntegerTester},
parse_program,
};

View File

@ -15,10 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input,
integers::{expect_computation_error, IntegerTester},
parse_program,
};

View File

@ -15,10 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input,
integers::{expect_computation_error, IntegerTester},
parse_program,
};

View File

@ -15,11 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
integers::IntegerTester,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input, integers::IntegerTester,
parse_program,
};
use leo_ast::InputValue;

View File

@ -15,11 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
integers::IntegerTester,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input, integers::IntegerTester,
parse_program,
};
use leo_ast::InputValue;

View File

@ -15,11 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
integers::IntegerTester,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input, integers::IntegerTester,
parse_program,
};
use leo_ast::InputValue;

View File

@ -15,11 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
integers::IntegerTester,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input, integers::IntegerTester,
parse_program,
};
use leo_ast::InputValue;

View File

@ -15,11 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_asg_error,
expect_compiler_error,
generate_main_input,
integers::IntegerTester,
assert_satisfied, expect_asg_error, expect_compiler_error, generate_main_input, integers::IntegerTester,
parse_program,
};
use leo_ast::InputValue;

View File

@ -41,10 +41,7 @@ pub mod tuples;
use leo_asg::{new_alloc_context, new_context, AsgContext};
use leo_ast::{InputValue, MainInput};
use leo_compiler::{
compiler::Compiler,
errors::CompilerError,
group::targets::edwards_bls12::EdwardsGroupType,
ConstrainedValue,
compiler::Compiler, errors::CompilerError, group::targets::edwards_bls12::EdwardsGroupType, ConstrainedValue,
OutputBytes,
};
use leo_input::types::{IntegerType, U32Type, UnsignedIntegerType};

View File

@ -15,14 +15,8 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
assert_satisfied,
expect_compiler_error,
generate_main_input,
generate_test_input_u32,
get_output,
parse_program,
parse_program_with_input,
EdwardsTestCompiler,
assert_satisfied, expect_compiler_error, generate_main_input, generate_test_input_u32, get_output, parse_program,
parse_program_with_input, EdwardsTestCompiler,
};
use leo_ast::InputValue;

View File

@ -33,25 +33,7 @@ pub enum ImportParserError {
AsgConvertError(#[from] AsgConvertError),
}
impl LeoError for ImportParserError {
fn get_path(&self) -> Option<&str> {
match self {
ImportParserError::Error(error) => error.get_path(),
ImportParserError::SyntaxError(error) => error.get_path(),
ImportParserError::AsgConvertError(error) => error.get_path(),
ImportParserError::DeprecatedError(error) => error.get_path(),
}
}
fn set_path(&mut self, path: &str, contents: &[String]) {
match self {
ImportParserError::Error(error) => error.set_path(path, contents),
ImportParserError::SyntaxError(error) => error.set_path(path, contents),
ImportParserError::AsgConvertError(error) => error.set_path(path, contents),
ImportParserError::DeprecatedError(error) => error.set_path(path, contents),
}
}
}
impl LeoError for ImportParserError {}
impl Into<AsgConvertError> for ImportParserError {
fn into(self) -> AsgConvertError {

View File

@ -17,8 +17,7 @@
use anyhow::{anyhow, Error, Result};
use reqwest::{
blocking::{Client, Response},
Method,
StatusCode,
Method, StatusCode,
};
use serde::Serialize;

View File

@ -20,8 +20,7 @@ use anyhow::{anyhow, Result};
use std::{
borrow::Cow,
fs::{
File,
{self},
File, {self},
},
io::{Read, Write},
path::Path,

View File

@ -16,9 +16,7 @@
use std::{
fs::{
create_dir_all,
File,
{self},
create_dir_all, File, {self},
},
io,
io::prelude::*,

View File

@ -23,19 +23,7 @@ pub mod updater;
use commands::{
package::{Add, Clone, Login, Logout, Publish, Remove},
Build,
Clean,
Command,
Deploy,
Init,
Lint,
New,
Prove,
Run,
Setup,
Test,
Update,
Watch,
Build, Clean, Command, Deploy, Init, Lint, New, Prove, Run, Setup, Test, Update, Watch,
};
use anyhow::Error;
@ -175,10 +163,13 @@ fn main() {
if !opt.quiet {
// init logger with optional debug flag
logger::init_logger("leo", match opt.debug {
false => 1,
true => 2,
});
logger::init_logger(
"leo",
match opt.debug {
false => 1,
true => 2,
},
);
}
handle_error(match opt.command {

View File

@ -21,14 +21,7 @@ use anyhow::Result;
use crate::{
commands::{
package::{Login, Logout},
Build,
Command,
Prove,
Run,
Setup,
Test,
Update,
UpdateAutomatic,
Build, Command, Prove, Run, Setup, Test, Update, UpdateAutomatic,
},
context::{create_context, Context},
};

View File

@ -22,8 +22,7 @@ use serde::Deserialize;
use std::{
borrow::Cow,
fs::{
File,
{self},
File, {self},
},
io::Write,
path::Path,

Some files were not shown because too many files have changed in this diff Show More