-
Notifications
You must be signed in to change notification settings - Fork 9
/
ast.go
1221 lines (1064 loc) · 31.4 KB
/
ast.go
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package parser
import (
"fmt"
"strconv"
"strings"
"evylang.dev/evy/pkg/lexer"
)
// Node represents a node in the AST.
type Node interface {
// Token returns the token of the Evy source program associated with the node.
Token() *lexer.Token
// String returns a string representation of the node.
String() string
// Type returns the Evy type of the node, such as num, []string, NONE.
Type() *Type
}
// Program is the top-level or root AST node. It represents the entire
// Evy program.
//
// Program implements the [Node] interface.
type Program struct {
token *lexer.Token
// Statements is the ordered list of top level block and basic statements of the given Evy program.
Statements []Node
// EventHandlers maps event names to their event handler statements.
// It is used in web interface to set connect up relevant handlers with JS event handlers.
EventHandlers map[string]*EventHandlerStmt
// CalledBuiltinFuncs is a list of builtin functions that are called
// in the program. It is used in web interface to hide or show
// Canvas and input widgets, such as sliders or readline text field.
CalledBuiltinFuncs []string
alwaysTerms bool
formatting *formatting
}
// Format returns a string of the formatted program with consistent
// indentation and vertical whitespace.
func (p *Program) Format() string {
var sb strings.Builder
p.formatting.w = &sb
p.formatting.format(p)
return sb.String()
}
// String returns a string representation of the Program node.
func (p *Program) String() string {
return newlineList(p.Statements)
}
// Token returns the token of the Evy source program associated with the
// Program node.
func (p *Program) Token() *lexer.Token {
return p.token
}
// Type returns [NONE_TYPE] for Program because a program does not have
// a type.
func (*Program) Type() *Type {
return NONE_TYPE
}
func (p *Program) alwaysTerminates() bool {
return p.alwaysTerms
}
// EmptyStmt is an AST node that represents an empty statement. An empty
// statement is a statement that does nothing. Empty statement is used
// for formatting, such as to add a blank line between statements.
//
// EmptyStmt implements the [Node] interface.
type EmptyStmt struct {
token *lexer.Token // The NL token
}
// String returns a string representation of the EmptyStmt node.
func (e *EmptyStmt) String() string {
return ""
}
// Token returns the token of the Evy source program associated with the
// EmptyStmt node.
func (e *EmptyStmt) Token() *lexer.Token {
return e.token
}
// Type returns [NONE_TYPE] for EmptyStmt because the empty statement
// does not have a type.
func (*EmptyStmt) Type() *Type { return NONE_TYPE }
// FuncDefStmt is an AST node that represents a function definition. It
// defines a new function with a name, a parameter list, return type,
// and a body. For example:
//
// func greet
// print "howdy!"
// end
//
// FuncDefStmt implements the [Node] interface.
type FuncDefStmt struct {
token *lexer.Token // The "func" token
Name string
Params []*Var
VariadicParam *Var
ReturnType *Type
VariadicParamType *Type
Body *BlockStatement
isCalled bool
}
// String returns a string representation of the FuncDefStmt node.
func (f *FuncDefStmt) String() string {
s := make([]string, len(f.Params))
for i, param := range f.Params {
s[i] = param.String()
}
params := strings.Join(s, ", ")
if f.VariadicParam != nil {
params += f.VariadicParam.String() + "..."
}
signature := f.Name + "(" + params + ")"
body := ""
if f.Body != nil {
body = f.Body.String()
}
return signature + "{\n" + body + "}\n"
}
// Token returns the token of the Evy source program associated with the
// FuncDefStmt node.
func (f *FuncDefStmt) Token() *lexer.Token {
return f.token
}
// Type returns the return type of the function.
func (f *FuncDefStmt) Type() *Type {
return f.ReturnType
}
// EventHandlerStmt is an AST node that represents an event handler
// definition. It includes the handler body, such as:
//
// on key k:string
// print "key pressed:" k
// end
//
// EventHandlerStmt implements the [Node] interface.
type EventHandlerStmt struct {
token *lexer.Token // The "on" token
Name string
Params []*Var
Body *BlockStatement
}
// String returns a string representation of the EventHandlerStmt node.
func (e *EventHandlerStmt) String() string {
body := e.Body.String()
return "on " + e.Name + " {\n" + body + "}\n"
}
// Token returns the token of the Evy source program associated with the
// EventHandlerStmt node.
func (e *EventHandlerStmt) Token() *lexer.Token {
return e.token
}
// Type returns [NONE_TYPE] for EventHandlerStmt because an event
// handler definition does not have a type.
func (e *EventHandlerStmt) Type() *Type {
return NONE_TYPE
}
// IfStmt is an AST node that represents a conditional statement. It
// specifies a condition that must be met for a block of statements to
// be executed. It can optionally have else-if and else blocks. For
// example:
//
// if 2 * 5 == 10
// print "✔"
// end
//
// IfStmt implements the [Node] interface.
type IfStmt struct {
token *lexer.Token
IfBlock *ConditionalBlock
ElseIfBlocks []*ConditionalBlock
Else *BlockStatement
}
// String returns a string representation of the IfStmt node.
func (i *IfStmt) String() string {
result := "if " + i.IfBlock.String()
for _, elseif := range i.ElseIfBlocks {
result += "else if" + elseif.String()
}
if i.Else != nil {
result += "else {\n" + i.Else.String() + "}\n"
}
return result
}
// Token returns the token of the Evy source program associated with the
// IfStmt node.
func (i *IfStmt) Token() *lexer.Token {
return i.token
}
// Type returns [NONE_TYPE] for IfStmt because an if statement doest not
// have a type.
func (i *IfStmt) Type() *Type {
return NONE_TYPE
}
func (i *IfStmt) alwaysTerminates() bool {
if i.Else == nil || !i.Else.alwaysTerminates() {
return false
}
if !i.IfBlock.alwaysTerminates() {
return false
}
for _, b := range i.ElseIfBlocks {
if !b.alwaysTerminates() {
return false
}
}
return true
}
// WhileStmt is an AST node that represents a while statement, such as
//
// while true
// print "🌞"
// end
//
// WhileStmt implements the [Node] interface.
type WhileStmt struct {
ConditionalBlock
}
// String returns a string representation of the WhileStmt node.
func (w *WhileStmt) String() string {
return "while " + w.ConditionalBlock.String()
}
// Token returns the token of the Evy source program associated with the
// WhileStmt node.
func (w *WhileStmt) Token() *lexer.Token {
return w.token
}
// Type returns [NONE_TYPE] for WhileStmt because a while statement does
// not have a type.
func (w *WhileStmt) Type() *Type {
return NONE_TYPE
}
func (*WhileStmt) alwaysTerminates() bool {
return false
}
// ForStmt is an AST node that represents a for loop. A for loop is a
// statement that repeats a block of code a certain number of times.
// The following code snippet is an example of a for loop:
//
// for n := range 1 10 2
// print n // 1 3 5 7 9
// end
//
// ForStmt implements the [Node] interface.
type ForStmt struct {
token *lexer.Token
LoopVar *Var
Range Node // StepRange or array/map/string expression
Block *BlockStatement
}
// String returns a string representation of the ForStmt node.
func (f *ForStmt) String() string {
header := "for "
if f.LoopVar != nil {
header += f.LoopVar.Name + " := "
}
header += f.Range.String()
return header + " {\n" + f.Block.String() + "}"
}
// Token returns the token of the Evy source program associated with the
// ForStmt node.
func (f *ForStmt) Token() *lexer.Token {
return f.token
}
// Type returns [NONE_TYPE] for ForStmt because a while statement does
// not have a type.
func (f *ForStmt) Type() *Type {
return NONE_TYPE
}
func (*ForStmt) alwaysTerminates() bool {
return false
}
// TypedDeclStmt is an AST node that represents a typed declaration
// statement. A typed declaration statement declares a variable of an
// explicitly specified type, such as n:num.
//
// TypedDeclStmt implements the [Node] interface.
type TypedDeclStmt struct {
token *lexer.Token
Decl *Decl
}
// String returns a string representation of the TypedDeclStmt node.
func (d *TypedDeclStmt) String() string {
return d.Decl.String()
}
// Token returns the token of the Evy source program associated with the
// TypedDeclStmt node.
func (d *TypedDeclStmt) Token() *lexer.Token {
return d.token
}
// Type returns the type of the variable that is declared.
func (d *TypedDeclStmt) Type() *Type {
return d.Decl.Var.T
}
// InferredDeclStmt is an AST node that represents an inferred
// declaration statement. It declares a variable with a type that is
// inferred from the value that is assigned to it. For example: n :=
// 1.
//
// InferredDeclStmt implements the [Node] interface.
type InferredDeclStmt struct {
token *lexer.Token
Decl *Decl
}
// String returns a string representation of the InferredDeclStmt node.
func (d *InferredDeclStmt) String() string {
return d.Decl.String()
}
// Token returns the token of the Evy source program associated with the
// InferredDeclStmt node.
func (d *InferredDeclStmt) Token() *lexer.Token {
return d.token
}
// Type returns the type of the variable that is declared.
func (d *InferredDeclStmt) Type() *Type {
return d.Decl.Var.T
}
// AssignmentStmt is an AST node that represents an assignment
// statement. An assignment statement assigns a value to a variable,
// such as n = 2.
//
// AssignmentStmt implements the [Node] interface.
type AssignmentStmt struct {
token *lexer.Token
Target Node // Variable, index or field expression
Value Node // literal, expression, variable...
}
// String returns a string representation of the AssignmentStmt node.
func (a *AssignmentStmt) String() string {
return a.Target.String() + " = " + a.Value.String()
}
// Token returns the token of the Evy source program associated with the
// AssignmentStmt node.
func (a *AssignmentStmt) Token() *lexer.Token {
return a.token
}
// Type returns the type of the variable that is assigned.
func (a *AssignmentStmt) Type() *Type {
return a.Target.Type()
}
// FuncCallStmt is an AST node that represents a standalone function
// call statement. It is a statement that calls a function without any
// surrounding expressions.
//
// FuncCallStmt implements the [Node] interface.
type FuncCallStmt struct {
token *lexer.Token // The IDENT of the function
FuncCall *FuncCall
}
// String returns a string representation of the FuncCallStmt node.
func (f *FuncCallStmt) String() string {
return f.FuncCall.String()
}
// Token returns the token of the Evy source program associated with the
// FuncCallStmt node.
func (f *FuncCallStmt) Token() *lexer.Token {
return f.token
}
// Type returns the return type of the called function.
func (f *FuncCallStmt) Type() *Type {
return f.FuncCall.FuncDef.ReturnType
}
// ReturnStmt is an AST node that represents a return statement. A
// return statement terminates the execution of a function and can
// return a value. For example:
//
// func square:num n:num
// return n * n
// end
//
// ReturnStmt implements the [Node] interface.
type ReturnStmt struct {
token *lexer.Token
Value Node // literal, expression, variable, ...
T *Type
}
// String returns a string representation of the ReturnStmt node.
func (r *ReturnStmt) String() string {
if r.Value == nil {
return "return"
}
return "return " + r.Value.String()
}
// Token returns the token of the Evy source program associated with the
// ReturnStmt node.
func (r *ReturnStmt) Token() *lexer.Token {
return r.token
}
// Type returns the type of the value returned by the return statement.
func (r *ReturnStmt) Type() *Type {
return r.T
}
func (*ReturnStmt) alwaysTerminates() bool {
return true
}
// BreakStmt is an AST node that represents a break statement. A break
// statement is used to terminate the current loop statement, for
// example:
//
// while true
// break
// end
//
// BreakStmt implements the [Node] interface.
type BreakStmt struct {
token *lexer.Token
}
// String returns a string representation of the eakStmt node.
func (*BreakStmt) String() string {
return "break"
}
// Token returns the token of the Evy source program associated with the
// BreakStmt node.
func (b *BreakStmt) Token() *lexer.Token {
return b.token
}
// Type returns [NONE_TYPE] for BreakStmt because the empty statement
// does not have a type.
func (*BreakStmt) Type() *Type {
return NONE_TYPE
}
func (b *BreakStmt) alwaysTerminates() bool {
return true
}
// BlockStatement is an AST node that represents a block of statements.
// A block of statements is a sequence of statements that are executed
// together, such as those used in [FuncDefStmt] and [IfStmt].
//
// BlockStatement implements the [Node] interface.
type BlockStatement struct {
token *lexer.Token // the NL before the first statement
Statements []Node
alwaysTerms bool
}
// String returns a string representation of the BlockStatement node.
func (b *BlockStatement) String() string {
return newlineList(b.Statements)
}
// Token returns the token of the Evy source program associated with the
// BlockStatement node.
func (b *BlockStatement) Token() *lexer.Token {
return b.token
}
// Type returns [NONE_TYPE] for BlockStatement because a block statement
// does not have a type.
func (b *BlockStatement) Type() *Type {
return NONE_TYPE
}
func (b *BlockStatement) alwaysTerminates() bool {
return b.alwaysTerms
}
// ConditionalBlock is an AST node that represents a conditional block.
// A conditional block is a block of statements that is executed only
// if a certain condition is met. Conditional blocks are used in
// [IfStmt] and [WhileStmt] statements.
//
// ConditionalBlock implements the [Node] interface.
type ConditionalBlock struct {
token *lexer.Token
Condition Node // must be of type bool
Block *BlockStatement
}
// String returns a string representation of the ConditionalBlock node.
func (c *ConditionalBlock) String() string {
condition := "(" + c.Condition.String() + ")"
return condition + " {\n" + c.Block.String() + "}"
}
// Token returns the token of the Evy source program associated with the
// ConditionalBlock node.
func (c *ConditionalBlock) Token() *lexer.Token {
return c.token
}
// Type returns [NONE_TYPE] for ConditionalBlock because a conditional
// block statement does not have a type.
func (c *ConditionalBlock) Type() *Type {
return NONE_TYPE
}
func (c *ConditionalBlock) alwaysTerminates() bool {
return c.Block.alwaysTerminates()
}
// FuncCall is an AST node that represents a function call. It can be
// used either as a standalone statement or as part of an expression.
//
// FuncCall implements the [Node] interface.
type FuncCall struct {
token *lexer.Token // The IDENT of the function
Name string
Arguments []Node
FuncDef *FuncDefStmt
}
// String returns a string representation of the FuncCall node.
func (f *FuncCall) String() string {
s := make([]string, len(f.Arguments))
for i, arg := range f.Arguments {
s[i] = arg.String()
}
args := strings.Join(s, ", ")
return f.Name + "(" + args + ")"
}
// Token returns the token of the Evy source program associated with the
// FuncCall node.
func (f *FuncCall) Token() *lexer.Token {
return f.token
}
// Type returns the return type of the called function.
func (f *FuncCall) Type() *Type {
return f.FuncDef.ReturnType
}
// UnaryExpression is an AST node that represents a unary expression,
// such as: -n.
//
// UnaryExpression implements the [Node] interface.
type UnaryExpression struct {
token *lexer.Token // The unary operation token, e.g. !
Op Operator
Right Node
}
// Token returns the token of the Evy source program associated with the
// UnaryExpression node.
func (u *UnaryExpression) Token() *lexer.Token {
return u.token
}
// String returns a string representation of the UnaryExpression node.
func (u *UnaryExpression) String() string {
return "(" + u.Op.String() + u.Right.String() + ")"
}
// Type returns the type of the UnaryExpression, such as bool or num.
func (u *UnaryExpression) Type() *Type {
return u.Right.Type()
}
// BinaryExpression is an AST node that represents a binary expression.
// A binary expression is an expression that has two operands and an
// operator, such as a + b.
//
// BinaryExpression implements the [Node] interface.
type BinaryExpression struct {
T *Type
token *lexer.Token // The binary operation token, e.g. +
Op Operator
Left Node
Right Node
}
// String returns a string representation of the BinaryExpression node.
func (b *BinaryExpression) String() string {
if b.Op == OP_AND || b.Op == OP_OR {
return "(" + b.Left.String() + " " + b.Op.String() + " " + b.Right.String() + ")"
}
return "(" + b.Left.String() + b.Op.String() + b.Right.String() + ")"
}
// Token returns the token of the Evy source program associated with the
// BinaryExpression node.
func (b *BinaryExpression) Token() *lexer.Token {
return b.token
}
// Type returns the type of the BinaryExpression, such as bool, num or string.
func (b *BinaryExpression) Type() *Type {
return b.T
}
func (b *BinaryExpression) infer() {
if b.T == EMPTY_ARRAY {
b.T = &Type{Name: ARRAY, Sub: ANY_TYPE}
}
}
// IndexExpression is an AST node that represents an indexing
// expression. It accesses the value of an element in an array, map or
// string. For example: array[i].
//
// IndexExpression implements the [Node] interface.
type IndexExpression struct {
T *Type
token *lexer.Token // The [ token
Left Node
Index Node
}
// String returns a string representation of the IndexExpression node.
func (i *IndexExpression) String() string {
return "(" + i.Left.String() + "[" + i.Index.String() + "])"
}
// Token returns the token of the Evy source program associated with the
// IndexExpression node.
func (i *IndexExpression) Token() *lexer.Token {
return i.token
}
// Type returns the type of the IndexExpression, for example num for an
// array of numbers with type []num.
func (i *IndexExpression) Type() *Type {
return i.T
}
// SliceExpression is an AST node
// that represents a slice expression. A slice expression is used
// to access a subsequence of an array or string, such as: array[1:4].
//
// SliceExpression implements the [Node] interface.
type SliceExpression struct {
T *Type
token *lexer.Token // The [ token
Left Node
Start Node
End Node
}
// String returns a string representation of the SliceExpression node.
func (s *SliceExpression) String() string {
start := ""
if s.Start != nil {
start = s.Start.String()
}
end := ""
if s.End != nil {
end = s.End.String()
}
return "(" + s.Left.String() + "[" + start + ":" + end + "])"
}
// Token returns the token of the Evy source program associated with the
// SliceExpression node.
func (s *SliceExpression) Token() *lexer.Token {
return s.token
}
// Type returns the type of the SliceExpression, which is the same type
// as the array that is sliced or string if a string is sliced.
func (s *SliceExpression) Type() *Type {
return s.T
}
// DotExpression is an AST node that represents a field access
// expression. A field access expression is an expression that accesses
// the value of a field in a map, such as person.age.
//
// DotExpression implements the [Node] interface.
type DotExpression struct {
T *Type
token *lexer.Token // The . token
Left Node
Key string // m := { age: 42}; m.age => key: "age"
}
// String returns a string representation of the DotExpression node.
func (d *DotExpression) String() string {
return "(" + d.Left.String() + "." + d.Key + ")"
}
// Token returns the token of the Evy source program associated with the
// DotExpression node.
func (d *DotExpression) Token() *lexer.Token {
return d.token
}
// Type returns the type of the DotExpression, which is the type of the
// map's values. For map := {a: true}, the type of map.a is bool.
func (d *DotExpression) Type() *Type {
return d.T
}
// GroupExpression is an AST node that represents a parenthesized
// expression. It groups together an expression so that it can be
// evaluated as a single unit, such as:(a+b)*3.
//
// GroupExpression implements the [Node] interface.
type GroupExpression struct {
token *lexer.Token
Expr Node
}
// String returns a string representation of the GroupExpression node.
func (d *GroupExpression) String() string {
return d.Expr.String()
}
// Token returns the token of the Evy source program associated with the
// GroupExpression node.
func (d *GroupExpression) Token() *lexer.Token {
return d.token
}
// Type returns the type of the GroupExpression, for example num for
// 2*(3+4).
func (d *GroupExpression) Type() *Type {
return d.Expr.Type()
}
func (d *GroupExpression) infer() {
if d.Type() == EMPTY_ARRAY {
d.Expr.(inferrer).infer()
}
}
// TypeAssertion is an AST node that represents a type assertion
// expression. A type assertion expression is used to enforce the
// specific type of an any value. For example:
//
// val:any
// val = 1
// print val.(num)+2 // 3
//
// TypeAssertion implements the [Node] interface.
type TypeAssertion struct {
T *Type
token *lexer.Token
Left Node
}
// String returns a string representation of the TypeAssertion node.
func (t *TypeAssertion) String() string {
return "(" + t.Left.String() + "." + "(" + t.T.String() + ")" + ")"
}
// Token returns the token of the Evy source program associated with the
// TypeAssertion node.
func (t *TypeAssertion) Token() *lexer.Token {
return t.token
}
// Type returns the type of the TypeAssertion, which is the type that is
// asserted.
func (t *TypeAssertion) Type() *Type {
return t.T
}
// Decl is an AST node that represents a variable declaration. A
// variable declaration is a statement that creates a new variable and
// assigns it a value. Variable declarations are used in
// [TypedDeclStmt] and [InferredDeclStmt] statements.
//
// Decl implements the [Node] interface.
type Decl struct {
token *lexer.Token
Var *Var
Value Node // literal, expression, variable, ...
}
// String returns a string representation of the Decl node.
func (d *Decl) String() string {
if d.Value == nil {
return d.Var.String()
}
return d.Var.String() + "=" + d.Value.String()
}
// Token returns the token of the Evy source program associated with the
// Decl node.
func (d *Decl) Token() *lexer.Token {
return d.token
}
// Type returns the type of the variable that is declared.
func (d *Decl) Type() *Type {
return d.Var.T
}
// StepRange is an AST node that represents a step range in a for loop.
// A step range is used to iterate over a sequence of numbers, starting
// from the first number and ending with the last number, incrementing
// by the step size. For example:
//
// for n := range 1 10 2
// print n // 1 3 5 7 9
// end
//
// StepRange implements the [Node] interface.
type StepRange struct {
token *lexer.Token
Start Node // num expression or nil
Stop Node // num expression
Step Node // num expression or nil
}
// String returns a string representation of the StepRange node.
func (s *StepRange) String() string {
start := "0"
if s.Start != nil {
start = s.Start.String()
}
stop := s.Stop.String()
step := "1"
if s.Step != nil {
step = s.Step.String()
}
return start + " " + stop + " " + step
}
// Token returns the token of the Evy source program associated with the
// StepRange node.
func (s *StepRange) Token() *lexer.Token {
return s.token
}
// Type returns [NUM_TYPE] for StepRange as a step range always
// represents a set of number value.
func (s *StepRange) Type() *Type {
return NUM_TYPE
}
// Var is an AST node that represents a variable, its name and type but
// not its value.
//
// Var implements the [Node] interface.
type Var struct {
token *lexer.Token
Name string
T *Type
isUsed bool
}
// String returns a string representation of the Var node.
func (v *Var) String() string {
return v.Name
}
// Token returns the token of the Evy source program associated with the
// Var node.
func (v *Var) Token() *lexer.Token {
return v.token
}
// Type returns the type of the variable.
func (v *Var) Type() *Type {
return v.T
}
// BoolLiteral is an AST node that represents a boolean literal. A
// boolean literal is a value that can be either true or false.
//
// BoolLiteral implements the [Node] interface.
type BoolLiteral struct {
token *lexer.Token
Value bool
}
// String returns a string representation of the BoolLiteral node.
func (b *BoolLiteral) String() string {
return strconv.FormatBool(b.Value)
}
// Token returns the token of the Evy source program associated with the
// BoolLiteral node.
func (b *BoolLiteral) Token() *lexer.Token {
return b.token
}
// Type returns [BOOL_TYPE] for BoolLiteral as a bool literal always has
// the bool type.
func (b *BoolLiteral) Type() *Type {
return BOOL_TYPE
}
// StringLiteral is an AST node that represents a string literal. A
// string literal is a sequence of characters enclosed in double
// quotes, such as "abc".
//
// StringLiteral implements the [Node] interface.
type StringLiteral struct {
token *lexer.Token
Value string
}
// String returns a string representation of the StringLiteral node.
func (s *StringLiteral) String() string {
return fmt.Sprintf("%q", s.Value)
}
// Token returns the token of the Evy source program associated with the
// StringLiteral node.
func (s *StringLiteral) Token() *lexer.Token {
return s.token
}
// Type returns [STRING_TYPE] for StringLiteral as a string literal
// always has the string type.
func (s *StringLiteral) Type() *Type {
return STRING_TYPE
}
// NumLiteral is an AST node that represents a numeric literal. A
// numeric literal is a number, such as 12 or 34.567.
//
// NumLiteral implements the [Node] interface.
type NumLiteral struct {
token *lexer.Token
Value float64
}
// String returns a string representation of the NumLiteral node.
func (n *NumLiteral) String() string {
return strconv.FormatFloat(n.Value, 'f', -1, 64)
}
// Token returns the token of the Evy source program associated with the
// NumLiteral node.
func (n *NumLiteral) Token() *lexer.Token {
return n.token
}
// Type returns [NUM_TYPE] for NumLiteral as a number literal always has
// the num type.
func (n *NumLiteral) Type() *Type {
return NUM_TYPE
}
// ArrayLiteral is an AST node that represents an array literal, such
// as: [1 2 3].
//
// ArrayLiteral implements the [Node] interface.
type ArrayLiteral struct {
token *lexer.Token
Elements []Node
T *Type
}