mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-19 15:41:36 +03:00
Merge pull request #432 from AleoHQ/fix/leo-ast-naming
Rename leo ast -> ast
This commit is contained in:
commit
a570a2a13e
@ -14,14 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// 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/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_ast::LeoAst;
|
use leo_ast::Ast;
|
||||||
use leo_grammar::Grammar;
|
use leo_grammar::Grammar;
|
||||||
|
|
||||||
use criterion::{criterion_group, criterion_main, Criterion};
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
use std::{path::Path, time::Duration};
|
use std::{path::Path, time::Duration};
|
||||||
|
|
||||||
fn leo_ast<'ast>(ast: &Grammar<'ast>) -> LeoAst {
|
fn ast<'ast>(ast: &Grammar<'ast>) -> Ast {
|
||||||
LeoAst::new("leo_tree", &ast)
|
Ast::new("leo_tree", &ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_big_if_else(c: &mut Criterion) {
|
fn bench_big_if_else(c: &mut Criterion) {
|
||||||
@ -29,7 +29,7 @@ fn bench_big_if_else(c: &mut Criterion) {
|
|||||||
let program_string = include_str!("./big_if_else.leo");
|
let program_string = include_str!("./big_if_else.leo");
|
||||||
let ast = Grammar::new(&filepath, program_string).unwrap();
|
let ast = Grammar::new(&filepath, program_string).unwrap();
|
||||||
|
|
||||||
c.bench_function("LeoAst::big_if_else", |b| b.iter(|| leo_ast(&ast)));
|
c.bench_function("Ast::big_if_else", |b| b.iter(|| ast(&ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_big_ternary(c: &mut Criterion) {
|
fn bench_big_ternary(c: &mut Criterion) {
|
||||||
@ -37,7 +37,7 @@ fn bench_big_ternary(c: &mut Criterion) {
|
|||||||
let program_string = include_str!("./big_ternary.leo");
|
let program_string = include_str!("./big_ternary.leo");
|
||||||
let ast = Grammar::new(&filepath, program_string).unwrap();
|
let ast = Grammar::new(&filepath, program_string).unwrap();
|
||||||
|
|
||||||
c.bench_function("LeoAst::big_ternary", |b| b.iter(|| leo_ast(&ast)));
|
c.bench_function("Ast::big_ternary", |b| b.iter(|| ast(&ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_big_circuit(c: &mut Criterion) {
|
fn bench_big_circuit(c: &mut Criterion) {
|
||||||
@ -45,7 +45,7 @@ fn bench_big_circuit(c: &mut Criterion) {
|
|||||||
let program_string = include_str!("./big_circuit.leo");
|
let program_string = include_str!("./big_circuit.leo");
|
||||||
let ast = Grammar::new(&filepath, program_string).unwrap();
|
let ast = Grammar::new(&filepath, program_string).unwrap();
|
||||||
|
|
||||||
c.bench_function("LeoAst::big_circuit", |b| b.iter(|| leo_ast(&ast)));
|
c.bench_function("Ast::big_circuit", |b| b.iter(|| ast(&ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_long_expr(c: &mut Criterion) {
|
fn bench_long_expr(c: &mut Criterion) {
|
||||||
@ -53,7 +53,7 @@ fn bench_long_expr(c: &mut Criterion) {
|
|||||||
let program_string = include_str!("./long_expr.leo");
|
let program_string = include_str!("./long_expr.leo");
|
||||||
let ast = Grammar::new(&filepath, program_string).unwrap();
|
let ast = Grammar::new(&filepath, program_string).unwrap();
|
||||||
|
|
||||||
c.bench_function("LeoAst::long_expr", |b| b.iter(|| leo_ast(&ast)));
|
c.bench_function("Ast::long_expr", |b| b.iter(|| ast(&ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_long_array(c: &mut Criterion) {
|
fn bench_long_array(c: &mut Criterion) {
|
||||||
@ -61,7 +61,7 @@ fn bench_long_array(c: &mut Criterion) {
|
|||||||
let program_string = include_str!("./long_array.leo");
|
let program_string = include_str!("./long_array.leo");
|
||||||
let ast = Grammar::new(&filepath, program_string).unwrap();
|
let ast = Grammar::new(&filepath, program_string).unwrap();
|
||||||
|
|
||||||
c.bench_function("LeoAst::long_array", |b| b.iter(|| leo_ast(&ast)));
|
c.bench_function("Ast::long_array", |b| b.iter(|| ast(&ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_many_foos(c: &mut Criterion) {
|
fn bench_many_foos(c: &mut Criterion) {
|
||||||
@ -69,7 +69,7 @@ fn bench_many_foos(c: &mut Criterion) {
|
|||||||
let program_string = include_str!("./many_foos.leo");
|
let program_string = include_str!("./many_foos.leo");
|
||||||
let ast = Grammar::new(&filepath, program_string).unwrap();
|
let ast = Grammar::new(&filepath, program_string).unwrap();
|
||||||
|
|
||||||
c.bench_function("LeoAst::many_foos", |b| b.iter(|| leo_ast(&ast)));
|
c.bench_function("Ast::many_foos", |b| b.iter(|| ast(&ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_many_assigns(c: &mut Criterion) {
|
fn bench_many_assigns(c: &mut Criterion) {
|
||||||
@ -77,7 +77,7 @@ fn bench_many_assigns(c: &mut Criterion) {
|
|||||||
let program_string = include_str!("./many_assigns.leo");
|
let program_string = include_str!("./many_assigns.leo");
|
||||||
let ast = Grammar::new(&filepath, program_string).unwrap();
|
let ast = Grammar::new(&filepath, program_string).unwrap();
|
||||||
|
|
||||||
c.bench_function("LeoAst::many_assigns", |b| b.iter(|| leo_ast(&ast)));
|
c.bench_function("Ast::many_assigns", |b| b.iter(|| ast(&ast)));
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(
|
criterion_group!(
|
||||||
|
@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
//! The abstract syntax tree (ast) for a Leo program.
|
//! The abstract syntax tree (ast) for a Leo program.
|
||||||
//!
|
//!
|
||||||
//! This module contains the [`LeoAst`] type, a wrapper around the [`Program`] type.
|
//! This module contains the [`Ast`] type, a wrapper around the [`Program`] type.
|
||||||
//! The [`LeoAst`] type is intended to be parsed and modified by different passes
|
//! The [`Ast`] type is intended to be parsed and modified by different passes
|
||||||
//! of the Leo compiler. The Leo compiler can generate a set of R1CS constraints from any [`LeoAst`].
|
//! of the Leo compiler. The Leo compiler can generate a set of R1CS constraints from any [`Ast`].
|
||||||
|
|
||||||
pub mod annotation;
|
pub mod annotation;
|
||||||
pub use self::annotation::*;
|
pub use self::annotation::*;
|
||||||
@ -63,34 +63,34 @@ use leo_grammar::Grammar;
|
|||||||
|
|
||||||
/// The abstract syntax tree (ast) for a Leo program.
|
/// The abstract syntax tree (ast) for a Leo program.
|
||||||
///
|
///
|
||||||
/// The [`LeoAst`] type represents a Leo program as a series of recursive data types.
|
/// The [`Ast`] type represents a Leo program as a series of recursive data types.
|
||||||
/// These data types form a tree that begins from a [`Program`] type root.
|
/// These data types form a tree that begins from a [`Program`] type root.
|
||||||
///
|
///
|
||||||
/// A new [`LeoAst`] can be created from a [`Grammar`] generated by the pest parser in the `grammar` module.
|
/// A new [`Ast`] can be created from a [`Grammar`] generated by the pest parser in the `grammar` module.
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct LeoAst {
|
pub struct Ast {
|
||||||
ast: Program,
|
ast: Program,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LeoAst {
|
impl Ast {
|
||||||
/// Creates a new syntax tree from a given program name and abstract syntax tree.
|
/// Creates a new ast from a given program name and grammar tree.
|
||||||
pub fn new<'ast>(program_name: &str, ast: &Grammar<'ast>) -> Self {
|
pub fn new<'ast>(program_name: &str, ast: &Grammar<'ast>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ast: Program::from(program_name, ast.as_repr()),
|
ast: Program::from(program_name, ast.as_repr()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reference to the inner program syntax tree representation.
|
/// Returns a reference to the inner program ast representation.
|
||||||
pub fn into_repr(self) -> Program {
|
pub fn into_repr(self) -> Program {
|
||||||
self.ast
|
self.ast
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes the syntax tree into a JSON string.
|
/// Serializes the ast into a JSON string.
|
||||||
pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
|
pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
|
||||||
Ok(serde_json::to_string_pretty(&self.ast)?)
|
Ok(serde_json::to_string_pretty(&self.ast)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes the JSON string into a syntax tree.
|
/// Deserializes the JSON string into a ast.
|
||||||
pub fn from_json_string(json: &str) -> Result<Self, serde_json::Error> {
|
pub fn from_json_string(json: &str) -> Result<Self, serde_json::Error> {
|
||||||
let ast: Program = serde_json::from_str(json)?;
|
let ast: Program = serde_json::from_str(json)?;
|
||||||
Ok(Self { ast })
|
Ok(Self { ast })
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// 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/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_ast::LeoAst;
|
use leo_ast::Ast;
|
||||||
use leo_grammar::{Grammar, ParserError};
|
use leo_grammar::{Grammar, ParserError};
|
||||||
use std::{env, fs, path::Path};
|
use std::{env, fs, path::Path};
|
||||||
|
|
||||||
@ -27,10 +27,10 @@ fn to_leo_tree(filepath: &Path) -> Result<String, ParserError> {
|
|||||||
let ast = Grammar::new(&program_filepath, &program_string)?;
|
let ast = Grammar::new(&program_filepath, &program_string)?;
|
||||||
|
|
||||||
// Parse the pest ast and constructs a ast.
|
// Parse the pest ast and constructs a ast.
|
||||||
let leo_ast = LeoAst::new("leo_tree", &ast);
|
let leo_ast = Ast::new("leo_tree", &ast);
|
||||||
|
|
||||||
// Serializes the tree into JSON format.
|
// Serializes the tree into JSON format.
|
||||||
let serialized_leo_ast = LeoAst::to_json_string(&leo_ast)?;
|
let serialized_leo_ast = Ast::to_json_string(&leo_ast)?;
|
||||||
|
|
||||||
Ok(serialized_leo_ast)
|
Ok(serialized_leo_ast)
|
||||||
}
|
}
|
||||||
|
@ -14,14 +14,14 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// 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/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use leo_ast::LeoAst;
|
use leo_ast::Ast;
|
||||||
#[cfg(not(feature = "ci_skip"))]
|
#[cfg(not(feature = "ci_skip"))]
|
||||||
use leo_ast::Program;
|
use leo_ast::Program;
|
||||||
use leo_grammar::Grammar;
|
use leo_grammar::Grammar;
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn to_ast(program_filepath: &Path) -> LeoAst {
|
fn to_ast(program_filepath: &Path) -> Ast {
|
||||||
// Loads the Leo code as a string from the given file path.
|
// Loads the Leo code as a string from the given file path.
|
||||||
let program_string = Grammar::load_file(program_filepath).unwrap();
|
let program_string = Grammar::load_file(program_filepath).unwrap();
|
||||||
|
|
||||||
@ -29,14 +29,14 @@ fn to_ast(program_filepath: &Path) -> LeoAst {
|
|||||||
let ast = Grammar::new(&program_filepath, &program_string).unwrap();
|
let ast = Grammar::new(&program_filepath, &program_string).unwrap();
|
||||||
|
|
||||||
// Parses the pest ast and constructs a Leo ast.
|
// Parses the pest ast and constructs a Leo ast.
|
||||||
LeoAst::new("leo_tree", &ast)
|
Ast::new("leo_tree", &ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(not(feature = "ci_skip"))]
|
#[cfg(not(feature = "ci_skip"))]
|
||||||
fn test_serialize() {
|
fn test_serialize() {
|
||||||
// Construct a ast from the given test file.
|
// Construct an ast from the given test file.
|
||||||
let leo_ast = {
|
let ast = {
|
||||||
let mut program_filepath = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut program_filepath = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
program_filepath.push("tests/serialization/main.leo");
|
program_filepath.push("tests/serialization/main.leo");
|
||||||
|
|
||||||
@ -44,20 +44,19 @@ fn test_serialize() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Serializes the ast into JSON format.
|
// Serializes the ast into JSON format.
|
||||||
let serialized_leo_ast: Program =
|
let serialized_ast: Program = serde_json::from_value(serde_json::to_value(ast.into_repr()).unwrap()).unwrap();
|
||||||
serde_json::from_value(serde_json::to_value(leo_ast.into_repr()).unwrap()).unwrap();
|
|
||||||
|
|
||||||
// Load the expected ast.
|
// Load the expected ast.
|
||||||
let expected: Program = serde_json::from_str(include_str!("expected_leo_ast.json")).unwrap();
|
let expected: Program = serde_json::from_str(include_str!("expected_leo_ast.json")).unwrap();
|
||||||
|
|
||||||
assert_eq!(expected, serialized_leo_ast);
|
assert_eq!(expected, serialized_ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(not(feature = "ci_skip"))]
|
#[cfg(not(feature = "ci_skip"))]
|
||||||
fn test_deserialize() {
|
fn test_deserialize() {
|
||||||
// Load the expected ast.
|
// Load the expected ast.
|
||||||
let expected_leo_ast = {
|
let expected_ast = {
|
||||||
let mut program_filepath = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut program_filepath = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
program_filepath.push("tests/serialization/main.leo");
|
program_filepath.push("tests/serialization/main.leo");
|
||||||
|
|
||||||
@ -66,15 +65,15 @@ fn test_deserialize() {
|
|||||||
|
|
||||||
// Construct an ast by deserializing a ast JSON file.
|
// Construct an ast by deserializing a ast JSON file.
|
||||||
let serialized_ast = include_str!("expected_leo_ast.json");
|
let serialized_ast = include_str!("expected_leo_ast.json");
|
||||||
let leo_ast = LeoAst::from_json_string(serialized_ast).unwrap();
|
let ast = Ast::from_json_string(serialized_ast).unwrap();
|
||||||
|
|
||||||
assert_eq!(expected_leo_ast, leo_ast);
|
assert_eq!(expected_ast, ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_deserialize_serialize() {
|
fn test_serialize_deserialize_serialize() {
|
||||||
// Construct a ast from the given test file.
|
// Construct an ast from the given test file.
|
||||||
let leo_ast = {
|
let ast = {
|
||||||
let mut program_filepath = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut program_filepath = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
program_filepath.push("tests/serialization/main.leo");
|
program_filepath.push("tests/serialization/main.leo");
|
||||||
|
|
||||||
@ -82,13 +81,13 @@ fn test_serialize_deserialize_serialize() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Serializes the ast into JSON format.
|
// Serializes the ast into JSON format.
|
||||||
let serialized_leo_ast = leo_ast.to_json_string().unwrap();
|
let serialized_ast = ast.to_json_string().unwrap();
|
||||||
|
|
||||||
// Deserializes the serialized ast into a LeoAst.
|
// Deserializes the serialized ast into an ast.
|
||||||
let leo_ast = LeoAst::from_json_string(&serialized_leo_ast).unwrap();
|
let ast = Ast::from_json_string(&serialized_ast).unwrap();
|
||||||
|
|
||||||
// Reserializes the ast into JSON format.
|
// Reserializes the ast into JSON format.
|
||||||
let reserialized_leo_ast = leo_ast.to_json_string().unwrap();
|
let reserialized_ast = ast.to_json_string().unwrap();
|
||||||
|
|
||||||
assert_eq!(serialized_leo_ast, reserialized_leo_ast);
|
assert_eq!(serialized_ast, reserialized_ast);
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ use crate::{
|
|||||||
OutputBytes,
|
OutputBytes,
|
||||||
OutputFile,
|
OutputFile,
|
||||||
};
|
};
|
||||||
use leo_ast::{Input, LeoAst, MainInput, Program};
|
use leo_ast::{Ast, Input, MainInput, Program};
|
||||||
use leo_grammar::Grammar;
|
use leo_grammar::Grammar;
|
||||||
use leo_imports::ImportParser;
|
use leo_imports::ImportParser;
|
||||||
use leo_input::LeoInputParser;
|
use leo_input::LeoInputParser;
|
||||||
@ -184,7 +184,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Construct the core ast from the pest ast.
|
// Construct the core ast from the pest ast.
|
||||||
let core_ast = LeoAst::new(&self.package_name, &pest_ast);
|
let core_ast = Ast::new(&self.package_name, &pest_ast);
|
||||||
|
|
||||||
// Store the main program file.
|
// Store the main program file.
|
||||||
self.program = core_ast.into_repr();
|
self.program = core_ast.into_repr();
|
||||||
@ -244,7 +244,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
|||||||
let package_name = &self.package_name;
|
let package_name = &self.package_name;
|
||||||
|
|
||||||
// Construct the core ast from the pest ast.
|
// Construct the core ast from the pest ast.
|
||||||
let core_ast = LeoAst::new(package_name, &ast);
|
let core_ast = Ast::new(package_name, &ast);
|
||||||
|
|
||||||
// Store the main program file.
|
// Store the main program file.
|
||||||
self.program = core_ast.into_repr();
|
self.program = core_ast.into_repr();
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
//!
|
//!
|
||||||
//! This module contains the [`Grammar`] type, a wrapper around the [`File`] type in this module.
|
//! This module contains the [`Grammar`] type, a wrapper around the [`File`] type in this module.
|
||||||
//! The [`Grammar`] type is the datatype generated by the pest parser using grammar from `leo.pest`.
|
//! The [`Grammar`] type is the datatype generated by the pest parser using grammar from `leo.pest`.
|
||||||
//! The [`Grammar`] type is intended to be parsed into a [`LeoAst`]. It should not be parsed by
|
//! The [`Grammar`] type is intended to be parsed into a [`Ast`]. It should not be parsed by
|
||||||
//! any other pass of the compiler.
|
//! any other pass of the compiler.
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@ -60,7 +60,7 @@ use std::{fs, path::Path};
|
|||||||
/// These data types form a tree that begins from a [`File`] type root.
|
/// These data types form a tree that begins from a [`File`] type root.
|
||||||
///
|
///
|
||||||
/// A new [`Grammar`] type can be created from a `*.leo` file at a [`Path`].
|
/// A new [`Grammar`] type can be created from a `*.leo` file at a [`Path`].
|
||||||
/// A [`Grammar`] type can be used to create a new [`LeoAst`] type.
|
/// A [`Grammar`] type can be used to create a new [`Ast`] type.
|
||||||
pub struct Grammar<'ast> {
|
pub struct Grammar<'ast> {
|
||||||
ast: files::File<'ast>,
|
ast: files::File<'ast>,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user