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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// Copyright (c) 2025-2026 Ćukasz Szpakowski
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
//! A module of syntax tree.
use std::fmt;
use std::sync::Arc;
use crate::error::*;
/// A structure of syntax tree.
///
/// The syntax tree creates from tokens by a parser. An interpreter can take the syntax tree to
/// interpretation.
#[derive(Clone, Debug)]
pub struct Tree(pub Vec<Node>);
/// A node enumeration.
#[derive(Clone, Debug)]
pub enum Node
{
/// A definition.
Def(Box<Def>),
/// A statement.
Stat(Box<Stat>),
}
/// A definition enumeration.
#[derive(Clone, Debug)]
pub enum Def
{
/// A module.
Mod(String, Box<Mod>, Pos),
/// A function.
Fun(String, Arc<Fun>, Pos),
}
impl Def
{
/// Returns the file position.
pub fn pos(&self) -> &Pos
{
match self {
Def::Mod(_, _, pos) => pos,
Def::Fun(_, _, pos) => pos,
}
}
/// Sets the file position.
pub fn set_pos(&mut self, pos: Pos)
{
match self {
Def::Mod(_, _, pos2) => *pos2 = pos,
Def::Fun(_, _, pos2) => *pos2 = pos,
}
}
}
/// A module structure.
#[derive(Clone, Debug)]
pub struct Mod(pub Vec<Node>);
/// A function structure.
#[derive(Clone, Debug)]
pub struct Fun(pub Vec<Arg>, pub Vec<Box<Stat>>);
/// An argument structure.
#[derive(Clone, Debug)]
pub struct Arg(pub String, pub Pos);
impl Arg
{
/// Returns the file position.
pub fn pos(&self) -> &Pos
{ &self.1 }
/// Sets the file position.
pub fn set_pos(&mut self, pos: Pos)
{ self.1 = pos; }
}
/// A statement enumeration.
#[derive(Clone, Debug)]
pub enum Stat
{
/// An expression statement.
Expr(Box<Expr>, Pos),
/// An assignment statement.
Assign(Box<Expr>, Box<Expr>, Pos),
/// An `if` statement.
If(Box<Expr>, Vec<Box<Stat>>, Vec<(Box<Expr>, Vec<Box<Stat>>)>, Option<Vec<Box<Stat>>>, Pos),
/// A `for` statement.
For(String, Box<Expr>, Vec<Box<Stat>>, Pos),
/// A `while` statement.
While(Box<Expr>, Vec<Box<Stat>>, Pos),
/// A `break` statement.
Break(Pos),
/// A `continue` statement.
Continue(Pos),
/// A `return` statement.
Return(Option<Box<Expr>>, Pos),
/// A `quit` statement.
Quit(Pos),
}
impl Stat
{
/// Returns the file position.
pub fn pos(&self) -> &Pos
{
match self {
Stat::Expr(_, pos) => pos,
Stat::Assign(_, _, pos) => pos,
Stat::If(_, _, _, _, pos) => pos,
Stat::For(_, _, _, pos) => pos,
Stat::While(_, _, pos) => pos,
Stat::Break(pos) => pos,
Stat::Continue(pos) => pos,
Stat::Return(_, pos) => pos,
Stat::Quit(pos) => pos,
}
}
/// Sets the file position.
pub fn set_pos(&mut self, pos: Pos)
{
match self {
Stat::Expr(_, pos2) => *pos2 = pos,
Stat::Assign(_, _, pos2) => *pos2 = pos,
Stat::If(_, _, _, _, pos2) => *pos2 = pos,
Stat::For(_, _, _, pos2) => *pos2 = pos,
Stat::While(_, _, pos2) => *pos2 = pos,
Stat::Break(pos2) => *pos2 = pos,
Stat::Continue(pos2) => *pos2 = pos,
Stat::Return(_, pos2) => *pos2 = pos,
Stat::Quit(pos2) => *pos2 = pos,
}
}
}
/// An expression.
#[derive(Clone, Debug)]
pub enum Expr
{
/// A literal.
Lit(Lit, Pos),
/// A variable.
Var(Name, Pos),
/// A function application.
App(Box<Expr>, Vec<Box<Expr>>, Pos),
/// An expression of unary operator.
UnaryOp(UnaryOp, Box<Expr>, Pos),
/// An expression of binary operator.
BinOp(BinOp, Box<Expr>, Box<Expr>, Pos),
/// A logical AND expression.
And(Box<Expr>, Box<Expr>, Pos),
/// A logical OR expression.
Or(Box<Expr>, Box<Expr>, Pos),
/// A field access.
Field(Box<Expr>, String, Pos),
/// A range.
Range(Box<Expr>, Box<Expr>, Option<Box<Expr>>, Pos),
/// An error propagation.
PropagateError(Box<Expr>, Pos),
}
impl Expr
{
/// Returns the file position.
pub fn pos(&self) -> &Pos
{
match self {
Expr::Lit(_, pos) => pos,
Expr::Var(_, pos) => pos,
Expr::App(_, _, pos) => pos,
Expr::UnaryOp(_, _, pos) => pos,
Expr::BinOp(_, _, _, pos) => pos,
Expr::And(_, _, pos) => pos,
Expr::Or(_, _, pos) => pos,
Expr::Field(_, _, pos) => pos,
Expr::Range(_, _, _, pos) => pos,
Expr::PropagateError(_, pos) => pos,
}
}
/// Sets the file position.
pub fn set_pos(&mut self, pos: Pos)
{
match self {
Expr::Lit(_, pos2) => *pos2 = pos,
Expr::Var(_, pos2) => *pos2 = pos,
Expr::App(_, _, pos2) => *pos2 = pos,
Expr::UnaryOp(_, _, pos2) => *pos2 = pos,
Expr::BinOp(_, _, _, pos2) => *pos2 = pos,
Expr::And(_, _, pos2) => *pos2 = pos,
Expr::Or(_, _, pos2) => *pos2 = pos,
Expr::Field(_, _, pos2) => *pos2 = pos,
Expr::Range(_, _, _, pos2) => *pos2 = pos,
Expr::PropagateError(_, pos2) => *pos2 = pos,
}
}
}
/// A literal.
#[derive(Clone, Debug)]
pub enum Lit
{
/// A none literal.
None,
/// A boolean literal
Bool(bool),
/// An integer number literal.
Int(i64),
/// A floating-point number literal.
Float(f32),
/// A stribg literal.
String(String),
/// A matrix literal.
Matrix(Vec<MatrixRow>),
/// A filled matrix literal.
FilledMatrix(MatrixRow, Box<Expr>),
/// An array literal.
Array(Vec<Box<Expr>>),
/// A filled array literal with one value.
FilledArray(Box<Expr>, Box<Expr>),
/// A structure literal.
Struct(Vec<FieldPair>),
}
/// An enumeration of matrix row.
#[derive(Clone, Debug)]
pub enum MatrixRow
{
/// An matrix row.
Row(Vec<Box<Expr>>),
/// A filled matrix row with one value.
FilledRow(Box<Expr>, Box<Expr>),
}
/// A structure of field pair.
#[derive(Clone, Debug)]
pub struct FieldPair(pub String, pub Box<Expr>, pub Pos);
impl FieldPair
{
/// Returns the file position.
pub fn pos(&self) -> &Pos
{ &self.2 }
/// Sets the file position.
pub fn set_pos(&mut self, pos: Pos)
{ self.2 = pos; }
}
/// An enumeration of unary operator.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum UnaryOp
{
/// A negation.
Neg,
/// A dot negation.
DotNeg,
/// A logical NOT.
Not,
/// A transpose.
Transpose,
}
/// An enumeration of binary operator.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum BinOp
{
/// An index operator.
Index,
/// A multiplication.
Mul,
/// A dot multiplication.
DotMul,
/// A division.
Div,
/// A dot division.
DotDiv,
/// An addition.
Add,
/// A dot addition.
DotAdd,
/// A subtraction.
Sub,
/// A dot subtraction.
DotSub,
/// Less than.
Lt,
/// Greater than or equal to.
Ge,
/// Greater than.
Gt,
/// Less than or equal to.
Le,
/// Equal.
Eq,
/// Not equal.
Ne,
}
/// An enumeration of variable name.
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Name
{
/// An absolute name with identifiers of modules and a variable identifier.
Abs(Vec<String>, String),
/// A relative name with identifiers of modules and a variable identifier.
Rel(Vec<String>, String),
/// A name with one identifier.
Var(String),
}
impl fmt::Display for Name
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self {
Name::Abs(idents, ident) => {
write!(f, "root")?;
for ident2 in idents {
write!(f, "::{}", ident2)?;
}
write!(f, "::{}", ident)
},
Name::Rel(idents, ident) => {
let mut is_first = true;
for ident2 in idents {
if !is_first {
write!(f, "::{}", ident2)?;
} else {
write!(f, "{}", ident2)?;
}
is_first = false;
}
write!(f, "::{}", ident)
},
Name::Var(ident) => write!(f, "{}", ident),
}
}
}