leo/typed/src/program.rs

112 lines
3.9 KiB
Rust
Raw Normal View History

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.
use crate::{load_annotation, Circuit, Function, FunctionInput, Identifier, ImportStatement, TestFunction};
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-06-08 06:57:22 +03:00
/// A simple program with statement expressions, program arguments and program returns.
#[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-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
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>) -> Self {
let mut imports = vec![];
let mut circuits = HashMap::new();
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![];
program_ast
.definitions
.to_owned()
.into_iter()
.for_each(|definition| match definition {
Definition::Import(import) => imports.push(ImportStatement::from(import)),
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-06-08 06:57:22 +03:00
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-04-17 22:51:02 +03:00
}
}
}
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![],
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
}
}