leo/ast/src/program.rs

158 lines
5.0 KiB
Rust
Raw Normal View History

2021-02-02 07:26:56 +03:00
// Copyright (C) 2019-2021 Aleo Systems Inc.
2020-08-18 13:50:26 +03:00
// 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/>.
2020-10-31 02:23:18 +03:00
//! A Leo program consists of import, circuit, and function definitions.
//! Each defined type consists of ast statements and expressions.
use crate::{
load_annotation,
Circuit,
DeprecatedError,
Function,
FunctionInput,
Identifier,
ImportStatement,
TestFunction,
};
2020-10-31 03:17:17 +03:00
use leo_grammar::{definitions::Definition, files::File};
2020-04-17 22:51:02 +03:00
2020-12-07 20:05:55 +03:00
use indexmap::IndexMap;
2020-06-25 03:03:41 +03:00
use serde::{Deserialize, Serialize};
2021-01-25 18:17:42 +03:00
use std::fmt;
2020-10-30 21:30:52 +03:00
/// Stores the Leo program abstract syntax tree.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
2020-06-08 06:57:22 +03:00
pub struct Program {
2020-06-20 01:47:09 +03:00
pub name: String,
pub expected_input: Vec<FunctionInput>,
pub imports: Vec<ImportStatement>,
2020-12-07 20:05:55 +03:00
pub circuits: IndexMap<Identifier, Circuit>,
pub functions: IndexMap<Identifier, Function>,
pub tests: IndexMap<Identifier, TestFunction>,
2020-06-08 06:57:22 +03:00
}
2020-04-20 23:06:47 +03:00
2021-01-25 18:17:42 +03:00
impl fmt::Display for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for import in self.imports.iter() {
import.fmt(f)?;
writeln!(f,)?;
}
writeln!(f,)?;
for (_, circuit) in self.circuits.iter() {
circuit.fmt(f)?;
writeln!(f,)?;
}
writeln!(f,)?;
for (_, function) in self.functions.iter() {
function.fmt(f)?;
writeln!(f,)?;
}
for (_, test) in self.tests.iter() {
write!(f, "test ")?;
test.function.fmt(f)?;
writeln!(f,)?;
}
write!(f, "")
}
}
const MAIN_FUNCTION_NAME: &str = "main";
2020-06-08 06:57:22 +03:00
impl<'ast> Program {
//! Logic to convert from an abstract syntax tree (ast) representation to a Leo program.
pub fn from(program_name: &str, program_ast: &File<'ast>) -> Result<Self, DeprecatedError> {
let mut imports = vec![];
2020-12-07 20:05:55 +03:00
let mut circuits = IndexMap::new();
let mut functions = IndexMap::new();
let mut tests = IndexMap::new();
2020-08-01 05:39:30 +03:00
let mut expected_input = vec![];
program_ast
.definitions
.to_owned()
.into_iter()
// Use of Infallible to say we never expect an Some(Ok(...))
.find_map::<Result<std::convert::Infallible, _>, _>(|definition| match definition {
Definition::Import(import) => {
imports.push(ImportStatement::from(import));
None
}
Definition::Circuit(circuit) => {
circuits.insert(Identifier::from(circuit.identifier.clone()), Circuit::from(circuit));
None
}
Definition::Function(function_def) => {
let function = Function::from(function_def);
if function.identifier.name.eq(MAIN_FUNCTION_NAME) {
expected_input = function.input.clone();
}
functions.insert(function.identifier.clone(), function);
None
}
Definition::Deprecated(deprecated) => {
Some(Err(DeprecatedError::from(deprecated)))
2020-08-16 05:20:41 +03:00
}
Definition::Annotated(annotated_definition) => {
let loaded_annotation = load_annotation(
2020-08-16 05:20:41 +03:00
annotated_definition,
&mut imports,
&mut circuits,
&mut functions,
&mut tests,
&mut expected_input,
);
match loaded_annotation {
Ok(_) => None,
Err(deprecated_err) => Some(Err(deprecated_err))
}
}
})
.transpose()?;
Ok(Self {
name: program_name.to_string(),
2020-08-01 05:39:30 +03:00
expected_input,
2020-04-17 22:51:02 +03:00
imports,
circuits,
2020-04-17 22:51:02 +03:00
functions,
2020-06-05 02:55:23 +03:00
tests,
})
}
}
2020-06-08 06:57:22 +03:00
impl Program {
pub fn new(name: String) -> Self {
2020-06-08 06:57:22 +03:00
Self {
name,
2020-08-01 05:39:30 +03:00
expected_input: vec![],
2020-06-08 06:57:22 +03:00
imports: vec![],
2020-12-07 20:05:55 +03:00
circuits: IndexMap::new(),
functions: IndexMap::new(),
tests: IndexMap::new(),
2020-06-08 06:57:22 +03:00
}
}
pub fn get_name(&self) -> String {
2020-06-20 01:47:09 +03:00
self.name.to_string()
2020-06-08 06:57:22 +03:00
}
pub fn name(mut self, name: String) -> Self {
2020-06-20 01:47:09 +03:00
self.name = name;
2020-06-08 06:57:22 +03:00
self
}
}