-
Notifications
You must be signed in to change notification settings - Fork 22
/
named.go
141 lines (116 loc) · 3.83 KB
/
named.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
140
141
//
// Copyright © 2018 Aljabr, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package symbol
import (
"fmt"
"reflect"
"github.com/kocircuit/kocircuit/lang/circuit/eval"
"github.com/kocircuit/kocircuit/lang/circuit/model"
pb "github.com/kocircuit/kocircuit/lang/go/eval/symbol/proto"
"github.com/kocircuit/kocircuit/lang/go/gate"
"github.com/kocircuit/kocircuit/lang/go/kit/tree"
)
type NamedSymbol struct {
Value reflect.Value `ko:"name=value"`
}
var _ Symbol = &NamedSymbol{}
// DisassembleToGo converts a Ko value into a Go value
func (named *NamedSymbol) DisassembleToGo(span *model.Span) (reflect.Value, error) {
return named.Value, nil
}
// DisassembleToPB converts a Ko value into a protobuf
func (named *NamedSymbol) DisassembleToPB(span *model.Span) (*pb.Symbol, error) {
return DeconstructKind(span, named.Value).DisassembleToPB(span)
}
func (named *NamedSymbol) String() string {
return tree.Sprint(named.Value.Interface())
}
func (named *NamedSymbol) Equal(span *model.Span, sym Symbol) bool {
if other, ok := sym.(*NamedSymbol); ok {
return reflect.DeepEqual(named.Value.Interface(), other.Value.Interface())
} else {
return false
}
}
func (named *NamedSymbol) Splay() tree.Tree {
return tree.Splay(named.Value.Interface())
}
func (named *NamedSymbol) Hash(span *model.Span) model.ID {
return DeconstructKind(span, named.Value).Hash(span)
}
func (named *NamedSymbol) GoType() reflect.Type {
return named.Value.Type()
}
func (named *NamedSymbol) Type() Type {
return NamedType{
Type: named.GoType(),
}
}
func (named *NamedSymbol) LiftToSeries(span *model.Span) *SeriesSymbol {
return singletonSeries(named)
}
func (named *NamedSymbol) Link(span *model.Span, name string, monadic bool) (eval.Shape, eval.Effect, error) {
return nil, nil, span.Errorf(nil, "linking argument to named")
}
func (named *NamedSymbol) Select(span *model.Span, path model.Path) (eval.Shape, eval.Effect, error) {
if len(path) == 0 {
return named, nil, nil
} else {
if step, err := named.Walk(span, path[0]); err != nil {
return nil, nil, err
} else {
return step.Select(span, path[1:])
}
}
}
func (named *NamedSymbol) Walk(span *model.Span, field string) (Symbol, error) {
v := named.Value
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
if !v.IsValid() {
return EmptySymbol{}, nil
}
if v.Kind() != reflect.Struct {
return nil, span.Errorf(nil, "cannot select %s into %v", field, named)
}
if fieldIndex, ok := gate.StripFields(v.Type()).FieldByKoName(field); ok {
return Deconstruct(span, v.Field(fieldIndex)), nil
} else {
return EmptySymbol{}, nil
}
}
func (named *NamedSymbol) Augment(span *model.Span, _ eval.Fields) (eval.Shape, eval.Effect, error) {
return nil, nil, span.Errorf(nil, "named value %v cannot be augmented", named)
}
func (named *NamedSymbol) Invoke(span *model.Span) (eval.Shape, eval.Effect, error) {
return nil, nil, span.Errorf(nil, "named value %v cannot be invoked", named)
}
type NamedType struct {
Type reflect.Type `ko:"name=type"`
}
var _ Type = NamedType{}
func (nt NamedType) IsType() {}
func (nt NamedType) String() string {
return tree.Sprint(nt)
}
func (nt NamedType) Splay() tree.Tree {
return tree.NoQuote{String_: fmt.Sprintf("Named<%s.%s>", nt.Type.PkgPath(), nt.Type.Name())}
}
// GoType returns the Go equivalent of the type.
func (nt NamedType) GoType() reflect.Type {
return nt.Type
}