From b9b459ecffaa533b24376262b7c872eea6f75830 Mon Sep 17 00:00:00 2001 From: collin Date: Mon, 27 Jul 2020 16:28:56 -0700 Subject: [PATCH] add main, record, registers, state, state_leaf sections to types --- leo-inputs/src/files/file.rs | 3 +- leo-inputs/src/leo-inputs.pest | 4 +- types/src/inputs/inputs.rs | 50 ++++++++++++------- types/src/inputs/mod.rs | 6 +++ .../src/inputs/program_inputs/main_inputs.rs | 10 ++++ types/src/inputs/program_inputs/mod.rs | 8 +++ .../inputs/program_inputs/program_inputs.rs | 16 ++++++ types/src/inputs/program_inputs/registers.rs | 10 ++++ types/src/inputs/program_state/mod.rs | 8 +++ .../inputs/program_state/private_state/mod.rs | 8 +++ .../private_state/private_state.rs | 16 ++++++ .../program_state/private_state/record.rs | 10 ++++ .../program_state/private_state/state_leaf.rs | 10 ++++ .../src/inputs/program_state/program_state.rs | 16 ++++++ .../inputs/program_state/public_state/mod.rs | 5 ++ .../public_state/public_state.rs | 12 +++++ .../program_state/public_state/state.rs | 10 ++++ 17 files changed, 180 insertions(+), 22 deletions(-) create mode 100644 types/src/inputs/program_inputs/main_inputs.rs create mode 100644 types/src/inputs/program_inputs/mod.rs create mode 100644 types/src/inputs/program_inputs/program_inputs.rs create mode 100644 types/src/inputs/program_inputs/registers.rs create mode 100644 types/src/inputs/program_state/mod.rs create mode 100644 types/src/inputs/program_state/private_state/mod.rs create mode 100644 types/src/inputs/program_state/private_state/private_state.rs create mode 100644 types/src/inputs/program_state/private_state/record.rs create mode 100644 types/src/inputs/program_state/private_state/state_leaf.rs create mode 100644 types/src/inputs/program_state/program_state.rs create mode 100644 types/src/inputs/program_state/public_state/mod.rs create mode 100644 types/src/inputs/program_state/public_state/public_state.rs create mode 100644 types/src/inputs/program_state/public_state/state.rs diff --git a/leo-inputs/src/files/file.rs b/leo-inputs/src/files/file.rs index 1c6a1ddeb9..292c533ccd 100644 --- a/leo-inputs/src/files/file.rs +++ b/leo-inputs/src/files/file.rs @@ -1,4 +1,4 @@ -use crate::{ast::Rule, common::EOI, sections::Section}; +use crate::{ast::Rule, common::EOI, sections::Section, tables::Table}; use pest::Span; use pest_ast::FromPest; @@ -6,6 +6,7 @@ use pest_ast::FromPest; #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::file))] pub struct File<'ast> { + pub tables: Vec>, pub sections: Vec>, pub eoi: EOI, #[pest_ast(outer())] diff --git a/leo-inputs/src/leo-inputs.pest b/leo-inputs/src/leo-inputs.pest index 520327f65f..2f153b513a 100644 --- a/leo-inputs/src/leo-inputs.pest +++ b/leo-inputs/src/leo-inputs.pest @@ -182,7 +182,7 @@ private = { "private" } visibility = { public | private } // Declared in sections/table.rs -table = {visibility ~ NEWLINE+ ~ section*} +table = {"[[" ~ visibility ~ "]]" ~ NEWLINE+ ~ section*} /// Utilities @@ -192,4 +192,4 @@ WHITESPACE = _{ " " | "\t" ~ (NEWLINE)* } /// Files // Declared in files/file.rs -file = { SOI ~ NEWLINE* ~ section* ~ NEWLINE* ~ EOI } +file = { SOI ~ NEWLINE* ~ table* ~ NEWLINE* ~ section* ~ NEWLINE* ~ EOI } diff --git a/types/src/inputs/inputs.rs b/types/src/inputs/inputs.rs index d1a17d2aca..e242c272e2 100644 --- a/types/src/inputs/inputs.rs +++ b/types/src/inputs/inputs.rs @@ -1,16 +1,20 @@ -use crate::{FunctionInput, InputValue}; -use leo_inputs::{files::File, InputParserError}; - -static MAIN_INPUT_FILE_HEADER: &'static str = "main"; +use crate::{FunctionInput, InputValue, ProgramInputs, ProgramState}; +use leo_inputs::{files::File, sections::header::Header, InputParserError}; #[derive(Clone)] pub struct Inputs { program_inputs: Vec>, + inputs: ProgramInputs, + state: ProgramState, } impl Inputs { pub fn new() -> Self { - Self { program_inputs: vec![] } + Self { + program_inputs: vec![], + inputs: ProgramInputs::new(), + state: ProgramState::new(), + } } pub fn get_inputs(&self) -> Vec> { @@ -29,29 +33,37 @@ impl Inputs { let mut program_inputs = vec![]; for section in file.sections.into_iter() { - if section.header.name.value.eq(MAIN_INPUT_FILE_HEADER) { - for input in &expected_inputs { - // find input with matching name - let matched_input = section.assignments.clone().into_iter().find(|assignment| { - // name match - assignment.parameter.variable.value.eq(&input.identifier.name) + match section.header { + Header::Main(_main) => { + for input in &expected_inputs { + // find input with matching name + let matched_input = section.assignments.clone().into_iter().find(|assignment| { + // name match + assignment.parameter.variable.value.eq(&input.identifier.name) // type match && assignment.parameter.type_.to_string().eq(&input._type.to_string()) - }); + }); - match matched_input { - Some(assignment) => { - let value = InputValue::from_expression(assignment.parameter.type_, assignment.expression)?; + match matched_input { + Some(assignment) => { + let value = + InputValue::from_expression(assignment.parameter.type_, assignment.expression)?; - // push value to vector - program_inputs.push(Some(value)); + // push value to vector + program_inputs.push(Some(value)); + } + None => return Err(InputParserError::InputNotFound(input.to_string())), } - None => return Err(InputParserError::InputNotFound(input.to_string())), } } + _ => unimplemented!("section not impl"), } } - Ok(Self { program_inputs }) + Ok(Self { + program_inputs, + inputs: ProgramInputs::new(), + state: ProgramState::new(), + }) } } diff --git a/types/src/inputs/mod.rs b/types/src/inputs/mod.rs index 5f93ed25d3..363cc5e48b 100644 --- a/types/src/inputs/mod.rs +++ b/types/src/inputs/mod.rs @@ -3,3 +3,9 @@ pub use inputs::*; pub mod input_value; pub use input_value::*; + +pub mod program_inputs; +pub use program_inputs::*; + +pub mod program_state; +pub use program_state::*; diff --git a/types/src/inputs/program_inputs/main_inputs.rs b/types/src/inputs/program_inputs/main_inputs.rs new file mode 100644 index 0000000000..708ef2e05a --- /dev/null +++ b/types/src/inputs/program_inputs/main_inputs.rs @@ -0,0 +1,10 @@ +use crate::InputValue; + +#[derive(Clone, PartialEq, Eq)] +pub struct MainInputs(Vec>); + +impl MainInputs { + pub fn new() -> Self { + Self(vec![]) + } +} diff --git a/types/src/inputs/program_inputs/mod.rs b/types/src/inputs/program_inputs/mod.rs new file mode 100644 index 0000000000..d929c034aa --- /dev/null +++ b/types/src/inputs/program_inputs/mod.rs @@ -0,0 +1,8 @@ +pub mod main_inputs; +pub use main_inputs::*; + +pub mod program_inputs; +pub use program_inputs::*; + +pub mod registers; +pub use registers::*; diff --git a/types/src/inputs/program_inputs/program_inputs.rs b/types/src/inputs/program_inputs/program_inputs.rs new file mode 100644 index 0000000000..ce3504b823 --- /dev/null +++ b/types/src/inputs/program_inputs/program_inputs.rs @@ -0,0 +1,16 @@ +use crate::{MainInputs, Registers}; + +#[derive(Clone, PartialEq, Eq)] +pub struct ProgramInputs { + main: MainInputs, + registers: Registers, +} + +impl ProgramInputs { + pub fn new() -> Self { + Self { + main: MainInputs::new(), + registers: Registers::new(), + } + } +} diff --git a/types/src/inputs/program_inputs/registers.rs b/types/src/inputs/program_inputs/registers.rs new file mode 100644 index 0000000000..006ecc2637 --- /dev/null +++ b/types/src/inputs/program_inputs/registers.rs @@ -0,0 +1,10 @@ +use crate::InputValue; + +#[derive(Clone, PartialEq, Eq)] +pub struct Registers(Vec>); + +impl Registers { + pub fn new() -> Self { + Self(vec![]) + } +} diff --git a/types/src/inputs/program_state/mod.rs b/types/src/inputs/program_state/mod.rs new file mode 100644 index 0000000000..cb46f6ba9c --- /dev/null +++ b/types/src/inputs/program_state/mod.rs @@ -0,0 +1,8 @@ +pub mod private_state; +pub use private_state::*; + +pub mod program_state; +pub use program_state::*; + +pub mod public_state; +pub use public_state::*; diff --git a/types/src/inputs/program_state/private_state/mod.rs b/types/src/inputs/program_state/private_state/mod.rs new file mode 100644 index 0000000000..2dafb8f530 --- /dev/null +++ b/types/src/inputs/program_state/private_state/mod.rs @@ -0,0 +1,8 @@ +pub mod private_state; +pub use private_state::*; + +pub mod record; +pub use record::*; + +pub mod state_leaf; +pub use state_leaf::*; diff --git a/types/src/inputs/program_state/private_state/private_state.rs b/types/src/inputs/program_state/private_state/private_state.rs new file mode 100644 index 0000000000..53e12f768f --- /dev/null +++ b/types/src/inputs/program_state/private_state/private_state.rs @@ -0,0 +1,16 @@ +use crate::{Record, StateLeaf}; + +#[derive(Clone, PartialEq, Eq)] +pub struct PrivateState { + record: Record, + state_leaf: StateLeaf, +} + +impl PrivateState { + pub fn new() -> Self { + Self { + record: Record::new(), + state_leaf: StateLeaf::new(), + } + } +} diff --git a/types/src/inputs/program_state/private_state/record.rs b/types/src/inputs/program_state/private_state/record.rs new file mode 100644 index 0000000000..4f9454ce4e --- /dev/null +++ b/types/src/inputs/program_state/private_state/record.rs @@ -0,0 +1,10 @@ +use crate::InputValue; + +#[derive(Clone, PartialEq, Eq)] +pub struct Record(Vec>); + +impl Record { + pub fn new() -> Self { + Self(vec![]) + } +} diff --git a/types/src/inputs/program_state/private_state/state_leaf.rs b/types/src/inputs/program_state/private_state/state_leaf.rs new file mode 100644 index 0000000000..15884af751 --- /dev/null +++ b/types/src/inputs/program_state/private_state/state_leaf.rs @@ -0,0 +1,10 @@ +use crate::InputValue; + +#[derive(Clone, PartialEq, Eq)] +pub struct StateLeaf(Vec>); + +impl StateLeaf { + pub fn new() -> Self { + Self(vec![]) + } +} diff --git a/types/src/inputs/program_state/program_state.rs b/types/src/inputs/program_state/program_state.rs new file mode 100644 index 0000000000..c5a5cae3b9 --- /dev/null +++ b/types/src/inputs/program_state/program_state.rs @@ -0,0 +1,16 @@ +use crate::{PrivateState, PublicState}; + +#[derive(Clone, PartialEq, Eq)] +pub struct ProgramState { + public: PublicState, + private: PrivateState, +} + +impl ProgramState { + pub fn new() -> Self { + Self { + public: PublicState::new(), + private: PrivateState::new(), + } + } +} diff --git a/types/src/inputs/program_state/public_state/mod.rs b/types/src/inputs/program_state/public_state/mod.rs new file mode 100644 index 0000000000..227bc30313 --- /dev/null +++ b/types/src/inputs/program_state/public_state/mod.rs @@ -0,0 +1,5 @@ +pub mod public_state; +pub use public_state::*; + +pub mod state; +pub use state::*; diff --git a/types/src/inputs/program_state/public_state/public_state.rs b/types/src/inputs/program_state/public_state/public_state.rs new file mode 100644 index 0000000000..29d23eba93 --- /dev/null +++ b/types/src/inputs/program_state/public_state/public_state.rs @@ -0,0 +1,12 @@ +use crate::State; + +#[derive(Clone, PartialEq, Eq)] +pub struct PublicState { + state: State, +} + +impl PublicState { + pub fn new() -> Self { + Self { state: State::new() } + } +} diff --git a/types/src/inputs/program_state/public_state/state.rs b/types/src/inputs/program_state/public_state/state.rs new file mode 100644 index 0000000000..818c456652 --- /dev/null +++ b/types/src/inputs/program_state/public_state/state.rs @@ -0,0 +1,10 @@ +use crate::InputValue; + +#[derive(Clone, PartialEq, Eq)] +pub struct State(Vec>); + +impl State { + pub fn new() -> Self { + Self(vec![]) + } +}