-
Notifications
You must be signed in to change notification settings - Fork 1
/
impls.rs
246 lines (218 loc) · 7.2 KB
/
impls.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
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
use std::fmt::Display;
use std::fmt::Write;
use console::style;
use super::{token::Const, types::*};
impl Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut s: String;
f.write_str(match self {
Self::Void => {
s = style("void").green().to_string();
&s
}
Self::Bool => {
s = style("bool").green().to_string();
&s
}
Self::Char => {
s = style("char").green().to_string();
&s
}
Self::Int => {
s = style("int").green().to_string();
&s
}
Self::Ptr(Ptr { base_type }) => {
if f.alternate() {
s = format!("pointer to: < {:#} >", base_type);
} else {
// s = format!("pointer to: ---> {}", base_type);
s = format!("* -> {}", base_type);
}
&s
}
Self::Array(Array { length, base_type, size_expr: _ }) => {
if f.alternate() {
s = format!("array of < {:#} > with size {}", base_type, length);
} else {
// s = format!("array [size: {}] of ---> {}", length, base_type);
if *length == 0 {
s = format!("[] -> {}", base_type);
} else {
s = format!("[{}] -> {}", length, base_type);
}
}
&s
}
Self::Func(Func { return_type, param_list, is_variadic: _ }) => {
if f.alternate() {
s = format!("function returning < {:#} > with parameters: (", return_type);
if let Some((first, remaining)) = param_list.split_first() {
write!(s, "{:#}", first.ctype)?;
for p in remaining {
write!(s, ", {:#}", p.ctype)?;
}
}
s.push(')');
} else {
s = "func (".to_string();
if let Some((first, remaining)) = param_list.split_first() {
write!(s, "{}", first.ctype)?;
for p in remaining {
write!(s, ", {}", p.ctype)?;
}
}
write!(s, "): {}", return_type)?;
}
&s
}
})
}
}
impl Display for Declarator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.value {
Const::Empty => write!(f, "{}", self.name),
_ => write!(f, "{} = {}", self.name, self.value),
}
}
}
impl Display for Parameter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl Display for FunctionDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} (", self.name)?;
if !self.params.is_empty() {
write!(f, "{}", self.params[0])?;
}
for param in self.params.iter().skip(1) {
write!(f, ", {}", param)?;
}
writeln!(f, ");")?;
for stmt in self.stmts.iter() {
write!(f, "{}", stmt)?;
}
writeln!(f)
}
}
impl Display for Statement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Empty => writeln!(f, ";"),
Self::ExprStmt(expr) => writeln!(f, "{};", expr),
Self::ReturnStmt(expr) => writeln!(f, "return\n{};", expr),
}
}
}
#[derive(PartialEq, Eq)]
pub enum NodePos {
Init,
Top,
Middle,
Bottom,
}
impl NodePos {
fn node_prelude(&self) -> &'static str {
match self {
NodePos::Init => "────",
NodePos::Top => "┌───",
NodePos::Middle => "├───",
NodePos::Bottom => "└───",
}
}
fn is_init(&self) -> bool {
*self == NodePos::Init
}
fn is_top(&self) -> bool {
*self == NodePos::Top
}
fn is_middle(&self) -> bool {
*self == NodePos::Middle
}
}
fn print_op(s: &mut String, prev: &str, pos: &NodePos, v: impl Display) {
s.push_str(&style(prev).dim().to_string());
s.push_str(&style(pos.node_prelude()).dim().to_string());
s.push_str(&style(v.to_string().as_str()).blue().to_string());
s.push('\n');
}
fn print_leaf(s: &mut String, prev: &str, pos: &NodePos, v: impl Display) {
// if prev.is_empty() { s.push_str(&style(v.to_string().as_str()).green().to_string()); } else {
s.push_str(&style(prev).dim().to_string());
s.push_str(&style(pos.node_prelude()).dim().to_string());
s.push_str(&style(v.to_string().as_str()).green().to_string());
s.push('\n');
// }
}
fn print_binary_node(s: &mut String, prev: &str, pos: &NodePos, v: impl Display, left: &Expr, right: &Expr) {
let next_prefix_top = if pos.is_init() || pos.is_top() { " " } else { "│ " };
print_expr_tree(left, s, &(prev.to_owned() + next_prefix_top), &NodePos::Top);
print_op(s, prev, pos, v);
let next_prefix = if pos.is_top() || pos.is_middle() { "│ " } else { " " };
print_expr_tree(right, s, &(prev.to_owned() + next_prefix), &NodePos::Bottom);
}
fn print_func_args(s: &mut String, prev: &str, pos: &NodePos, expr: &[Expr]) {
if let Some((last, elems)) = expr.split_last() {
let next_prefix = prev.to_owned() + if pos.is_top() || pos.is_middle() { "│ " } else { " " };
for e in elems {
print_expr_tree(e, s, &next_prefix, &NodePos::Middle);
}
print_expr_tree(last, s, &next_prefix, &NodePos::Bottom);
}
}
fn print_expr_tree(this: &Expr, s: &mut String, prev: &str, pos: &NodePos) {
match this {
Expr::Const(v) => print_leaf(s, prev, pos, v),
Expr::StringLiteral(v) => print_leaf(s, prev, pos, v),
Expr::Id(v) => print_leaf(s, prev, pos, v),
Expr::BinOp(BinOp { op, left, right }) => print_binary_node(s, prev, pos, op, left, right),
Expr::AssignExpr(AssignExpr { left, assign, right }) => print_binary_node(s, prev, pos, assign, left, right),
Expr::CommaExpr(CommaExpr { left, right }) => print_binary_node(s, prev, pos, ",", left, right),
Expr::CondExpr(CondExpr { cond, left, right }) => {
let prefix_str = if pos.is_top() || prev.is_empty() { " " } else { "│ " };
print_expr_tree(cond, s, &(prev.to_owned() + prefix_str), &NodePos::Top);
print_op(s, prev, pos, "? :");
let next_prefix = prev.to_owned() + if pos.is_top() || pos.is_middle() { "│ " } else { " " };
print_expr_tree(left, s, &next_prefix, &NodePos::Middle);
print_expr_tree(right, s, &next_prefix, &NodePos::Bottom);
}
Expr::FunctionCall(expr, args) => {
let prefix_str = if pos.is_top() || prev.is_empty() { " " } else { "│ " };
print_expr_tree(expr, s, &(prev.to_owned() + prefix_str), &NodePos::Top);
print_op(s, prev, pos, "<fn>()");
print_func_args(s, prev, pos, args)
}
Expr::Postfix(PostfixOP { op, expr }) => {
print_op(s, prev, pos, op);
let next_prefix = prev.to_owned() + if pos.is_top() { "│ " } else { " " };
print_expr_tree(expr, s, &next_prefix, &NodePos::Bottom);
}
Expr::UnaryOp(UnaryOp { op, expr }) => {
print_op(s, prev, pos, op);
let next_prefix = prev.to_owned() + if pos.is_top() { "│ " } else { " " };
print_expr_tree(expr, s, &next_prefix, &NodePos::Bottom);
}
Expr::MemberAccess(expr, field) => {
let prefix_str = if pos.is_top() || prev.is_empty() { " " } else { "│ " };
print_expr_tree(expr, s, &(prev.to_owned() + prefix_str), &NodePos::Top);
let op = ".".to_owned() + field;
print_op(s, prev, pos, &op);
}
Expr::MemberAccessP(expr, field) => {
let prefix_str = if pos.is_top() || prev.is_empty() { " " } else { "│ " };
print_expr_tree(expr, s, &(prev.to_owned() + prefix_str), &NodePos::Top);
let op = "->".to_owned() + field;
print_op(s, prev, pos, &op);
}
}
}
impl Display for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut s = String::new();
print_expr_tree(self, &mut s, "", &NodePos::Init);
f.write_str(s.as_str())
}
}