diff --git a/internal/bcd/ast/constant.go b/internal/bcd/ast/constant.go new file mode 100644 index 000000000..63e7443e3 --- /dev/null +++ b/internal/bcd/ast/constant.go @@ -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 +} diff --git a/internal/bcd/ast/untyped.go b/internal/bcd/ast/untyped.go index eebae3458..3ae67baf0 100644 --- a/internal/bcd/ast/untyped.go +++ b/internal/bcd/ast/untyped.go @@ -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) } diff --git a/internal/bcd/contract/parser.go b/internal/bcd/contract/parser.go index a705f4422..d2248f1d5 100644 --- a/internal/bcd/contract/parser.go +++ b/internal/bcd/contract/parser.go @@ -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 -