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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use super::Ast;
use crate::{compiler_error, Fill};
/// The variable a value can have will depend on a variety of contexts
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum VariableValue {
/// Points directly at a cell
///
/// ```csvpp
/// ---
/// foo,[[var=bar]],baz
/// ```
Absolute(a1_notation::Address),
/// If a variable is defined in the code section it will have an AST as a value.
///
/// ```csvpp
/// foo := 42
/// ---
/// =foo,bar,baz
/// ```
Ast(Ast),
/// It's scoped to a column relative to a fill.
///
/// ```csvpp
/// ---
/// ![[fill]]foo,[[var=foo]],baz
/// ```
ColumnRelative {
column: a1_notation::Column,
fill: Fill,
},
/// References a row (outside of a fill)
///
/// ```csvpp
/// ---
/// ![[var=foo]]foo,bar,baz
/// ```
Row(a1_notation::Row),
/// A row within a fill.
///
/// ```csvpp
/// ---
/// ![[var=foo / fill=20]]foo,bar,baz
/// ```
RowRelative { row: a1_notation::Row, fill: Fill },
}
impl VariableValue {
pub(crate) fn into_ast(self, position: Option<a1_notation::Address>) -> Ast {
if let Some(position) = position {
match self {
// absolute value, just turn it into a Ast
VariableValue::Absolute(address) => Ast::new(address.into()),
// already an AST, just return it
VariableValue::Ast(ast) => ast,
// it's relative to a fill - so if it's referenced inside the
// fill, it's the value at that location. If it's outside the fill
// it's the range that it represents
VariableValue::ColumnRelative { fill, column } => {
let fill_a1: a1_notation::A1 = fill.into();
Ast::new(if fill_a1.contains(&position.into()) {
position.with_x(column.x).into()
} else {
let row_range: a1_notation::A1 = fill.into();
row_range.with_x(column.x).into()
})
}
VariableValue::Row(row) => {
let a1: a1_notation::A1 = row.into();
Ast::new(a1.into())
}
VariableValue::RowRelative { fill, .. } => {
let fill_a1: a1_notation::A1 = fill.into();
Ast::new(if fill_a1.contains(&position.into()) {
// we're within the scope (fill) so it's the row we're on
let row_a1: a1_notation::A1 = position.row.into();
row_a1.into()
} else {
// we're outside the scope (fill), so it represents the entire
// range contained by it (the scope)
let row_range: a1_notation::A1 = fill.into();
row_range.into()
})
}
}
} else {
match self {
VariableValue::Absolute(address) => Ast::new(address.into()),
VariableValue::Ast(ast) => ast,
VariableValue::Row(row) => {
let a1: a1_notation::A1 = row.into();
Ast::new(a1.into())
}
_ => compiler_error(
"Attempted to load a spreadsheet-relative value in a non-spreadsheet context",
),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::*;
use crate::*;
use std::panic;
fn build_position() -> a1_notation::Address {
a1_notation::Address::new(0, 0)
}
#[test]
fn into_ast_absolute_some_position() {
assert_eq!(
VariableValue::Absolute(build_position()).into_ast(Some(build_position())),
a1_notation::cell(0, 0).into()
);
}
#[test]
fn into_ast_absolute_none_position() {
assert_eq!(
VariableValue::Absolute(a1_notation::Address::new(0, 0)).into_ast(None),
a1_notation::cell(0, 0).into()
);
}
#[test]
fn into_ast_ast_some_position() {
assert_eq!(
VariableValue::Ast(1.into()).into_ast(Some(build_position())),
1.into()
);
}
#[test]
fn into_ast_ast_none_position() {
assert_eq!(VariableValue::Ast(1.into()).into_ast(None), 1.into());
}
#[test]
fn into_ast_column_relative_none_position() {
assert!(panic::catch_unwind(|| {
VariableValue::ColumnRelative {
column: 5.into(),
fill: Fill::new(0, Some(20)),
}
.into_ast(None)
})
.is_err());
}
#[test]
fn into_ast_column_relative_some_position_in_fill() {
assert_eq!(
VariableValue::ColumnRelative {
column: 5.into(),
fill: Fill::new(0, Some(20)),
}
.into_ast(Some(build_position())),
Node::reference("F1").into()
);
}
#[test]
fn into_ast_column_relative_some_position_outside_fill() {
assert_eq!(
VariableValue::ColumnRelative {
column: 5.into(),
fill: Fill::new(20, Some(20)),
}
.into_ast(Some(build_position())),
Node::reference("F21:F40").into()
);
}
#[test]
fn into_ast_row_none_position() {
assert_eq!(
VariableValue::Row(1.into()).into_ast(None),
Node::reference("2:2").into()
);
}
#[test]
fn into_ast_row_some_position() {
assert_eq!(
VariableValue::Row(1.into()).into_ast(Some(build_position())),
Node::reference("2:2").into()
);
}
#[test]
fn into_ast_row_relative_none_position() {
assert!(panic::catch_unwind(|| {
VariableValue::RowRelative {
row: 5.into(),
fill: Fill::new(0, Some(20)),
}
.into_ast(None)
})
.is_err());
}
#[test]
fn into_ast_row_relative_some_position_in_fill() {
assert_eq!(
VariableValue::RowRelative {
row: 5.into(),
fill: Fill::new(0, Some(20)),
}
.into_ast(Some(build_position())),
Node::reference("1:1").into()
);
}
#[test]
fn into_ast_row_relative_some_position_outside_fill() {
assert_eq!(
VariableValue::RowRelative {
row: 5.into(),
fill: Fill::new(20, Some(20)),
}
.into_ast(Some(build_position())),
Node::reference("21:40").into()
);
}
}