Skip to content

Commit f1572f1

Browse files
authored
Support for make, go, defer, and channels (deepnoodle-ai#178)
1 parent bb550c2 commit f1572f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1735
-236
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bench:
1414
# https://code.visualstudio.com/api/working-with-extensions/publishing-extension#packaging-extensions
1515
.PHONY: install-tools
1616
install-tools:
17-
npm install -g vsce
17+
npm install -g vsce typescript
1818

1919
.PHONY: extension-login
2020
extension-login:

ast/expressions.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,35 @@ func (r *Range) String() string {
596596
out.WriteString(r.container.String())
597597
return out.String()
598598
}
599+
600+
// Receive is an expression node that describes receiving from a channel
601+
type Receive struct {
602+
// the "<-" token
603+
token token.Token
604+
605+
// the channel to receive from
606+
channel Node
607+
}
608+
609+
// NewReceive creates a new Receive node.
610+
func NewReceive(token token.Token, channel Node) *Receive {
611+
return &Receive{token: token, channel: channel}
612+
}
613+
614+
func (r *Receive) ExpressionNode() {}
615+
616+
func (r *Receive) IsExpression() bool { return true }
617+
618+
func (r *Receive) Token() token.Token { return r.token }
619+
620+
func (r *Receive) Literal() string { return r.token.Literal }
621+
622+
func (r *Receive) Channel() Node { return r.channel }
623+
624+
func (r *Receive) String() string {
625+
var out bytes.Buffer
626+
out.WriteString(r.Literal())
627+
out.WriteString(" ")
628+
out.WriteString(r.channel.String())
629+
return out.String()
630+
}

ast/statements.go

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ast
22

33
import (
44
"bytes"
5+
"fmt"
56
"strings"
67

78
"github.com/risor-io/risor/token"
@@ -146,9 +147,9 @@ func (c *Const) String() string {
146147
return out.String()
147148
}
148149

149-
// Control defines a return, break, or continue statement.
150+
// Branch defines a break or continue statement.
150151
type Control struct {
151-
// "return", "break", or "continue"
152+
// "break", or "continue"
152153
token token.Token
153154

154155
// optional value, for return statements
@@ -183,6 +184,39 @@ func (c *Control) String() string {
183184
return out.String()
184185
}
185186

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+
186220
// Block is a node that holds a sequence of statements. This is used to
187221
// represent the body of a function, loop, or a conditional.
188222
type Block struct {
@@ -211,10 +245,8 @@ func (b *Block) EndsWithReturn() bool {
211245
return false
212246
}
213247
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
218250
}
219251

220252
func (b *Block) String() string {
@@ -519,3 +551,81 @@ func (e *SetAttr) String() string {
519551
out.WriteString(e.value.String())
520552
return out.String()
521553
}
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

Comments
 (0)