From 1551a82e1880e2d4c891108788996c33bc63bacd Mon Sep 17 00:00:00 2001 From: damirka Date: Thu, 11 Mar 2021 23:07:01 +0300 Subject: [PATCH] Adds [constants] section to input file: ``` [constants] x: bool = true; // will be constant in program y: u32 = 100; ``` --- ast/src/input/program_input/main_input.rs | 23 ++++++++++++++----- ast/src/input/program_input/program_input.rs | 1 + input/src/definitions/definition.rs | 9 -------- input/src/definitions/mod.rs | 3 --- input/src/leo-input.pest | 10 ++++---- .../const_.rs => sections/constants.rs} | 4 ++-- input/src/sections/header.rs | 5 +++- input/src/sections/mod.rs | 3 +++ 8 files changed, 32 insertions(+), 26 deletions(-) rename input/src/{definitions/const_.rs => sections/constants.rs} (93%) diff --git a/ast/src/input/program_input/main_input.rs b/ast/src/input/program_input/main_input.rs index 66b2ed5b61..fa0f613450 100644 --- a/ast/src/input/program_input/main_input.rs +++ b/ast/src/input/program_input/main_input.rs @@ -56,18 +56,29 @@ impl MainInput { self.input.insert(key, value); } + pub fn insert_constant(&mut self, key: String, value: Option) { + self.constants.insert(key, value); + } + /// Parses main input definitions and stores them in `self`. pub fn parse(&mut self, definitions: Vec) -> Result<(), InputParserError> { for definition in definitions { let name = definition.parameter.variable.value; - let is_const = definition.const_.is_some(); let value = InputValue::from_expression(definition.parameter.type_, definition.expression)?; - if is_const { - self.constants.insert(name, Some(value)); - } else { - self.input.insert(name, Some(value)); - } + self.insert(name, Some(value)); + } + + Ok(()) + } + + /// Parses constants input definitions and stores them in `self`. + pub fn parse_constants(&mut self, definitions: Vec) -> Result<(), InputParserError> { + for definition in definitions { + let name = definition.parameter.variable.value; + let value = InputValue::from_expression(definition.parameter.type_, definition.expression)?; + + self.insert_constant(name, Some(value)); } Ok(()) diff --git a/ast/src/input/program_input/program_input.rs b/ast/src/input/program_input/program_input.rs index d2a7ef4f68..c7d86a6d7f 100644 --- a/ast/src/input/program_input/program_input.rs +++ b/ast/src/input/program_input/program_input.rs @@ -58,6 +58,7 @@ impl ProgramInput { /// Parse each input included in a file and store them in `self`. pub fn parse(&mut self, section: Section) -> Result<(), InputParserError> { match section.header { + Header::Constants(_constants) => self.main.parse_constants(section.definitions), Header::Main(_main) => self.main.parse(section.definitions), Header::Registers(_registers) => self.registers.parse(section.definitions), header => Err(InputParserError::input_section_header(header)), diff --git a/input/src/definitions/definition.rs b/input/src/definitions/definition.rs index 10dec65811..c0c64bbe67 100644 --- a/input/src/definitions/definition.rs +++ b/input/src/definitions/definition.rs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use super::Const; use crate::{ast::Rule, common::LineEnd, expressions::Expression, parameters::Parameter}; use pest::Span; @@ -23,17 +22,9 @@ use pest_ast::FromPest; #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::definition))] pub struct Definition<'ast> { - pub const_: Option>, pub parameter: Parameter<'ast>, pub expression: Expression<'ast>, pub line_end: LineEnd, #[pest_ast(outer())] pub span: Span<'ast>, } - -impl<'ast> Definition<'ast> { - /// Check whether `const` keyword is placed before definition. - pub fn is_const(&self) -> bool { - self.const_.is_some() - } -} diff --git a/input/src/definitions/mod.rs b/input/src/definitions/mod.rs index e79c5ff093..d59ffd9d5d 100644 --- a/input/src/definitions/mod.rs +++ b/input/src/definitions/mod.rs @@ -16,6 +16,3 @@ pub mod definition; pub use definition::*; - -pub mod const_; -pub use const_::*; diff --git a/input/src/leo-input.pest b/input/src/leo-input.pest index 5981ddf903..b2fa676cec 100644 --- a/input/src/leo-input.pest +++ b/input/src/leo-input.pest @@ -210,19 +210,19 @@ registers = { "registers" } // Declared in sections/state.rs state = { "state" } +// Declared in sections/constants.rs +constants = { "constants" } + // Declared in sections/state_leaf.rs state_leaf = { "state_leaf" } // Declared in sections/header.rs -header = { main | record | registers | state_leaf | state | identifier } +header = { main | constants | record | registers | state_leaf | state | identifier } /// Definitions -// Declared in definition/const_modifier.rs -const_ = { "const" } - // Declared in definition/definition.rs -definition = { const_? ~ parameter ~ "=" ~ expression ~ LINE_END } +definition = { parameter ~ "=" ~ expression ~ LINE_END } /// Table diff --git a/input/src/definitions/const_.rs b/input/src/sections/constants.rs similarity index 93% rename from input/src/definitions/const_.rs rename to input/src/sections/constants.rs index b9dff97f29..5e238f59c1 100644 --- a/input/src/definitions/const_.rs +++ b/input/src/sections/constants.rs @@ -20,8 +20,8 @@ use pest::Span; use pest_ast::FromPest; #[derive(Clone, Debug, FromPest, PartialEq)] -#[pest_ast(rule(Rule::const_))] -pub struct Const<'ast> { +#[pest_ast(rule(Rule::constants))] +pub struct Constants<'ast> { #[pest_ast(outer())] pub span: Span<'ast>, } diff --git a/input/src/sections/header.rs b/input/src/sections/header.rs index ca437c0a45..e0296bf165 100644 --- a/input/src/sections/header.rs +++ b/input/src/sections/header.rs @@ -17,7 +17,7 @@ use crate::{ ast::Rule, common::Identifier, - sections::{Main, Record, Registers, State, StateLeaf}, + sections::{Constants, Main, Record, Registers, State, StateLeaf}, }; use pest::Span; @@ -27,6 +27,7 @@ use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::header))] pub enum Header<'ast> { + Constants(Constants<'ast>), Main(Main<'ast>), Record(Record<'ast>), Registers(Registers<'ast>), @@ -38,6 +39,7 @@ pub enum Header<'ast> { impl<'ast> Header<'ast> { pub fn span(self) -> Span<'ast> { match self { + Header::Constants(constants) => constants.span, Header::Main(main) => main.span, Header::Record(record) => record.span, Header::Registers(registers) => registers.span, @@ -51,6 +53,7 @@ impl<'ast> Header<'ast> { impl<'ast> fmt::Display for Header<'ast> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { + Header::Constants(_constants) => write!(f, "constants"), Header::Main(_main) => write!(f, "main"), Header::Record(_record) => write!(f, "record"), Header::Registers(_registers) => write!(f, "registers"), diff --git a/input/src/sections/mod.rs b/input/src/sections/mod.rs index 918990dd8d..45025d9cb0 100644 --- a/input/src/sections/mod.rs +++ b/input/src/sections/mod.rs @@ -14,6 +14,9 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . +pub mod constants; +pub use constants::*; + pub mod header; pub use header::*;