mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-24 10:52:29 +03:00
char type added to ast and asg
This commit is contained in:
parent
b3ec8ca8b9
commit
819fc9087f
@ -109,6 +109,7 @@ pub enum ConstValue {
|
|||||||
Field(BigInt),
|
Field(BigInt),
|
||||||
Address(StrTendril),
|
Address(StrTendril),
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
|
Char(char),
|
||||||
|
|
||||||
// compounds
|
// compounds
|
||||||
Tuple(Vec<ConstValue>),
|
Tuple(Vec<ConstValue>),
|
||||||
@ -302,6 +303,7 @@ impl ConstValue {
|
|||||||
ConstValue::Field(_) => Type::Field,
|
ConstValue::Field(_) => Type::Field,
|
||||||
ConstValue::Address(_) => Type::Address,
|
ConstValue::Address(_) => Type::Address,
|
||||||
ConstValue::Boolean(_) => Type::Boolean,
|
ConstValue::Boolean(_) => Type::Boolean,
|
||||||
|
ConstValue::Char(_) => Type::Char,
|
||||||
ConstValue::Tuple(sub_consts) => {
|
ConstValue::Tuple(sub_consts) => {
|
||||||
Type::Tuple(sub_consts.iter().map(|x| x.get_type()).collect::<Option<Vec<Type>>>()?)
|
Type::Tuple(sub_consts.iter().map(|x| x.get_type()).collect::<Option<Vec<Type>>>()?)
|
||||||
}
|
}
|
||||||
|
@ -247,6 +247,10 @@ impl AsgConvertError {
|
|||||||
Self::new_from_span(format!("failed to parse boolean value '{}'", value), span)
|
Self::new_from_span(format!("failed to parse boolean value '{}'", value), span)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn invalid_char(value: &str, span: &Span) -> Self {
|
||||||
|
Self::new_from_span(format!("failed to parse char value '{}'", value), span)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn invalid_int(value: &str, span: &Span) -> Self {
|
pub fn invalid_int(value: &str, span: &Span) -> Self {
|
||||||
Self::new_from_span(format!("failed to parse int value '{}'", value), span)
|
Self::new_from_span(format!("failed to parse int value '{}'", value), span)
|
||||||
}
|
}
|
||||||
|
@ -118,6 +118,23 @@ impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Char(value, span) => {
|
||||||
|
match expected_type.map(PartialType::full).flatten() {
|
||||||
|
Some(Type::Char) | None => (),
|
||||||
|
Some(x) => {
|
||||||
|
return Err(AsgConvertError::unexpected_type(
|
||||||
|
&x.to_string(),
|
||||||
|
Some(&*Type::Char.to_string()),
|
||||||
|
span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Constant {
|
||||||
|
parent: Cell::new(None),
|
||||||
|
span: Some(span.clone()),
|
||||||
|
value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_char(&value, span))?),
|
||||||
|
}
|
||||||
|
}
|
||||||
Field(value, span) => {
|
Field(value, span) => {
|
||||||
match expected_type.map(PartialType::full).flatten() {
|
match expected_type.map(PartialType::full).flatten() {
|
||||||
Some(Type::Field) | None => (),
|
Some(Type::Field) | None => (),
|
||||||
@ -215,6 +232,9 @@ impl<'a> Into<leo_ast::ValueExpression> for &Constant<'a> {
|
|||||||
ConstValue::Boolean(value) => {
|
ConstValue::Boolean(value) => {
|
||||||
leo_ast::ValueExpression::Boolean(value.to_string().into(), self.span.clone().unwrap_or_default())
|
leo_ast::ValueExpression::Boolean(value.to_string().into(), self.span.clone().unwrap_or_default())
|
||||||
}
|
}
|
||||||
|
ConstValue::Char(value) => {
|
||||||
|
leo_ast::ValueExpression::Char(value.to_string().into(), self.span.clone().unwrap_or_default())
|
||||||
|
}
|
||||||
ConstValue::Field(value) => {
|
ConstValue::Field(value) => {
|
||||||
leo_ast::ValueExpression::Field(value.to_string().into(), self.span.clone().unwrap_or_default())
|
leo_ast::ValueExpression::Field(value.to_string().into(), self.span.clone().unwrap_or_default())
|
||||||
}
|
}
|
||||||
|
@ -178,6 +178,7 @@ impl<'a> Scope<'a> {
|
|||||||
Ok(match type_ {
|
Ok(match type_ {
|
||||||
Address => Type::Address,
|
Address => Type::Address,
|
||||||
Boolean => Type::Boolean,
|
Boolean => Type::Boolean,
|
||||||
|
Char => Type::Char,
|
||||||
Field => Type::Field,
|
Field => Type::Field,
|
||||||
Group => Type::Group,
|
Group => Type::Group,
|
||||||
IntegerType(int_type) => Type::Integer(int_type.clone()),
|
IntegerType(int_type) => Type::Integer(int_type.clone()),
|
||||||
|
@ -25,6 +25,7 @@ pub enum Type<'a> {
|
|||||||
// Data types
|
// Data types
|
||||||
Address,
|
Address,
|
||||||
Boolean,
|
Boolean,
|
||||||
|
Char,
|
||||||
Field,
|
Field,
|
||||||
Group,
|
Group,
|
||||||
Integer(IntegerType),
|
Integer(IntegerType),
|
||||||
@ -134,6 +135,7 @@ impl<'a> fmt::Display for Type<'a> {
|
|||||||
match self {
|
match self {
|
||||||
Type::Address => write!(f, "address"),
|
Type::Address => write!(f, "address"),
|
||||||
Type::Boolean => write!(f, "bool"),
|
Type::Boolean => write!(f, "bool"),
|
||||||
|
Type::Char => write!(f, "char"),
|
||||||
Type::Field => write!(f, "field"),
|
Type::Field => write!(f, "field"),
|
||||||
Type::Group => write!(f, "group"),
|
Type::Group => write!(f, "group"),
|
||||||
Type::Integer(sub_type) => sub_type.fmt(f),
|
Type::Integer(sub_type) => sub_type.fmt(f),
|
||||||
@ -199,6 +201,7 @@ impl<'a> Into<leo_ast::Type> for &Type<'a> {
|
|||||||
match self {
|
match self {
|
||||||
Address => leo_ast::Type::Address,
|
Address => leo_ast::Type::Address,
|
||||||
Boolean => leo_ast::Type::Boolean,
|
Boolean => leo_ast::Type::Boolean,
|
||||||
|
Char => leo_ast::Type::Char,
|
||||||
Field => leo_ast::Type::Field,
|
Field => leo_ast::Type::Field,
|
||||||
Group => leo_ast::Type::Group,
|
Group => leo_ast::Type::Group,
|
||||||
Integer(int_type) => leo_ast::Type::IntegerType(int_type.clone()),
|
Integer(int_type) => leo_ast::Type::IntegerType(int_type.clone()),
|
||||||
|
@ -24,6 +24,7 @@ pub enum ValueExpression {
|
|||||||
// todo: deserialize values here
|
// todo: deserialize values here
|
||||||
Address(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
Address(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
||||||
Boolean(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
Boolean(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
||||||
|
Char(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
||||||
Field(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
Field(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
||||||
Group(Box<GroupValue>),
|
Group(Box<GroupValue>),
|
||||||
Implicit(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
Implicit(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
|
||||||
@ -40,6 +41,7 @@ impl fmt::Display for ValueExpression {
|
|||||||
match &self {
|
match &self {
|
||||||
Address(address, _) => write!(f, "{}", address),
|
Address(address, _) => write!(f, "{}", address),
|
||||||
Boolean(boolean, _) => write!(f, "{}", boolean),
|
Boolean(boolean, _) => write!(f, "{}", boolean),
|
||||||
|
Char(character, _) => write!(f, "{}", character),
|
||||||
Field(field, _) => write!(f, "{}", field),
|
Field(field, _) => write!(f, "{}", field),
|
||||||
Implicit(implicit, _) => write!(f, "{}", implicit),
|
Implicit(implicit, _) => write!(f, "{}", implicit),
|
||||||
Integer(value, type_, _) => write!(f, "{}{}", value, type_),
|
Integer(value, type_, _) => write!(f, "{}{}", value, type_),
|
||||||
@ -52,7 +54,12 @@ impl Node for ValueExpression {
|
|||||||
fn span(&self) -> &Span {
|
fn span(&self) -> &Span {
|
||||||
use ValueExpression::*;
|
use ValueExpression::*;
|
||||||
match &self {
|
match &self {
|
||||||
Address(_, span) | Boolean(_, span) | Field(_, span) | Implicit(_, span) | Integer(_, _, span) => span,
|
Address(_, span)
|
||||||
|
| Boolean(_, span)
|
||||||
|
| Char(_, span)
|
||||||
|
| Field(_, span)
|
||||||
|
| Implicit(_, span)
|
||||||
|
| Integer(_, _, span) => span,
|
||||||
Group(group) => match &**group {
|
Group(group) => match &**group {
|
||||||
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => span,
|
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => span,
|
||||||
},
|
},
|
||||||
@ -62,9 +69,12 @@ impl Node for ValueExpression {
|
|||||||
fn set_span(&mut self, new_span: Span) {
|
fn set_span(&mut self, new_span: Span) {
|
||||||
use ValueExpression::*;
|
use ValueExpression::*;
|
||||||
match self {
|
match self {
|
||||||
Address(_, span) | Boolean(_, span) | Field(_, span) | Implicit(_, span) | Integer(_, _, span) => {
|
Address(_, span)
|
||||||
*span = new_span
|
| Boolean(_, span)
|
||||||
}
|
| Char(_, span)
|
||||||
|
| Field(_, span)
|
||||||
|
| Implicit(_, span)
|
||||||
|
| Integer(_, _, span) => *span = new_span,
|
||||||
Group(group) => match &mut **group {
|
Group(group) => match &mut **group {
|
||||||
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => *span = new_span,
|
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => *span = new_span,
|
||||||
},
|
},
|
||||||
|
@ -69,6 +69,7 @@ impl InputValue {
|
|||||||
match data_type {
|
match data_type {
|
||||||
DataType::Address(_) => Err(InputParserError::implicit_type(data_type, implicit)),
|
DataType::Address(_) => Err(InputParserError::implicit_type(data_type, implicit)),
|
||||||
DataType::Boolean(_) => Err(InputParserError::implicit_type(data_type, implicit)),
|
DataType::Boolean(_) => Err(InputParserError::implicit_type(data_type, implicit)),
|
||||||
|
DataType::Char(_) => Err(InputParserError::implicit_type(data_type, implicit)),
|
||||||
DataType::Integer(integer_type) => Ok(InputValue::from_number(integer_type, implicit.to_string())),
|
DataType::Integer(integer_type) => Ok(InputValue::from_number(integer_type, implicit.to_string())),
|
||||||
DataType::Group(_) => Err(InputParserError::implicit_group(implicit)),
|
DataType::Group(_) => Err(InputParserError::implicit_group(implicit)),
|
||||||
DataType::Field(_) => Ok(InputValue::Field(implicit.to_string())),
|
DataType::Field(_) => Ok(InputValue::Field(implicit.to_string())),
|
||||||
|
@ -31,6 +31,7 @@ pub enum Type {
|
|||||||
// Data types
|
// Data types
|
||||||
Address,
|
Address,
|
||||||
Boolean,
|
Boolean,
|
||||||
|
Char,
|
||||||
Field,
|
Field,
|
||||||
Group,
|
Group,
|
||||||
IntegerType(IntegerType),
|
IntegerType(IntegerType),
|
||||||
@ -66,6 +67,7 @@ impl Type {
|
|||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Type::Address, Type::Address) => true,
|
(Type::Address, Type::Address) => true,
|
||||||
(Type::Boolean, Type::Boolean) => true,
|
(Type::Boolean, Type::Boolean) => true,
|
||||||
|
(Type::Char, Type::Char) => true,
|
||||||
(Type::Field, Type::Field) => true,
|
(Type::Field, Type::Field) => true,
|
||||||
(Type::Group, Type::Group) => true,
|
(Type::Group, Type::Group) => true,
|
||||||
(Type::IntegerType(left), Type::IntegerType(right)) => left.eq(&right),
|
(Type::IntegerType(left), Type::IntegerType(right)) => left.eq(&right),
|
||||||
@ -108,6 +110,7 @@ impl From<InputDataType> for Type {
|
|||||||
match data_type {
|
match data_type {
|
||||||
InputDataType::Address(_type) => Type::Address,
|
InputDataType::Address(_type) => Type::Address,
|
||||||
InputDataType::Boolean(_type) => Type::Boolean,
|
InputDataType::Boolean(_type) => Type::Boolean,
|
||||||
|
InputDataType::Char(_type) => Type::Char,
|
||||||
InputDataType::Field(_type) => Type::Field,
|
InputDataType::Field(_type) => Type::Field,
|
||||||
InputDataType::Group(_type) => Type::Group,
|
InputDataType::Group(_type) => Type::Group,
|
||||||
InputDataType::Integer(type_) => Type::IntegerType(IntegerType::from(type_)),
|
InputDataType::Integer(type_) => Type::IntegerType(IntegerType::from(type_)),
|
||||||
@ -147,6 +150,7 @@ impl fmt::Display for Type {
|
|||||||
match *self {
|
match *self {
|
||||||
Type::Address => write!(f, "address"),
|
Type::Address => write!(f, "address"),
|
||||||
Type::Boolean => write!(f, "bool"),
|
Type::Boolean => write!(f, "bool"),
|
||||||
|
Type::Char => write!(f, "char"),
|
||||||
Type::Field => write!(f, "field"),
|
Type::Field => write!(f, "field"),
|
||||||
Type::Group => write!(f, "group"),
|
Type::Group => write!(f, "group"),
|
||||||
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
|
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
|
||||||
|
@ -43,6 +43,9 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
|
|||||||
Ok(match value {
|
Ok(match value {
|
||||||
ConstValue::Address(value) => ConstrainedValue::Address(Address::constant(value.to_string(), span)?),
|
ConstValue::Address(value) => ConstrainedValue::Address(Address::constant(value.to_string(), span)?),
|
||||||
ConstValue::Boolean(value) => ConstrainedValue::Boolean(Boolean::Constant(*value)),
|
ConstValue::Boolean(value) => ConstrainedValue::Boolean(Boolean::Constant(*value)),
|
||||||
|
ConstValue::Char(value) => {
|
||||||
|
ConstrainedValue::Field(FieldType::constant(format!("{}", *value as u32), span)?)
|
||||||
|
}
|
||||||
ConstValue::Field(value) => ConstrainedValue::Field(FieldType::constant(value.to_string(), span)?),
|
ConstValue::Field(value) => ConstrainedValue::Field(FieldType::constant(value.to_string(), span)?),
|
||||||
ConstValue::Group(value) => ConstrainedValue::Group(G::constant(value, span)?),
|
ConstValue::Group(value) => ConstrainedValue::Group(G::constant(value, span)?),
|
||||||
ConstValue::Int(value) => ConstrainedValue::Integer(Integer::new(value)),
|
ConstValue::Int(value) => ConstrainedValue::Integer(Integer::new(value)),
|
||||||
|
@ -73,6 +73,9 @@ type_integer_signed = {
|
|||||||
| type_i128
|
| type_i128
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Declared in types/char_type.rs
|
||||||
|
type_char = { "char" }
|
||||||
|
|
||||||
// Declared in types/field_type.rs
|
// Declared in types/field_type.rs
|
||||||
type_field = { "field" }
|
type_field = { "field" }
|
||||||
|
|
||||||
|
23
input/src/types/char_type.rs
Normal file
23
input/src/types/char_type.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// 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::ast::Rule;
|
||||||
|
|
||||||
|
use pest_ast::FromPest;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
|
||||||
|
#[pest_ast(rule(Rule::type_char))]
|
||||||
|
pub struct CharType {}
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::Rule,
|
ast::Rule,
|
||||||
types::{BooleanType, FieldType, GroupType, IntegerType},
|
types::{BooleanType, CharType, FieldType, GroupType, IntegerType},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::types::AddressType;
|
use crate::types::AddressType;
|
||||||
@ -27,6 +27,7 @@ use pest_ast::FromPest;
|
|||||||
pub enum DataType {
|
pub enum DataType {
|
||||||
Address(AddressType),
|
Address(AddressType),
|
||||||
Boolean(BooleanType),
|
Boolean(BooleanType),
|
||||||
|
Char(CharType),
|
||||||
Field(FieldType),
|
Field(FieldType),
|
||||||
Group(GroupType),
|
Group(GroupType),
|
||||||
Integer(IntegerType),
|
Integer(IntegerType),
|
||||||
@ -37,6 +38,7 @@ impl std::fmt::Display for DataType {
|
|||||||
match self {
|
match self {
|
||||||
DataType::Address(_) => write!(f, "address"),
|
DataType::Address(_) => write!(f, "address"),
|
||||||
DataType::Boolean(_) => write!(f, "bool"),
|
DataType::Boolean(_) => write!(f, "bool"),
|
||||||
|
DataType::Char(_) => write!(f, "char"),
|
||||||
DataType::Field(_) => write!(f, "field"),
|
DataType::Field(_) => write!(f, "field"),
|
||||||
DataType::Group(_) => write!(f, "group"),
|
DataType::Group(_) => write!(f, "group"),
|
||||||
DataType::Integer(ref integer) => write!(f, "{}", integer),
|
DataType::Integer(ref integer) => write!(f, "{}", integer),
|
||||||
|
@ -26,6 +26,9 @@ pub use array_type::*;
|
|||||||
pub mod boolean_type;
|
pub mod boolean_type;
|
||||||
pub use boolean_type::*;
|
pub use boolean_type::*;
|
||||||
|
|
||||||
|
pub mod char_type;
|
||||||
|
pub use char_type::*;
|
||||||
|
|
||||||
pub mod data_type;
|
pub mod data_type;
|
||||||
pub use data_type::*;
|
pub use data_type::*;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user