Merge branch 'testnet3' of github.com:AleoHQ/leo into bug/1604-package-name-parsing-and-aliases-fix

This commit is contained in:
gluax 2022-02-18 13:08:05 -08:00
commit 63f57649de
16 changed files with 232 additions and 193 deletions

View File

@ -5,7 +5,7 @@ updates:
schedule:
interval: daily
time: "10:00"
target-branch: "testnet2"
target-branch: "testnet3"
open-pull-requests-limit: 20
ignore:
- dependency-name: snarkvm-curves

35
Cargo.lock generated
View File

@ -267,9 +267,9 @@ dependencies = [
[[package]]
name = "clap"
version = "3.0.14"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b63edc3f163b3c71ec8aa23f9bd6070f77edbf3d1d198b164afa90ff00e4ec62"
checksum = "e5f1fea81f183005ced9e59cdb01737ef2423956dac5a6d731b06b2ecfaa3467"
dependencies = [
"atty",
"bitflags",
@ -433,11 +433,12 @@ dependencies = [
[[package]]
name = "crypto-common"
version = "0.1.1"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d6b536309245c849479fba3da410962a43ed8e51c26b729208ec0ac2798d0"
checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8"
dependencies = [
"generic-array 0.14.5",
"typenum",
]
[[package]]
@ -490,13 +491,12 @@ dependencies = [
[[package]]
name = "digest"
version = "0.10.1"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b697d66081d42af4fba142d56918a3cb21dc8eb63372c6b85d14f44fb9c5979b"
checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
dependencies = [
"block-buffer 0.10.0",
"crypto-common",
"generic-array 0.14.5",
]
[[package]]
@ -1121,13 +1121,26 @@ dependencies = [
"tendril",
]
[[package]]
name = "leo-ast-passes"
version = "1.5.3"
dependencies = [
"indexmap",
"leo-ast",
"leo-errors",
"leo-parser",
"leo-span",
]
[[package]]
name = "leo-compiler"
version = "1.5.3"
dependencies = [
"leo-ast",
"leo-ast-passes",
"leo-errors",
"leo-parser",
"leo-span",
"sha2",
]
@ -1166,7 +1179,7 @@ version = "1.5.3"
dependencies = [
"ansi_term",
"assert_cmd",
"clap 3.0.14",
"clap 3.1.0",
"color-backtrace",
"colored",
"console",
@ -2221,13 +2234,13 @@ dependencies = [
[[package]]
name = "sha2"
version = "0.10.1"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99c3bd8169c58782adad9290a9af5939994036b76187f7b4f0e6de91dbbfc0ec"
checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676"
dependencies = [
"cfg-if 1.0.0",
"cpufeatures",
"digest 0.10.1",
"digest 0.10.3",
]
[[package]]

View File

@ -30,7 +30,7 @@ members = [
# "asg",
# "asg-passes",
"ast",
# "ast-passes",
"ast-passes",
"compiler",
"errors",
"grammar",
@ -107,7 +107,7 @@ git = "https://github.com/AleoHQ/snarkVM.git"
rev = "51633e2"
[dependencies.clap]
version = "3.0.10"
version = "3.1"
[dependencies.color-backtrace]
version = "0.5.1"

View File

@ -36,6 +36,10 @@ version = "1.5.3"
path = "../parser"
version = "1.5.3"
[dependencies.leo-span]
path = "../span"
version = "1.5.3"
#[dependencies.leo-stdlib]
#path = "../stdlib"
#version = "1.5.3"

View File

@ -1,4 +1,4 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
@ -14,8 +14,15 @@
// 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/>.
//! Creates a struct that implements a ReconstructingReducer
//! such that it applies changes to the AST nodes for canonicalization.
//! An example of these changes are transforming Self -> to the circuit name.
use leo_ast::*;
use leo_errors::{AstError, Result, Span};
use leo_errors::{AstError, Result};
use leo_span::{sym, Span, Symbol};
use indexmap::IndexMap;
/// Replace Self when it is in a enclosing circuit type.
/// Error when Self is outside an enclosing circuit type.
@ -30,14 +37,6 @@ pub struct Canonicalizer {
in_circuit: bool,
}
impl AstPass for Canonicalizer {
fn do_pass(ast: Program) -> Result<Ast> {
Ok(Ast::new(
ReconstructingDirector::new(Self::default()).reduce_program(&ast)?,
))
}
}
impl Canonicalizer {
pub fn canonicalize_accesses(
&mut self,
@ -50,34 +49,34 @@ impl Canonicalizer {
for access in accesses.iter() {
match self.canonicalize_assignee_access(access) {
AssigneeAccess::ArrayIndex(index) => {
left = Box::new(Expression::ArrayAccess(ArrayAccessExpression {
left = Box::new(Expression::Access(AccessExpression::Array(ArrayAccess {
array: left,
index: Box::new(index),
span: span.clone(),
}));
})));
}
AssigneeAccess::ArrayRange(start, stop) => {
left = Box::new(Expression::ArrayRangeAccess(ArrayRangeAccessExpression {
left = Box::new(Expression::Access(AccessExpression::ArrayRange(ArrayRangeAccess {
array: left,
left: start.map(Box::new),
right: stop.map(Box::new),
span: span.clone(),
}));
})));
}
AssigneeAccess::Tuple(positive_number, _) => {
left = Box::new(Expression::TupleAccess(TupleAccessExpression {
left = Box::new(Expression::Access(AccessExpression::Tuple(TupleAccess {
tuple: left,
index: positive_number,
span: span.clone(),
}));
})));
}
AssigneeAccess::Member(identifier) => {
left = Box::new(Expression::CircuitMemberAccess(CircuitMemberAccessExpression {
circuit: left,
left = Box::new(Expression::Access(AccessExpression::Member(MemberAccess {
inner: left,
name: identifier,
span: span.clone(),
type_: None,
}));
})));
}
}
}
@ -85,7 +84,7 @@ impl Canonicalizer {
Ok(left)
}
pub fn compound_operation_converstion(&mut self, operation: &AssignOperation) -> Result<BinaryOperation> {
pub fn compound_operation_conversion(&mut self, operation: &AssignOperation) -> Result<BinaryOperation> {
match operation {
AssignOperation::Assign => unreachable!(),
AssignOperation::Add => Ok(BinaryOperation::Add),
@ -125,11 +124,11 @@ impl Canonicalizer {
}
}
fn canonicalize_circuit_implied_variable_definition(
fn canonicalize_circuit_variable_initializer(
&mut self,
member: &CircuitImpliedVariableDefinition,
) -> CircuitImpliedVariableDefinition {
CircuitImpliedVariableDefinition {
member: &CircuitVariableInitializer,
) -> CircuitVariableInitializer {
CircuitVariableInitializer {
identifier: member.identifier.clone(),
expression: member
.expression
@ -156,7 +155,7 @@ impl Canonicalizer {
return Expression::Binary(BinaryExpression {
left,
right,
op: binary.op.clone(),
op: binary.op,
span: binary.span.clone(),
});
}
@ -184,6 +183,62 @@ impl Canonicalizer {
});
}
Expression::Access(access) => {
let access = match access {
AccessExpression::Array(array_access) => {
let array = Box::new(self.canonicalize_expression(&array_access.array));
let index = Box::new(self.canonicalize_expression(&array_access.index));
AccessExpression::Array(ArrayAccess {
array,
index,
span: array_access.span.clone(),
})
}
AccessExpression::ArrayRange(array_range_access) => {
let array = Box::new(self.canonicalize_expression(&array_range_access.array));
let left = array_range_access
.left
.as_ref()
.map(|left| Box::new(self.canonicalize_expression(left)));
let right = array_range_access
.right
.as_ref()
.map(|right| Box::new(self.canonicalize_expression(right)));
AccessExpression::ArrayRange(ArrayRangeAccess {
array,
left,
right,
span: array_range_access.span.clone(),
})
}
AccessExpression::Member(member_access) => AccessExpression::Member(MemberAccess {
inner: Box::new(self.canonicalize_expression(&member_access.inner)),
name: member_access.name.clone(),
span: member_access.span.clone(),
type_: None,
}),
AccessExpression::Tuple(tuple_access) => {
let tuple = Box::new(self.canonicalize_expression(&tuple_access.tuple));
AccessExpression::Tuple(TupleAccess {
tuple,
index: tuple_access.index.clone(),
span: tuple_access.span.clone(),
})
}
AccessExpression::Static(static_access) => AccessExpression::Static(StaticAccess {
inner: Box::new(self.canonicalize_expression(&static_access.inner)),
name: static_access.name.clone(),
type_: self.canonicalize_self_type(static_access.type_.as_ref()),
span: static_access.span.clone(),
}),
};
return Expression::Access(access);
}
Expression::ArrayInline(array_inline) => {
let elements = array_inline
.elements
@ -214,36 +269,6 @@ impl Canonicalizer {
});
}
Expression::ArrayAccess(array_access) => {
let array = Box::new(self.canonicalize_expression(&array_access.array));
let index = Box::new(self.canonicalize_expression(&array_access.index));
return Expression::ArrayAccess(ArrayAccessExpression {
array,
index,
span: array_access.span.clone(),
});
}
Expression::ArrayRangeAccess(array_range_access) => {
let array = Box::new(self.canonicalize_expression(&array_range_access.array));
let left = array_range_access
.left
.as_ref()
.map(|left| Box::new(self.canonicalize_expression(left)));
let right = array_range_access
.right
.as_ref()
.map(|right| Box::new(self.canonicalize_expression(right)));
return Expression::ArrayRangeAccess(ArrayRangeAccessExpression {
array,
left,
right,
span: array_range_access.span.clone(),
});
}
Expression::TupleInit(tuple_init) => {
let elements = tuple_init
.elements
@ -257,20 +282,10 @@ impl Canonicalizer {
});
}
Expression::TupleAccess(tuple_access) => {
let tuple = Box::new(self.canonicalize_expression(&tuple_access.tuple));
return Expression::TupleAccess(TupleAccessExpression {
tuple,
index: tuple_access.index.clone(),
span: tuple_access.span.clone(),
});
}
Expression::CircuitInit(circuit_init) => {
let mut name = circuit_init.name.clone();
if name.name.as_ref() == "Self" && self.circuit_name.is_some() {
name = self.circuit_name.as_ref().unwrap().clone();
if name.name == sym::SelfUpper && self.circuit_name.is_some() {
name = self.circuit_name.clone().unwrap();
}
return Expression::CircuitInit(CircuitInitExpression {
@ -278,26 +293,11 @@ impl Canonicalizer {
members: circuit_init
.members
.iter()
.map(|member| self.canonicalize_circuit_implied_variable_definition(member))
.map(|member| self.canonicalize_circuit_variable_initializer(member))
.collect(),
span: circuit_init.span.clone(),
});
}
Expression::CircuitMemberAccess(circuit_member_access) => {
return Expression::CircuitMemberAccess(CircuitMemberAccessExpression {
circuit: Box::new(self.canonicalize_expression(&circuit_member_access.circuit)),
name: circuit_member_access.name.clone(),
span: circuit_member_access.span.clone(),
type_: None,
});
}
Expression::CircuitStaticFunctionAccess(circuit_static_func_access) => {
return Expression::CircuitStaticFunctionAccess(CircuitStaticFunctionAccessExpression {
circuit: Box::new(self.canonicalize_expression(&circuit_static_func_access.circuit)),
name: circuit_static_func_access.name.clone(),
span: circuit_static_func_access.span.clone(),
});
}
Expression::Call(call) => {
return Expression::Call(CallExpression {
function: Box::new(self.canonicalize_expression(&call.function)),
@ -310,7 +310,7 @@ impl Canonicalizer {
});
}
Expression::Identifier(identifier) => {
if identifier.name.as_ref() == "Self" && self.circuit_name.is_some() {
if identifier.name == sym::SelfUpper && self.circuit_name.is_some() {
return Expression::Identifier(self.circuit_name.as_ref().unwrap().clone());
}
}
@ -478,6 +478,13 @@ impl Canonicalizer {
fn canonicalize_circuit_member(&mut self, circuit_member: &CircuitMember) -> CircuitMember {
match circuit_member {
CircuitMember::CircuitConst(identifier, type_, value) => {
return CircuitMember::CircuitConst(
identifier.clone(),
type_.clone(),
self.canonicalize_expression(value),
);
}
CircuitMember::CircuitVariable(_, _) => {}
CircuitMember::CircuitFunction(function) => {
let input = function
@ -488,14 +495,16 @@ impl Canonicalizer {
let output = self.canonicalize_self_type(function.output.as_ref());
let block = self.canonicalize_block(&function.block);
return CircuitMember::CircuitFunction(Function {
return CircuitMember::CircuitFunction(Box::new(Function {
annotations: function.annotations.clone(),
identifier: function.identifier.clone(),
const_: function.const_,
input,
output,
block,
core_mapping: function.core_mapping.clone(),
span: function.span.clone(),
});
}));
}
}
@ -513,35 +522,18 @@ impl ReconstructingReducer for Canonicalizer {
}
fn reduce_type(&mut self, _type_: &Type, new: Type, span: &Span) -> Result<Type> {
match new {
Type::Array(type_, dimensions) => {
if let Some(mut dimensions) = dimensions {
if dimensions.is_zero() {
return Err(AstError::invalid_array_dimension_size(span).into());
}
let mut next = Type::Array(type_, Some(ArrayDimensions(vec![dimensions.remove_last().unwrap()])));
let mut array = next.clone();
loop {
if dimensions.is_empty() {
break;
}
array = Type::Array(
Box::new(next),
Some(ArrayDimensions(vec![dimensions.remove_last().unwrap()])),
);
next = array.clone();
}
Ok(array)
} else {
Ok(Type::Array(type_, None))
}
match new.clone() {
Type::Array(base, dims) if dims.is_empty() => Ok(Type::Array(base, dims)),
// Reduce `ArrayDimensions` into nested `Array` types.
Type::Array(base, dims) => {
let mut iter = dims.0.into_iter().rev();
let ctor = |ty, dim| Type::Array(ty, ArrayDimensions::single(dim));
let dim = iter.next().unwrap();
let base = ctor(base, dim);
Ok(iter.fold(base, |ty, dim| ctor(Box::new(ty), dim)))
}
Type::SelfType if !self.in_circuit => Err(AstError::big_self_outside_of_circuit(span).into()),
_ => Ok(new.clone()),
_ => Ok(new),
}
}
@ -609,43 +601,16 @@ impl ReconstructingReducer for Canonicalizer {
return Err(AstError::invalid_array_dimension_size(&array_init.span).into());
}
let element = Box::new(element);
if array_init.dimensions.0.len() == 1 {
return Ok(ArrayInitExpression {
element,
dimensions: array_init.dimensions.clone(),
span: array_init.span.clone(),
});
}
let mut dimensions = array_init.dimensions.clone();
let mut next = Expression::ArrayInit(ArrayInitExpression {
let mk_expr = |element, dim| ArrayInitExpression {
element,
dimensions: ArrayDimensions(vec![dimensions.remove_last().unwrap()]),
dimensions: ArrayDimensions::single(dim),
span: array_init.span.clone(),
});
};
let mut outer_element = Box::new(next.clone());
for (index, dimension) in dimensions.0.iter().rev().enumerate() {
if index == dimensions.0.len() - 1 {
break;
}
next = Expression::ArrayInit(ArrayInitExpression {
element: outer_element,
dimensions: ArrayDimensions(vec![dimension.clone()]),
span: array_init.span.clone(),
});
outer_element = Box::new(next.clone());
}
Ok(ArrayInitExpression {
element: outer_element,
dimensions: ArrayDimensions(vec![dimensions.remove_first().unwrap()]),
span: array_init.span.clone(),
})
let mut iter = array_init.dimensions.iter().rev().cloned();
// We know the array has non-zero dimensions.
let init = mk_expr(Box::new(element), iter.next().unwrap());
Ok(iter.fold(init, |elem, dim| mk_expr(Box::new(Expression::ArrayInit(elem)), dim)))
}
fn reduce_assign(
@ -662,7 +627,7 @@ impl ReconstructingReducer for Canonicalizer {
&assign.span,
)?;
let right = Box::new(value);
let op = self.compound_operation_converstion(&assign.operation)?;
let op = self.compound_operation_conversion(&assign.operation)?;
let new_value = Expression::Binary(BinaryExpression {
left,
@ -691,8 +656,9 @@ impl ReconstructingReducer for Canonicalizer {
&mut self,
function: &Function,
identifier: Identifier,
annotations: Vec<Annotation>,
annotations: IndexMap<Symbol, Annotation>,
input: Vec<FunctionInput>,
const_: bool,
output: Option<Type>,
block: Block,
) -> Result<Function> {
@ -705,22 +671,23 @@ impl ReconstructingReducer for Canonicalizer {
identifier,
annotations,
input,
const_,
output: new_output,
block,
core_mapping: function.core_mapping.clone(),
span: function.span.clone(),
})
}
fn reduce_circuit(
&mut self,
circuit: &Circuit,
_circuit: &Circuit,
circuit_name: Identifier,
members: Vec<CircuitMember>,
) -> Result<Circuit> {
self.circuit_name = Some(circuit_name.clone());
let circ = Circuit {
circuit_name,
core_mapping: circuit.core_mapping.clone(),
members: members
.iter()
.map(|member| self.canonicalize_circuit_member(member))

View File

@ -1,4 +1,4 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
@ -14,5 +14,18 @@
// 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/>.
//! Implements the AstPass trait for the Canonicalizer
//! which is a ReconstructingReducer trait to canonicalize AST nodes.
//! This allows for easy calling of the Canonicalization pass.
pub mod canonicalizer;
pub use canonicalizer::*;
use leo_ast::{Ast, AstPass, Program, ReconstructingDirector};
use leo_errors::Result;
impl AstPass for Canonicalizer {
fn do_pass(self, ast: Program) -> Result<Ast> {
Ok(Ast::new(ReconstructingDirector::new(self).reduce_program(&ast)?))
}
}

View File

@ -19,5 +19,7 @@
pub mod canonicalization;
pub use canonicalization::*;
pub mod import_resolution;
pub use import_resolution::*;
// Temporarily disable import resolution
// until we migrate stdlib and then import resolution.
/* pub mod import_resolution;
pub use import_resolution::*; */

View File

@ -14,6 +14,11 @@
// 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/>.
//! This module contains both a Director to reconstruct the AST
//! which maps over every node of the AST and calls a reducer.
//! The Trait for a reducer are methods that can be overridden
//! to make changes to how AST nodes are rebuilt.
pub mod reconstructing_reducer;
pub use reconstructing_reducer::*;

View File

@ -14,8 +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/>.
//! This module contains the reducer which iterates through ast nodes - converting them into
//! asg nodes and saving relevant information.
//! This module contains a Director for how to map over the AST
//! and applies a reducer call to each node.
use crate::*;

View File

@ -14,6 +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/>.
//! This module contains a Reducer Trait for the AST.
//! It implements default methods for each node to be made
//! given the information of the old node.
use crate::*;
use leo_errors::Result;

View File

@ -22,6 +22,10 @@ rust-version = "1.56.1"
path = "../ast"
version = "1.5.3"
[dependencies.leo-ast-passes]
path = "../ast-passes"
version = "1.5.3"
[dependencies.leo-errors]
path = "../errors"
version = "1.5.3"
@ -30,6 +34,10 @@ version = "1.5.3"
path = "../parser"
version = "1.5.3"
[dependencies.leo-span]
path = "../span"
version = "1.5.3"
[dependencies.sha2]
version = "0.10"

View File

@ -22,8 +22,10 @@
#![allow(clippy::upper_case_acronyms)]
#![doc = include_str!("../README.md")]
use leo_ast::AstPass;
use leo_errors::emitter::Handler;
use leo_errors::{CompilerError, Result};
use leo_span::symbol::create_session_if_not_set_then;
use sha2::{Digest, Sha256};
use std::fs;
@ -33,16 +35,18 @@ use std::path::PathBuf;
pub struct Compiler<'a> {
handler: &'a Handler,
main_file_path: PathBuf,
output_directory: PathBuf,
}
impl<'a> Compiler<'a> {
///
/// Returns a new Leo compiler.
///
pub fn new(handler: &'a Handler, main_file_path: PathBuf) -> Self {
pub fn new(handler: &'a Handler, main_file_path: PathBuf, output_directory: PathBuf) -> Self {
Self {
handler,
main_file_path,
output_directory,
}
}
@ -63,20 +67,34 @@ impl<'a> Compiler<'a> {
}
///
/// Returns a compiled Leo program.
/// Runs the compiler stages.
///
pub fn compile(self) -> Result<leo_ast::Ast> {
fn compiler_stages(self) -> Result<leo_ast::Ast> {
// Load the program file.
let program_string = fs::read_to_string(&self.main_file_path)
.map_err(|e| CompilerError::file_read_error(self.main_file_path.clone(), e))?;
// Use the parser to construct the abstract syntax tree (ast).
let ast: leo_ast::Ast = leo_parser::parse_ast(
let mut ast: leo_ast::Ast = leo_parser::parse_ast(
self.handler,
self.main_file_path.to_str().unwrap_or_default(),
program_string,
)?;
// Write the AST snapshot post parsing.
ast.to_json_file_without_keys(self.output_directory.clone(), "inital_ast.json", &["span"])?;
// Canonicalize the AST.
ast = leo_ast_passes::Canonicalizer::do_pass(Default::default(), ast.into_repr())?;
// Write the AST snapshot post parsing
ast.to_json_file_without_keys(self.output_directory, "canonicalization_ast.json", &["span"])?;
Ok(ast)
}
///
/// Returns a compiled Leo program.
///
pub fn compile(self) -> Result<leo_ast::Ast> {
create_session_if_not_set_then(|_| self.compiler_stages())
}
}

View File

@ -18,8 +18,9 @@ use crate::{commands::Command, context::Context};
use leo_compiler::Compiler;
use leo_errors::{CliError, Result};
use leo_package::{
inputs::*,
outputs::{ChecksumFile, CircuitFile, OutputsDirectory, OUTPUTS_DIRECTORY_NAME},
// inputs::*,
// outputs::CircuitFile
outputs::{ChecksumFile, OutputsDirectory, OUTPUTS_DIRECTORY_NAME},
source::{MainFile, MAIN_FILENAME, SOURCE_DIRECTORY_NAME},
};
@ -93,6 +94,7 @@ pub struct BuildOptions {
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Build {
#[allow(dead_code)]
#[structopt(flatten)]
pub(crate) compiler_options: BuildOptions,
}
@ -178,7 +180,7 @@ impl Command for Build {
// Initialize error handler
let handler = leo_errors::emitter::Handler::default();
let program = Compiler::new(&handler, main_file_path);
let program = Compiler::new(&handler, main_file_path, output_directory);
// Compute the current program checksum
let program_checksum = program.checksum()?;

View File

@ -29,7 +29,7 @@ use commands::{
// Deploy, Init, Lint, New, Prove, Run, Setup, Test, Update, Watch,
};
use leo_errors::Result;
use snarkvm_utilities::Write;
// use snarkvm_utilities::Write;
use std::{path::PathBuf, process::exit};
use structopt::{clap::AppSettings, StructOpt};

View File

@ -15,9 +15,9 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_errors::Result;
use std::path::PathBuf;
// use std::path::PathBuf;
use crate::{
/* use crate::{
commands::{
// package::{Login, Logout},
Build,
@ -25,11 +25,11 @@ use crate::{
// Prove, Run, Setup, Test,
},
context::{create_context, Context},
};
}; */
/// Path to the only complex Leo program that we have
/// - relative to source dir - where Cargo.toml is located
const PEDERSEN_HASH_PATH: &str = "./examples/pedersen-hash/";
// const PEDERSEN_HASH_PATH: &str = "./examples/pedersen-hash/";
#[test]
pub fn init_logger() -> Result<()> {
@ -218,10 +218,10 @@ pub fn format_event() -> Result<()> {
// Ok(())
// }
/// Create context for Pedersen Hash example
fn context() -> Result<Context> {
// /// Create context for Pedersen Hash example
/* fn context() -> Result<Context> {
let path = PathBuf::from(&PEDERSEN_HASH_PATH);
let context = create_context(path, None)?;
Ok(context)
}
} */

View File

@ -15,25 +15,29 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_ast::Ast;
use leo_errors::{emitter::Handler, Result};
use leo_errors::emitter::Handler;
use leo_span::symbol::create_session_if_not_set_then;
use std::{env, fs, path::Path};
fn to_leo_tree(filepath: &Path) -> Result<String> {
fn to_leo_tree(filepath: &Path) -> Result<String, String> {
// Loads the Leo code as a string from the given file path.
let program_filepath = filepath.to_path_buf();
let program_string = fs::read_to_string(&program_filepath).expect("failed to open input file");
let code = fs::read_to_string(&program_filepath).expect("failed to open input file");
// Parses the Leo file constructing an ast which is then serialized.
create_session_if_not_set_then(|_| {
let handler = Handler::default();
let ast = leo_parser::parse_ast(&handler, filepath.to_str().unwrap(), &program_string)?;
Ok(Ast::to_json_string(&ast).expect("serialization failed"))
Handler::with(|h| {
let ast = leo_parser::parse_ast(&h, filepath.to_str().unwrap(), &code)?;
let json = Ast::to_json_string(&ast)?;
println!("{}", json);
Ok(json)
})
.map_err(|b| b.to_string())
})
}
fn main() -> Result<()> {
fn main() -> Result<(), String> {
// Parse the command-line arguments as strings.
let cli_arguments = env::args().collect::<Vec<String>>();
@ -51,7 +55,6 @@ fn main() -> Result<()> {
// Construct the serialized syntax tree.
let serialized_leo_tree = to_leo_tree(input_filepath)?;
println!("{}", serialized_leo_tree);
// Determine the output directory.
let output_directory = match cli_arguments.len() == 3 {