-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvariable.rs
43 lines (37 loc) · 1016 Bytes
/
variable.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::expression::{assignment, Resolved};
use crate::parser::ast::Ident;
use crate::{Context, Expression, State, TypeDef, Value};
use std::fmt;
#[derive(Debug, PartialEq)]
pub struct Variable {
ident: Ident,
}
impl Variable {
// TODO:
//
// - Error if variable has not been assigned yet.
pub(crate) fn new(ident: Ident) -> Self {
Self { ident }
}
}
impl Expression for Variable {
fn resolve(&self, ctx: &mut Context) -> Resolved {
Ok(ctx
.state()
.variable(&self.ident)
.cloned()
.unwrap_or(Value::Null))
}
fn type_def(&self, state: &State) -> TypeDef {
let target = assignment::Target::Internal(self.ident.clone(), None);
state
.assignment(&target)
.cloned()
.unwrap_or_else(|| TypeDef::new().null().infallible())
}
}
impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.ident.fmt(f)
}
}