-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate.pl
More file actions
executable file
·196 lines (171 loc) · 5.33 KB
/
generate.pl
File metadata and controls
executable file
·196 lines (171 loc) · 5.33 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env perl -w
use v5.16;
use strict;
sub main {
my $maxArity = 9;
write_file("CrystalMethod.java", gen_builder_class($maxArity));
for my $i (1..$maxArity) {
my $suffix = suffix($i);
write_file("Multimethod$suffix.java", gen_multimethod_interface($i));
write_file("Function$suffix.java", gen_functional_interface($i)) unless $i == 1;
}
}
sub write_file {
my ($filename, $content) = @_;
open(my $fh, ">", "src/main/java/com/github/rschmitt/crystalmethod/$filename")
or die "Unable to open $filename for write: $!";
print $fh $content;
close $fh;
}
sub gen_builder_class {
my $maxArity = shift;
my $m = join "\n", map gen_builder_method($_), (1..$maxArity);
my $s = join "\n", map gen_statics($_), (1..$maxArity);
return <<HERE
package com.github.rschmitt.crystalmethod;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
public class CrystalMethod {
$s
$m
}
HERE
}
sub gen_builder_method {
my $count = shift;
my $multimethod = gen_multimethod($count);
my $dispatchFn = gen_function($count, "D");
my $function = gen_function($count, "R");
my $typeVars = gen_type_vars($count);
my $args = get_args($count);
return <<HERE
\@SuppressWarnings("unchecked")
public static <T extends $multimethod, D, $typeVars, R> T buildMultimethod(
$dispatchFn dispatchFn,
Map<D, $function> methods,
Class<T> type
) {
Map<D, $function> myCopy = new HashMap<>();
myCopy.putAll(methods);
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class<?>[]{type},
(Object proxy, Method method, Object[] args) -> {
if ("getDispatchFn".equals(method.getName()))
return dispatchFn;
else if ("getDispatchMap".equals(method.getName()))
return Collections.unmodifiableMap(myCopy);
D dispatchVal = dispatchFn.apply($args);
return myCopy.get(dispatchVal).apply($args);
});
}
HERE
}
sub gen_statics {
my $count = shift;
my $suffix = suffix($count);
my $multimethod = gen_multimethod($count);
my $typevars = gen_type_vars($count);
my $function = gen_function($count, "R");
my $dispatchFn = gen_function($count, "D");
my $paramList = gen_param_list($count);
my $args = join ", ", map {"arg$_"} (1..$count);
return <<HERE
private static final ConcurrentMap<Class, Multimethod$suffix> globalMultimethods$suffix = new ConcurrentHashMap<>();
public static <T extends $multimethod, D, $typevars, R> void defMulti(
$dispatchFn dispatchFn,
Class<T> type
) {
Multimethod$suffix multimethod = CrystalMethod.buildMultimethod(dispatchFn, new HashMap(), type);
globalMultimethods$suffix.putIfAbsent(type, multimethod);
}
public static <T extends $multimethod, D, $typevars, R> void addMethod(
D dispatchVal,
$function method,
Class<T> type
) {
globalMultimethods$suffix.compute(type, (t, m) -> {
Map<D, $function> oldMap = m.getDispatchMap();
Map<D, $function> newMap = new HashMap<>();
newMap.putAll(oldMap);
newMap.put(dispatchVal, method);
return CrystalMethod.buildMultimethod(m.getDispatchFn(), newMap, type);
});
}
public static <T extends $multimethod, D, $typevars, R> R invoke(Class<T> type, $paramList) {
return (R) globalMultimethods$suffix.get(type).apply($args);
}
HERE
}
sub get_args {
my $count = shift;
my @args;
for my $i (1..$count) {
my $idx = $i - 1;
push @args, "(T$i) args[$idx]";
}
return join ", ", @args;
}
sub gen_multimethod_interface {
my $count = shift;
my $args = gen_param_list($count);
my $function = gen_function($count, "R");
my $dispatchFn = gen_function($count, "D");
my $multimethod = gen_multimethod($count);
return <<HERE
package com.github.rschmitt.crystalmethod;
import java.util.Map;
import java.util.function.Function;
\@FunctionalInterface
public interface $multimethod extends $function {
default Map<D, $function> getDispatchMap() {
return null;
}
default $dispatchFn getDispatchFn() {
return null;
}
}
HERE
}
sub gen_functional_interface {
my $count = shift;
my $function = gen_function($count, "R");
my $args = gen_param_list($count);
return <<HERE
package com.github.rschmitt.crystalmethod;
\@FunctionalInterface
public interface $function {
R apply($args);
}
HERE
}
sub gen_function {
my ($count, $returnType) = @_;
my $suffix = suffix($count);
my $typeVars = gen_type_vars($count);
return "Function$suffix<$typeVars, $returnType>";
}
sub gen_multimethod {
my ($count, $returnType) = @_;
my $suffix = suffix($count);
my $typeVars = gen_type_vars($count);
return "Multimethod$suffix<D, $typeVars, R>";
}
sub gen_type_vars {
my ($count) = @_;
return join ", ", map {"T$_"} (1..$count);
}
sub gen_param_list {
my $count = shift;
return join ", ", map {"T$_ arg$_"} (1..$count);
}
sub suffix {
my ($arity) = @_;
return $arity == 1 ? "" : "$arity";
}
main();