Skip to content

Commit

Permalink
feat(parser:net): switch
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Nov 5, 2024
1 parent 1fb78fa commit cd39352
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
34 changes: 34 additions & 0 deletions internal/compiler/parser/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func parseSingleReceiverSide(
deferredConn := actx.DeferredConn()
portAddr := actx.PortAddr()
chainedConn := actx.ChainedNormConn()
switchStmt := actx.SwitchStmt()

meta := core.Meta{
Text: actx.GetText(),
Expand All @@ -204,6 +205,8 @@ func parseSingleReceiverSide(
return parseChainedConnExpr(chainedConn, meta)
case portAddr != nil:
return parsePortAddrReceiver(portAddr)
case switchStmt != nil:
return parseSwitchStmt(switchStmt)
default:
return src.ConnectionReceiver{}, &compiler.Error{
Message: "missing receiver side",
Expand All @@ -212,6 +215,37 @@ func parseSingleReceiverSide(
}
}

func parseSwitchStmt(
switchStmt generated.ISwitchStmtContext,
) (src.ConnectionReceiver, *compiler.Error) {
meta := core.Meta{
Text: switchStmt.GetText(),
Start: core.Position{
Line: switchStmt.GetStart().GetLine(),
Column: switchStmt.GetStart().GetColumn(),
},
Stop: core.Position{
Line: switchStmt.GetStop().GetLine(),
Column: switchStmt.GetStop().GetColumn(),
},
}

unparsedCases := switchStmt.AllNormConnDef()
cases := make([]src.NormalConnection, 0, len(unparsedCases))
for _, connDef := range unparsedCases {
parsedConn, err := parseNormConn(connDef)
if err != nil {
return src.ConnectionReceiver{}, err
}
cases = append(cases, *parsedConn.Normal)
}

return src.ConnectionReceiver{
Switch: cases,
Meta: meta,
}, nil
}

func parseChainedConnExpr(
actx generated.IChainedNormConnContext,
connMeta core.Meta,
Expand Down
11 changes: 6 additions & 5 deletions internal/compiler/sourcecode/sourcecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,11 @@ type ArrayBypassConnection struct {
}

type ConnectionReceiver struct {
PortAddr *PortAddr `json:"portAddr,omitempty"`
DeferredConnection *Connection `json:"deferredConnection,omitempty"`
ChainedConnection *Connection `json:"chainedConnection,omitempty"`
Meta core.Meta `json:"meta,omitempty"`
PortAddr *PortAddr `json:"portAddr,omitempty"`
DeferredConnection *Connection `json:"deferredConnection,omitempty"`
ChainedConnection *Connection `json:"chainedConnection,omitempty"`
Switch []NormalConnection `json:"switch,omitempty"`
Meta core.Meta `json:"meta,omitempty"`
}

type ConnectionSideSelectors []string
Expand All @@ -324,10 +325,10 @@ type ConnectionSender struct {
PortAddr *PortAddr `json:"portAddr,omitempty"`
Const *Const `json:"const,omitempty"`
Range *Range `json:"range,omitempty"`
StructSelector []string `json:"selector,omitempty"`
Unary *Unary `json:"unary,omitempty"`
Binary *Binary `json:"binary,omitempty"`
Ternary *Ternary `json:"ternary,omitempty"`
StructSelector []string `json:"selector,omitempty"`
Meta core.Meta `json:"meta,omitempty"`
}

Expand Down

0 comments on commit cd39352

Please sign in to comment.