char type added to ast and asg

This commit is contained in:
gluax 2021-05-12 08:39:55 -04:00
parent b3ec8ca8b9
commit 819fc9087f
13 changed files with 84 additions and 5 deletions

View File

@ -109,6 +109,7 @@ pub enum ConstValue {
Field(BigInt),
Address(StrTendril),
Boolean(bool),
Char(char),
// compounds
Tuple(Vec<ConstValue>),
@ -302,6 +303,7 @@ impl ConstValue {
ConstValue::Field(_) => Type::Field,
ConstValue::Address(_) => Type::Address,
ConstValue::Boolean(_) => Type::Boolean,
ConstValue::Char(_) => Type::Char,
ConstValue::Tuple(sub_consts) => {
Type::Tuple(sub_consts.iter().map(|x| x.get_type()).collect::<Option<Vec<Type>>>()?)
}

View File

@ -247,6 +247,10 @@ impl AsgConvertError {
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 {
Self::new_from_span(format!("failed to parse int value '{}'", value), span)
}

View File

@ -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) => {
match expected_type.map(PartialType::full).flatten() {
Some(Type::Field) | None => (),
@ -215,6 +232,9 @@ impl<'a> Into<leo_ast::ValueExpression> for &Constant<'a> {
ConstValue::Boolean(value) => {
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) => {
leo_ast::ValueExpression::Field(value.to_string().into(), self.span.clone().unwrap_or_default())
}

View File

@ -178,6 +178,7 @@ impl<'a> Scope<'a> {
Ok(match type_ {
Address => Type::Address,
Boolean => Type::Boolean,
Char => Type::Char,
Field => Type::Field,
Group => Type::Group,
IntegerType(int_type) => Type::Integer(int_type.clone()),

View File

@ -25,6 +25,7 @@ pub enum Type<'a> {
// Data types
Address,
Boolean,
Char,
Field,
Group,
Integer(IntegerType),
@ -134,6 +135,7 @@ impl<'a> fmt::Display for Type<'a> {
match self {
Type::Address => write!(f, "address"),
Type::Boolean => write!(f, "bool"),
Type::Char => write!(f, "char"),
Type::Field => write!(f, "field"),
Type::Group => write!(f, "group"),
Type::Integer(sub_type) => sub_type.fmt(f),
@ -199,6 +201,7 @@ impl<'a> Into<leo_ast::Type> for &Type<'a> {
match self {
Address => leo_ast::Type::Address,
Boolean => leo_ast::Type::Boolean,
Char => leo_ast::Type::Char,
Field => leo_ast::Type::Field,
Group => leo_ast::Type::Group,
Integer(int_type) => leo_ast::Type::IntegerType(int_type.clone()),

View File

@ -24,6 +24,7 @@ pub enum ValueExpression {
// todo: deserialize values here
Address(#[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),
Group(Box<GroupValue>),
Implicit(#[serde(with = "crate::common::tendril_json")] StrTendril, Span),
@ -40,6 +41,7 @@ impl fmt::Display for ValueExpression {
match &self {
Address(address, _) => write!(f, "{}", address),
Boolean(boolean, _) => write!(f, "{}", boolean),
Char(character, _) => write!(f, "{}", character),
Field(field, _) => write!(f, "{}", field),
Implicit(implicit, _) => write!(f, "{}", implicit),
Integer(value, type_, _) => write!(f, "{}{}", value, type_),
@ -52,7 +54,12 @@ impl Node for ValueExpression {
fn span(&self) -> &Span {
use ValueExpression::*;
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 {
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => span,
},
@ -62,9 +69,12 @@ impl Node for ValueExpression {
fn set_span(&mut self, new_span: Span) {
use ValueExpression::*;
match self {
Address(_, span) | Boolean(_, span) | Field(_, span) | Implicit(_, span) | Integer(_, _, span) => {
*span = new_span
}
Address(_, span)
| Boolean(_, span)
| Char(_, span)
| Field(_, span)
| Implicit(_, span)
| Integer(_, _, span) => *span = new_span,
Group(group) => match &mut **group {
GroupValue::Single(_, span) | GroupValue::Tuple(GroupTuple { span, .. }) => *span = new_span,
},

View File

@ -69,6 +69,7 @@ impl InputValue {
match data_type {
DataType::Address(_) => 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::Group(_) => Err(InputParserError::implicit_group(implicit)),
DataType::Field(_) => Ok(InputValue::Field(implicit.to_string())),

View File

@ -31,6 +31,7 @@ pub enum Type {
// Data types
Address,
Boolean,
Char,
Field,
Group,
IntegerType(IntegerType),
@ -66,6 +67,7 @@ impl Type {
match (self, other) {
(Type::Address, Type::Address) => true,
(Type::Boolean, Type::Boolean) => true,
(Type::Char, Type::Char) => true,
(Type::Field, Type::Field) => true,
(Type::Group, Type::Group) => true,
(Type::IntegerType(left), Type::IntegerType(right)) => left.eq(&right),
@ -108,6 +110,7 @@ impl From<InputDataType> for Type {
match data_type {
InputDataType::Address(_type) => Type::Address,
InputDataType::Boolean(_type) => Type::Boolean,
InputDataType::Char(_type) => Type::Char,
InputDataType::Field(_type) => Type::Field,
InputDataType::Group(_type) => Type::Group,
InputDataType::Integer(type_) => Type::IntegerType(IntegerType::from(type_)),
@ -147,6 +150,7 @@ impl fmt::Display for Type {
match *self {
Type::Address => write!(f, "address"),
Type::Boolean => write!(f, "bool"),
Type::Char => write!(f, "char"),
Type::Field => write!(f, "field"),
Type::Group => write!(f, "group"),
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),

View File

@ -43,6 +43,9 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
Ok(match value {
ConstValue::Address(value) => ConstrainedValue::Address(Address::constant(value.to_string(), span)?),
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::Group(value) => ConstrainedValue::Group(G::constant(value, span)?),
ConstValue::Int(value) => ConstrainedValue::Integer(Integer::new(value)),

View File

@ -73,6 +73,9 @@ type_integer_signed = {
| type_i128
}
// Declared in types/char_type.rs
type_char = { "char" }
// Declared in types/field_type.rs
type_field = { "field" }

View 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 {}

View File

@ -16,7 +16,7 @@
use crate::{
ast::Rule,
types::{BooleanType, FieldType, GroupType, IntegerType},
types::{BooleanType, CharType, FieldType, GroupType, IntegerType},
};
use crate::types::AddressType;
@ -27,6 +27,7 @@ use pest_ast::FromPest;
pub enum DataType {
Address(AddressType),
Boolean(BooleanType),
Char(CharType),
Field(FieldType),
Group(GroupType),
Integer(IntegerType),
@ -37,6 +38,7 @@ impl std::fmt::Display for DataType {
match self {
DataType::Address(_) => write!(f, "address"),
DataType::Boolean(_) => write!(f, "bool"),
DataType::Char(_) => write!(f, "char"),
DataType::Field(_) => write!(f, "field"),
DataType::Group(_) => write!(f, "group"),
DataType::Integer(ref integer) => write!(f, "{}", integer),

View File

@ -26,6 +26,9 @@ pub use array_type::*;
pub mod boolean_type;
pub use boolean_type::*;
pub mod char_type;
pub use char_type::*;
pub mod data_type;
pub use data_type::*;