2020-08-18 13:50:26 +03:00
|
|
|
// Copyright (C) 2019-2020 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/>.
|
|
|
|
|
2020-06-08 06:57:22 +03:00
|
|
|
//! A typed Leo program consists of import, circuit, and function definitions.
|
|
|
|
//! Each defined type consists of typed statements and expressions.
|
2020-03-28 05:00:19 +03:00
|
|
|
|
2020-10-27 01:51:46 +03:00
|
|
|
use crate::{load_annotation, Circuit, Function, FunctionInput, Identifier, ImportStatement, TestFunction};
|
2020-08-06 08:45:52 +03:00
|
|
|
use leo_ast::{definitions::Definition, files::File};
|
2020-04-17 22:51:02 +03:00
|
|
|
|
2020-06-25 03:03:41 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-05-14 01:19:25 +03:00
|
|
|
use std::collections::HashMap;
|
2020-03-28 05:00:19 +03:00
|
|
|
|
2020-06-08 06:57:22 +03:00
|
|
|
/// A simple program with statement expressions, program arguments and program returns.
|
2020-08-02 07:50:47 +03:00
|
|
|
#[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,
|
2020-10-02 06:17:47 +03:00
|
|
|
pub expected_input: Vec<FunctionInput>,
|
2020-10-27 01:51:46 +03:00
|
|
|
pub imports: Vec<ImportStatement>,
|
2020-06-08 06:57:22 +03:00
|
|
|
pub circuits: HashMap<Identifier, Circuit>,
|
|
|
|
pub functions: HashMap<Identifier, Function>,
|
|
|
|
pub tests: HashMap<Identifier, TestFunction>,
|
|
|
|
}
|
2020-04-20 23:06:47 +03:00
|
|
|
|
2020-07-29 02:57:03 +03:00
|
|
|
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.
|
2020-07-29 11:12:17 +03:00
|
|
|
pub fn from(program_name: &str, program_ast: &File<'ast>) -> Self {
|
2020-08-08 02:35:55 +03:00
|
|
|
let mut imports = vec![];
|
2020-05-14 23:32:16 +03:00
|
|
|
let mut circuits = HashMap::new();
|
2020-04-14 07:21:15 +03:00
|
|
|
let mut functions = HashMap::new();
|
2020-06-05 02:55:23 +03:00
|
|
|
let mut tests = HashMap::new();
|
2020-08-01 05:39:30 +03:00
|
|
|
let mut expected_input = vec![];
|
2020-04-14 07:21:15 +03:00
|
|
|
|
2020-08-06 08:45:52 +03:00
|
|
|
program_ast
|
|
|
|
.definitions
|
|
|
|
.to_owned()
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|definition| match definition {
|
2020-10-27 01:51:46 +03:00
|
|
|
Definition::Import(import) => imports.push(ImportStatement::from(import)),
|
2020-08-06 08:45:52 +03:00
|
|
|
Definition::Circuit(circuit) => {
|
|
|
|
circuits.insert(Identifier::from(circuit.identifier.clone()), Circuit::from(circuit));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
Definition::TestFunction(test_def) => {
|
|
|
|
let test = TestFunction::from(test_def);
|
2020-08-16 05:20:41 +03:00
|
|
|
tests.insert(test.function.identifier.clone(), test);
|
|
|
|
}
|
|
|
|
Definition::Annotated(annotated_definition) => {
|
|
|
|
load_annotation(
|
|
|
|
annotated_definition,
|
|
|
|
&mut imports,
|
|
|
|
&mut circuits,
|
|
|
|
&mut functions,
|
|
|
|
&mut tests,
|
|
|
|
&mut expected_input,
|
|
|
|
);
|
2020-08-06 08:45:52 +03:00
|
|
|
}
|
|
|
|
});
|
2020-04-14 07:21:15 +03:00
|
|
|
|
2020-06-08 06:57:22 +03:00
|
|
|
Self {
|
2020-07-29 11:12:17 +03:00
|
|
|
name: program_name.to_string(),
|
2020-08-01 05:39:30 +03:00
|
|
|
expected_input,
|
2020-04-17 22:51:02 +03:00
|
|
|
imports,
|
2020-05-14 23:32:16 +03:00
|
|
|
circuits,
|
2020-04-17 22:51:02 +03:00
|
|
|
functions,
|
2020-06-05 02:55:23 +03:00
|
|
|
tests,
|
2020-04-17 22:51:02 +03:00
|
|
|
}
|
2020-03-28 05:00:19 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 06:57:22 +03:00
|
|
|
|
|
|
|
impl Program {
|
2020-06-21 01:24:46 +03:00
|
|
|
pub fn new(name: String) -> Self {
|
2020-06-08 06:57:22 +03:00
|
|
|
Self {
|
2020-06-21 01:24:46 +03:00
|
|
|
name,
|
2020-08-01 05:39:30 +03:00
|
|
|
expected_input: vec![],
|
2020-06-08 06:57:22 +03:00
|
|
|
imports: vec![],
|
|
|
|
circuits: HashMap::new(),
|
|
|
|
functions: HashMap::new(),
|
|
|
|
tests: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|