forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_decl.go
74 lines (71 loc) · 1.54 KB
/
op_decl.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
package gno
import (
"fmt"
)
func (m *Machine) doOpValueDecl() {
s := m.PopStmt().(*ValueDecl)
lb := m.LastBlock()
nt := Type(nil)
if s.Type != nil {
nt = m.PopValue().GetType()
}
var rvs []TypedValue
if s.Values != nil {
rvs = m.PopValues(len(s.NameExprs))
}
for i := 0; i < len(s.NameExprs); i++ {
var tv TypedValue
if rvs == nil {
// NOTE: Go/Gno wart.
// implicit interface casting could
// requiring the consideration of the typed-nil case.
if nt == nil {
tv = TypedValue{}
} else {
tv = TypedValue{T: nt, V: defaultValue(m.Alloc, nt)}
}
} else {
tv = rvs[i]
}
if nt != nil {
if nt.Kind() == InterfaceKind {
if isUntyped(tv.T) {
ConvertUntypedTo(&tv, nil)
} else {
// keep type as is.
}
} else {
if isUntyped(tv.T) {
ConvertUntypedTo(&tv, nt)
} else {
if debug {
if nt.TypeID() != tv.T.TypeID() &&
baseOf(nt).TypeID() != tv.T.TypeID() {
panic(fmt.Sprintf(
"type mismatch: %s vs %s",
nt.TypeID(),
tv.T.TypeID(),
))
}
}
tv.T = nt
}
}
} else if s.Const {
// leave untyped as is.
} else if isUntyped(tv.T) {
ConvertUntypedTo(&tv, nil)
}
nx := s.NameExprs[i]
ptr := lb.GetPointerTo(m.Store, nx.Path)
ptr.Assign2(m.Alloc, m.Store, m.Realm, tv, false)
}
}
func (m *Machine) doOpTypeDecl() {
s := m.PopStmt().(*TypeDecl)
t := m.PopValue().GetType()
tv := asValue(t)
last := m.LastBlock()
ptr := last.GetPointerTo(m.Store, s.Path)
ptr.Assign2(m.Alloc, m.Store, m.Realm, tv, false)
}