Skip to main content

minicas_core/ast/
node_variable.rs

1use crate::Ty;
2use std::fmt;
3
4/// AST object representing some unknown value.
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct Var {
7    pub id: String,
8    pub ty: Option<Ty>,
9}
10
11impl Var {
12    /// Constructs a new variable node with an unknown type.
13    pub fn new_untyped<V: Into<String>>(identifier: V) -> Self {
14        Self {
15            id: identifier.into(),
16            ty: None,
17        }
18    }
19
20    /// Constructs a new variable node with the specified type.
21    pub fn new_with_type<V: Into<String>>(identifier: V, ty: Ty) -> Self {
22        Self {
23            id: identifier.into(),
24            ty: Some(ty),
25        }
26    }
27
28    /// Returns the type of the constant value.
29    pub fn returns(&self) -> Option<Ty> {
30        self.ty
31    }
32
33    /// Returns the identifier this variable is referenced by.
34    pub fn ident(&self) -> &str {
35        self.id.as_str()
36    }
37}
38
39impl fmt::Display for Var {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        fmt::Display::fmt(&self.id, f)
42    }
43}