mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 07:48:04 +03:00
make suggested changes
This commit is contained in:
parent
1604170adc
commit
cc5552c7ef
@ -20,17 +20,32 @@ use leo_span::Span;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub enum ParamMode {
|
||||||
|
Const,
|
||||||
|
Private,
|
||||||
|
Public,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ParamMode {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
use ParamMode::*;
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Const => write!(f, "const"),
|
||||||
|
Private => write!(f, "private"),
|
||||||
|
Public => write!(f, "public"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A function parameter.
|
/// A function parameter.
|
||||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct FunctionInputVariable {
|
pub struct FunctionInputVariable {
|
||||||
/// The name the parameter is accessible as in the function's body.
|
/// The name the parameter is accessible as in the function's body.
|
||||||
pub identifier: Identifier,
|
pub identifier: Identifier,
|
||||||
/// Is it a const parameter?
|
/// Is it a const parameter?
|
||||||
pub const_: bool,
|
pub mode: ParamMode,
|
||||||
/// Is it a public parameter?
|
|
||||||
pub public: bool,
|
|
||||||
/// Is it a mutable parameter?
|
|
||||||
pub mutable: bool,
|
|
||||||
/// What's the parameter's type?
|
/// What's the parameter's type?
|
||||||
pub type_: Type,
|
pub type_: Type,
|
||||||
/// The parameters span from any annotations to its type.
|
/// The parameters span from any annotations to its type.
|
||||||
@ -39,17 +54,7 @@ pub struct FunctionInputVariable {
|
|||||||
|
|
||||||
impl FunctionInputVariable {
|
impl FunctionInputVariable {
|
||||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
if self.public {
|
write!(f, "{} ", self.mode)?;
|
||||||
write!(f, "public ")?;
|
|
||||||
} else {
|
|
||||||
write!(f, "private ")?;
|
|
||||||
}
|
|
||||||
if self.const_ {
|
|
||||||
write!(f, "const ")?;
|
|
||||||
}
|
|
||||||
if self.mutable {
|
|
||||||
write!(f, "mut ")?;
|
|
||||||
}
|
|
||||||
write!(f, "{}: ", self.identifier)?;
|
write!(f, "{}: ", self.identifier)?;
|
||||||
write!(f, "{}", self.type_)
|
write!(f, "{}", self.type_)
|
||||||
}
|
}
|
||||||
|
@ -15,14 +15,13 @@
|
|||||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{Expression, Identifier, Type};
|
use crate::{Expression, Identifier, ParamMode, Type};
|
||||||
|
|
||||||
/// A single definition inside a section in a state or an input file.
|
/// A single definition inside a section in a state or an input file.
|
||||||
/// Definitions should be structured as: `<name>: <type_> = <value>;`
|
/// Definitions should be structured as: `<name>: <type_> = <value>;`
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct Definition {
|
pub struct Definition {
|
||||||
pub const_: bool,
|
pub mode: ParamMode,
|
||||||
pub public: bool,
|
|
||||||
pub type_: Type,
|
pub type_: Type,
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
pub value: Expression,
|
pub value: Expression,
|
||||||
|
@ -274,9 +274,7 @@ pub trait ReconstructingReducer {
|
|||||||
) -> Result<FunctionInputVariable> {
|
) -> Result<FunctionInputVariable> {
|
||||||
Ok(FunctionInputVariable {
|
Ok(FunctionInputVariable {
|
||||||
identifier,
|
identifier,
|
||||||
const_: variable.const_,
|
mode: variable.mode,
|
||||||
mutable: variable.mutable,
|
|
||||||
public: variable.public,
|
|
||||||
type_,
|
type_,
|
||||||
span: variable.span.clone(),
|
span: variable.span.clone(),
|
||||||
})
|
})
|
||||||
|
@ -61,12 +61,23 @@ impl ParserContext<'_> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_function_parameter_mode(&mut self) -> Result<ParamMode> {
|
||||||
|
let public = self.eat(Token::Public);
|
||||||
|
let const_ = self.eat(Token::Const);
|
||||||
|
|
||||||
|
match (public, const_) {
|
||||||
|
(None, Some(_)) => Ok(ParamMode::Const),
|
||||||
|
(None, None) => Ok(ParamMode::Private),
|
||||||
|
(Some(_), None) => Ok(ParamMode::Public),
|
||||||
|
(Some(p), Some(c)) => Err(ParserError::inputs_multiple_variable_types_specified(&(p.span + c.span)).into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Returns a [`FunctionInput`] AST node if the next tokens represent a function parameter.
|
/// Returns a [`FunctionInput`] AST node if the next tokens represent a function parameter.
|
||||||
///
|
///
|
||||||
pub fn parse_function_parameters(&mut self) -> Result<FunctionInput> {
|
pub fn parse_function_parameters(&mut self) -> Result<FunctionInput> {
|
||||||
let public = self.eat(Token::Public).is_some();
|
let mode = self.parse_function_parameter_mode()?;
|
||||||
let const_ = self.eat(Token::Const);
|
|
||||||
let mutable = self.eat(Token::Mut);
|
let mutable = self.eat(Token::Mut);
|
||||||
|
|
||||||
let name = self.expect_ident()?;
|
let name = self.expect_ident()?;
|
||||||
@ -78,9 +89,7 @@ impl ParserContext<'_> {
|
|||||||
self.expect(Token::Colon)?;
|
self.expect(Token::Colon)?;
|
||||||
let type_ = self.parse_type()?.0;
|
let type_ = self.parse_type()?.0;
|
||||||
Ok(FunctionInput::Variable(FunctionInputVariable {
|
Ok(FunctionInput::Variable(FunctionInputVariable {
|
||||||
const_: const_.is_some(),
|
mode,
|
||||||
mutable: const_.is_none(),
|
|
||||||
public,
|
|
||||||
type_,
|
type_,
|
||||||
span: name.span.clone(),
|
span: name.span.clone(),
|
||||||
identifier: name,
|
identifier: name,
|
||||||
|
@ -67,8 +67,7 @@ impl ParserContext<'_> {
|
|||||||
/// `<identifier> : <type> = <expression>;`
|
/// `<identifier> : <type> = <expression>;`
|
||||||
/// Returns [`Definition`].
|
/// Returns [`Definition`].
|
||||||
pub fn parse_input_definition(&mut self) -> Result<Definition> {
|
pub fn parse_input_definition(&mut self) -> Result<Definition> {
|
||||||
let public = self.eat(Token::Public).is_some();
|
let mode = self.parse_function_parameter_mode()?;
|
||||||
let const_ = self.eat(Token::Const).is_some();
|
|
||||||
|
|
||||||
let name = self.expect_ident()?;
|
let name = self.expect_ident()?;
|
||||||
self.expect(Token::Colon)?;
|
self.expect(Token::Colon)?;
|
||||||
@ -78,8 +77,7 @@ impl ParserContext<'_> {
|
|||||||
self.expect(Token::Semicolon)?;
|
self.expect(Token::Semicolon)?;
|
||||||
|
|
||||||
Ok(Definition {
|
Ok(Definition {
|
||||||
const_,
|
mode,
|
||||||
public,
|
|
||||||
name,
|
name,
|
||||||
type_,
|
type_,
|
||||||
value,
|
value,
|
||||||
|
@ -358,4 +358,12 @@ create_errors!(
|
|||||||
msg: format!("Found the char `{}`, but expected `{}`", found, expected),
|
msg: format!("Found the char `{}`, but expected `{}`", found, expected),
|
||||||
help: None,
|
help: None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For when a user specified more than a type on a parameter.
|
||||||
|
@formatted
|
||||||
|
inputs_multiple_variable_types_specified {
|
||||||
|
args: (),
|
||||||
|
msg: "A parameter cannot be both public and const.",
|
||||||
|
help: None,
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
@ -131,7 +131,7 @@ symbols! {
|
|||||||
main,
|
main,
|
||||||
Mut: "mut",
|
Mut: "mut",
|
||||||
prelude,
|
prelude,
|
||||||
Public: "public",
|
Public,
|
||||||
Return: "return",
|
Return: "return",
|
||||||
Star: "*",
|
Star: "*",
|
||||||
std,
|
std,
|
||||||
|
Loading…
Reference in New Issue
Block a user