From f80c3d236aa23d52474f0e39c33eea8aa1528334 Mon Sep 17 00:00:00 2001 From: howardwu Date: Thu, 4 Feb 2021 17:31:44 -0800 Subject: [PATCH] Renames InnerProgram to InternalProgram --- asg/src/lib.rs | 4 +- asg/src/program/mod.rs | 32 ++++++---- asg/src/reducer/monoidal_reducer.rs | 2 +- compiler/src/compiler.rs | 6 +- imports/src/parser/parse_package.rs | 2 +- .../local_data_commitment.rs | 6 +- wasm/src/asg.rs | 62 +++++++++++++++++++ 7 files changed, 90 insertions(+), 24 deletions(-) create mode 100644 wasm/src/asg.rs diff --git a/asg/src/lib.rs b/asg/src/lib.rs index 76eb75f661..539946ddfe 100644 --- a/asg/src/lib.rs +++ b/asg/src/lib.rs @@ -110,9 +110,9 @@ pub fn load_asg_from_ast( content: leo_ast::Program, resolver: &mut T, ) -> Result { - InnerProgram::new(&content, resolver) + InternalProgram::new(&content, resolver) } pub fn load_asg(content: &str, resolver: &mut T) -> Result { - InnerProgram::new(&load_ast("input.leo", content)?, resolver) + InternalProgram::new(&load_ast("input.leo", content)?, resolver) } diff --git a/asg/src/program/mod.rs b/asg/src/program/mod.rs index 93683d7127..0a8685a1db 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -33,7 +33,7 @@ use uuid::Uuid; /// Stores the Leo program abstract semantic graph (ASG). #[derive(Clone)] -pub struct InnerProgram { +pub struct InternalProgram { /// The unique id of the program. pub id: Uuid, @@ -57,7 +57,7 @@ pub struct InnerProgram { pub scope: Scope, } -pub type Program = Arc>; +pub type Program = Arc>; /// Enumerates what names are imported from a package. enum ImportSymbol { @@ -110,7 +110,7 @@ fn resolve_import_package_access( } } -impl InnerProgram { +impl InternalProgram { /// Returns a new Leo program asg from the given Leo program ast and imports. /// /// stages: @@ -298,7 +298,7 @@ impl InnerProgram { circuits.insert(name.name.clone(), body); } - Ok(Arc::new(RefCell::new(InnerProgram { + Ok(Arc::new(RefCell::new(InternalProgram { id: Uuid::new_v4(), name: value.name.clone(), test_functions, @@ -395,10 +395,13 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program { tests: all_test_functions .into_iter() .map(|(_, (function, ident))| { - (function.function.name.borrow().clone(), leo_ast::TestFunction { - function: function.function.as_ref().into(), - input_file: ident, - }) + ( + function.function.name.borrow().clone(), + leo_ast::TestFunction { + function: function.function.as_ref().into(), + input_file: ident, + }, + ) }) .collect(), functions: all_functions @@ -417,7 +420,7 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program { } } -impl Into for &InnerProgram { +impl Into for &InternalProgram { fn into(self) -> leo_ast::Program { leo_ast::Program { name: self.name.clone(), @@ -442,10 +445,13 @@ impl Into for &InnerProgram { .test_functions .iter() .map(|(_, function)| { - (function.0.function.name.borrow().clone(), leo_ast::TestFunction { - function: function.0.function.as_ref().into(), - input_file: function.1.clone(), - }) + ( + function.0.function.name.borrow().clone(), + leo_ast::TestFunction { + function: function.0.function.as_ref().into(), + input_file: function.1.clone(), + }, + ) }) .collect(), } diff --git a/asg/src/reducer/monoidal_reducer.rs b/asg/src/reducer/monoidal_reducer.rs index a49091c0d8..cd37dd601d 100644 --- a/asg/src/reducer/monoidal_reducer.rs +++ b/asg/src/reducer/monoidal_reducer.rs @@ -157,7 +157,7 @@ pub trait MonoidalReducerProgram: MonoidalReducerStatement { fn reduce_program( &mut self, - input: &InnerProgram, + input: &InternalProgram, imported_modules: Vec, test_functions: Vec, functions: Vec, diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 09d4bc236b..cfc1ec0564 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -19,9 +19,7 @@ use crate::{ constraints::{generate_constraints, generate_test_constraints}, errors::CompilerError, - GroupType, - OutputBytes, - OutputFile, + GroupType, OutputBytes, OutputFile, }; use leo_ast::{Ast, Input, MainInput, Program}; use leo_grammar::Grammar; @@ -189,7 +187,7 @@ impl> Compiler { tracing::debug!("Program parsing complete\n{:#?}", self.program); // Create a new symbol table from the program, imported_programs, and program_input. - let asg = leo_asg::InnerProgram::new(&self.program, &mut leo_imports::ImportParser::default())?; + let asg = leo_asg::InternalProgram::new(&self.program, &mut leo_imports::ImportParser::default())?; tracing::debug!("ASG generation complete"); diff --git a/imports/src/parser/parse_package.rs b/imports/src/parser/parse_package.rs index 6a1dd68e0a..d6aa4706b5 100644 --- a/imports/src/parser/parse_package.rs +++ b/imports/src/parser/parse_package.rs @@ -34,7 +34,7 @@ impl ImportParser { return self.parse_package(package.path(), remaining_segments, span); } let program = Self::parse_import_file(package, span)?; - let asg = leo_asg::InnerProgram::new(&program, self)?; + let asg = leo_asg::InternalProgram::new(&program, self)?; Ok(asg) } diff --git a/state/src/local_data_commitment/local_data_commitment.rs b/state/src/local_data_commitment/local_data_commitment.rs index 02bd5d7ca9..62d3d43e79 100644 --- a/state/src/local_data_commitment/local_data_commitment.rs +++ b/state/src/local_data_commitment/local_data_commitment.rs @@ -31,18 +31,18 @@ use snarkvm_utilities::{bytes::ToBytes, to_bytes, FromBytes}; use std::convert::TryFrom; /// Returns `true` if the path to the local data commitment leaf is a valid path in the record -/// commitment merkle tree. +/// commitment Merkle tree. pub fn verify_local_data_commitment( system_parameters: &SystemParameters, ast_input: &AstInput, ) -> Result { - // verify record commitment. + // Verify record commitment. let typed_record = ast_input.get_record(); let dpc_record_values = verify_record_commitment(system_parameters, typed_record)?; let record_commitment: Vec = dpc_record_values.commitment; let record_serial_number: Vec = dpc_record_values.serial_number; - // parse typed state values. + // Parse typed state values. let typed_state = ast_input.get_state(); let state_values = StateValues::try_from(typed_state)?; let leaf_index: u32 = state_values.leaf_index; diff --git a/wasm/src/asg.rs b/wasm/src/asg.rs new file mode 100644 index 0000000000..fd066a357d --- /dev/null +++ b/wasm/src/asg.rs @@ -0,0 +1,62 @@ +// 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 . + +use leo_ast::Ast as LeoAst; +use leo_grammar::Grammar as LeoGrammar; + +use std::path::Path; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct Ast(String); + +#[wasm_bindgen] +impl Ast { + #[wasm_bindgen(constructor)] + pub fn new(filepath: &str, program_name: &str, program_string: &str) -> Self { + let grammar = LeoGrammar::new(&Path::new(filepath), &program_string).unwrap(); + let ast = LeoAst::new(program_name, &grammar).unwrap(); + Self(ast.to_json_string().unwrap()) + } + + #[wasm_bindgen] + pub fn to_string(&self) -> String { + self.0.clone() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use wasm_bindgen_test::*; + + #[wasm_bindgen_test] + fn ast_test() { + let expected = include_str!("../.resources/basic/expected_ast.json"); + + let filepath = "../.resources/basic/main.leo"; + let program_name = "basic"; + let program_string = include_str!("../.resources/basic/main.leo"); + + let candidate = Ast::new(filepath, program_name, program_string).to_string(); + + let expected = JsValue::from_str(expected); + let candidate = JsValue::from_serde(&candidate).unwrap(); + + assert_eq!(expected, candidate); + } +}