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
|
use std::fmt::{self, Display, Formatter};
use crate::lex::token::Location;
use super::nodes::{
ArrayAccessExpression, ArrayExpression, BinaryOperatorNode, BlockExpression, CallExpression,
FnExpression, Identifier, IfExpression, LoopExpression, MemberAccessExpression,
SimpleLiteralNode, StrExpression, StrPartKind, UnaryOperatorNode,
};
#[derive(Debug, Clone)]
pub struct Expression {
// TODO: Sometimes this location is duplicated for no reason,
// i.e. in the UnaryExpression, since the token for the UnaryOperatorNode
// will be the same as the location for the expression...
// Can we do something about it or is some duplication fine?
pub at: Location,
pub kind: ExpressionKind,
}
#[derive(Debug, Clone)]
pub enum ExpressionKind {
Binary {
left: Box<Expression>,
op: BinaryOperatorNode,
right: Box<Expression>,
},
Unary {
op: UnaryOperatorNode,
right: Box<Expression>,
},
Call(Box<CallExpression>),
ArrayAccess(Box<ArrayAccessExpression>),
MemberAccess(Box<MemberAccessExpression>),
Group(Box<Expression>),
Block(Box<BlockExpression>),
If(Box<IfExpression>),
Loop(Box<LoopExpression>),
StrLiteral(Box<StrExpression>),
FnLiteral(Box<FnExpression>),
ArrayLiteral(ArrayExpression),
SimpleLiteral(SimpleLiteralNode),
Identifier(Identifier),
}
impl Display for Expression {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.nested_fmt(f, 0)
}
}
impl Expression {
pub(crate) fn nested_fmt(&self, f: &mut Formatter<'_>, depth: usize) -> fmt::Result {
let pad = " ".repeat(depth);
match &self.kind {
ExpressionKind::Binary { left, op, right } => {
writeln!(f, "{}Binary:", pad)?;
writeln!(f, "{}- Left:", pad)?;
left.nested_fmt(f, depth + 1)?;
writeln!(f, "{}- Operator: {:?}", pad, op)?;
writeln!(f, "{}- Right:", pad)?;
right.nested_fmt(f, depth + 1)?;
}
ExpressionKind::Unary { op, right } => {
writeln!(f, "{}Unary:", pad)?;
writeln!(f, "{}- Operator: {:?}", pad, op)?;
writeln!(f, "{}- Right:", pad)?;
right.nested_fmt(f, depth + 1)?;
}
ExpressionKind::Call(expr) => {
writeln!(f, "{}Function Call:", pad)?;
writeln!(f, "{}- Called:", pad)?;
expr.called.nested_fmt(f, depth + 1)?;
for (i, e) in expr.arguments.iter().enumerate() {
writeln!(f, "{}- Argument {}:", pad, i)?;
e.nested_fmt(f, depth + 1)?;
}
}
ExpressionKind::ArrayAccess(expr) => {
writeln!(f, "{}Array Access:", pad)?;
writeln!(f, "{}- Array:", pad)?;
expr.array.nested_fmt(f, depth + 1)?;
writeln!(f, "{}- Index:", pad)?;
expr.index.nested_fmt(f, depth + 1)?;
}
ExpressionKind::MemberAccess(expr) => {
writeln!(f, "{}Member Access:", pad)?;
writeln!(f, "{}- Object:", pad)?;
expr.object.nested_fmt(f, depth + 1)?;
writeln!(f, "{}- Member Name: {}", pad, expr.member_name)?;
}
ExpressionKind::Group(expr) => {
writeln!(f, "{}Group:", pad)?;
expr.nested_fmt(f, depth + 1)?;
}
ExpressionKind::Block(expr) => {
Self::block_fmt(f, &expr, depth + 1)?;
}
ExpressionKind::StrLiteral(expr) => {
writeln!(f, "{}Str:", pad)?;
for (i, statement) in expr.parts.iter().enumerate() {
writeln!(f, "{}- {}:", pad, i)?;
match &statement.kind {
StrPartKind::Literal(literal) => {
writeln!(f, "{}{}", " ".repeat(depth + 1), literal.clone())
}
StrPartKind::Embed(block) => block.nested_fmt(f, depth + 1),
}?;
}
}
ExpressionKind::FnLiteral(expr) => {
write!(f, "{}Fn (", pad)?;
// Write self receiver
if expr.header.has_self_receiver {
write!(f, "self, ")?;
}
// Write parameters
for p in expr.header.parameters.iter() {
write!(
f,
"{}: {}, ",
p.identifier,
p.type_constraint.as_ref().unwrap_or(&"_".into())
)?;
}
// Write return type
writeln!(
f,
") -> {}:",
expr.header.return_type.as_ref().unwrap_or(&"_".into())
)?;
Self::block_fmt(f, &expr.body, depth + 1)?;
}
ExpressionKind::ArrayLiteral(expr) => {
writeln!(f, "{}Array Literal:", pad)?;
for (i, c) in expr.elements.iter().enumerate() {
writeln!(f, "{}- Element {}:", pad, i)?;
c.nested_fmt(f, depth + 1)?;
}
}
ExpressionKind::SimpleLiteral(literal) => {
writeln!(f, "{}Literal: {:?}", pad, literal)?;
}
ExpressionKind::Identifier(identifier) => {
writeln!(f, "{}Identifier: {:?}", pad, identifier)?;
}
ExpressionKind::If(expr) => {
writeln!(f, "{}If:", pad)?;
for (i, c) in expr.conditionals.iter().enumerate() {
writeln!(f, "{}- Condition {}:", pad, i)?;
c.condition.nested_fmt(f, depth + 1)?;
writeln!(f, "{}- Body {}:", pad, i)?;
Self::block_fmt(f, &c.block, depth + 1)?;
}
if let Some(e) = &expr.else_block {
writeln!(f, "{}- Else:", pad)?;
Self::block_fmt(f, e, depth + 1)?;
}
}
ExpressionKind::Loop(expr) => {
writeln!(f, "{}Loop:", pad)?;
if let Some(loop_condition) = &expr.condition {
writeln!(f, "{}- Condition:", pad)?;
loop_condition.nested_fmt(f, depth + 1)?;
}
writeln!(f, "{}- Body:", pad)?;
Self::block_fmt(f, &expr.body, depth + 1)?;
}
}
Ok(())
}
fn block_fmt(f: &mut Formatter<'_>, block: &BlockExpression, depth: usize) -> fmt::Result {
let pad = " ".repeat(depth);
writeln!(f, "{}Block:", pad)?;
for (i, statement) in block.statements.iter().enumerate() {
writeln!(f, "{}- {}:", pad, i)?;
statement.nested_fmt(f, depth + 1)?;
}
if let Some(tail_expression) = &block.tail_expression {
writeln!(f, "{}- Tail:", pad)?;
tail_expression.nested_fmt(f, depth + 1)?;
}
Ok(())
}
}
|