mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 23:23:50 +03:00
Add NodeID to Literal
This commit is contained in:
parent
ec3aa4bd75
commit
5a1b9efd80
@ -14,7 +14,7 @@
|
||||
// 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::{DefinitionStatement, Identifier};
|
||||
use crate::{DefinitionStatement, Identifier, NodeID};
|
||||
|
||||
use leo_span::Symbol;
|
||||
|
||||
@ -36,6 +36,7 @@ pub fn serialize<S: Serializer>(
|
||||
joined.serialize(serializer)
|
||||
}
|
||||
|
||||
// TODO (@d0cd) Fix serialization to be compatible with NodeID.
|
||||
pub fn deserialize<'de, D: Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<IndexMap<Vec<Identifier>, DefinitionStatement>, D::Error> {
|
||||
@ -44,7 +45,11 @@ pub fn deserialize<'de, D: Deserializer<'de>>(
|
||||
.map(|(name, program)| {
|
||||
(
|
||||
name.split(',')
|
||||
.map(|ident_name| Identifier { name: Symbol::intern(ident_name), span: Default::default() })
|
||||
.map(|ident_name| Identifier {
|
||||
name: Symbol::intern(ident_name),
|
||||
span: Default::default(),
|
||||
id: NodeID::default(),
|
||||
})
|
||||
.collect::<Vec<Identifier>>(),
|
||||
program,
|
||||
)
|
||||
|
@ -54,7 +54,7 @@ simple_node_impl!(Identifier);
|
||||
impl Identifier {
|
||||
/// Constructs a new identifier with `name` and a default span.
|
||||
pub fn new(name: Symbol) -> Self {
|
||||
Self { name, span: Span::default() }
|
||||
Self { name, span: Span::default(), id: NodeID::default() }
|
||||
}
|
||||
|
||||
/// Check if the Identifier name matches the other name.
|
||||
@ -139,7 +139,12 @@ impl<'de> Deserialize<'de> for Identifier {
|
||||
None => return Err(E::custom("missing 'span' in serialized Identifier struct")),
|
||||
};
|
||||
|
||||
Ok(Identifier { name, span })
|
||||
let id: NodeID = match key.get("id") {
|
||||
Some(id) => to_json_string(id)?,
|
||||
None => return Err(E::custom("missing 'id' in serialized Identifier struct")),
|
||||
};
|
||||
|
||||
Ok(Identifier { name, span, id })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,34 +24,34 @@ use super::*;
|
||||
pub enum Literal {
|
||||
// todo: deserialize values here
|
||||
/// An address literal, e.g., `aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8s7pyjh9`.
|
||||
Address(String, #[serde(with = "leo_span::span_json")] Span),
|
||||
Address(String, #[serde(with = "leo_span::span_json")] Span, NodeID),
|
||||
/// A boolean literal, either `true` or `false`.
|
||||
Boolean(bool, #[serde(with = "leo_span::span_json")] Span),
|
||||
Boolean(bool, #[serde(with = "leo_span::span_json")] Span, NodeID),
|
||||
/// A field literal, e.g., `42field`.
|
||||
/// A signed number followed by the keyword `field`.
|
||||
Field(String, #[serde(with = "leo_span::span_json")] Span),
|
||||
Field(String, #[serde(with = "leo_span::span_json")] Span, NodeID),
|
||||
/// A group literal, either product or affine.
|
||||
/// For example, `42group` or `(12, 52)group`.
|
||||
Group(Box<GroupLiteral>),
|
||||
/// An integer literal, e.g., `42`.
|
||||
Integer(IntegerType, String, #[serde(with = "leo_span::span_json")] Span),
|
||||
Integer(IntegerType, String, #[serde(with = "leo_span::span_json")] Span, NodeID),
|
||||
/// A scalar literal, e.g. `1scalar`.
|
||||
/// An unsigned number followed by the keyword `scalar`.
|
||||
Scalar(String, #[serde(with = "leo_span::span_json")] Span),
|
||||
Scalar(String, #[serde(with = "leo_span::span_json")] Span, NodeID),
|
||||
/// A string literal, e.g., `"foobar"`.
|
||||
String(String, #[serde(with = "leo_span::span_json")] Span),
|
||||
String(String, #[serde(with = "leo_span::span_json")] Span, NodeID),
|
||||
}
|
||||
|
||||
impl fmt::Display for Literal {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match &self {
|
||||
Self::Address(address, _) => write!(f, "{address}"),
|
||||
Self::Boolean(boolean, _) => write!(f, "{boolean}"),
|
||||
Self::Field(field, _) => write!(f, "{field}field"),
|
||||
Self::Address(address, _, _) => write!(f, "{address}"),
|
||||
Self::Boolean(boolean, _, _) => write!(f, "{boolean}"),
|
||||
Self::Field(field, _, _) => write!(f, "{field}field"),
|
||||
Self::Group(group) => write!(f, "{group}group"),
|
||||
Self::Integer(type_, value, _) => write!(f, "{value}{type_}"),
|
||||
Self::Scalar(scalar, _) => write!(f, "{scalar}scalar"),
|
||||
Self::String(string, _) => write!(f, "\"{string}\""),
|
||||
Self::Integer(type_, value, _, _) => write!(f, "{value}{type_}"),
|
||||
Self::Scalar(scalar, _, _) => write!(f, "{scalar}scalar"),
|
||||
Self::String(string, _, _) => write!(f, "\"{string}\""),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -59,39 +59,49 @@ impl fmt::Display for Literal {
|
||||
impl Node for Literal {
|
||||
fn span(&self) -> Span {
|
||||
match &self {
|
||||
Self::Address(_, span)
|
||||
| Self::Boolean(_, span)
|
||||
| Self::Field(_, span)
|
||||
| Self::Integer(_, _, span)
|
||||
| Self::Scalar(_, span)
|
||||
| Self::String(_, span) => *span,
|
||||
Self::Group(group) => match &**group {
|
||||
GroupLiteral::Single(_, span) => *span,
|
||||
GroupLiteral::Tuple(tuple) => tuple.span,
|
||||
},
|
||||
Self::Address(_, span, _)
|
||||
| Self::Boolean(_, span, _)
|
||||
| Self::Field(_, span, _)
|
||||
| Self::Integer(_, _, span, _)
|
||||
| Self::Scalar(_, span, _)
|
||||
| Self::String(_, span, _) => *span,
|
||||
Self::Group(group) => *group.span(),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_span(&mut self, new_span: Span) {
|
||||
match self {
|
||||
Self::Address(_, span)
|
||||
| Self::Boolean(_, span)
|
||||
| Self::Field(_, span)
|
||||
| Self::Integer(_, _, span)
|
||||
| Self::Scalar(_, span)
|
||||
| Self::String(_, span) => *span = new_span,
|
||||
Self::Group(group) => match &mut **group {
|
||||
GroupLiteral::Single(_, span) => *span = new_span,
|
||||
GroupLiteral::Tuple(tuple) => tuple.span = new_span,
|
||||
},
|
||||
Self::Address(_, span, _)
|
||||
| Self::Boolean(_, span, _)
|
||||
| Self::Field(_, span, _)
|
||||
| Self::Integer(_, _, span, _)
|
||||
| Self::Scalar(_, span, _)
|
||||
| Self::String(_, span, _) => *span = new_span,
|
||||
Self::Group(group) => group.set_span(new_span),
|
||||
}
|
||||
}
|
||||
|
||||
fn id(&self) -> NodeID {
|
||||
todo!()
|
||||
match &self {
|
||||
Self::Address(_, _, id)
|
||||
| Self::Boolean(_, _, id)
|
||||
| Self::Field(_, _, id)
|
||||
| Self::Integer(_, _, _, id)
|
||||
| Self::Scalar(_, _, id)
|
||||
| Self::String(_, _, id) => *id,
|
||||
Self::Group(group) => *group.id(),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: NodeID) {
|
||||
todo!()
|
||||
match self {
|
||||
Self::Address(_, _, old_id)
|
||||
| Self::Boolean(_, _, old_id)
|
||||
| Self::Field(_, _, old_id)
|
||||
| Self::Integer(_, _, _, old_id)
|
||||
| Self::Scalar(_, _, old_id)
|
||||
| Self::String(_, _, old_id) => *old_id = id,
|
||||
Self::Group(group) => group.set_id(id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ impl Finalize {
|
||||
_ => Type::Tuple(Tuple(output.iter().map(|output| output.type_()).collect())),
|
||||
};
|
||||
|
||||
Self { identifier, input, output, output_type, block, span }
|
||||
Self { identifier, input, output, output_type, block, span, id: NodeID::default() }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,18 @@ impl Function {
|
||||
_ => Type::Tuple(Tuple(output.iter().map(|output| get_output_type(output)).collect())),
|
||||
};
|
||||
|
||||
Function { annotations, variant, identifier, input, output, output_type, block, finalize, span }
|
||||
Function {
|
||||
annotations,
|
||||
variant,
|
||||
identifier,
|
||||
input,
|
||||
output,
|
||||
output_type,
|
||||
block,
|
||||
finalize,
|
||||
span,
|
||||
id: NodeID::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns function name.
|
||||
|
@ -14,7 +14,8 @@
|
||||
// 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::groups::GroupCoordinate;
|
||||
use crate::{groups::GroupCoordinate, NodeID};
|
||||
|
||||
use leo_span::Span;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -24,7 +25,7 @@ use std::fmt;
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum GroupLiteral {
|
||||
/// Product group literal, e.g., `42group`.
|
||||
Single(String, #[serde(with = "leo_span::span_json")] Span),
|
||||
Single(String, #[serde(with = "leo_span::span_json")] Span, NodeID),
|
||||
/// An affine group literal with (x, y) coordinates.
|
||||
Tuple(GroupTuple),
|
||||
}
|
||||
@ -32,23 +33,37 @@ pub enum GroupLiteral {
|
||||
impl GroupLiteral {
|
||||
pub fn set_span(&mut self, new_span: Span) {
|
||||
match self {
|
||||
Self::Single(_, old_span) => *old_span = new_span,
|
||||
Self::Single(_, old_span, _) => *old_span = new_span,
|
||||
Self::Tuple(tuple) => tuple.span = new_span,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn span(&self) -> &Span {
|
||||
match self {
|
||||
Self::Single(_, span) => span,
|
||||
Self::Single(_, span, _) => span,
|
||||
Self::Tuple(tuple) => &tuple.span,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &NodeID {
|
||||
match self {
|
||||
Self::Single(_, _, id) => id,
|
||||
Self::Tuple(tuple) => &tuple.id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_id(&mut self, id: NodeID) {
|
||||
match self {
|
||||
Self::Single(_, _, old_id) => *old_id = id,
|
||||
Self::Tuple(tuple) => tuple.id = id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for GroupLiteral {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Single(string, _) => write!(f, "{string}"),
|
||||
Self::Single(string, _, _) => write!(f, "{string}"),
|
||||
Self::Tuple(tuple) => write!(f, "{}", tuple.x), // Temporarily emit x coordinate only.
|
||||
}
|
||||
}
|
||||
@ -63,4 +78,6 @@ pub struct GroupTuple {
|
||||
pub y: GroupCoordinate,
|
||||
/// The span from `(` to `)`.
|
||||
pub span: Span,
|
||||
/// The ID of the node.
|
||||
pub id: NodeID,
|
||||
}
|
||||
|
@ -35,11 +35,11 @@ impl TryFrom<(Type, Expression)> for InputValue {
|
||||
fn try_from(value: (Type, Expression)) -> Result<Self> {
|
||||
Ok(match value {
|
||||
(type_, Expression::Literal(lit)) => match (type_, lit) {
|
||||
(Type::Address, Literal::Address(value, _)) => Self::Address(value),
|
||||
(Type::Boolean, Literal::Boolean(value, _)) => Self::Boolean(value),
|
||||
(Type::Field, Literal::Field(value, _)) => Self::Field(value),
|
||||
(Type::Address, Literal::Address(value, _, _)) => Self::Address(value),
|
||||
(Type::Boolean, Literal::Boolean(value, _, _)) => Self::Boolean(value),
|
||||
(Type::Field, Literal::Field(value, _, _)) => Self::Field(value),
|
||||
(Type::Group, Literal::Group(value)) => Self::Group(*value),
|
||||
(Type::Integer(expected), Literal::Integer(actual, value, span)) => {
|
||||
(Type::Integer(expected), Literal::Integer(actual, value, span, _)) => {
|
||||
if expected == actual {
|
||||
Self::Integer(expected, value)
|
||||
} else {
|
||||
|
@ -14,7 +14,7 @@
|
||||
// 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::{GroupLiteral, Identifier, IntegerType, Literal, Type};
|
||||
use crate::{GroupLiteral, Identifier, IntegerType, Literal, NodeID, Type};
|
||||
|
||||
use leo_errors::{type_name, FlattenError, LeoError, Result};
|
||||
use leo_span::{Span, Symbol};
|
||||
@ -871,13 +871,13 @@ impl TryFrom<&Literal> for Value {
|
||||
/// Converts a literal to a value.
|
||||
fn try_from(literal: &Literal) -> Result<Self, Self::Error> {
|
||||
Ok(match literal {
|
||||
Literal::Address(string, span) => Self::Address(string.clone(), *span),
|
||||
Literal::Boolean(bool, span) => Self::Boolean(*bool, *span),
|
||||
Literal::Field(string, span) => Self::Field(string.clone(), *span),
|
||||
Literal::Address(string, span, _) => Self::Address(string.clone(), *span),
|
||||
Literal::Boolean(bool, span, _) => Self::Boolean(*bool, *span),
|
||||
Literal::Field(string, span, _) => Self::Field(string.clone(), *span),
|
||||
Literal::Group(group_literal) => Self::Group(group_literal.clone()),
|
||||
Literal::Scalar(string, span) => Self::Scalar(string.clone(), *span),
|
||||
Literal::String(string, span) => Self::String(string.clone(), *span),
|
||||
Literal::Integer(integer_type, string, span) => match integer_type {
|
||||
Literal::Scalar(string, span, _) => Self::Scalar(string.clone(), *span),
|
||||
Literal::String(string, span, _) => Self::String(string.clone(), *span),
|
||||
Literal::Integer(integer_type, string, span, _) => match integer_type {
|
||||
IntegerType::U8 => Self::U8(string.parse()?, *span),
|
||||
IntegerType::U16 => Self::U16(string.parse()?, *span),
|
||||
IntegerType::U32 => Self::U32(string.parse()?, *span),
|
||||
@ -898,23 +898,23 @@ impl From<Value> for Literal {
|
||||
use Value::*;
|
||||
match v {
|
||||
Input(_, _) => todo!("We need to test if this is hittable"),
|
||||
Address(v, span) => Literal::Address(v, span),
|
||||
Boolean(v, span) => Literal::Boolean(v, span),
|
||||
Address(v, span) => Literal::Address(v, span, NodeID::default()),
|
||||
Boolean(v, span) => Literal::Boolean(v, span, NodeID::default()),
|
||||
Struct(_ident, _values) => todo!("We need to test if this is hittable"),
|
||||
Field(v, span) => Literal::Field(v, span),
|
||||
Field(v, span) => Literal::Field(v, span, NodeID::default()),
|
||||
Group(v) => Literal::Group(v),
|
||||
I8(v, span) => Literal::Integer(IntegerType::I8, v.to_string(), span),
|
||||
I16(v, span) => Literal::Integer(IntegerType::I16, v.to_string(), span),
|
||||
I32(v, span) => Literal::Integer(IntegerType::I32, v.to_string(), span),
|
||||
I64(v, span) => Literal::Integer(IntegerType::I64, v.to_string(), span),
|
||||
I128(v, span) => Literal::Integer(IntegerType::I128, v.to_string(), span),
|
||||
U8(v, span) => Literal::Integer(IntegerType::U8, v.to_string(), span),
|
||||
U16(v, span) => Literal::Integer(IntegerType::U16, v.to_string(), span),
|
||||
U32(v, span) => Literal::Integer(IntegerType::U32, v.to_string(), span),
|
||||
U64(v, span) => Literal::Integer(IntegerType::U64, v.to_string(), span),
|
||||
U128(v, span) => Literal::Integer(IntegerType::U128, v.to_string(), span),
|
||||
Scalar(v, span) => Literal::Scalar(v, span),
|
||||
String(v, span) => Literal::String(v, span),
|
||||
I8(v, span) => Literal::Integer(IntegerType::I8, v.to_string(), span, NodeID::default()),
|
||||
I16(v, span) => Literal::Integer(IntegerType::I16, v.to_string(), span, NodeID::default()),
|
||||
I32(v, span) => Literal::Integer(IntegerType::I32, v.to_string(), span, NodeID::default()),
|
||||
I64(v, span) => Literal::Integer(IntegerType::I64, v.to_string(), span, NodeID::default()),
|
||||
I128(v, span) => Literal::Integer(IntegerType::I128, v.to_string(), span, NodeID::default()),
|
||||
U8(v, span) => Literal::Integer(IntegerType::U8, v.to_string(), span, NodeID::default()),
|
||||
U16(v, span) => Literal::Integer(IntegerType::U16, v.to_string(), span, NodeID::default()),
|
||||
U32(v, span) => Literal::Integer(IntegerType::U32, v.to_string(), span, NodeID::default()),
|
||||
U64(v, span) => Literal::Integer(IntegerType::U64, v.to_string(), span, NodeID::default()),
|
||||
U128(v, span) => Literal::Integer(IntegerType::U128, v.to_string(), span, NodeID::default()),
|
||||
Scalar(v, span) => Literal::Scalar(v, span, NodeID::default()),
|
||||
String(v, span) => Literal::String(v, span, NodeID::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user