@@ -2,6 +2,7 @@ package ast
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"strings"
6
7
7
8
"github.com/risor-io/risor/token"
@@ -146,9 +147,9 @@ func (c *Const) String() string {
146
147
return out .String ()
147
148
}
148
149
149
- // Control defines a return, break, or continue statement.
150
+ // Branch defines a break or continue statement.
150
151
type Control struct {
151
- // "return", " break", or "continue"
152
+ // "break", or "continue"
152
153
token token.Token
153
154
154
155
// optional value, for return statements
@@ -183,6 +184,39 @@ func (c *Control) String() string {
183
184
return out .String ()
184
185
}
185
186
187
+ // Return defines a return statement.
188
+ type Return struct {
189
+ // "return"
190
+ token token.Token
191
+
192
+ // optional value
193
+ value Expression
194
+ }
195
+
196
+ // NewReturn creates a new Return node.
197
+ func NewReturn (token token.Token , value Expression ) * Return {
198
+ return & Return {token : token , value : value }
199
+ }
200
+
201
+ func (r * Return ) StatementNode () {}
202
+
203
+ func (r * Return ) IsExpression () bool { return false }
204
+
205
+ func (r * Return ) Token () token.Token { return r .token }
206
+
207
+ func (r * Return ) Literal () string { return r .token .Literal }
208
+
209
+ func (r * Return ) Value () Expression { return r .value }
210
+
211
+ func (r * Return ) String () string {
212
+ var out bytes.Buffer
213
+ out .WriteString (r .Literal ())
214
+ if r .value != nil {
215
+ out .WriteString (" " + r .value .Literal ())
216
+ }
217
+ return out .String ()
218
+ }
219
+
186
220
// Block is a node that holds a sequence of statements. This is used to
187
221
// represent the body of a function, loop, or a conditional.
188
222
type Block struct {
@@ -211,10 +245,8 @@ func (b *Block) EndsWithReturn() bool {
211
245
return false
212
246
}
213
247
last := b .statements [count - 1 ]
214
- if cntrl , ok := last .(* Control ); ok {
215
- return cntrl .IsReturn ()
216
- }
217
- return false
248
+ _ , isReturn := last .(* Return )
249
+ return isReturn
218
250
}
219
251
220
252
func (b * Block ) String () string {
@@ -519,3 +551,81 @@ func (e *SetAttr) String() string {
519
551
out .WriteString (e .value .String ())
520
552
return out .String ()
521
553
}
554
+
555
+ // A Go statement node represents a go statement.
556
+ type Go struct {
557
+ token token.Token
558
+ call Expression
559
+ }
560
+
561
+ // NewGo creates a new Go statement node.
562
+ func NewGo (token token.Token , call Expression ) * Go {
563
+ return & Go {token : token , call : call }
564
+ }
565
+
566
+ func (g * Go ) StatementNode () {}
567
+
568
+ func (g * Go ) IsExpression () bool { return false }
569
+
570
+ func (g * Go ) Token () token.Token { return g .token }
571
+
572
+ func (g * Go ) Literal () string { return g .token .Literal }
573
+
574
+ func (g * Go ) Call () Expression { return g .call }
575
+
576
+ func (g * Go ) String () string {
577
+ return fmt .Sprintf ("go %s" , g .call .String ())
578
+ }
579
+
580
+ // A Defer statement node represents a defer statement.
581
+ type Defer struct {
582
+ token token.Token
583
+ call Expression
584
+ }
585
+
586
+ // NewDefer creates a new Defer node.
587
+ func NewDefer (token token.Token , call Expression ) * Defer {
588
+ return & Defer {token : token , call : call }
589
+ }
590
+
591
+ func (d * Defer ) StatementNode () {}
592
+
593
+ func (d * Defer ) IsExpression () bool { return false }
594
+
595
+ func (d * Defer ) Token () token.Token { return d .token }
596
+
597
+ func (d * Defer ) Literal () string { return d .token .Literal }
598
+
599
+ func (d * Defer ) Call () Expression { return d .call }
600
+
601
+ func (d * Defer ) String () string {
602
+ return fmt .Sprintf ("defer %s" , d .call .String ())
603
+ }
604
+
605
+ // A Send statement node represents a channel send operation.
606
+ type Send struct {
607
+ token token.Token
608
+ channel Expression
609
+ value Expression
610
+ }
611
+
612
+ // NewSend creates a new Send node.
613
+ func NewSend (token token.Token , channel , value Expression ) * Send {
614
+ return & Send {token : token , channel : channel , value : value }
615
+ }
616
+
617
+ func (s * Send ) StatementNode () {}
618
+
619
+ func (s * Send ) IsExpression () bool { return false }
620
+
621
+ func (s * Send ) Token () token.Token { return s .token }
622
+
623
+ func (s * Send ) Literal () string { return s .token .Literal }
624
+
625
+ func (s * Send ) Channel () Expression { return s .channel }
626
+
627
+ func (s * Send ) Value () Expression { return s .value }
628
+
629
+ func (s * Send ) String () string {
630
+ return fmt .Sprintf ("%s <- %s" , s .channel .String (), s .value .String ())
631
+ }
0 commit comments