1use crate::IntegerType;
18
19use super::*;
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub struct Literal {
24 pub span: Span,
25 pub id: NodeID,
26 pub variant: LiteralVariant,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub enum LiteralVariant {
31 Address(String),
33 Boolean(bool),
35 Field(String),
38 Group(String),
40 Integer(IntegerType, String),
42 Scalar(String),
45 Unsuffixed(String),
47 String(String),
49}
50
51impl fmt::Display for LiteralVariant {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 match &self {
54 Self::Address(address) => write!(f, "{address}"),
55 Self::Boolean(boolean) => write!(f, "{boolean}"),
56 Self::Field(field) => write!(f, "{field}field"),
57 Self::Group(group) => write!(f, "{group}group"),
58 Self::Integer(type_, value) => write!(f, "{value}{type_}"),
59 Self::Scalar(scalar) => write!(f, "{scalar}scalar"),
60 Self::Unsuffixed(value) => write!(f, "{value}"),
61 Self::String(string) => write!(f, "\"{string}\""),
62 }
63 }
64}
65
66impl fmt::Display for Literal {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 self.variant.fmt(f)
69 }
70}
71
72crate::simple_node_impl!(Literal);
73
74struct DisplayDecimal<'a>(&'a Literal);
75
76impl Literal {
77 pub fn field(s: String, span: Span, id: NodeID) -> Self {
78 Literal { variant: LiteralVariant::Field(s), span, id }
79 }
80
81 pub fn group(s: String, span: Span, id: NodeID) -> Self {
82 Literal { variant: LiteralVariant::Group(s), span, id }
83 }
84
85 pub fn address(s: String, span: Span, id: NodeID) -> Self {
86 Literal { variant: LiteralVariant::Address(s), span, id }
87 }
88
89 pub fn scalar(s: String, span: Span, id: NodeID) -> Self {
90 Literal { variant: LiteralVariant::Scalar(s), span, id }
91 }
92
93 pub fn boolean(s: bool, span: Span, id: NodeID) -> Self {
94 Literal { variant: LiteralVariant::Boolean(s), span, id }
95 }
96
97 pub fn integer(integer_type: IntegerType, s: String, span: Span, id: NodeID) -> Self {
98 Literal { variant: LiteralVariant::Integer(integer_type, s), span, id }
99 }
100
101 pub fn unsuffixed(s: String, span: Span, id: NodeID) -> Self {
102 Literal { variant: LiteralVariant::Unsuffixed(s), span, id }
103 }
104
105 pub fn display_decimal(&self) -> impl '_ + fmt::Display {
109 DisplayDecimal(self)
110 }
111
112 pub fn as_u32(&self) -> Option<u32> {
114 if let LiteralVariant::Integer(_, s) = &self.variant {
115 u32::from_str_by_radix(&s.replace("_", "")).ok()
116 } else {
117 None
118 }
119 }
120}
121
122impl fmt::Display for DisplayDecimal<'_> {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 fn prepare_snarkvm_string(s: &str, suffix: &str) -> String {
128 let (neg, rest) = s.strip_prefix("-").map(|rest| ("-", rest)).unwrap_or(("", s));
130 let mut rest = rest.trim_start_matches('0');
132 if rest.is_empty() {
133 rest = "0";
134 }
135 format!("{neg}{rest}{suffix}")
136 }
137
138 match &self.0.variant {
139 LiteralVariant::Address(address) => write!(f, "{address}"),
140 LiteralVariant::Boolean(boolean) => write!(f, "{boolean}"),
141 LiteralVariant::Field(field) => write!(f, "{}", prepare_snarkvm_string(field, "field")),
142 LiteralVariant::Group(group) => write!(f, "{}", prepare_snarkvm_string(group, "group")),
143 LiteralVariant::Integer(type_, value) => {
144 let string = value.replace('_', "");
145 if value.starts_with('-') {
146 let v = i128::from_str_by_radix(&string).expect("Failed to parse integer?");
147 write!(f, "{v}{type_}")
148 } else {
149 let v = u128::from_str_by_radix(&string).expect("Failed to parse integer?");
150 write!(f, "{v}{type_}")
151 }
152 }
153 LiteralVariant::Scalar(scalar) => write!(f, "{}", prepare_snarkvm_string(scalar, "scalar")),
154 LiteralVariant::Unsuffixed(value) => write!(f, "{value}"),
155 LiteralVariant::String(string) => write!(f, "\"{string}\""),
156 }
157 }
158}
159
160impl From<Literal> for Expression {
161 fn from(value: Literal) -> Self {
162 Expression::Literal(value)
163 }
164}
165
166pub trait FromStrRadix: Sized {
170 fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError>;
171}
172
173macro_rules! implement_from_str_radix {
174 ($($ty:ident)*) => {
175 $(
176 impl FromStrRadix for $ty {
177 fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError> {
178 if let Some(stripped) = src.strip_prefix("0x") {
179 Self::from_str_radix(stripped, 16)
180 } else if let Some(stripped) = src.strip_prefix("0o") {
181 Self::from_str_radix(stripped, 8)
182 } else if let Some(stripped) = src.strip_prefix("0b") {
183 Self::from_str_radix(stripped, 2)
184 } else if let Some(stripped) = src.strip_prefix("-0x") {
185 let mut s = String::new();
189 s.push('-');
190 s.push_str(stripped);
191 Self::from_str_radix(&s, 16)
192 } else if let Some(stripped) = src.strip_prefix("-0o") {
193 let mut s = String::new();
195 s.push('-');
196 s.push_str(stripped);
197 Self::from_str_radix(&s, 8)
198 } else if let Some(stripped) = src.strip_prefix("-0b") {
199 let mut s = String::new();
201 s.push('-');
202 s.push_str(stripped);
203 Self::from_str_radix(&s, 2)
204 } else {
205 Self::from_str_radix(src, 10)
206 }
207 }
208 }
209 )*
210 };
211}
212
213implement_from_str_radix! { u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 }