new parser parse global consts

This commit is contained in:
gluax 2021-03-26 14:01:10 -04:00
parent 406692c2aa
commit 5287b238cf
7 changed files with 14 additions and 211 deletions

View File

@ -270,7 +270,10 @@ impl<'a> Program<'a> {
}
for (name, global_const) in program.global_consts.iter() {
assert_eq!(name.name, global_const.variable_name.identifier.name);
global_const
.variable_names
.iter()
.map(|variable_name| assert!(name.contains(&variable_name.identifier.name)));
// TODO re-enable
// let gc = GlobalConst::init(scope, global_const)?;
// scope.global_consts.borrow_mut().insert(name.name.clone(), gc);

View File

@ -1,127 +0,0 @@
// Copyright (C) 2019-2021 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/>.
use crate::{
statements::{Declare, VariableName},
Expression,
GlobalConst,
Node,
Span,
Type,
};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct GlobalConsts {
pub declaration_type: Declare,
pub variable_names: Vec<VariableName>,
pub type_: Option<Type>,
pub value: Expression,
pub span: Span,
}
impl GlobalConsts {
pub fn into_global_const(self) -> Vec<GlobalConst> {
let mut global_consts = vec![];
let mut types: Vec<Option<Type>> = vec![];
if self.type_.is_some() {
match self.type_.clone().unwrap() {
Type::Tuple(types_old) => {
for type_ in &types_old {
types.push(Some(type_.clone()));
}
}
_ => types.push(self.type_.clone()),
}
}
let values = match self.value.clone() {
Expression::TupleInit(expr) => expr.elements,
_ => vec![self.value.clone()],
};
for (i, variable_name) in self.variable_names.iter().enumerate() {
global_consts.push(GlobalConst {
declaration_type: self.declaration_type.clone(),
variable_name: variable_name.clone(),
type_: types.get(i).unwrap_or(&None).clone(),
value: values.get(i).unwrap_or(&self.value.clone()).clone(),
span: self.span.clone(),
});
}
global_consts
}
}
impl fmt::Display for GlobalConsts {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ", self.declaration_type)?;
if self.variable_names.len() == 1 {
// mut a
write!(f, "{}", self.variable_names[0])?;
} else {
// (a, mut b)
let names = self
.variable_names
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(",");
write!(f, "({})", names)?;
}
if self.type_.is_some() {
write!(f, ": {}", self.type_.as_ref().unwrap())?;
}
write!(f, " = {};", self.value)
}
}
// impl<'ast> From<GrammarGlobalConst<'ast>> for GlobalConsts {
// fn from(global_const: GrammarGlobalConst<'ast>) -> Self {
// let variable_names = global_const
// .variables
// .names
// .into_iter()
// .map(VariableName::from)
// .collect::<Vec<_>>();
// let type_ = global_const.variables.type_.map(Type::from);
// GlobalConsts {
// declaration_type: Declare::Const,
// variable_names,
// type_,
// value: Expression::from(global_const.expression),
// span: Span::from(global_const.span),
// }
// }
// }
impl Node for GlobalConsts {
fn span(&self) -> &Span {
&self.span
}
fn set_span(&mut self, span: Span) {
self.span = span;
}
}

View File

@ -1,56 +0,0 @@
// Copyright (C) 2019-2021 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/>.
use crate::{
statements::{Declare, VariableName},
Expression,
Node,
Span,
Type,
};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct GlobalConst {
pub declaration_type: Declare,
pub variable_name: VariableName,
pub type_: Option<Type>,
pub value: Expression,
pub span: Span,
}
impl fmt::Display for GlobalConst {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.declaration_type, self.variable_name)?;
if self.type_.is_some() {
write!(f, ": {}", self.type_.as_ref().unwrap())?;
}
write!(f, " = {};", self.value)
}
}
impl Node for GlobalConst {
fn span(&self) -> &Span {
&self.span
}
fn set_span(&mut self, span: Span) {
self.span = span;
}
}

View File

@ -1,21 +0,0 @@
// Copyright (C) 2019-2021 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/>.
pub mod all_global_consts;
pub use all_global_consts::*;
pub mod global_const;
pub use global_const::*;

View File

@ -41,9 +41,6 @@ pub use self::expression::*;
pub mod functions;
pub use self::functions::*;
pub mod global_consts;
pub use self::global_consts::*;
pub mod groups;
pub use self::groups::*;

View File

@ -17,7 +17,7 @@
//! A Leo program consists of import, circuit, and function definitions.
//! Each defined type consists of ast statements and expressions.
use crate::{Circuit, Function, FunctionInput, GlobalConst, Identifier, ImportStatement};
use crate::{Circuit, DefinitionStatement, Function, FunctionInput, Identifier, ImportStatement};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
@ -30,7 +30,7 @@ pub struct Program {
pub expected_input: Vec<FunctionInput>,
pub imports: Vec<ImportStatement>,
pub circuits: IndexMap<Identifier, Circuit>,
pub global_consts: IndexMap<Identifier, GlobalConst>,
pub global_consts: IndexMap<String, DefinitionStatement>,
pub functions: IndexMap<Identifier, Function>,
}

View File

@ -55,7 +55,14 @@ impl ParserContext {
// });
}
Token::Const => {
// TODO
let statement = self.parse_definition_statement()?;
let variable_names = statement
.variable_names
.iter()
.fold("".to_string(), |joined, variable_name| {
format!("{}, {}", joined, variable_name.identifier.name)
});
global_consts.insert(variable_names, statement);
}
_ => {
return Err(SyntaxError::unexpected(