Skip to main content

minicas_core/ast/
node_const.rs

1use crate::{Ty, TyValue};
2use std::fmt;
3
4/// AST object representing some constant value.
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct Const(pub(crate) TyValue);
7
8impl Const {
9    /// Constructs a new const node.
10    pub fn new<V: Into<TyValue>>(val: V) -> Self {
11        Self(val.into())
12    }
13
14    /// Returns the type of the constant value.
15    pub fn returns(&self) -> Ty {
16        return self.0.ty();
17    }
18
19    /// Returns the constant value.
20    pub fn value(&self) -> &TyValue {
21        &self.0
22    }
23}
24
25impl<T: Into<TyValue>> From<T> for Const {
26    fn from(input: T) -> Const {
27        Const(input.into())
28    }
29}
30
31impl fmt::Display for Const {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        fmt::Display::fmt(&self.0, f)
34    }
35}