Skip to content

Commit

Permalink
chore(gnovm): use blank identifier constants (gnolang#2158)
Browse files Browse the repository at this point in the history
Replaces "_" with blank identifier constants.
  • Loading branch information
deelawn authored May 28, 2024
1 parent 77071e4 commit 055018a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Flds(args ...interface{}) FieldTypeExprs {

func Recv(n, t interface{}) FieldTypeExpr {
if n == "" {
n = "_"
n = blankIdentifier
}
return FieldTypeExpr{
Name: N(n),
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func checkDuplicates(fset *FileSet) bool {
name = d.Name
case *ValueDecl:
for _, nx := range d.NameExprs {
if nx.Name == "_" {
if nx.Name == blankIdentifier {
continue
}
if _, ok := defined[nx.Name]; ok {
Expand All @@ -327,7 +327,7 @@ func checkDuplicates(fset *FileSet) bool {
default:
continue
}
if name == "_" {
if name == blankIdentifier {
continue
}
if _, ok := defined[name]; ok {
Expand Down
10 changes: 5 additions & 5 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ type ValueDecl struct {
func (x *ValueDecl) GetDeclNames() []Name {
ns := make([]Name, 0, len(x.NameExprs))
for _, nx := range x.NameExprs {
if nx.Name == "_" {
if nx.Name == blankIdentifier {
// ignore
} else {
ns = append(ns, nx.Name)
Expand All @@ -1031,7 +1031,7 @@ type TypeDecl struct {
}

func (x *TypeDecl) GetDeclNames() []Name {
if x.NameExpr.Name == "_" {
if x.NameExpr.Name == blankIdentifier {
return nil // ignore
} else {
return []Name{x.NameExpr.Name}
Expand Down Expand Up @@ -1588,8 +1588,8 @@ func (sb *StaticBlock) GetParentNode(store Store) BlockNode {
// Implements BlockNode.
// As a side effect, notes externally defined names.
func (sb *StaticBlock) GetPathForName(store Store, n Name) ValuePath {
if n == "_" {
return NewValuePathBlock(0, 0, "_")
if n == blankIdentifier {
return NewValuePathBlock(0, 0, blankIdentifier)
}
// Check local.
gen := 1
Expand Down Expand Up @@ -1780,7 +1780,7 @@ func (sb *StaticBlock) Define2(isConst bool, n Name, st Type, tv TypedValue) {
if tv.T == nil && tv.V != nil {
panic("StaticBlock.Define2() requires .T if .V is set")
}
if n == "_" {
if n == blankIdentifier {
return // ignore
}
idx, exists := sb.GetLocalIndex(n)
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/gnolang/op_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (m *Machine) doOpEval() {
m.PopExpr()
switch x.Kind {
case INT:
x.Value = strings.ReplaceAll(x.Value, "_", "")
x.Value = strings.ReplaceAll(x.Value, blankIdentifier, "")
// temporary optimization
bi := big.NewInt(0)
// TODO optimize.
Expand Down Expand Up @@ -84,7 +84,7 @@ func (m *Machine) doOpEval() {
V: BigintValue{V: bi},
})
case FLOAT:
x.Value = strings.ReplaceAll(x.Value, "_", "")
x.Value = strings.ReplaceAll(x.Value, blankIdentifier, "")

if reFloat.MatchString(x.Value) {
value := x.Value
Expand Down
34 changes: 18 additions & 16 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/gnolang/gno/tm2/pkg/errors"
)

const blankIdentifer string = "_"
const (
blankIdentifier = "_"
)

// In the case of a *FileSet, some declaration steps have to happen
// in a restricted parallel way across all the files.
Expand Down Expand Up @@ -191,7 +193,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
var defined bool
for _, lx := range n.Lhs {
ln := lx.(*NameExpr).Name
if ln == "_" {
if ln == blankIdentifier {
// ignore.
} else {
_, ok := last.GetLocalIndex(ln)
Expand Down Expand Up @@ -235,7 +237,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
case *FuncTypeExpr:
for i := range n.Params {
p := &n.Params[i]
if p.Name == "" || p.Name == "_" {
if p.Name == "" || p.Name == blankIdentifier {
// create a hidden var with leading dot.
// NOTE: document somewhere.
pn := fmt.Sprintf(".arg_%d", i)
Expand All @@ -244,7 +246,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
}
for i := range n.Results {
r := &n.Results[i]
if r.Name == "_" {
if r.Name == blankIdentifier {
// create a hidden var with leading dot.
// NOTE: document somewhere.
rn := fmt.Sprintf(".res_%d", i)
Expand Down Expand Up @@ -683,8 +685,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
}
// specific and general cases
switch n.Name {
case "_":
n.Path = NewValuePathBlock(0, 0, "_")
case blankIdentifier:
n.Path = NewValuePathBlock(0, 0, blankIdentifier)
return n, TRANS_CONTINUE
case "iota":
pd := lastDecl(ns)
Expand Down Expand Up @@ -1246,7 +1248,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
}

// Type assertions on the blank identifier are illegal.
if nx, ok := n.X.(*NameExpr); ok && string(nx.Name) == blankIdentifer {
if nx, ok := n.X.(*NameExpr); ok && string(nx.Name) == blankIdentifier {
panic("cannot use _ as value or type")
}

Expand Down Expand Up @@ -1929,8 +1931,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
pn := fn.GetParentNode(nil).(*PackageNode)
for i := 0; i < numNames; i++ {
nx := &n.NameExprs[i]
if nx.Name == "_" {
nx.Path = NewValuePathBlock(0, 0, "_")
if nx.Name == blankIdentifier {
nx.Path = NewValuePathBlock(0, 0, blankIdentifier)
} else {
pn.Define2(n.Const, nx.Name, sts[i], tvs[i])
nx.Path = last.GetPathForName(nil, nx.Name)
Expand All @@ -1939,8 +1941,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
} else {
for i := 0; i < numNames; i++ {
nx := &n.NameExprs[i]
if nx.Name == "_" {
nx.Path = NewValuePathBlock(0, 0, "_")
if nx.Name == blankIdentifier {
nx.Path = NewValuePathBlock(0, 0, blankIdentifier)
} else {
last.Define2(n.Const, nx.Name, sts[i], tvs[i])
nx.Path = last.GetPathForName(nil, nx.Name)
Expand Down Expand Up @@ -3054,7 +3056,7 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De
// NOTE: unlike the *ValueDecl case, this case doesn't
// preprocess d itself (only d.Type).
if cd.IsMethod {
if cd.Recv.Name == "" || cd.Recv.Name == "_" {
if cd.Recv.Name == "" || cd.Recv.Name == blankIdentifier {
// create a hidden var with leading dot.
// NOTE: document somewhere.
cd.Recv.Name = ".recv"
Expand Down Expand Up @@ -3173,7 +3175,7 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) {
}
if d.Name == "" { // use default
d.Name = pv.PkgName
} else if d.Name == "_" { // no definition
} else if d.Name == blankIdentifier { // no definition
return
} else if d.Name == "." { // dot import
panic("dot imports not allowed in Gno")
Expand All @@ -3200,8 +3202,8 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) {
}
for i := 0; i < len(d.NameExprs); i++ {
nx := &d.NameExprs[i]
if nx.Name == "_" {
nx.Path.Name = "_"
if nx.Name == blankIdentifier {
nx.Path.Name = blankIdentifier
} else {
last2 := skipFile(last)
last2.Predefine(d.Const, nx.Name)
Expand Down Expand Up @@ -3381,7 +3383,7 @@ func constUntypedBigint(source Expr, i64 int64) *ConstExpr {
}

func fillNameExprPath(last BlockNode, nx *NameExpr, isDefineLHS bool) {
if nx.Name == "_" {
if nx.Name == blankIdentifier {
// Blank name has no path; caller error.
panic("should not happen")
}
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,7 @@ func (b *Block) GetPointerToInt(store Store, index int) PointerValue {
func (b *Block) GetPointerTo(store Store, path ValuePath) PointerValue {
if path.IsBlockBlankPath() {
if debug {
if path.Name != "_" {
if path.Name != blankIdentifier {
panic(fmt.Sprintf(
"zero value path is reserved for \"_\", but got %s",
path.Name))
Expand Down

0 comments on commit 055018a

Please sign in to comment.