leo_ast/expressions/
literal.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::IntegerType;
18
19use super::*;
20
21/// A literal.
22#[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    /// An address literal, e.g., `aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8s7pyjh9` or `hello.aleo`.
32    Address(String),
33    /// A boolean literal, either `true` or `false`.
34    Boolean(bool),
35    /// A field literal, e.g., `42field`.
36    /// A signed number followed by the keyword `field`.
37    Field(String),
38    /// A group literal, eg `42group`.
39    Group(String),
40    /// An integer literal, e.g., `42u32`.
41    Integer(IntegerType, String),
42    /// A scalar literal, e.g. `1scalar`.
43    /// An unsigned number followed by the keyword `scalar`.
44    Scalar(String),
45    /// An unsuffixed literal, e.g. `42` (without a type suffix)
46    Unsuffixed(String),
47    /// A string literal, e.g., `"foobar"`.
48    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    /// For displaying a literal as decimal, regardless of the radix in which it was parsed.
106    ///
107    /// In particular this is useful for outputting .aleo files.
108    pub fn display_decimal(&self) -> impl '_ + fmt::Display {
109        DisplayDecimal(self)
110    }
111
112    /// For an integer literal, parse it and cast it to a u32.
113    ///
114    /// Panics if `self` is not an integer literal.
115    pub fn as_u32(&self) -> Option<u32> {
116        if let LiteralVariant::Integer(_, s) = &self.variant {
117            u32::from_str_by_radix(&s.replace("_", "")).ok()
118        } else {
119            panic!("`as_u32` must only be called on integer literals");
120        }
121    }
122}
123
124impl fmt::Display for DisplayDecimal<'_> {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        // This function is duplicated in `interpreter/src/cursor.rs`,
127        // but there's not really a great place to put a common implementation
128        // right now.
129        fn prepare_snarkvm_string(s: &str, suffix: &str) -> String {
130            // If there's a `-`, separate it from the rest of the string.
131            let (neg, rest) = s.strip_prefix("-").map(|rest| ("-", rest)).unwrap_or(("", s));
132            // Remove leading zeros.
133            let mut rest = rest.trim_start_matches('0');
134            if rest.is_empty() {
135                rest = "0";
136            }
137            format!("{neg}{rest}{suffix}")
138        }
139
140        match &self.0.variant {
141            LiteralVariant::Address(address) => write!(f, "{address}"),
142            LiteralVariant::Boolean(boolean) => write!(f, "{boolean}"),
143            LiteralVariant::Field(field) => write!(f, "{}", prepare_snarkvm_string(field, "field")),
144            LiteralVariant::Group(group) => write!(f, "{}", prepare_snarkvm_string(group, "group")),
145            LiteralVariant::Integer(type_, value) => {
146                let string = value.replace('_', "");
147                if value.starts_with('-') {
148                    let v = i128::from_str_by_radix(&string).expect("Failed to parse integer?");
149                    write!(f, "{v}{type_}")
150                } else {
151                    let v = u128::from_str_by_radix(&string).expect("Failed to parse integer?");
152                    write!(f, "{v}{type_}")
153                }
154            }
155            LiteralVariant::Scalar(scalar) => write!(f, "{}", prepare_snarkvm_string(scalar, "scalar")),
156            LiteralVariant::Unsuffixed(value) => write!(f, "{value}"),
157            LiteralVariant::String(string) => write!(f, "\"{string}\""),
158        }
159    }
160}
161
162impl From<Literal> for Expression {
163    fn from(value: Literal) -> Self {
164        Expression::Literal(value)
165    }
166}
167
168/// This trait allows to parse integer literals of any type generically.
169///
170/// The literal may optionally start with a `-` and/or `0x` or `0o` or 0b`.
171pub trait FromStrRadix: Sized {
172    fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError>;
173}
174
175macro_rules! implement_from_str_radix {
176    ($($ty:ident)*) => {
177        $(
178            impl FromStrRadix for $ty {
179                fn from_str_by_radix(src: &str) -> Result<Self, std::num::ParseIntError> {
180                    if let Some(stripped) = src.strip_prefix("0x") {
181                        Self::from_str_radix(stripped, 16)
182                    } else if let Some(stripped) = src.strip_prefix("0o") {
183                        Self::from_str_radix(stripped, 8)
184                    } else if let Some(stripped) = src.strip_prefix("0b") {
185                        Self::from_str_radix(stripped, 2)
186                    } else if let Some(stripped) = src.strip_prefix("-0x") {
187                        // We have to remove the 0x prefix and put back in a - to use
188                        // std's parsing. Alternatively we could jump through
189                        // a few hoops to avoid allocating.
190                        let mut s = String::new();
191                        s.push('-');
192                        s.push_str(stripped);
193                        Self::from_str_radix(&s, 16)
194                    } else if let Some(stripped) = src.strip_prefix("-0o") {
195                        // Ditto.
196                        let mut s = String::new();
197                        s.push('-');
198                        s.push_str(stripped);
199                        Self::from_str_radix(&s, 8)
200                    } else if let Some(stripped) = src.strip_prefix("-0b") {
201                        // Ditto.
202                        let mut s = String::new();
203                        s.push('-');
204                        s.push_str(stripped);
205                        Self::from_str_radix(&s, 2)
206                    } else {
207                        Self::from_str_radix(src, 10)
208                    }
209                }
210            }
211        )*
212    };
213}
214
215implement_from_str_radix! { u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 }