Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use blank identifier constants #2158

Merged
merged 6 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 == blankNameIdentifer {
continue
}
if _, ok := defined[nx.Name]; ok {
Expand All @@ -327,7 +327,7 @@ func checkDuplicates(fset *FileSet) bool {
default:
continue
}
if name == "_" {
if name == blankNameIdentifer {
continue
}
if _, ok := defined[name]; ok {
Expand Down
12 changes: 6 additions & 6 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 == blankNameIdentifer {
// 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 == blankNameIdentifer {
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 == blankNameIdentifer {
return NewValuePathBlock(0, 0, blankNameIdentifer)
}
// 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 == blankNameIdentifer {
return // ignore
}
idx, exists := sb.GetLocalIndex(n)
Expand Down Expand Up @@ -1976,7 +1976,7 @@ func (vp ValuePath) Validate() {
panic("uverse value path must have depth 0")
}
case VPBlock:
// 0 ok ("_" blank)
// 0 ok (blankIdentifier blank)
deelawn marked this conversation as resolved.
Show resolved Hide resolved
case VPField:
if vp.Depth > 1 {
panic("field value path must have depth 0 or 1")
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
35 changes: 19 additions & 16 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"github.com/gnolang/gno/tm2/pkg/errors"
)

const blankIdentifer string = "_"
const (
blankIdentifier string = "_"
blankNameIdentifer Name = "_"
deelawn marked this conversation as resolved.
Show resolved Hide resolved
)

// 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 +194,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 == blankNameIdentifer {
// ignore.
} else {
_, ok := last.GetLocalIndex(ln)
Expand Down Expand Up @@ -235,7 +238,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 == blankNameIdentifer {
// create a hidden var with leading dot.
// NOTE: document somewhere.
pn := fmt.Sprintf(".arg_%d", i)
Expand All @@ -244,7 +247,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 == blankNameIdentifer {
// create a hidden var with leading dot.
// NOTE: document somewhere.
rn := fmt.Sprintf(".res_%d", i)
Expand Down Expand Up @@ -683,8 +686,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
}
// specific and general cases
switch n.Name {
case "_":
n.Path = NewValuePathBlock(0, 0, "_")
case blankNameIdentifer:
n.Path = NewValuePathBlock(0, 0, blankNameIdentifer)
return n, TRANS_CONTINUE
case "iota":
pd := lastDecl(ns)
Expand Down Expand Up @@ -1241,7 +1244,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 @@ -1924,8 +1927,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 == blankNameIdentifer {
nx.Path = NewValuePathBlock(0, 0, blankNameIdentifer)
} else {
pn.Define2(n.Const, nx.Name, sts[i], tvs[i])
nx.Path = last.GetPathForName(nil, nx.Name)
Expand All @@ -1934,8 +1937,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 == blankNameIdentifer {
nx.Path = NewValuePathBlock(0, 0, blankNameIdentifer)
} else {
last.Define2(n.Const, nx.Name, sts[i], tvs[i])
nx.Path = last.GetPathForName(nil, nx.Name)
Expand Down Expand Up @@ -3049,7 +3052,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 == blankNameIdentifer {
// create a hidden var with leading dot.
// NOTE: document somewhere.
cd.Recv.Name = ".recv"
Expand Down Expand Up @@ -3168,7 +3171,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 == blankNameIdentifer { // no definition
return
} else if d.Name == "." { // dot import
panic("dot imports not allowed in Gno")
Expand All @@ -3195,8 +3198,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 == blankNameIdentifer {
nx.Path.Name = blankNameIdentifer
} else {
last2 := skipFile(last)
last2.Predefine(d.Const, nx.Name)
Expand Down Expand Up @@ -3376,7 +3379,7 @@ func constUntypedBigint(source Expr, i64 int64) *ConstExpr {
}

func fillNameExprPath(last BlockNode, nx *NameExpr, isDefineLHS bool) {
if nx.Name == "_" {
if nx.Name == blankNameIdentifer {
// Blank name has no path; caller error.
panic("should not happen")
}
Expand Down
10 changes: 5 additions & 5 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (dbv DataByteValue) SetByte(b byte) {
// value after a block var escapes "to the heap".
// *(PointerValue.TypedValue) must have already become
// initialized, namely T set if a typed-nil.
// Index is -1 for the shared "_" block var,
// Index is -1 for the shared blankIdentifier block var,
deelawn marked this conversation as resolved.
Show resolved Hide resolved
// and -2 for (gno and native) map items.
//
// Allocation for PointerValue is not immediate,
Expand All @@ -179,7 +179,7 @@ type PointerValue struct {
}

const (
PointerIndexBlockBlank = -1 // for the "_" identifier in blocks
PointerIndexBlockBlank = -1 // for the blankIdentifier identifier in blocks
deelawn marked this conversation as resolved.
Show resolved Hide resolved
PointerIndexMap = -2 // Base is Map, use Key.
PointerIndexNative = -3 // Base is *NativeValue.
)
Expand Down Expand Up @@ -2257,7 +2257,7 @@ type Block struct {
Source BlockNode
Values []TypedValue
Parent Value
Blank TypedValue // captures "_" // XXX remove and replace with global instance.
Blank TypedValue // captures blankIdentifier // XXX remove and replace with global instance.
deelawn marked this conversation as resolved.
Show resolved Hide resolved
bodyStmt bodyStmt // XXX expose for persistence, not needed for MVP.
}

Expand Down Expand Up @@ -2343,7 +2343,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 != blankNameIdentifer {
panic(fmt.Sprintf(
"zero value path is reserved for \"_\", but got %s",
path.Name))
Expand All @@ -2365,7 +2365,7 @@ func (b *Block) GetPointerTo(store Store, path ValuePath) PointerValue {
return b.GetPointerToInt(store, int(path.Index))
}

// Result is used has lhs for any assignments to "_".
// Result is used has lhs for any assignments to blankIdentifier.
func (b *Block) GetBlankRef() *TypedValue {
return &b.Blank
}
Expand Down
Loading