minicas_core/ast/
node_variable.rs1use crate::Ty;
2use std::fmt;
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct Var {
7 pub id: String,
8 pub ty: Option<Ty>,
9}
10
11impl Var {
12 pub fn new_untyped<V: Into<String>>(identifier: V) -> Self {
14 Self {
15 id: identifier.into(),
16 ty: None,
17 }
18 }
19
20 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 pub fn returns(&self) -> Option<Ty> {
30 self.ty
31 }
32
33 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}