Return starlarkNodes from the functions that parse them

Currently, mk2rbc is structured around having a global
"receiver" object that accepts all starlarkNodes. As soon
as they are parsed they are added to the receiver.

Returning the parsed nodes to the calling function is more
flexible, as it allows the calling function to restructure
them as necessary. This is the first step to supporting
complicated statements involving $(eval), such as
`$(foreach v,$(MY_LIST),$(eval MY_LIST_2 += $(v)))`

Test: go test
Change-Id: Ia194123cf090d2b9559a25b975ccbc5749357cc0
This commit is contained in:
Cole Faust
2022-01-31 15:48:29 -08:00
parent 7940c6a5b5
commit dd569aea07
3 changed files with 104 additions and 189 deletions

View File

@@ -255,29 +255,17 @@ type switchCase struct {
nodes []starlarkNode
}
func (cb *switchCase) newNode(node starlarkNode) {
cb.nodes = append(cb.nodes, node)
}
func (cb *switchCase) emit(gctx *generationContext) {
cb.gate.emit(gctx)
gctx.indentLevel++
hasStatements := false
emitNode := func(node starlarkNode) {
for _, node := range cb.nodes {
if _, ok := node.(*commentNode); !ok {
hasStatements = true
}
node.emit(gctx)
}
if len(cb.nodes) > 0 {
emitNode(cb.nodes[0])
for _, node := range cb.nodes[1:] {
emitNode(node)
}
if !hasStatements {
gctx.emitPass()
}
} else {
if !hasStatements {
gctx.emitPass()
}
gctx.indentLevel--
@@ -288,22 +276,8 @@ type switchNode struct {
ssCases []*switchCase
}
func (ssw *switchNode) newNode(node starlarkNode) {
switch br := node.(type) {
case *switchCase:
ssw.ssCases = append(ssw.ssCases, br)
default:
panic(fmt.Errorf("expected switchCase node, got %t", br))
}
}
func (ssw *switchNode) emit(gctx *generationContext) {
if len(ssw.ssCases) == 0 {
gctx.emitPass()
} else {
ssw.ssCases[0].emit(gctx)
for _, ssCase := range ssw.ssCases[1:] {
ssCase.emit(gctx)
}
for _, ssCase := range ssw.ssCases {
ssCase.emit(gctx)
}
}