-
Notifications
You must be signed in to change notification settings - Fork 22
/
opaque.go
120 lines (95 loc) · 3.4 KB
/
opaque.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
//
// 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/kit/tree"
)
type OpaqueSymbol struct {
Value reflect.Value `ko:"name=value"`
}
var _ Symbol = &OpaqueSymbol{}
func (opaque *OpaqueSymbol) Interface() interface{} {
return opaque.Value.Interface()
}
// DisassembleToGo converts a Ko value into a Go value
func (opaque *OpaqueSymbol) DisassembleToGo(span *model.Span) (reflect.Value, error) {
return opaque.Value, nil
}
// DisassembleToPB converts a Ko value into a protobuf
func (opaque *OpaqueSymbol) DisassembleToPB(span *model.Span) (*pb.Symbol, error) {
return nil, span.Errorf(nil, "cannot disassemble opaque symbol %v", opaque)
}
func (opaque *OpaqueSymbol) String() string {
return tree.Sprint(opaque)
}
func (opaque *OpaqueSymbol) Equal(span *model.Span, sym Symbol) bool {
if other, ok := sym.(*OpaqueSymbol); ok {
return opaque.Value.Interface() == other.Value.Interface()
} else {
return false
}
}
func (opaque *OpaqueSymbol) Hash(span *model.Span) model.ID {
return model.StringID("#█")
}
func (opaque *OpaqueSymbol) LiftToSeries(span *model.Span) *SeriesSymbol {
return singletonSeries(opaque)
}
func (opaque *OpaqueSymbol) Link(span *model.Span, name string, monadic bool) (eval.Shape, eval.Effect, error) {
return nil, nil, span.Errorf(nil, "linking argument to opaque")
}
func (opaque *OpaqueSymbol) Select(span *model.Span, path model.Path) (eval.Shape, eval.Effect, error) {
if len(path) == 0 {
return opaque, nil, nil
}
return nil, nil, span.Errorf(nil, "opaque value %v of type %s cannot be selected into", opaque, opaque.Value.Type().String())
}
func (opaque *OpaqueSymbol) Augment(span *model.Span, _ eval.Fields) (eval.Shape, eval.Effect, error) {
return nil, nil, span.Errorf(nil, "opaque value %v cannot be augmented", opaque)
}
func (opaque *OpaqueSymbol) Invoke(span *model.Span) (eval.Shape, eval.Effect, error) {
return nil, nil, span.Errorf(nil, "opaque value %v cannot be invoked", opaque)
}
func (opaque *OpaqueSymbol) GoType() reflect.Type {
return opaque.Value.Type()
}
func (opaque *OpaqueSymbol) Type() Type {
return &OpaqueType{Type: opaque.Value.Type()}
}
func (opaque *OpaqueSymbol) Splay() tree.Tree {
return opaque.Type().Splay()
}
// OpaqueType captures a specific Go type.
type OpaqueType struct {
Type reflect.Type `ko:"name=type"`
}
var _ Type = &OpaqueType{}
func (ot *OpaqueType) IsType() {}
func (ot *OpaqueType) String() string {
return tree.Sprint(ot)
}
func (ot *OpaqueType) Splay() tree.Tree {
return tree.NoQuote{String_: fmt.Sprintf("Opaque<%v>", ot.Type)}
}
// GoType returns the Go equivalent of the type.
func (ot *OpaqueType) GoType() reflect.Type {
return ot.Type
}