leo_interpreter/
util.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 leo_errors::{InterpreterHalt, Result};
18use leo_span::{Span, Symbol};
19
20use snarkvm::prelude::{Identifier, TestnetV0};
21
22#[macro_export]
23macro_rules! tc_fail {
24    () => {
25        panic!("type checker failure")
26    };
27}
28
29#[macro_export]
30macro_rules! halt_no_span {
31    ($($x:tt)*) => {
32        return Err(InterpreterHalt::new(format!($($x)*)).into())
33    }
34}
35
36#[macro_export]
37macro_rules! halt {
38    ($span: expr) => {
39        return Err(InterpreterHalt::new_spanned(String::new(), $span).into())
40
41    };
42
43    ($span: expr, $($x:tt)*) => {
44        return Err(InterpreterHalt::new_spanned(format!($($x)*), $span).into())
45    };
46}
47
48pub trait ExpectTc {
49    type T;
50    fn expect_tc(self, span: Span) -> Result<Self::T>;
51}
52
53impl<T> ExpectTc for Option<T> {
54    type T = T;
55
56    fn expect_tc(self, span: Span) -> Result<Self::T> {
57        match self {
58            Some(t) => Ok(t),
59            None => Err(InterpreterHalt::new_spanned("type failure".into(), span).into()),
60        }
61    }
62}
63
64impl<T, U: std::fmt::Debug> ExpectTc for Result<T, U> {
65    type T = T;
66
67    fn expect_tc(self, span: Span) -> Result<Self::T> {
68        self.map_err(|_e| InterpreterHalt::new_spanned("type failure".into(), span).into())
69    }
70}
71
72pub fn snarkvm_identifier_to_symbol(id: &Identifier<TestnetV0>) -> Symbol {
73    let s = id.to_string();
74    Symbol::intern(&s)
75}