-
Notifications
You must be signed in to change notification settings - Fork 5
/
constant.go
101 lines (84 loc) · 1.89 KB
/
constant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ast
import (
"bytes"
"fmt"
"strings"
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/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
}