1#!./perl -w
2
3# How to generate the logic of the lookup table Perl_keyword() in toke.c
4
5use strict;
6package Toke;
7use vars qw(@ISA %types);
8require ExtUtils::Constant::Base;
9@ISA = 'ExtUtils::Constant::Base';
10
11%types = (pos => "KEY_", neg => "-KEY_");
12
13# We're allowing scalar references to produce evil customisation.
14sub valid_type {
15  defined $types{$_[1]} or ref $_[1];
16}
17
18
19# This might actually be a return statement
20sub assignment_clause_for_type {
21  my ($self, $args, $value) = @_;
22  my ($type, $item) = @{$args}{qw(type item)};
23  my $comment = '';
24  $comment = " /* Weight $item->{weight} */" if defined $item->{weight};
25  return "return $types{$type}$value;$comment" if $types{$type};
26  "$$type$value;$comment";
27}
28
29sub return_statement_for_notfound {
30  "return 0;"
31}
32
33# Ditch the default "const"
34sub name_param_definition {
35  "char *" . $_[0]->name_param;
36}
37
38sub C_constant_return_type {
39  "I32";
40}
41
42
43sub C_constant_prefix_param {
44  "aTHX_ ";
45}
46
47sub C_constant_prefix_param_defintion {
48  "pTHX_ ";
49}
50
51sub namelen_param_definition {
52  'I32 ' . $_[0] -> namelen_param;
53}
54
55package main;
56
57my @pos = qw(__DATA__ __END__ AUTOLOAD BEGIN CHECK DESTROY do delete defined
58	     END else eval elsif exists for format foreach grep goto glob INIT
59	     if last local m my map next no our pos print printf package
60	     prototype q qr qq qw qx redo return require s scalar sort split
61	     study sub tr tie tied use undef until untie unless while y);
62
63my @neg = qw(__FILE__ __LINE__ __PACKAGE__ and abs alarm atan2 accept bless
64	     bind binmode CORE cmp chr cos chop close chdir chomp chmod chown
65	     crypt chroot caller connect closedir continue die dump dbmopen
66	     dbmclose eq eof err exp exit exec each endgrent endpwent
67	     endnetent endhostent endservent endprotoent fork fcntl flock
68	     fileno formline getppid getpgrp getpwent getpwnam getpwuid
69	     getpeername getprotoent getpriority getprotobyname
70	     getprotobynumber gethostbyname gethostbyaddr gethostent
71	     getnetbyname getnetbyaddr getnetent getservbyname getservbyport
72	     getservent getsockname getsockopt getgrent getgrnam getgrgid
73	     getlogin getc gt ge gmtime hex int index ioctl join keys kill lt
74	     le lc log link lock lstat length listen lcfirst localtime mkdir
75	     msgctl msgget msgrcv msgsnd ne not or ord oct open opendir pop
76	     push pack pipe quotemeta ref read rand recv rmdir reset rename
77	     rindex reverse readdir readlink readline readpipe rewinddir seek
78	     send semop select semctl semget setpgrp seekdir setpwent setgrent
79	     setnetent setsockopt sethostent setservent setpriority
80	     setprotoent shift shmctl shmget shmread shmwrite shutdown sin
81	     sleep socket socketpair sprintf splice sqrt srand stat substr
82	     system symlink syscall sysopen sysread sysseek syswrite tell time
83	     times telldir truncate uc utime umask unpack unlink unshift
84	     ucfirst values vec warn wait write waitpid wantarray x xor);
85
86my %frequencies = (map {/(.*):\t(.*)/} <DATA>);
87
88my @names;
89push @names, map {{name=>$_, type=>"pos", weight=>$frequencies{$_}}} @pos;
90push @names, map {{name=>$_, type=>"neg", weight=>$frequencies{$_}}} @neg;
91push @names, {name=>'elseif', type=>\"", value=><<'EOC'};
92/* This is somewhat hacky.  */
93if(ckWARN_d(WARN_SYNTAX))
94  Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "elseif should be elsif");
95break;
96EOC
97
98print Toke->C_constant ({subname=>'Perl_keyword', breakout=>~0}, @names);
99
100__DATA__
101my:	3785925
102if:	2482605
103sub:	2053554
104return:	1401629
105unless:	913955
106shift:	904125
107eq:	797065
108defined:	694277
109use:	686081
110else:	527806
111qw:	415641
112or:	405163
113s:	403691
114require:	375220
115ref:	347102
116elsif:	322365
117undef:	311156
118and:	284867
119foreach:	281720
120local:	262973
121push:	256975
122package:	245661
123print:	220904
124our:	194417
125die:	192203
126length:	163975
127next:	153355
128m:	148776
129caller:	148457
130exists:	145939
131eval:	136977
132keys:	131427
133join:	130820
134substr:	121344
135while:	120305
136for:	118158
137map:	115207
138ne:	112906
139__END__:	112636
140vec:	110566
141goto:	109258
142do:	96004
143last:	95078
144split:	93678
145warn:	91372
146grep:	75912
147delete:	74966
148sprintf:	72704
149q:	69076
150bless:	62111
151no:	61989
152not:	55868
153qq:	55149
154index:	51465
155CORE:	47391
156pop:	46933
157close:	44077
158scalar:	43953
159wantarray:	43024
160open:	39060
161x:	38549
162lc:	38487
163__PACKAGE__:	36767
164stat:	36702
165unshift:	36504
166sort:	36394
167chr:	35654
168time:	32168
169qr:	28519
170splice:	25143
171BEGIN:	24125
172tr:	22665
173chomp:	22337
174ord:	22221
175chdir:	20317
176unlink:	18616
177int:	18549
178chmod:	18455
179each:	18414
180uc:	16961
181pack:	14491
182lstat:	13859
183binmode:	12301
184select:	12209
185closedir:	11986
186readdir:	11716
187reverse:	10571
188chop:	10172
189tie:	10131
190values:	10110
191tied:	9749
192read:	9434
193opendir:	9007
194fileno:	8591
195exit:	8262
196localtime:	7993
197unpack:	7849
198abs:	7767
199printf:	6874
200cmp:	6808
201ge:	5666
202pos:	5503
203redo:	5219
204rindex:	5005
205rename:	4918
206syswrite:	4437
207system:	4326
208lock:	4210
209oct:	4195
210le:	4052
211gmtime:	4040
212utime:	3849
213sysread:	3729
214hex:	3629
215END:	3565
216quotemeta:	3120
217mkdir:	2951
218continue:	2925
219AUTOLOAD:	2713
220tell:	2578
221write:	2525
222rmdir:	2493
223seek:	2174
224glob:	2172
225study:	1933
226rand:	1824
227format:	1735
228umask:	1658
229eof:	1618
230prototype:	1602
231readlink:	1537
232truncate:	1351
233fcntl:	1257
234sysopen:	1230
235ucfirst:	1012
236getc:	981
237gethostbyname:	970
238ioctl:	967
239formline:	959
240gt:	897
241__FILE__:	888
242until:	818
243sqrt:	766
244getprotobyname:	755
245sysseek:	721
246getpeername:	713
247getpwuid:	681
248xor:	619
249y:	567
250syscall:	560
251CHECK:	538
252connect:	526
253err:	522
254sleep:	519
255sin:	499
256send:	496
257getpwnam:	483
258cos:	447
259exec:	429
260link:	425
261exp:	423
262untie:	420
263INIT:	418
264waitpid:	414
265__DATA__:	395
266symlink:	386
267kill:	382
268setsockopt:	356
269atan2:	350
270pipe:	344
271lt:	335
272fork:	327
273times:	310
274getservbyname:	299
275telldir:	294
276bind:	290
277dump:	274
278flock:	260
279recv:	250
280getsockopt:	243
281getsockname:	235
282accept:	233
283getprotobynumber:	232
284rewinddir:	218
285__LINE__:	209
286qx:	177
287lcfirst:	165
288getlogin:	158
289reset:	127
290gethostbyaddr:	68
291getgrgid:	67
292srand:	41
293chown:	34
294seekdir:	20
295readline:	19
296semctl:	17
297getpwent:	12
298getgrnam:	11
299getppid:	10
300crypt:	8
301DESTROY:	7
302getpriority:	5
303getservent:	4
304gethostent:	3
305setpriority:	2
306setnetent:	1
307