chiark / gitweb /
Invalidate string rep. Before make expandable buffer.
[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/^\w+$/) {
67                 $c_entry= $_;
68             } elsif (m/^(\w+)\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 (keys %$r_table) {
148         $r_entry= $r_table->{$c_entry};
149         $pa_decl= "int pa_${c_table}_${c_entry}(ClientData cd,".
150             " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
151         $do_decl= "int do_${c_table}_${c_entry}(";
152         @do_al= ('ClientData cd', 'Tcl_Interp *ip');
153         @do_aa= qw(cd ip);
154         $pa_init= '';
155         $pa_argc= "  objc--; objv++;\n";
156         $pa_vars= "  int rc;\n";
157         $pa_body= '';
158         $pa_rslt= '';
159         $pa_free= '';
160         $pa_fini= '';
161         $any_mand= 0;
162         $any_optl= 0;
163         $any_eerr= 0;
164         foreach $arg (@{ $r_entry->{A} }) {
165             $n= $arg->{N};
166             $t= $arg->{T};
167             $a= $arg->{A};
168             push @do_al, make_decl($n, $t, $arg->{A});
169             $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
170             if ($arg->{O}) {
171                 if ($any_mand) {
172                     $pa_argc .= "  if (objc < $any_mand) {".
173                         " e=\"too few args\"; goto e_err; }\n";
174                     $pa_body .= "  objc -= $any_mand;\n";
175                     $any_mand= 0;
176                     $any_eerr= 1;
177                 }
178                 $pa_body .= "  if (!objc--) goto end_optional;\n";
179                 $any_optl= 1;
180             } else {
181                 die if $any_optl;
182                 $any_mand++;
183             }
184             $paarg= "&a_$n";
185             $pafin= '';
186             if ($t eq 'enum') {
187                 $pa_vars .= "  const void *v_$n= 0;\n";
188                 $paarg= "&v_$n";
189                 $pafin= "\n  a_$n= v_$n; ";
190                 $a =~ m/\,/ or die "invalid enum type \`$a'\n";
191                 $a_tab = lc($`).'s';
192                 $a = "$a_tab, sizeof($`), $'";
193                 o('h', 210, "extern const $` $a_tab".'[]'.";\n");
194             }
195             if (exists $type_fini{$t}) {
196                 $pa_fini .= '  '.subst_in("a_$n", $type_fini{$t})."\n";
197             }
198             $pa_body .= "  rc= pat_$t(ip, *objv++, $paarg";
199             $pa_body .= ", ".$a if length $a;
200             $pa_body .= ");$pafin if (rc) goto rc_err;\n";
201             push @do_aa, "a_$n";
202         }
203         if (exists $r_entry->{V}) {
204             if ($any_mand) {
205                 $pa_body .= "  objc -= $any_mand;\n";
206             }
207             $va= $r_entry->{V};
208             push @do_al, subst_in_decl("${va}c", 'int @');
209             push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
210             push @do_aa, "objc+1", "objv-1";
211         } else {
212             if (!$any_optl) {
213                 $pa_argc .= "  if (objc != $any_mand) {".
214                     " e=\"wrong number of args\"; goto e_err; }\n";
215                 $any_eerr= 1;
216             }
217         }
218         if ($any_optl) {
219             $pa_body .= "end_optional:\n";
220         }
221         if (exists $r_entry->{R}) {
222             $t= $r_entry->{R};
223             push @do_al, make_decl("*result", $t);
224             $pa_vars .= make_decl_init("result", $t, '', \$pa_init);
225             push @do_aa, "&result";
226             $pa_rslt .= "  Tcl_SetObjResult(ip, ret_$t(ip, result));\n";
227         }
228         $pa_body .= "\n";
229         $pa_body .= "  rc= do_${c_table}_${c_entry}(";
230         $pa_body .= join ', ', @do_aa;
231         $pa_body .= ");\n";
232         $pa_body .= "  if (rc) goto rc_err;\n";
233
234         $pa_rslt .= "  rc= TCL_OK;\n\n";
235         $pa_rslt .= "rc_err:\n";
236         
237         $pa_fini .= "  return rc;\n";
238         if ($any_eerr) {
239             $pa_vars .= "  const char *e;\n";
240             $pa_fini .= "\n";
241             $pa_fini .= "e_err:\n";
242             $pa_fini .= "  setstringresult(ip,e);";
243             $pa_fini .= "  rc= TCL_ERROR; goto rc_err;\n";
244         }
245         $pa_vars .= "\n";
246         $pa_init .= "\n" if length $pa_init;
247         $pa_fini .= "}\n\n";
248
249         if (length $c_table) {
250             $static= 'static ';
251         } else {
252             $static= '';
253             o('h',90, "$pa_decl;\n");
254         }
255         o('c',100,
256           $static.$pa_decl." {\n".
257           $pa_vars.
258           $pa_init.
259           $pa_argc.
260           $pa_body.
261           $pa_rslt.
262           $pa_free.
263           $pa_fini);
264         $do_decl .= join ', ', @do_al;
265         $do_decl .= ")";
266         o('h',100, $do_decl.";\n") or die $!;
267         
268
269         $op_tab .= sprintf("  { %-20s %-40s%s },\n",
270                            "\"$c_entry\",",
271                            "pa_${c_table}_${c_entry}",
272                            $r_entry->{I});
273     }
274     if (length $c_table) {
275         $decl= "const $x_table->{C} ".lc($x_table->{C}).'s[]';
276         o('h', 500, "extern $decl;\n");
277         o('c', 100,
278           "$decl = {\n".
279           $op_tab.
280           "  { 0 }\n".
281           "};\n\n");
282     }
283 }
284
285 o(c, 0, "#include \"$prefix.h\"\n");
286
287 o(h, 0,
288   "#ifndef INCLUDED_\U${prefix}_H\n".
289   "#define INCLUDED_\U${prefix}_H\n\n".
290   "#include <tcl.h>\n");
291
292 o(h, 400,
293   "void setstringresult(Tcl_Interp*, const char*);\n".
294   "int pat_enum(Tcl_Interp*, Tcl_Obj*, const void**,".
295   "             const void*, size_t, const char *what);\n");
296
297 o(h, 999,
298   "#endif /*INCLUDED_\U${prefix}_H*/\n");
299
300 if (defined $output) {
301     $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
302 } else {
303     $oh= 'STDOUT';
304 }
305
306 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
307 foreach $pr (sort keys %{ $o{$write} }) {
308     print $oh "\n" or die $!;
309     print $oh $o{$write}{$pr} or die $!;
310 }
311
312 die if $oh->error;
313 die $! unless $oh->close;
314
315 if (defined $output) {
316     rename "$output.tmp", $output or die $!;
317 }
318
319 sub o ($$) {
320     my ($wh,$pr,$s) = @_;
321     $o{$wh}{sprintf "%010d", $pr} .= $s;
322 }
323
324 sub make_decl_init ($$$$) {
325     my ($n, $t, $a, $initcode) = @_;
326     my ($o,$init);
327     $o= make_decl($n,$t,$a);
328     if (exists $type_init{$t}) {
329         $init= $type_init{$t};
330         $$initcode .= "  ".subst_in("$n", $init)."\n"
331             if length $init;
332     } else {
333         $o .= ' =0';
334     }
335     return "  ".$o.";\n";
336 }
337
338 sub make_decl ($$$) {
339     my ($n, $t, $ta) = @_;
340     my ($type);
341     if ($t eq 'enum') {
342         $ta =~ m/\,/ or die "invalid enum type \`$t'\n";
343         $c= "const $` *@";
344     } else { 
345         defined $types{$t} or die "unknown type $t\n";
346         $c= $types{$t}{C};
347     }
348     return subst_in_decl($n,$c);
349 }
350
351 sub subst_in_decl ($$$) {
352     my ($val, $pat, $why) = @_;
353     local ($_) = subst_in($val, $pat, $why);
354     s/ *(\**) *$/$1/;
355     return $_;
356 }
357     
358 sub subst_in ($$$) {
359     my ($val, $pat, $why) = @_;
360     $pat =~ m/\@/ or die "$pat for $val in $why ?";
361     $pat =~ s/\@/$val/g;
362     return $pat;
363 }
364
365 sub badsyntax ($$$) {
366     die "$_[0]:$_[1]: $_[2]\n";
367 }
368
369 __DATA__
370 Type int:       int
371 Type obj:       Tcl_Obj *@
372
373 Type charfrom(const char *opts, const char *what):      int