chiark / gitweb /
Padding works.
[chiark-tcl.git] / base / tcmdifgen
1 #!/usr/bin/perl
2
3 use IO;
4 use Data::Dumper;
5
6 parse('builtins','DATA');
7
8 while (@ARGV) {
9     $_= shift @ARGV;
10     if (m/^\-p(\w+)/) {
11         $prefix= $1;
12     } elsif (m/^\-w(c|h)$/) {
13         $write= $1;
14     } elsif (m/^\-o(.+)$/) {
15         $output= $1;
16     } elsif (m/^\-/) {
17         die "unknown option $_\n";
18     } else {
19         if (!defined $prefix) { $prefix= $_;  $prefix =~ s/\.[^.]+$//; }
20         $x= new IO::File $_,'r' or die "$_: $!\n";
21         parse($_,$x);
22     }
23 }
24
25 die "must say -w<something>\n" if !defined $write;
26
27 sub zilch () {
28     undef $c_table;
29     undef $c_entryextra;
30     undef $c_of;
31 }
32
33 sub parse ($$) {
34     my ($wh,$f) = @_;
35     while (defined($_= $f->getline)) {
36         chomp; s/\s+$//;
37         next if m/^\s*\#/;
38         next if !m/\S/;
39         while (s/\t/ ' 'x(8 - (length $`) % 8) /e) { }
40
41         s/^\s*//;
42         $this_indent= length $&;
43         while (@i && $this_indent < $i[0]) {
44             shift @i;
45         }
46         if ($this_indent && (!@i || $this_indent > $i[0])) {
47             unshift @i, $this_indent;
48         }
49
50         if (@i==0 && m/^Table\s+(\w+)\s+(\w+)$/) {
51             zilch();
52             $c_table= $1;
53             $table_x{$c_table}{C}= $2;
54             $entrytype_x{$2}= '';
55         } elsif (@i==0 && m/^Untabled$/) {
56             zilch();
57             $c_table= '';
58         } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
59             o(lc $1, 30, "#include $2\n");
60         } elsif (@i==0 && m/^EntryExtra\s+(\w+)$/) {
61             zilch();
62             $c_entryextra= $1;
63         } elsif (@i>=1 && defined $c_entryextra) {
64             $entrytype_x{$c_entryextra} .= "  $_\n";
65         } elsif (@i==1 && m/^[a-z].*$/ && defined $c_table) {
66             if (m/^[-_0-9A-Za-z]+$/) {
67                 $c_entry= $_;
68             } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) {
69                 $c_entry= $1;
70                 $tables{$c_table}{$c_entry}{I} .= ", $2";
71             } else {
72                 badsyntax($wh,$.,"bad entry");
73             }
74             $tables{$c_table}{$c_entry}{A} = [ ];
75         } elsif (@i==2 && m/^(\w+)\s+\.\.\.$/ && defined $c_entry) {
76             $tables{$c_table}{$c_entry}{V}= $1;
77         } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
78                  && defined $c_entry) {
79             ($opt, $var, $type) = ($1,$2,$3);
80             if ($type =~ m/^\w+$/) {
81                 $xtypeargs='';
82             } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
83                 $type= $1;
84                 $xtypeargs= $2;
85             }
86             push @{ $tables{$c_table}{$c_entry}{A} },
87                 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
88         } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
89             $tables{$c_table}{$c_entry}{R}= $1;
90         } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
91             ($typename,$ctype)= ($1,$2);
92             $ctype .= ' @' unless $ctype =~ m/\@/;
93             if ($typename =~ m/^\w+$/) {
94                 $xtypeargs='';
95             } elsif ($typename =~ m/^(\w+)\((.+)\)$/) {
96                 $typename=$1;
97                 $xtypeargs=$2;
98             } else {
99                 badsyntax($wh,$.,"bad type name/args");
100             }
101             $types{$typename}= { C => $ctype, X => $xtypeargs };
102         } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
103             $type_init{$1}= $2;
104         } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
105             $type_fini{$1}= $2;
106         } else {
107             badsyntax($wh,$., sprintf
108                       "bad directive (indent level %d)", scalar @i);
109         }
110     }
111     $f->error and die $!;
112     $f->close;
113 }
114
115 #print Dumper(\%tables),"\n";
116 #print Dumper(\%types),"\n";
117
118 foreach $t (sort keys %types) {
119     $type= $types{$t};
120     $c= $type->{C};
121     $xta= $type->{X};
122     $decl= "int pat_$t(Tcl_Interp *ip, Tcl_Obj *obj, ";
123     $decl .= subst_in_decl('*val', $c, "type $t");
124     $decl .= ", $xta",  if length $xta;
125     $decl .= ");\n";
126     o('h',160, $decl);
127
128     $decl= "Tcl_Obj *ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c).");\n";
129     o('h',170, $decl);
130 }
131
132 foreach $c_entrytype (sort keys %entrytype_x) {
133     o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
134     o('h', 100,
135       "struct $c_entrytype {\n".
136       "  const char *name;\n".
137       "  Tcl_ObjCmdProc *func;\n".
138       $entrytype_x{$c_entrytype}.
139       "};\n\n");
140 }
141
142 foreach $c_table (sort keys %tables) {
143     $r_table= $tables{$c_table};
144     $x_table= $table_x{$c_table};
145     $op_tab= '';
146
147     foreach $c_entry (sort keys %$r_table) {
148         $c_entry_c= $c_entry; $c_entry_c =~ y/-/_/;
149         $r_entry= $r_table->{$c_entry};
150         $pa_decl= "int pa_${c_table}_${c_entry_c}(ClientData cd,".
151             " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
152         $do_decl= "int do_${c_table}_${c_entry_c}(";
153         @do_al= ('ClientData cd', 'Tcl_Interp *ip');
154         @do_aa= qw(cd ip);
155         $pa_init= '';
156         $pa_argc= "  objc--; objv++;\n";
157         $pa_vars= "  int rc;\n";
158         $pa_body= '';
159         $pa_rslt= '';
160         $pa_free= '';
161         $pa_fini= '';
162         $any_mand= 0;
163         $any_optl= 0;
164         $any_eerr= 0;
165         foreach $arg (@{ $r_entry->{A} }) {
166             $n= $arg->{N};
167             $t= $arg->{T};
168             $a= $arg->{A};
169             push @do_al, make_decl($n, $t, $arg->{A});
170             $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
171             if ($arg->{O}) {
172                 if ($any_mand) {
173                     $pa_argc .= "  if (objc < $any_mand) {".
174                         " e=\"too few args\"; goto e_err; }\n";
175                     $pa_body .= "  objc -= $any_mand;\n";
176                     $any_mand= 0;
177                     $any_eerr= 1;
178                 }
179                 $pa_body .= "  if (!objc--) goto end_optional;\n";
180                 $any_optl= 1;
181             } else {
182                 die if $any_optl;
183                 $any_mand++;
184             }
185             $paarg= "&a_$n";
186             $pafin= '';
187             if ($t eq 'enum') {
188                 $pa_vars .= "  const void *v_$n= 0;\n";
189                 $paarg= "&v_$n";
190                 $pafin= "\n  a_$n= v_$n; ";
191                 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
192                 $a_tab = lc($`).'s';
193                 $a = "$a_tab, sizeof($`), $'";
194                 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
195             }
196             if (exists $type_fini{$t}) {
197                 $pa_fini .= '  '.subst_in("a_$n", $type_fini{$t})."\n";
198             }
199             $pa_body .= "  rc= pat_$t(ip, *objv++, $paarg";
200             $pa_body .= ", ".$a if length $a;
201             $pa_body .= ");$pafin if (rc) goto rc_err;\n";
202             push @do_aa, "a_$n";
203         }
204         if (exists $r_entry->{V}) {
205             if ($any_mand) {
206                 $pa_body .= "  objc -= $any_mand;\n";
207             }
208             $va= $r_entry->{V};
209             push @do_al, subst_in_decl("${va}c", 'int @');
210             push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
211             push @do_aa, "objc+1", "objv-1";
212         } else {
213             if (!$any_optl) {
214                 $pa_argc .= "  if (objc != $any_mand) {".
215                     " e=\"wrong number of args\"; goto e_err; }\n";
216                 $any_eerr= 1;
217             }
218         }
219         if ($any_optl) {
220             $pa_body .= "end_optional:\n";
221         }
222         if (exists $r_entry->{R}) {
223             $t= $r_entry->{R};
224             push @do_al, make_decl("*result", $t);
225             $pa_vars .= make_decl_init("result", $t, '', \$pa_init);
226             push @do_aa, "&result";
227             $pa_rslt .= "  Tcl_SetObjResult(ip, ret_$t(ip, result));\n";
228         }
229         $pa_body .= "\n";
230         $pa_body .= "  rc= do_${c_table}_${c_entry_c}(";
231         $pa_body .= join ', ', @do_aa;
232         $pa_body .= ");\n";
233         $pa_body .= "  if (rc) goto rc_err;\n";
234
235         $pa_rslt .= "  rc= TCL_OK;\n\n";
236         $pa_rslt .= "rc_err:\n";
237         
238         $pa_fini .= "  return rc;\n";
239         if ($any_eerr) {
240             $pa_vars .= "  const char *e;\n";
241             $pa_fini .= "\n";
242             $pa_fini .= "e_err:\n";
243             $pa_fini .= "  setstringresult(ip,e);";
244             $pa_fini .= "  rc= TCL_ERROR; goto rc_err;\n";
245         }
246         $pa_vars .= "\n";
247         $pa_init .= "\n" if length $pa_init;
248         $pa_fini .= "}\n\n";
249
250         if (length $c_table) {
251             $static= 'static ';
252         } else {
253             $static= '';
254             o('h',90, "$pa_decl;\n");
255         }
256         o('c',100,
257           $static.$pa_decl." {\n".
258           $pa_vars.
259           $pa_init.
260           $pa_argc.
261           $pa_body.
262           $pa_rslt.
263           $pa_free.
264           $pa_fini);
265         $do_decl .= join ', ', @do_al;
266         $do_decl .= ")";
267         o('h',100, $do_decl.";\n") or die $!;
268         
269
270         $op_tab .= sprintf("  { %-20s %-40s%s },\n",
271                            "\"$c_entry\",",
272                            "pa_${c_table}_${c_entry_c}",
273                            $r_entry->{I});
274     }
275     if (length $c_table) {
276         $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
277         o('h', 500, "extern $decl;\n");
278         o('c', 100,
279           "$decl = {\n".
280           $op_tab.
281           "  { 0 }\n".
282           "};\n\n");
283     }
284 }
285
286 o(c, 0, "#include \"$prefix.h\"\n");
287
288 o(h, 0,
289   "#ifndef INCLUDED_\U${prefix}_H\n".
290   "#define INCLUDED_\U${prefix}_H\n\n".
291   "#include <tcl.h>\n");
292
293 o(h, 400,
294   "void setstringresult(Tcl_Interp*, const char*);\n".
295   "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,".
296   "             const void*, size_t, const char *what);\n");
297
298 o(h, 999,
299   "#endif /*INCLUDED_\U${prefix}_H*/\n");
300
301 if (defined $output) {
302     $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
303 } else {
304     $oh= 'STDOUT';
305 }
306
307 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
308 foreach $pr (sort keys %{ $o{$write} }) {
309     print $oh "\n" or die $!;
310     print $oh $o{$write}{$pr} or die $!;
311 }
312
313 die if $oh->error;
314 die $! unless $oh->close;
315
316 if (defined $output) {
317     rename "$output.tmp", $output or die $!;
318 }
319
320 sub o ($$) {
321     my ($wh,$pr,$s) = @_;
322     $o{$wh}{sprintf "%010d", $pr} .= $s;
323 }
324
325 sub make_decl_init ($$$$) {
326     my ($n, $t, $a, $initcode) = @_;
327     my ($o,$init);
328     $o= make_decl($n,$t,$a);
329     if (exists $type_init{$t}) {
330         $init= $type_init{$t};
331         $$initcode .= "  ".subst_in("$n", $init)."\n"
332             if length $init;
333     } else {
334         $o .= ' =0';
335     }
336     return "  ".$o.";\n";
337 }
338
339 sub make_decl ($$$) {
340     my ($n, $t, $ta) = @_;
341     my ($type);
342     if ($t eq 'enum') {
343         $ta =~ m/\,/ or die "invalid enum type \`$t'\n";
344         $c= "const $` *@";
345     } else { 
346         defined $types{$t} or die "unknown type $t\n";
347         $c= $types{$t}{C};
348     }
349     return subst_in_decl($n,$c);
350 }
351
352 sub subst_in_decl ($$$) {
353     my ($val, $pat, $why) = @_;
354     local ($_) = subst_in($val, $pat, $why);
355     s/ *(\**) *$/$1/;
356     return $_;
357 }
358     
359 sub subst_in ($$$) {
360     my ($val, $pat, $why) = @_;
361     $pat =~ m/\@/ or die "$pat for $val in $why ?";
362     $pat =~ s/\@/$val/g;
363     return $pat;
364 }
365
366 sub badsyntax ($$$) {
367     die "$_[0]:$_[1]: $_[2]\n";
368 }
369
370 __DATA__
371 Type int:       int
372 Type obj:       Tcl_Obj *@
373
374 Type charfrom(const char *opts, const char *what):      int