-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.go
127 lines (114 loc) · 3.3 KB
/
generic.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
/*
Copyright 2015 Lee Boynton
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 ell
import (
. "github.com/boynton/ell/data"
)
func methodSignature(formalArgs *List) (Value, error) {
sig := ""
for formalArgs != EmptyList {
s := formalArgs.Car //might be a symbol, might be a list
tname := ""
if lst, ok := s.(*List); ok { //specialized
t := Cadr(lst)
if tp, ok := t.(*Type); ok {
tname = tp.Name()
} else {
return nil, NewError(SyntaxErrorKey, "Specialized argument must be of the form <symbol> or (<symbol> <type>), got ", s)
}
} else if s.Type() == SymbolType { //unspecialized
tname = "<any>"
} else {
return nil, NewError(SyntaxErrorKey, "Specialized argument must be of the form <symbol> or (<symbol> <type>), got ", s)
}
sig += tname
formalArgs = formalArgs.Cdr
}
return Intern(sig), nil
}
func arglistSignature(args []Value) string {
sig := ""
for _, arg := range args {
sig += (arg.Type().(*Type)).Name()
}
return sig
}
func signatureCombos(argtypes []Value) []string {
switch len(argtypes) {
case 0:
return []string{}
case 1:
return []string{TypeNameString(argtypes[0]), TypeNameString(AnyType)}
default:
//get the combinations of the tail, and concat both the type and <any> onto each of those combos
rest := signatureCombos(argtypes[1:]) // ["<number>" "<any>"]
result := make([]string, 0, len(rest)*2)
this := argtypes[0]
for _, s := range rest {
result = append(result, TypeNameString(this)+s)
}
for _, s := range rest {
result = append(result, TypeNameString(AnyType)+s)
}
return result
}
}
func TypeNameString(tval Value) string {
if tp, ok := tval.(*Type); ok {
return tp.Name()
}
return ""
}
var cachedSigs = make(map[string][]Value)
func arglistSignatures(args []Value) []Value {
key := arglistSignature(args)
sigs, ok := cachedSigs[key]
if !ok {
var argtypes []Value
for _, arg := range args {
argtypes = append(argtypes, arg.Type())
}
stringSigs := signatureCombos(argtypes)
sigs = make([]Value, 0, len(stringSigs))
for _, sig := range stringSigs {
sigs = append(sigs, Intern(sig))
}
cachedSigs[key] = sigs
}
return sigs
}
var GenfnsSymbol = Intern("*genfns*")
var MethodsKeyword = Intern("methods:")
func getfn(sym Value, args []Value) (Value, error) {
sigs := arglistSignatures(args)
gfs := GetGlobal(GenfnsSymbol)
if p, ok := gfs.(*Struct); ok {
gf := p.Get(sym)
if p2, ok := gf.(*Instance); ok {
if p3, ok := p2.Value.(*Struct); ok {
methods := p3.Get(MethodsKeyword)
if p3, ok := methods.(*Struct); ok {
for _, sig := range sigs {
fun := p3.Get(sig)
if fun != Null {
return fun, nil
}
}
}
}
} else {
return nil, NewError(ErrorKey, "Not a generic function: ", sym)
}
}
return nil, NewError(ErrorKey, "Generic function ", sym, ", has no matching method for: ", args)
}