-
Notifications
You must be signed in to change notification settings - Fork 6
/
sapling_state.go
127 lines (110 loc) · 2.72 KB
/
sapling_state.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package ast
import (
"bytes"
"fmt"
"strings"
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/consts"
)
// SaplingState -
type SaplingState struct {
Default
Type *Int
MemoSize int64
}
// NewSaplingState -
func NewSaplingState(depth int) *SaplingState {
return &SaplingState{
Default: NewDefault(consts.SAPLINGSTATE, 1, depth),
Type: NewInt(depth),
}
}
// MarshalJSON -
func (ss *SaplingState) MarshalJSON() ([]byte, error) {
var builder bytes.Buffer
builder.WriteString(`{"prim": "sapling_state", "args":[`)
builder.WriteString(fmt.Sprintf(`{"int": "%d"}`, ss.MemoSize))
builder.WriteByte(']')
if len(ss.Annots) > 0 {
if _, err := builder.WriteString(fmt.Sprintf(`, "annots": ["%s"]`, strings.Join(ss.Annots, `","`))); err != nil {
return nil, err
}
}
builder.WriteByte('}')
return builder.Bytes(), nil
}
// String -
func (ss *SaplingState) String() string {
var s strings.Builder
s.WriteString(fmt.Sprintf("[%d] %s memo_size=%d\n", ss.ID, ss.Prim, ss.MemoSize))
if ss.Type.Value != nil {
s.WriteString(strings.Repeat(consts.DefaultIndent, ss.Depth))
s.WriteString(fmt.Sprintf("Int=%d", ss.Type.Value))
}
return s.String()
}
// ParseType -
func (ss *SaplingState) ParseType(node *base.Node, id *int) error {
if err := ss.Default.ParseType(node, id); err != nil {
return err
}
ss.MemoSize = node.Args[0].IntValue.Int64()
return nil
}
// ParseValue -
func (ss *SaplingState) ParseValue(node *base.Node) error {
return ss.Type.ParseValue(node)
}
// ToMiguel -
func (ss *SaplingState) ToMiguel() (*MiguelNode, error) {
node, err := ss.Default.ToMiguel()
if err != nil {
return nil, err
}
node.Children = make([]*MiguelNode, 0)
child, err := ss.Type.ToMiguel()
if err != nil {
return nil, err
}
node.Children = append(node.Children, child)
return node, nil
}
// ToBaseNode -
func (ss *SaplingState) ToBaseNode(optimized bool) (*base.Node, error) {
return ss.Type.ToBaseNode(optimized)
}
// Docs -
func (ss *SaplingState) Docs(inferredName string) ([]Typedef, string, error) {
return []Typedef{
{
Name: ss.GetName(),
Type: fmt.Sprintf("%s(%d)", ss.Prim, ss.MemoSize),
},
}, ss.Prim, nil
}
// Distinguish -
func (ss *SaplingState) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*SaplingState)
if !ok {
return nil, nil
}
return ss.Default.Distinguish(&second.Default)
}
// EqualType -
func (ss *SaplingState) EqualType(node Node) bool {
if !ss.Default.EqualType(node) {
return false
}
second, ok := node.(*SaplingState)
if !ok {
return false
}
return ss.Type.EqualType(second.Type)
}
// FindByName -
func (ss *SaplingState) FindByName(name string, isEntrypoint bool) Node {
if ss.GetName() == name {
return ss
}
return nil
}