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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
mod array;
mod int;
mod r#struct;
mod time;
pub use self::time::*;
pub use array::*;
pub use int::*;
pub use r#struct::*;
use crate::ty::Type;
use std::fmt::{Debug, Display};
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[allow(missing_docs)]
pub enum Value {
Void,
Time(TimeValue),
Int(IntValue),
Array(ArrayValue),
Struct(StructValue),
}
impl Value {
pub fn zero(ty: &Type) -> Value {
use crate::ty::TypeKind::*;
match ty.as_ref() {
VoidType => Value::Void,
IntType(w) => IntValue::zero(*w).into(),
EnumType(_) => unimplemented!("zero value for {}", ty),
ArrayType(l, ty) => ArrayValue::zero(*l, ty).into(),
StructType(tys) => StructValue::zero(tys).into(),
_ => panic!("no zero value for {}", ty),
}
}
pub fn get_time(&self) -> Option<&TimeValue> {
match self {
Value::Time(v) => Some(v),
_ => None,
}
}
pub fn unwrap_time(&self) -> &TimeValue {
self.get_time().expect("value is not a time")
}
pub fn get_int(&self) -> Option<&IntValue> {
match self {
Value::Int(v) => Some(v),
_ => None,
}
}
pub fn unwrap_int(&self) -> &IntValue {
self.get_int().expect("value is not an integer")
}
pub fn get_array(&self) -> Option<&ArrayValue> {
match self {
Value::Array(v) => Some(v),
_ => None,
}
}
pub fn unwrap_array(&self) -> &ArrayValue {
self.get_array().expect("value is not an array")
}
pub fn get_struct(&self) -> Option<&StructValue> {
match self {
Value::Struct(v) => Some(v),
_ => None,
}
}
pub fn unwrap_struct(&self) -> &StructValue {
self.get_struct().expect("value is not a struct")
}
pub fn ty(&self) -> Type {
use crate::ty::*;
match self {
Value::Void => void_ty(),
Value::Time(v) => v.ty(),
Value::Int(v) => v.ty(),
Value::Array(v) => v.ty(),
Value::Struct(v) => v.ty(),
}
}
pub fn is_zero(&self) -> bool {
match self {
Value::Int(v) => v.is_zero(),
Value::Time(v) => v.is_zero(),
Value::Void | Value::Array(..) | Value::Struct(..) => false,
}
}
pub fn is_one(&self) -> bool {
match self {
Value::Int(v) => v.is_one(),
Value::Void | Value::Time(_) | Value::Array(..) | Value::Struct(..) => false,
}
}
}
impl From<TimeValue> for Value {
fn from(v: TimeValue) -> Value {
Value::Time(v)
}
}
impl From<IntValue> for Value {
fn from(v: IntValue) -> Value {
Value::Int(v)
}
}
impl From<ArrayValue> for Value {
fn from(v: ArrayValue) -> Value {
Value::Array(v)
}
}
impl From<StructValue> for Value {
fn from(v: StructValue) -> Value {
Value::Struct(v)
}
}
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Value::Void => write!(f, "void"),
Value::Time(v) => write!(f, "time {}", v),
Value::Int(v) => Display::fmt(v, f),
Value::Array(v) => Display::fmt(v, f),
Value::Struct(v) => Display::fmt(v, f),
}
}
}
impl Debug for Value {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}