Skip to content

Commit

Permalink
Fix: constants in parameters and storage
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 14, 2021
1 parent 336ead0 commit de1618a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
101 changes: 101 additions & 0 deletions internal/bcd/ast/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package ast

import (
"bytes"
"fmt"
"strings"

"github.com/baking-bad/bcdhub/internal/bcd/base"
"github.com/baking-bad/bcdhub/internal/bcd/consts"
)

// Constant -
type Constant struct {
Default

KeyHash string
}

// NewConstant -
func NewConstant(depth int) *Constant {
return &Constant{
Default: NewDefault(consts.CONSTANT, 1, depth),
}
}

// MarshalJSON -
func (c *Constant) MarshalJSON() ([]byte, error) {
var builder bytes.Buffer
builder.WriteString(`{"prim": "constant", "args":[`)
builder.WriteString(fmt.Sprintf(`{"string": "%s"}`, c.KeyHash))
builder.WriteByte(']')
builder.WriteByte('}')
return builder.Bytes(), nil
}

// String -
func (c *Constant) String() string {
var s strings.Builder
s.WriteString(fmt.Sprintf("[%d] %s key_hash=%s", c.ID, c.Prim, c.KeyHash))
return s.String()
}

// ParseType -
func (c *Constant) ParseType(node *base.Node, id *int) error {
if err := c.Default.ParseType(node, id); err != nil {
return err
}

if len(node.Args) == 0 {
return nil
}

if node.Args[0].StringValue == nil {
return nil
}

c.KeyHash = *node.Args[0].StringValue
return nil
}

// ParseValue -
func (c *Constant) ParseValue(node *base.Node) error {
return nil
}

// ToMiguel -
func (c *Constant) ToMiguel() (*MiguelNode, error) {
return c.Default.ToMiguel()
}

// Docs -
func (c *Constant) Docs(inferredName string) ([]Typedef, string, error) {
return []Typedef{
{
Name: c.GetName(),
Type: fmt.Sprintf("%s(%s)", c.Prim, c.KeyHash),
},
}, c.Prim, nil
}

// Distinguish -
func (c *Constant) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*Constant)
if !ok {
return nil, nil
}
return c.Default.Distinguish(&second.Default)
}

// EqualType -
func (c *Constant) EqualType(node Node) bool {
return c.Default.EqualType(node)
}

// FindByName -
func (c *Constant) FindByName(name string, isEntrypoint bool) Node {
if c.GetName() == name {
return c
}
return nil
}
2 changes: 2 additions & 0 deletions internal/bcd/ast/untyped.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ func typeNode(node *base.Node, depth int, id *int) (Node, error) {
ast = NewChest(depth + 1)
case consts.CHESTKEY:
ast = NewChestKey(depth + 1)
case consts.CONSTANT:
ast = NewConstant(depth + 1)
default:
return nil, errors.Wrap(consts.ErrUnknownPrim, node.Prim)
}
Expand Down
15 changes: 13 additions & 2 deletions internal/bcd/contract/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,19 @@ func (p *Parser) FindConstants() ([]string, error) {
return nil, nil
}

err := p.parse(p.Code.Code, p.handleConstants)
return p.Constants.Values(), err
if err := p.parse(p.Code.Code, p.handleConstants); err != nil {
return nil, err
}

if err := p.parse(p.Code.Storage, p.handleConstants); err != nil {
return nil, err
}

if err := p.parse(p.Code.Parameter, p.handleConstants); err != nil {
return nil, err
}

return p.Constants.Values(), nil
}

// IsUpgradable -
Expand Down

0 comments on commit de1618a

Please sign in to comment.