Skip to content

Commit

Permalink
refactor(gnovm): remove TRANS_BREAK from transcriber (#3626)
Browse files Browse the repository at this point in the history
was unused until now, and it looks redundant if we have TRANS_SKIP
anyway.
  • Loading branch information
thehowl authored Feb 11, 2025
1 parent 31fcf6a commit 07e3c49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 89 deletions.
7 changes: 3 additions & 4 deletions gnovm/pkg/gnolang/string_methods.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 25 additions & 85 deletions gnovm/pkg/gnolang/transcribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type (
const (
TRANS_CONTINUE TransCtrl = iota
TRANS_SKIP
TRANS_BREAK
TRANS_EXIT
)

Expand Down Expand Up @@ -101,7 +100,7 @@ const (
TRANS_IMPORT_PATH
TRANS_CONST_TYPE
TRANS_CONST_VALUE
TRANS_VAR_NAME // XXX stringer
TRANS_VAR_NAME
TRANS_VAR_TYPE
TRANS_VAR_VALUE
TRANS_TYPE_TYPE
Expand All @@ -113,8 +112,6 @@ const (
// - TRANS_SKIP to break out of the
// ENTER,CHILDS1,[BLOCK,CHILDS2]?,LEAVE sequence for that node,
// i.e. skipping (the rest of) it;
// - TRANS_BREAK to break out of looping in CHILDS1 or CHILDS2,
// but still perform TRANS_LEAVE.
// - TRANS_EXIT to stop traversing altogether.
//
// Do not mutate ns.
Expand Down Expand Up @@ -168,9 +165,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
}
for idx := range cnn.Args {
cnn.Args[idx] = transcribe(t, nns, TRANS_CALL_ARG, idx, cnn.Args[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand Down Expand Up @@ -248,16 +243,12 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
k, v := kvx.Key, kvx.Value
if k != nil {
k = transcribe(t, nns, TRANS_COMPOSITE_KEY, idx, k, &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
v = transcribe(t, nns, TRANS_COMPOSITE_VALUE, idx, v, &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
cnn.Elts[idx] = KeyValueExpr{Key: k, Value: v}
Expand All @@ -269,9 +260,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
}
for idx := range cnn.HeapCaptures {
cnn.HeapCaptures[idx] = *(transcribe(t, nns, TRANS_FUNCLIT_HEAP_CAPTURE, idx, &cnn.HeapCaptures[idx], &c).(*NameExpr))
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -285,9 +274,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_FUNCLIT_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand Down Expand Up @@ -321,9 +308,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
case *InterfaceTypeExpr:
for idx := range cnn.Methods {
cnn.Methods[idx] = *transcribe(t, nns, TRANS_INTERFACETYPE_METHOD, idx, &cnn.Methods[idx], &c).(*FieldTypeExpr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -341,9 +326,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
}
for idx := range cnn.Results {
cnn.Results[idx] = *transcribe(t, nns, TRANS_FUNCTYPE_RESULT, idx, &cnn.Results[idx], &c).(*FieldTypeExpr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -359,9 +342,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
case *StructTypeExpr:
for idx := range cnn.Fields {
cnn.Fields[idx] = *transcribe(t, nns, TRANS_STRUCTTYPE_FIELD, idx, &cnn.Fields[idx], &c).(*FieldTypeExpr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -373,17 +354,13 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
case *AssignStmt:
for idx := range cnn.Lhs {
cnn.Lhs[idx] = transcribe(t, nns, TRANS_ASSIGN_LHS, idx, cnn.Lhs[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
for idx := range cnn.Rhs {
cnn.Rhs[idx] = transcribe(t, nns, TRANS_ASSIGN_RHS, idx, cnn.Rhs[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -398,9 +375,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_BLOCK_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -409,9 +384,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_DECL_BODY, idx, cnn.Body[idx], &c).(SimpleDeclStmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand Down Expand Up @@ -455,9 +428,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_FOR_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand Down Expand Up @@ -506,9 +477,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_IF_CASE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand Down Expand Up @@ -544,18 +513,14 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_RANGE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
case *ReturnStmt:
for idx := range cnn.Results {
cnn.Results[idx] = transcribe(t, nns, TRANS_RETURN_RESULT, idx, cnn.Results[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -564,9 +529,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
case *SelectStmt:
for idx := range cnn.Cases {
cnn.Cases[idx] = *transcribe(t, nns, TRANS_SELECT_CASE, idx, &cnn.Cases[idx], &c).(*SelectCaseStmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -585,9 +548,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_SELECTCASE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand Down Expand Up @@ -632,9 +593,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
}
for idx := range cnn.Clauses {
cnn.Clauses[idx] = *transcribe(t, nns, TRANS_SWITCH_CASE, idx, &cnn.Clauses[idx], &c).(*SwitchClauseStmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -652,18 +611,14 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
}
for idx := range cnn.Cases {
cnn.Cases[idx] = transcribe(t, nns, TRANS_SWITCHCASE_CASE, idx, cnn.Cases[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_SWITCHCASE_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -688,9 +643,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
// iterate over Body; its length can change if a statement is decomposed.
for idx := 0; idx < len(cnn.Body); idx++ {
cnn.Body[idx] = transcribe(t, nns, TRANS_FUNC_BODY, idx, cnn.Body[idx], &c).(Stmt)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -709,9 +662,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
}
for idx := range cnn.Values {
cnn.Values[idx] = transcribe(t, nns, TRANS_VAR_VALUE, idx, cnn.Values[idx], &c).(Expr)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand All @@ -730,9 +681,7 @@ func transcribe(t Transform, ns []Node, ftype TransField, index int, n Node, nc
}
for idx := range cnn.Decls {
cnn.Decls[idx] = transcribe(t, nns, TRANS_FILE_BODY, idx, cnn.Decls[idx], &c).(Decl)
if isBreak(c) {
break
} else if isStopOrSkip(nc, c) {
if isStopOrSkip(nc, c) {
return
}
}
Expand Down Expand Up @@ -768,12 +717,3 @@ func isStopOrSkip(oldnc *TransCtrl, nc TransCtrl) (stop bool) {
panic("should not happen")
}
}

// returns true if transcribe() should break (a loop).
func isBreak(nc TransCtrl) (brek bool) {
if nc == TRANS_BREAK {
return true
} else {
return false
}
}

0 comments on commit 07e3c49

Please sign in to comment.