/
slice.go
139 lines (128 loc) · 3.12 KB
/
slice.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
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2014 Gordon Klaus. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"github.com/gordonklaus/flux/go/types"
. "github.com/gordonklaus/flux/gui"
)
type sliceNode struct {
*nodeBase
x, low, high, max, y *port
}
func newSliceNode() *sliceNode {
n := &sliceNode{}
n.nodeBase = newNodeBase(n)
n.x = n.newInput(nil)
n.x.connsChanged = n.connsChanged
n.low = n.newInput(newVar("low", nil))
n.high = n.newInput(newVar("high", nil))
n.y = n.newOutput(nil)
n.text.SetText("[:]")
n.text.SetTextColor(color(&types.Func{}, true, false))
n.connsChanged()
return n
}
func (n *sliceNode) connectable(t types.Type, dst *port) bool {
if dst == n.x {
ok := false
switch t := underlying(t).(type) {
case *types.Basic:
ok = t.Info&types.IsString != 0 && n.max == nil
case *types.Slice:
ok = true
case *types.Pointer:
_, ok = underlying(t.Elem).(*types.Array)
}
return ok
}
return assignable(t, dst.obj.Type)
}
func (n *sliceNode) connsChanged() {
t := untypedToTyped(inputType(n.x))
if p, ok := underlying(t).(*types.Pointer); ok {
t = &types.Slice{underlying(p.Elem).(*types.Array).Elem}
}
var i types.Type
if t != nil {
i = types.Typ[types.Int]
}
n.x.setType(t)
n.low.setType(i)
if n.high != nil {
n.high.setType(i)
}
if n.max != nil {
n.max.setType(i)
}
n.y.setType(t)
}
func (n *sliceNode) KeyPress(event KeyEvent) {
if event.Text == "," {
_, str := underlying(inputType(n.x)).(*types.Basic)
switch {
case n.high == nil:
n.high = n.newInput(newVar("high", nil))
case n.max == nil && !str:
n.max = n.newInput(newVar("max", nil))
default:
n.removePortBase(n.high)
n.high = nil
if !str {
n.removePortBase(n.max)
n.max = nil
}
}
n.connsChanged()
SetKeyFocus(n)
} else {
n.nodeBase.KeyPress(event)
}
}
type copyNode struct {
*nodeBase
dst, src, n *port
}
func newCopyNode(godefer string) *copyNode {
n := ©Node{}
n.nodeBase = newGoDeferNodeBase(n, godefer)
n.text.SetText("copy")
n.text.SetTextColor(color(&types.Func{}, true, false))
n.dst = n.newInput(newVar("dst", nil))
n.src = n.newInput(newVar("src", nil))
n.n = n.newOutput(newVar("n", nil))
n.dst.connsChanged = n.connsChanged
n.src.connsChanged = n.connsChanged
n.addSeqPorts()
return n
}
func (n *copyNode) connectable(t types.Type, p *port) bool {
var elem types.Type
if dst, ok := underlying(n.dst.obj.Type).(*types.Slice); ok {
elem = dst.Elem
}
switch src := underlying(n.src.obj.Type).(type) {
case *types.Basic:
elem = types.Typ[types.Byte]
case *types.Slice:
elem = src.Elem
}
switch t := underlying(t).(type) {
case *types.Basic:
return p == n.src && t.Info&types.IsString != 0 && (elem == nil || types.IsIdentical(elem, types.Typ[types.Byte]))
case *types.Slice:
return elem == nil || types.IsIdentical(t.Elem, elem)
}
return false
}
func (n *copyNode) connsChanged() {
dst := inputType(n.dst)
src := untypedToTyped(inputType(n.src))
n.dst.setType(dst)
n.src.setType(src)
if dst != nil && src != nil {
n.n.setType(types.Typ[types.Int])
} else {
n.n.setType(nil)
}
}