-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathexpression.rs
876 lines (773 loc) · 27.9 KB
/
expression.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
use std::borrow::Cow;
use std::fmt::Display;
use crate::ast::{
Ident, ItemVisibility, Path, Pattern, Recoverable, Statement, StatementKind,
UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData, Visibility,
};
use crate::hir::def_collector::errors::DefCollectorErrorKind;
use crate::macros_api::StructId;
use crate::node_interner::{ExprId, InternedExpressionKind, InternedStatementKind, QuotedTypeId};
use crate::token::{Attributes, FunctionAttribute, Token, Tokens};
use crate::{Kind, Type};
use acvm::{acir::AcirField, FieldElement};
use iter_extended::vecmap;
use noirc_errors::{Span, Spanned};
use super::{AsTraitPath, UnaryRhsMemberAccess};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ExpressionKind {
Literal(Literal),
Block(BlockExpression),
Prefix(Box<PrefixExpression>),
Index(Box<IndexExpression>),
Call(Box<CallExpression>),
MethodCall(Box<MethodCallExpression>),
Constructor(Box<ConstructorExpression>),
MemberAccess(Box<MemberAccessExpression>),
Cast(Box<CastExpression>),
Infix(Box<InfixExpression>),
If(Box<IfExpression>),
Variable(Path),
Tuple(Vec<Expression>),
Lambda(Box<Lambda>),
Parenthesized(Box<Expression>),
Quote(Tokens),
Unquote(Box<Expression>),
Comptime(BlockExpression, Span),
Unsafe(BlockExpression, Span),
AsTraitPath(AsTraitPath),
// This variant is only emitted when inlining the result of comptime
// code. It is used to translate function values back into the AST while
// guaranteeing they have the same instantiated type and definition id without resolving again.
Resolved(ExprId),
// This is an interned ExpressionKind during comptime code.
// The actual ExpressionKind can be retrieved with a NodeInterner.
Interned(InternedExpressionKind),
/// Interned statements are allowed to be parsed as expressions in case they resolve
/// to an StatementKind::Expression or StatementKind::Semi.
InternedStatement(InternedStatementKind),
Error,
}
/// A Vec of unresolved names for type variables.
/// For `fn foo<A, B>(...)` this corresponds to vec!["A", "B"].
pub type UnresolvedGenerics = Vec<UnresolvedGeneric>;
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum UnresolvedGeneric {
Variable(Ident),
Numeric {
ident: Ident,
typ: UnresolvedType,
},
/// Already-resolved generics can be parsed as generics when a macro
/// splices existing types into a generic list. In this case we have
/// to validate the type refers to a named generic and treat that
/// as a ResolvedGeneric when this is resolved.
Resolved(QuotedTypeId, Span),
}
impl UnresolvedGeneric {
pub fn span(&self) -> Span {
match self {
UnresolvedGeneric::Variable(ident) => ident.0.span(),
UnresolvedGeneric::Numeric { ident, typ } => ident.0.span().merge(typ.span),
UnresolvedGeneric::Resolved(_, span) => *span,
}
}
pub fn kind(&self) -> Result<Kind, DefCollectorErrorKind> {
match self {
UnresolvedGeneric::Variable(_) => Ok(Kind::Normal),
UnresolvedGeneric::Numeric { typ, .. } => {
let typ = self.resolve_numeric_kind_type(typ)?;
Ok(Kind::Numeric(Box::new(typ)))
}
UnresolvedGeneric::Resolved(..) => {
panic!("Don't know the kind of a resolved generic here")
}
}
}
fn resolve_numeric_kind_type(
&self,
typ: &UnresolvedType,
) -> Result<Type, DefCollectorErrorKind> {
use crate::ast::UnresolvedTypeData::{FieldElement, Integer};
match typ.typ {
FieldElement => Ok(Type::FieldElement),
Integer(sign, bits) => Ok(Type::Integer(sign, bits)),
// Only fields and integers are supported for numeric kinds
_ => Err(DefCollectorErrorKind::UnsupportedNumericGenericType {
ident: self.ident().clone(),
typ: typ.typ.clone(),
}),
}
}
pub(crate) fn ident(&self) -> &Ident {
match self {
UnresolvedGeneric::Variable(ident) | UnresolvedGeneric::Numeric { ident, .. } => ident,
UnresolvedGeneric::Resolved(..) => panic!("UnresolvedGeneric::Resolved no ident"),
}
}
}
impl Display for UnresolvedGeneric {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UnresolvedGeneric::Variable(ident) => write!(f, "{ident}"),
UnresolvedGeneric::Numeric { ident, typ } => write!(f, "let {ident}: {typ}"),
UnresolvedGeneric::Resolved(..) => write!(f, "(resolved)"),
}
}
}
impl From<Ident> for UnresolvedGeneric {
fn from(value: Ident) -> Self {
UnresolvedGeneric::Variable(value)
}
}
impl ExpressionKind {
pub fn into_path(self) -> Option<Path> {
match self {
ExpressionKind::Variable(path) => Some(path),
_ => None,
}
}
pub fn into_infix(self) -> Option<InfixExpression> {
match self {
ExpressionKind::Infix(infix) => Some(*infix),
_ => None,
}
}
pub fn prefix(operator: UnaryOp, rhs: Expression) -> ExpressionKind {
match (operator, &rhs) {
(
UnaryOp::Minus,
Expression { kind: ExpressionKind::Literal(Literal::Integer(field, sign)), .. },
) => ExpressionKind::Literal(Literal::Integer(*field, !sign)),
_ => ExpressionKind::Prefix(Box::new(PrefixExpression { operator, rhs })),
}
}
pub fn array(contents: Vec<Expression>) -> ExpressionKind {
ExpressionKind::Literal(Literal::Array(ArrayLiteral::Standard(contents)))
}
pub fn repeated_array(repeated_element: Expression, length: Expression) -> ExpressionKind {
ExpressionKind::Literal(Literal::Array(ArrayLiteral::Repeated {
repeated_element: Box::new(repeated_element),
length: Box::new(length),
}))
}
pub fn slice(contents: Vec<Expression>) -> ExpressionKind {
ExpressionKind::Literal(Literal::Slice(ArrayLiteral::Standard(contents)))
}
pub fn repeated_slice(repeated_element: Expression, length: Expression) -> ExpressionKind {
ExpressionKind::Literal(Literal::Slice(ArrayLiteral::Repeated {
repeated_element: Box::new(repeated_element),
length: Box::new(length),
}))
}
pub fn integer(contents: FieldElement) -> ExpressionKind {
ExpressionKind::Literal(Literal::Integer(contents, false))
}
pub fn boolean(contents: bool) -> ExpressionKind {
ExpressionKind::Literal(Literal::Bool(contents))
}
pub fn string(contents: String) -> ExpressionKind {
ExpressionKind::Literal(Literal::Str(contents))
}
pub fn raw_string(contents: String, hashes: u8) -> ExpressionKind {
ExpressionKind::Literal(Literal::RawStr(contents, hashes))
}
pub fn format_string(contents: String) -> ExpressionKind {
ExpressionKind::Literal(Literal::FmtStr(contents))
}
pub fn constructor(
(typ, fields): (UnresolvedType, Vec<(Ident, Expression)>),
) -> ExpressionKind {
ExpressionKind::Constructor(Box::new(ConstructorExpression {
typ,
fields,
struct_type: None,
}))
}
}
impl Recoverable for ExpressionKind {
fn error(_: Span) -> Self {
ExpressionKind::Error
}
}
impl Recoverable for Expression {
fn error(span: Span) -> Self {
Expression::new(ExpressionKind::Error, span)
}
}
impl Recoverable for Option<Expression> {
fn error(span: Span) -> Self {
Some(Expression::new(ExpressionKind::Error, span))
}
}
#[derive(Debug, Eq, Clone)]
pub struct Expression {
pub kind: ExpressionKind,
pub span: Span,
}
// This is important for tests. Two expressions are the same, if their Kind is the same
// We are ignoring Span
impl PartialEq<Expression> for Expression {
fn eq(&self, rhs: &Expression) -> bool {
self.kind == rhs.kind
}
}
impl Expression {
pub fn new(kind: ExpressionKind, span: Span) -> Expression {
Expression { kind, span }
}
pub fn member_access_or_method_call(
lhs: Expression,
rhs: UnaryRhsMemberAccess,
span: Span,
) -> Expression {
let kind = match rhs.method_call {
None => {
let rhs = rhs.method_or_field;
ExpressionKind::MemberAccess(Box::new(MemberAccessExpression { lhs, rhs }))
}
Some(method_call) => ExpressionKind::MethodCall(Box::new(MethodCallExpression {
object: lhs,
method_name: rhs.method_or_field,
generics: method_call.turbofish,
arguments: method_call.args,
is_macro_call: method_call.macro_call,
})),
};
Expression::new(kind, span)
}
pub fn index(collection: Expression, index: Expression, span: Span) -> Expression {
let kind = ExpressionKind::Index(Box::new(IndexExpression { collection, index }));
Expression::new(kind, span)
}
pub fn cast(lhs: Expression, r#type: UnresolvedType, span: Span) -> Expression {
let kind = ExpressionKind::Cast(Box::new(CastExpression { lhs, r#type }));
Expression::new(kind, span)
}
pub fn call(
lhs: Expression,
is_macro_call: bool,
arguments: Vec<Expression>,
span: Span,
) -> Expression {
let func = Box::new(lhs);
let kind =
ExpressionKind::Call(Box::new(CallExpression { func, is_macro_call, arguments }));
Expression::new(kind, span)
}
}
pub type BinaryOp = Spanned<BinaryOpKind>;
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, Clone)]
pub enum BinaryOpKind {
Add,
Subtract,
Multiply,
Divide,
Equal,
NotEqual,
Less,
LessEqual,
Greater,
GreaterEqual,
And,
Or,
Xor,
ShiftRight,
ShiftLeft,
Modulo,
}
impl BinaryOpKind {
/// Comparator operators return a 0 or 1
/// When seen in the middle of an infix operator,
/// they transform the infix expression into a predicate expression
pub fn is_comparator(self) -> bool {
matches!(
self,
BinaryOpKind::Equal
| BinaryOpKind::NotEqual
| BinaryOpKind::LessEqual
| BinaryOpKind::Less
| BinaryOpKind::Greater
| BinaryOpKind::GreaterEqual
)
}
pub fn is_valid_for_field_type(self) -> bool {
matches!(
self,
BinaryOpKind::Add
| BinaryOpKind::Subtract
| BinaryOpKind::Multiply
| BinaryOpKind::Divide
| BinaryOpKind::Equal
| BinaryOpKind::NotEqual
)
}
pub fn as_string(self) -> &'static str {
match self {
BinaryOpKind::Add => "+",
BinaryOpKind::Subtract => "-",
BinaryOpKind::Multiply => "*",
BinaryOpKind::Divide => "/",
BinaryOpKind::Equal => "==",
BinaryOpKind::NotEqual => "!=",
BinaryOpKind::Less => "<",
BinaryOpKind::LessEqual => "<=",
BinaryOpKind::Greater => ">",
BinaryOpKind::GreaterEqual => ">=",
BinaryOpKind::And => "&",
BinaryOpKind::Or => "|",
BinaryOpKind::Xor => "^",
BinaryOpKind::ShiftRight => ">>",
BinaryOpKind::ShiftLeft => "<<",
BinaryOpKind::Modulo => "%",
}
}
pub fn as_token(self) -> Token {
match self {
BinaryOpKind::Add => Token::Plus,
BinaryOpKind::Subtract => Token::Minus,
BinaryOpKind::Multiply => Token::Star,
BinaryOpKind::Divide => Token::Slash,
BinaryOpKind::Equal => Token::Equal,
BinaryOpKind::NotEqual => Token::NotEqual,
BinaryOpKind::Less => Token::Less,
BinaryOpKind::LessEqual => Token::LessEqual,
BinaryOpKind::Greater => Token::Greater,
BinaryOpKind::GreaterEqual => Token::GreaterEqual,
BinaryOpKind::And => Token::Ampersand,
BinaryOpKind::Or => Token::Pipe,
BinaryOpKind::Xor => Token::Caret,
BinaryOpKind::ShiftLeft => Token::ShiftLeft,
BinaryOpKind::ShiftRight => Token::ShiftRight,
BinaryOpKind::Modulo => Token::Percent,
}
}
}
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, Clone)]
pub enum UnaryOp {
Minus,
Not,
MutableReference,
/// If implicitly_added is true, this operation was implicitly added by the compiler for a
/// field dereference. The compiler may undo some of these implicitly added dereferences if
/// the reference later turns out to be needed (e.g. passing a field by reference to a function
/// requiring an &mut parameter).
Dereference {
implicitly_added: bool,
},
}
impl UnaryOp {
/// Converts a token to a unary operator
/// If you want the parser to recognize another Token as being a prefix operator, it is defined here
pub fn from(token: &Token) -> Option<UnaryOp> {
match token {
Token::Minus => Some(UnaryOp::Minus),
Token::Bang => Some(UnaryOp::Not),
_ => None,
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Literal {
Array(ArrayLiteral),
Slice(ArrayLiteral),
Bool(bool),
Integer(FieldElement, /*sign*/ bool), // false for positive integer and true for negative
Str(String),
RawStr(String, u8),
FmtStr(String),
Unit,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PrefixExpression {
pub operator: UnaryOp,
pub rhs: Expression,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct InfixExpression {
pub lhs: Expression,
pub operator: BinaryOp,
pub rhs: Expression,
}
// This is an infix expression with 'as' as the binary operator
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CastExpression {
pub lhs: Expression,
pub r#type: UnresolvedType,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct IfExpression {
pub condition: Expression,
pub consequence: Expression,
pub alternative: Option<Expression>,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Lambda {
pub parameters: Vec<(Pattern, UnresolvedType)>,
pub return_type: UnresolvedType,
pub body: Expression,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct FunctionDefinition {
pub name: Ident,
// The `Attributes` container holds both `primary` (ones that change the function kind)
// and `secondary` attributes (ones that do not change the function kind)
pub attributes: Attributes,
/// True if this function was defined with the 'unconstrained' keyword
pub is_unconstrained: bool,
/// True if this function was defined with the 'comptime' keyword
pub is_comptime: bool,
/// Indicate if this function was defined with the 'pub' keyword
pub visibility: ItemVisibility,
pub generics: UnresolvedGenerics,
pub parameters: Vec<Param>,
pub body: BlockExpression,
pub span: Span,
pub where_clause: Vec<UnresolvedTraitConstraint>,
pub return_type: FunctionReturnType,
pub return_visibility: Visibility,
}
impl FunctionDefinition {
pub fn is_private(&self) -> bool {
self.visibility == ItemVisibility::Private
}
pub fn is_test(&self) -> bool {
if let Some(attribute) = &self.attributes.function {
matches!(attribute, FunctionAttribute::Test(..))
} else {
false
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Param {
pub visibility: Visibility,
pub pattern: Pattern,
pub typ: UnresolvedType,
pub span: Span,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum FunctionReturnType {
/// Returns type is not specified.
Default(Span),
/// Everything else.
Ty(UnresolvedType),
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ArrayLiteral {
Standard(Vec<Expression>),
Repeated { repeated_element: Box<Expression>, length: Box<Expression> },
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CallExpression {
pub func: Box<Expression>,
pub arguments: Vec<Expression>,
pub is_macro_call: bool,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MethodCallExpression {
pub object: Expression,
pub method_name: Ident,
/// Method calls have an optional list of generics if the turbofish operator was used
pub generics: Option<Vec<UnresolvedType>>,
pub arguments: Vec<Expression>,
pub is_macro_call: bool,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ConstructorExpression {
pub typ: UnresolvedType,
pub fields: Vec<(Ident, Expression)>,
/// This may be filled out during macro expansion
/// so that we can skip re-resolving the type name since it
/// would be lost at that point.
pub struct_type: Option<StructId>,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MemberAccessExpression {
pub lhs: Expression,
pub rhs: Ident,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct IndexExpression {
pub collection: Expression, // XXX: For now, this will be the name of the array, as we do not support other collections
pub index: Expression, // XXX: We accept two types of indices, either a normal integer or a constant
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BlockExpression {
pub statements: Vec<Statement>,
}
impl BlockExpression {
pub fn pop(&mut self) -> Option<StatementKind> {
self.statements.pop().map(|stmt| stmt.kind)
}
pub fn len(&self) -> usize {
self.statements.len()
}
pub fn is_empty(&self) -> bool {
self.statements.is_empty()
}
}
impl Display for Expression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.kind.fmt(f)
}
}
impl Display for ExpressionKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use ExpressionKind::*;
match self {
Literal(literal) => literal.fmt(f),
Block(block) => block.fmt(f),
Prefix(prefix) => prefix.fmt(f),
Index(index) => index.fmt(f),
Call(call) => call.fmt(f),
MethodCall(call) => call.fmt(f),
Cast(cast) => cast.fmt(f),
Infix(infix) => infix.fmt(f),
If(if_expr) => if_expr.fmt(f),
Variable(path) => path.fmt(f),
Constructor(constructor) => constructor.fmt(f),
MemberAccess(access) => access.fmt(f),
Tuple(elements) => {
let elements = vecmap(elements, ToString::to_string);
write!(f, "({})", elements.join(", "))
}
Lambda(lambda) => lambda.fmt(f),
Parenthesized(sub_expr) => write!(f, "({sub_expr})"),
Comptime(block, _) => write!(f, "comptime {block}"),
Unsafe(block, _) => write!(f, "unsafe {block}"),
Error => write!(f, "Error"),
Resolved(_) => write!(f, "?Resolved"),
Interned(_) => write!(f, "?Interned"),
Unquote(expr) => write!(f, "$({expr})"),
Quote(tokens) => {
let tokens = vecmap(&tokens.0, ToString::to_string);
write!(f, "quote {{ {} }}", tokens.join(" "))
}
AsTraitPath(path) => write!(f, "{path}"),
InternedStatement(_) => write!(f, "?InternedStatement"),
}
}
}
impl Display for Literal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Literal::Array(ArrayLiteral::Standard(elements)) => {
let contents = vecmap(elements, ToString::to_string);
write!(f, "[{}]", contents.join(", "))
}
Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => {
write!(f, "[{repeated_element}; {length}]")
}
Literal::Slice(ArrayLiteral::Standard(elements)) => {
let contents = vecmap(elements, ToString::to_string);
write!(f, "&[{}]", contents.join(", "))
}
Literal::Slice(ArrayLiteral::Repeated { repeated_element, length }) => {
write!(f, "&[{repeated_element}; {length}]")
}
Literal::Bool(boolean) => write!(f, "{}", if *boolean { "true" } else { "false" }),
Literal::Integer(integer, sign) => {
if *sign {
write!(f, "-{}", integer.to_u128())
} else {
write!(f, "{}", integer.to_u128())
}
}
Literal::Str(string) => write!(f, "\"{string}\""),
Literal::RawStr(string, num_hashes) => {
let hashes: String =
std::iter::once('#').cycle().take(*num_hashes as usize).collect();
write!(f, "r{hashes}\"{string}\"{hashes}")
}
Literal::FmtStr(string) => write!(f, "f\"{string}\""),
Literal::Unit => write!(f, "()"),
}
}
}
impl Display for BlockExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{{")?;
for statement in &self.statements {
let statement = statement.kind.to_string();
for line in statement.lines() {
writeln!(f, " {line}")?;
}
}
write!(f, "}}")
}
}
impl Display for PrefixExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({} {})", self.operator, self.rhs)
}
}
impl Display for UnaryOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UnaryOp::Minus => write!(f, "-"),
UnaryOp::Not => write!(f, "!"),
UnaryOp::MutableReference => write!(f, "&mut"),
UnaryOp::Dereference { .. } => write!(f, "*"),
}
}
}
impl Display for IndexExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}[{}]", self.collection, self.index)
}
}
impl Display for CallExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let args = vecmap(&self.arguments, ToString::to_string);
write!(f, "{}({})", self.func, args.join(", "))
}
}
impl Display for MethodCallExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let args = vecmap(&self.arguments, ToString::to_string);
write!(f, "{}.{}({})", self.object, self.method_name, args.join(", "))
}
}
impl Display for CastExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({} as {})", self.lhs, self.r#type)
}
}
impl Display for ConstructorExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let fields =
self.fields.iter().map(|(ident, expr)| format!("{ident}: {expr}")).collect::<Vec<_>>();
write!(f, "({} {{ {} }})", self.typ, fields.join(", "))
}
}
impl Display for MemberAccessExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}.{})", self.lhs, self.rhs)
}
}
impl Display for InfixExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({} {} {})", self.lhs, self.operator.contents, self.rhs)
}
}
impl Display for BinaryOpKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BinaryOpKind::Add => write!(f, "+"),
BinaryOpKind::Subtract => write!(f, "-"),
BinaryOpKind::Multiply => write!(f, "*"),
BinaryOpKind::Divide => write!(f, "/"),
BinaryOpKind::Equal => write!(f, "=="),
BinaryOpKind::NotEqual => write!(f, "!="),
BinaryOpKind::Less => write!(f, "<"),
BinaryOpKind::LessEqual => write!(f, "<="),
BinaryOpKind::Greater => write!(f, ">"),
BinaryOpKind::GreaterEqual => write!(f, ">="),
BinaryOpKind::And => write!(f, "&"),
BinaryOpKind::Or => write!(f, "|"),
BinaryOpKind::Xor => write!(f, "^"),
BinaryOpKind::ShiftLeft => write!(f, "<<"),
BinaryOpKind::ShiftRight => write!(f, ">>"),
BinaryOpKind::Modulo => write!(f, "%"),
}
}
}
impl Display for IfExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "if {} {}", self.condition, self.consequence)?;
if let Some(alternative) = &self.alternative {
write!(f, " else {alternative}")?;
}
Ok(())
}
}
impl Display for Lambda {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let parameters = vecmap(&self.parameters, |(name, r#type)| format!("{name}: {type}"));
write!(f, "|{}| -> {} {{ {} }}", parameters.join(", "), self.return_type, self.body)
}
}
impl Display for AsTraitPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<{} as {}>::{}", self.typ, self.trait_path, self.impl_item)
}
}
impl FunctionDefinition {
pub fn normal(
name: &Ident,
generics: &UnresolvedGenerics,
parameters: &[(Ident, UnresolvedType)],
body: &BlockExpression,
where_clause: &[UnresolvedTraitConstraint],
return_type: &FunctionReturnType,
) -> FunctionDefinition {
let p = parameters
.iter()
.map(|(ident, unresolved_type)| Param {
visibility: Visibility::Private,
pattern: Pattern::Identifier(ident.clone()),
typ: unresolved_type.clone(),
span: ident.span().merge(unresolved_type.span),
})
.collect();
FunctionDefinition {
name: name.clone(),
attributes: Attributes::empty(),
is_unconstrained: false,
is_comptime: false,
visibility: ItemVisibility::Private,
generics: generics.clone(),
parameters: p,
body: body.clone(),
span: name.span(),
where_clause: where_clause.to_vec(),
return_type: return_type.clone(),
return_visibility: Visibility::Private,
}
}
pub fn signature(&self) -> String {
let parameters = vecmap(&self.parameters, |Param { visibility, pattern, typ, span: _ }| {
if *visibility == Visibility::Public {
format!("{pattern}: {visibility} {typ}")
} else {
format!("{pattern}: {typ}")
}
});
let where_clause = vecmap(&self.where_clause, ToString::to_string);
let where_clause_str = if !where_clause.is_empty() {
format!(" where {}", where_clause.join(", "))
} else {
"".to_string()
};
let return_type = if matches!(&self.return_type, FunctionReturnType::Default(_)) {
String::new()
} else {
format!(" -> {}", self.return_type)
};
format!("fn {}({}){}{}", self.name, parameters.join(", "), return_type, where_clause_str)
}
}
impl Display for FunctionDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{:?}", self.attributes)?;
write!(f, "fn {} {}", self.signature(), self.body)
}
}
impl FunctionReturnType {
pub fn get_type(&self) -> Cow<UnresolvedType> {
match self {
FunctionReturnType::Default(span) => {
Cow::Owned(UnresolvedType { typ: UnresolvedTypeData::Unit, span: *span })
}
FunctionReturnType::Ty(typ) => Cow::Borrowed(typ),
}
}
}
impl Display for FunctionReturnType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FunctionReturnType::Default(_) => f.write_str(""),
FunctionReturnType::Ty(ty) => write!(f, "{ty}"),
}
}
}