-
Notifications
You must be signed in to change notification settings - Fork 24
/
writecmds.pl
executable file
·289 lines (253 loc) · 7.12 KB
/
writecmds.pl
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/perl
use strict;
use warnings;
use YAML;
use POSIX qw(strftime);
my %tdisp = (
'any' => 'CI_ANYVALUE',
'float' => 'CI_NUMERIC',
'integer' => 'CI_NUMERIC',
'string' => 'CI_STRING',
'agent' => 'CI_AGENT',
'bytestring' => 'CI_BYTESTR',
'variable' => 'CI_VARIABLE',
'any' => 'CI_OTHER',
'anything' => 'CI_OTHER',
'condition' => undef,
'comparison' => undef,
'decimal' => 'CI_NUMERIC',
'decimal variable' => 'CI_OTHER',
'byte-string' => 'CI_BYTESTR',
'label' => undef,
'vector' => 'CI_VECTOR',
'bareword' => 'CI_BAREWORD',
'token' => 'CI_BAREWORD',
'subcommand' => 'CI_SUBCOMMAND',
'command' => 'CI_COMMAND',
);
# zero-tolerance policy
$SIG{__WARN__} = sub { die $_[0] };
my $data = YAML::LoadFile($ARGV[0]);
my $disp_id = 1;
my %disp_tbl;
my @init_funcs;
print qq{// THIS IS AN AUTOMATICALLY GENERATED FILE\n};
print qq{// DO NOT EDIT\n};
print qq{// Generated at }, strftime("%c", localtime(time)), qq{\n};
print qq{\n\n};
print qq{#include <string>\n};
print qq{#include <cstdio>\n};
print qq{#include <climits>\n};
print qq{#include "cmddata.h"\n};
print qq{#include "caosVM.h"\n};
print qq{#include "dialect.h"\n};
print qq{#include "openc2e.h"\n};
print qq{\n\n};
foreach my $variant_name (sort keys %{$data->{variants}}) {
my $variant = $data->{variants}{$variant_name};
for my $key (keys %$variant) {
$variant->{$key}{key} = $key;
}
my @cmds = values %$variant;
inject_ns(\@cmds);
writelookup(\@cmds);
checkdup(\@cmds, "$variant_name commands");
sortname(\@cmds);
miscprep($variant_name, \@cmds);
printarr(\@cmds, $variant_name, "${variant_name}_cmds");
printinit($variant_name, "${variant_name}_cmds");
}
printdispatch();
print "void registerAutoDelegates() {\n";
for my $f(@init_funcs) {
print "\t$f();\n";
}
print "}\n";
exit 0;
sub miscprep {
my ($variant, $cmds) = @_;
for my $cmd (@$cmds) {
$cmd->{evalcost}{$variant} = $cmd->{evalcost}{default} unless defined $cmd->{evalcost}{$variant};
if ($cmd->{type} ne 'command' && $cmd->{evalcost}{$variant} != 0) {
print STDERR "$cmd->{lookup_key} has non-zero evalcost in an expression cost.";
print STDERR "This causes a race condition which can potentially lead to crashes.";
print STDERR "If you really need this, please contact bd_. Aborting for now.";
exit 1;
}
}
}
sub printinit {
my ($variant, $cmdarr, $exparr) = @_;
print "static void init_$variant() {\n";
print qq{\tdialects["$variant"] = boost::shared_ptr<Dialect>(new Dialect($cmdarr, std::string("$variant")));\n};
print "}\n";
push @init_funcs, "init_$variant";
}
sub printdispatch {
print "#ifdef VCPP_BROKENNESS\n";
print "void dispatchCAOS(class caosVM *vm, int idx) {\n";
print "\tswitch (idx) {\n";
for my $impl (keys %disp_tbl) {
print "\t\tcase $disp_tbl{$impl}: vm->$impl(); break;\n";
}
print qq{\t\tdefault:\n\t\t\{\n};
print qq{\t\t\tchar buf[256];\n};
print qq{\t\t\tsprintf(buf, "%d", idx);\n};
print qq{\t\t\tthrow caosException(std::string("Unknown dispatchCAOS index: ") + buf);\n};
print qq{\t\t\}\n};
print "\t}\n";
print "}\n";
print "#endif\n";
}
sub writelookup {
my $cmds = shift;
for my $cmd (@$cmds) {
my $prefix = 'expr ';
if ($cmd->{type} eq 'command') {
$prefix = 'cmd ';
}
$cmd->{lookup_key} = $prefix . lc($cmd->{name});
}
}
sub printarr {
my ($cmds, $variant, $arrname) = @_;
my $buf = '';
$buf .= "static const struct cmdinfo $arrname\[\] = {\n";
my $idx = 0;
for my $cmd (@$cmds) {
my $argp = 'NULL';
if (defined $cmd->{arguments}) {
my $args = '';
for my $arg (@{$cmd->{arguments}}) {
my $type = $tdisp{$arg->{type}};
if (!defined $type) {
undef $args;
last;
}
$args .= "$type, ";
}
if (defined $args) {
$argp = "${arrname}_t_$cmd->{type}_$cmd->{key}";
print "static const enum ci_type $argp\[\] = { ";
print $args;
print "CI_END };\n";
}
}
$buf .= "\t{ // $idx $cmd->{key}\n";
$idx++;
$buf .= "#ifndef VCPP_BROKENNESS\n";
unless (defined($cmd->{implementation})) {
$cmd->{implementation} = 'caosVM::dummy_cmd';
}
unless (defined($cmd->{saveimpl})) {
$cmd->{saveimpl} = 'caosVM::dummy_cmd';
}
$buf .= "\t\t&$cmd->{implementation}, // handler\n";
$buf .= "\t\t&$cmd->{saveimpl}, // savehandler\n";
$buf .= "#else\n";
$buf .= sprintf "\t\t%d, // handler_idx\n", handler_idx($cmd->{implementation});
$buf .= sprintf "\t\t%d, // savehandler_idx\n", handler_idx($cmd->{saveimpl});
$buf .= "#endif\n";
$buf .= qq{\t\t"$cmd->{lookup_key}", // lookup_key\n};
$buf .= qq{\t\t"$cmd->{key}", // key\n};
$buf .= qq{\t\t"}. lc $cmd->{match}. qq{", // name\n};
$buf .= qq{\t\t"$cmd->{name}", // fullname\n};
$buf .= qq{\t\t"}. cescape($cmd->{description}). qq{", // docs\n};
$buf .= "\t\t". scalar(@{$cmd->{arguments}}). ", // argc\n";
$buf .= "\t\t$cmd->{stackdelta}, // stackdelta\n";
$buf .= "\t\t$argp, // argtypes\n";
my $rettype = $tdisp{$cmd->{type}};
if (!defined $rettype) {
die "Unknown return type $cmd->{type} in $cmd->{name}: ".YAML::Dump($cmd);
}
$buf .= "\t\t$rettype, // rettype\n";
my $cost = $cmd->{evalcost}{$variant};
$buf .= "\t\t$cost // evalcost\n";
$buf .= "\t},\n";
}
$buf .= "\t{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, CI_END, 0 }\n";
$buf .= "};\n";
print $buf;
}
sub sortname {
my $cmds = shift;
@$cmds = sort { $a->{lookup_key} cmp $b->{lookup_key} } @$cmds;
}
sub inject_ns {
my $cmds = shift;
my %ns;
my %names;
for my $cmd (@$cmds) {
my $type = ($cmd->{type} eq 'command') ? 'command' : 'any';
$ns{$cmd->{namespace}}{$type}++ if defined $cmd->{namespace};
$names{lc "$type $cmd->{name}"}++;
}
for my $ns (keys %ns) {
for my $type (keys %{$ns{$ns}}) {
next if $ns eq 'face'; # hack
next if exists $names{"$type " . lc $ns};
my $key = 'k_' . uc $ns;
$key =~ s/[^a-zA-Z0-9_]//g;
push @$cmds, {
arguments => [ {
name => "cmd",
type => "subcommand",
} ],
category => "internal",
description => "",
evalcost => { default => 0 },
filename => "",
implementation => undef,
match => uc $ns,
name => lc $ns,
pragma => {},
status => 'internal',
key => $key,
type => $type,
syntaxstring => (uc $ns) . " (command/expr) subcommand (subcommand)\n",
stackdelta => "INT_MAX",
};
}
}
}
sub checkdup {
my ($cmds, $desc) = @_;
my %mark;
for my $cmd (@$cmds) {
if (!defined $cmd->{lookup_key}) {
print STDERR "No name for $cmd->{key}\n";
exit 1;
}
push @{$mark{$cmd->{lookup_key}}}, $cmd;
if (scalar @{$mark{$cmd->{lookup_key}}} > 1) {
# Please do not disable this assert
# bsearch()'s behavior is unpredictable with duplicate keys
print STDERR "Duplicate command in $desc: $cmd->{lookup_key}\n";
print STDERR YAML::Dump($mark{$cmd->{lookup_key}});
exit 1;
}
}
}
our %cescapes;
BEGIN { %cescapes = (
"\n" => "\\n",
"\r" => "\\r",
"\t" => "\\t",
"\\" => "\\\\",
"\"" => "\\\"",
); }
sub cescape {
my $str = shift;
if (!defined $str) { return ""; }
my $ces = join "", keys %cescapes;
$str =~ s/([\Q$ces\E])/$cescapes{$1}/ge;
return $str;
}
sub handler_idx {
my $impl = $_[0];
unless (defined($disp_tbl{$impl})) {
$disp_tbl{$impl} = $disp_id++;
}
return $disp_tbl{$impl};
}
# vim: set noet: