Adds [constants] section to input file:

```
[constants]
x: bool = true; // will be constant in program
y: u32 = 100;
```
This commit is contained in:
damirka 2021-03-11 23:07:01 +03:00
parent 47e4d23ab1
commit 1551a82e18
8 changed files with 32 additions and 26 deletions

View File

@ -56,18 +56,29 @@ impl MainInput {
self.input.insert(key, value);
}
pub fn insert_constant(&mut self, key: String, value: Option<InputValue>) {
self.constants.insert(key, value);
}
/// Parses main input definitions and stores them in `self`.
pub fn parse(&mut self, definitions: Vec<Definition>) -> 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<Definition>) -> 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(())

View File

@ -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)),

View File

@ -14,7 +14,6 @@
// 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/>.
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<Const<'ast>>,
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()
}
}

View File

@ -16,6 +16,3 @@
pub mod definition;
pub use definition::*;
pub mod const_;
pub use const_::*;

View File

@ -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

View File

@ -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>,
}

View File

@ -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"),

View File

@ -14,6 +14,9 @@
// 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/>.
pub mod constants;
pub use constants::*;
pub mod header;
pub use header::*;