-
Notifications
You must be signed in to change notification settings - Fork 43
/
scope.go
123 lines (102 loc) · 2.76 KB
/
scope.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
package hinter
import (
"fmt"
"math/big"
)
// ScopeManager handles all operations regarding scopes:
// - Creating a new scope
// - Exiting current scope
// - Variable declaration and assignment inside a certain scope
// - Accessing variable values
type ScopeManager struct {
scopes []map[string]any
}
func InitializeScopeManager(ctx *HintRunnerContext, scope map[string]any) {
if ctx.ScopeManager.scopes == nil {
ctx.ScopeManager = *NewScopeManager(scope)
}
}
func NewScopeManager(globals map[string]any) *ScopeManager {
return &ScopeManager{
scopes: []map[string]any{
// One scope needed (current execution scope)
globals,
},
}
}
func DefaultNewScopeManager() *ScopeManager {
return NewScopeManager(make(map[string]any))
}
func (sm *ScopeManager) EnterScope(newScope map[string]any) {
sm.scopes = append(sm.scopes, newScope)
}
func (sm *ScopeManager) ExitScope() error {
if len(sm.scopes) < 2 {
return fmt.Errorf("expected at least one existing scope")
}
sm.scopes = sm.scopes[:(len(sm.scopes) - 1)]
return nil
}
func (sm *ScopeManager) AssignVariable(name string, value any) error {
scope, err := sm.getCurrentScope()
if err != nil {
return err
}
(*scope)[name] = value
return nil
}
func (sm *ScopeManager) AssignVariables(values map[string]any) error {
for name, value := range values {
err := sm.AssignVariable(name, value)
if err != nil {
return err
}
}
return nil
}
func (sm *ScopeManager) GetVariableValue(name string) (any, error) {
scope, err := sm.getCurrentScope()
if err != nil {
return nil, err
}
if value, ok := (*scope)[name]; ok {
return value, nil
}
return nil, fmt.Errorf("variable %s not found in current scope", name)
}
func (sm *ScopeManager) GetVariableValueAsBigInt(name string) (*big.Int, error) {
value, err := sm.GetVariableValue(name)
if err != nil {
return nil, err
}
valueBig, ok := value.(*big.Int)
if !ok {
return nil, fmt.Errorf("value: %s is not a *big.Int", value)
}
return valueBig, nil
}
func (sm *ScopeManager) GetVariableValueAsUint64(name string) (uint64, error) {
value, err := sm.GetVariableValue(name)
if err != nil {
return 0, err
}
valueUint, ok := value.(uint64)
if !ok {
return 0, fmt.Errorf("value: %s is not a uint64", value)
}
return valueUint, nil
}
func (sm *ScopeManager) getCurrentScope() (*map[string]any, error) {
if len(sm.scopes) == 0 {
return nil, fmt.Errorf("expected at least one existing scope")
}
return &sm.scopes[len(sm.scopes)-1], nil
}
func (sm *ScopeManager) GetZeroDictionaryManager() (ZeroDictionaryManager, bool) {
dictionaryManagerValue, err := sm.GetVariableValue("__dict_manager")
if err != nil {
return ZeroDictionaryManager{}, false
}
dictionaryManager, ok := dictionaryManagerValue.(ZeroDictionaryManager)
return dictionaryManager, ok
}