Move variable assignment handling to generation context

This allows the parsing code to be cleaner, as it
doesn't have to care about variable assignments.

Bug: 228518745
Test: go test
Change-Id: I33425c2fb51acab4901bfa82a53d337b75210f8e
This commit is contained in:
Cole Faust
2022-04-07 13:59:24 -07:00
parent 40f8c75752
commit f06326648b
5 changed files with 83 additions and 99 deletions

View File

@@ -196,7 +196,6 @@ type assignmentNode struct {
flavor assignmentFlavor
location ErrorLocation
isTraced bool
previous *assignmentNode
}
func (asgn *assignmentNode) emit(gctx *generationContext) {
@@ -209,7 +208,7 @@ func (asgn *assignmentNode) emit(gctx *generationContext) {
gctx.newLine()
gctx.tracedCount++
gctx.writef(`print("%s.%d: %s := ", `, gctx.starScript.mkFile, gctx.tracedCount, asgn.lhs.name())
asgn.lhs.emitGet(gctx, true)
asgn.lhs.emitGet(gctx)
gctx.writef(")")
}
}
@@ -271,6 +270,7 @@ type switchCase struct {
func (cb *switchCase) emit(gctx *generationContext) {
cb.gate.emit(gctx)
gctx.indentLevel++
gctx.pushVariableAssignments()
hasStatements := false
for _, node := range cb.nodes {
if _, ok := node.(*commentNode); !ok {
@@ -282,6 +282,7 @@ func (cb *switchCase) emit(gctx *generationContext) {
gctx.emitPass()
}
gctx.indentLevel--
gctx.popVariableAssignments()
}
// A single complete if ... elseif ... else ... endif sequences
@@ -302,6 +303,7 @@ type foreachNode struct {
}
func (f *foreachNode) emit(gctx *generationContext) {
gctx.pushVariableAssignments()
gctx.newLine()
gctx.writef("for %s in ", f.varName)
f.list.emit(gctx)
@@ -318,4 +320,5 @@ func (f *foreachNode) emit(gctx *generationContext) {
gctx.emitPass()
}
gctx.indentLevel--
gctx.popVariableAssignments()
}