drop old code.
[apps/madmutt.git] / lib-lua / luapkg2c.pl
1 #!/usr/bin/perl -w
2 #  This program is free software; you can redistribute it and/or modify
3 #  it under the terms of the GNU General Public License as published by
4 #  the Free Software Foundation; either version 2 of the License, or (at
5 #  your option) any later version.
6 #
7 #  This program is distributed in the hope that it will be useful, but
8 #  WITHOUT ANY WARRANTY; without even the implied warranty of
9 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 #  General Public License for more details.
11 #
12 #  You should have received a copy of the GNU General Public License
13 #  along with this program; if not, write to the Free Software
14 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15 #  MA 02110-1301, USA.
16 #
17 #  Copyright © 2007 Pierre Habouzit
18
19 use strict;
20
21 my %types;
22 my %pkgs;
23
24 #{{{ stream functions
25
26 sub stream_open($) {
27     my $name = shift;
28     open FILE, $name;
29     return (file => $name, line => 0, f => \*FILE);
30 }
31
32 sub stream_close($) {
33     my $stream = shift;
34     close $stream->{f};
35 }
36
37 sub stream_getline($) {
38     my $stream = shift;
39     my $file   = $stream->{f};
40     while (<$file>) {
41         ${$stream}{line}++;
42         return $_;
43     }
44     return;
45 }
46
47 #}}}
48
49 #{{{ file:line functions
50
51 sub fatal($$) {
52     my ($stream, $msg) = @_;
53     printf STDERR "%s:%d: %s\n", $stream->{file}, $stream->{line}, $msg;
54     exit 1;
55 }
56
57 sub get_pos($$) {
58     my ($h,$k) = @_;
59     return $h->{$k}{file}.':'.$h->{$k}{line};
60 }
61
62 sub put_line($$) {
63     my ($src,$inc) = @_;
64     printf "#line %d \"%s\"\n", $src->{line} + $inc, $src->{file};
65 }
66
67 #}}}
68
69 #{{{ parsers
70
71 sub parse_type($$) {
72     my ($src, $type) = @_;
73     my %t = (
74         name => $type,
75         file => $src->{file},
76         line => $src->{line},
77     );
78
79     if (defined $types{$type}) {
80         my $pos = get_pos(\%types, $type);
81         fatal($src, "already defined type: `$type' at $pos");
82     }
83
84     while (stream_getline($src)) {
85         last if (/^\s*\}\s*;\s*/);
86         if (/^\s*\.(kind|ctype|dtor|ctor|push|check)\s*=\s*(.*?)\s*;\s*$/) {
87             $t{$1} = $2;
88             if ($1 eq "kind") {
89                 if ($2 =~ /^'([bis])'$/) {
90                     $t{kind} = $1;
91                 } else {
92                     fatal($src, "error: .kind should be [ibs] got $2)");
93                 }
94             }
95         } elsif (!/^\s*$/) {
96             fatal($src, "syntax error: unknown type directive");
97         }
98     }
99
100     for ('kind', 'ctype') {
101         fatal($src, "incomplete type $type: missing .$_") unless defined $t{$_};
102     }
103     if ($t{kind} eq 's') {
104         for ('dtor', 'ctor') {
105             fatal($src, "incomplete type $type: missing .$_") unless defined $t{$_};
106         }
107     }
108     unless (defined $t{check}) {
109         if ($t{kind} eq 'b') {
110             $t{check} = '(luaL_checkint($L, $$) == 0)';
111         }
112
113         if ($t{kind} eq 'i') {
114             $t{check} = 'luaL_checkint($L, $$)';
115         }
116
117         if ($t{kind} eq 's') {
118             $t{check} = 'luaL_checkstring($L, $$)';
119         }
120     }
121
122     $t{ctypefmt} .= ' %s' unless (($t{ctypefmt} = $t{ctype}) =~ s/:/ \%s :/);
123     $t{ctype} =~ s/:.*//;
124
125     return $types{$type} = \%t;
126 }
127
128 sub parse_package($$) {
129     my ($src, $pkg) = @_;
130     my %p = (
131         name => $pkg,
132         file => $src->{file},
133         line => $src->{line},
134     );
135
136     if (defined $pkgs{$pkg}) {
137         my $pos = get_pos(\%pkgs, $pkg);
138         fatal($src, "already defined package: `$pkg' at $pos");
139     }
140
141     while (stream_getline($src)) {
142         if (/^\s*\}\s*(\w+)\s*;\s*$/) {
143             $p{cname} = $1;
144             last;
145         }
146
147         if (/^\s*(.*?)\s*=\s*(.*?)\s*;\s*$/) {
148             my ($lhs, $rhs) = ($1, $2);
149             my %m = ( file => $src->{file}, line => $src->{line} );
150
151             if ($lhs =~ /^((?:const\s+)?\w+?)\s*(\w+)$/) {
152                 $m{type} = $1;
153                 $m{name} = $2;
154                 $m{init} = $rhs;
155                 push @{$p{props}}, $2;
156             } elsif ($lhs =~ /^((?:const\s+)?\w+?)\s*(\w*)\((.*)\)$/) {
157                 $m{type}  = $1;
158                 $m{name}  = $2;
159                 $m{proto} = $3;
160                 $m{cfun}  = $rhs;
161                 push @{$p{meths}}, $2;
162             } else {
163                 fatal($src, "syntax error");
164             }
165
166             if (defined $p{members}{$m{name}}) {
167                 my $pos = get_pos($p{members}{$m{name}}, 0);
168                 fatal($src, "already defined package member: `$m{name}' at $pos");
169             }
170
171             $p{members}{$m{name}} = \%m;
172             next;
173         }
174
175         if (!/^\s*$/) {
176             fatal($src, "syntax error");
177         }
178     }
179
180     return $pkgs{$pkg} = \%p;
181 }
182
183 sub find_type($$) {
184     my ($ref, $typ) = @_;
185
186     if ($typ =~ /^const\s+(\w*)$/) {
187         fatal($ref, "undefined type `$1'") unless defined $types{$1};
188         return (const => 1, type => $types{$1});
189     }
190
191     fatal($ref, "undefined type `$typ'") unless defined $types{$typ};
192     return (const => 0, type => $types{$typ});
193 }
194
195 #}}}
196
197 #{{{ dump_fun
198
199 sub dump_fun($$) {
200     my ($pkg, $f) = @_;
201     my %t    = find_type($f, $f->{type});
202     my $call = $f->{cfun};
203     $call =~ s/\$/var/;
204
205     print "static int luaM_${pkg}_$f->{name}(lua_State *L) {\n";
206
207     if ($f->{proto} ne "void") {
208         my $i = 1;
209         for my $tmp (split /,/,$f->{proto}) {
210             s/^\s+//;
211             s/\s+$//;
212
213             my %pt = find_type($f, $tmp);
214             my $check = $pt{type}->{check};
215             $check =~ s/\$L/L/;
216             $check =~ s/\$\$/\%d/;
217
218             put_line($pt{type}, 0);
219             printf "    $pt{type}->{ctype} var$i = $check\n", $i++;
220         }
221     }
222
223     die "UNIMPLEMENTED" if defined $t{type}->{push};
224
225     if ($t{type}->{kind} eq 'b') {
226         put_line($f, 0);
227         print "    lua_pushboolean(L, $call);\n";
228     }
229
230     if ($t{type}->{kind} eq 'i') {
231         put_line($f, 0);
232         print "    lua_pushint(L, $call);\n";
233     }
234
235     if ($t{type}->{kind} eq 's') {
236         if ($t{const}) {
237             put_line($f, 0);
238             print "    lua_pushstring(L, $call);\n";
239         } else {
240             print "    {\n";
241             put_line($f, 0);
242             print "        char *s = $call;\n";
243             print "        lua_pushstring(L, s);\n";
244             print "        p_delete(&s);\n";
245             print "    }\n";
246         }
247     }
248
249     printf "    return %d;\n", ($f->{type} ne "void");
250     print "}\n";
251 }
252
253 #}}}
254
255 #{{{ dump_struct
256
257 sub dump_struct_full($) {
258     my ($pkg) = @_;
259
260     put_line($pkg, 0);
261     print "struct luaM_$pkg->{name}_t $pkg->{cname} = {\n";
262
263     foreach (@{$pkg->{props}}) {
264         my $p = $pkg->{members}{$_};
265         my %t = find_type($p, $p->{type});
266
267         if ($t{const}) {
268             put_line($p, 0);
269             print "    $p->{init},\n";
270         } else {
271             print "    0,\n"    if ($t{type}->{kind} eq 'b');
272             print "    0,\n"    if ($t{type}->{kind} eq 'i');
273             print "    NULL,\n" if ($t{type}->{kind} eq 's');
274         }
275     }
276
277     print <<EOF;
278 };
279
280 static void $pkg->{cname}_init(void)
281 {
282 EOF
283
284     foreach (@{$pkg->{props}}) {
285         my $p   = $pkg->{members}{$_};
286         my %t   = find_type($p, $p->{type});
287         my $var = $pkg->{cname}.".".$p->{name};
288
289         next if $t{const};
290
291         put_line($p, 0);
292         if ($t{type}->{dtor}) {
293             my $dtor = $t{type}->{dtor};
294             $dtor =~ s/\$L/L/;
295             $dtor =~ s/\$\$/\&$var/;
296             print "    $dtor;\n";
297         }
298         print "    $var = $p->{init};\n"
299     }
300     print "};\n";
301 };
302
303 sub dump_struct_short($) {
304     my ($pkg) = @_;
305
306     print "struct luaM_$pkg->{name}_t {\n";
307     foreach (@{$pkg->{props}}) {
308         my $p = $pkg->{members}{$_};
309         my %t = find_type($p, $p->{type});
310         if ($t{const}) {
311             printf "    const $t{type}->{ctypefmt};\n", $p->{name};
312         } else {
313             printf "    $t{type}->{ctypefmt};\n", $p->{name};
314         }
315     }
316     put_line($pkg, 0);
317     print <<EOF;
318 };
319
320 extern struct luaM_$pkg->{name}_t $pkg->{cname};
321 EOF
322 }
323
324 sub dump_struct_static($) {
325     my ($pkg) = @_;
326
327     print "static struct {\n";
328     foreach (@{$pkg->{props}}) {
329         my $p = $pkg->{members}{$_};
330         my %t = find_type($p, $p->{type});
331         if ($t{const}) {
332             printf "    const $t{type}->{ctypefmt};\n", $p->{name};
333         } else {
334             printf "    $t{type}->{ctypefmt};\n", $p->{name};
335         }
336     }
337     put_line($pkg, 0);
338     print "} $pkg->{cname} = {\n";
339
340     foreach (@{$pkg->{props}}) {
341         my $p = $pkg->{members}{$_};
342         my %t = find_type($p, $p->{type});
343
344         if ($t{const}) {
345             put_line($p, 0);
346             print "    $p->{init},\n";
347         } else {
348             print "    0,\n"    if ($t{type}->{kind} eq 'b');
349             print "    0,\n"    if ($t{type}->{kind} eq 'i');
350             print "    NULL,\n" if ($t{type}->{kind} eq 's');
351         }
352     }
353
354     print <<EOF;
355 };
356
357 static void $pkg->{cname}_init(void)
358 {
359 EOF
360
361     foreach (@{$pkg->{props}}) {
362         my $p   = $pkg->{members}{$_};
363         my %t   = find_type($p, $p->{type});
364         my $var = $pkg->{cname}.".".$p->{name};
365
366         next if $t{const};
367
368         put_line($p, 0);
369         if ($t{type}->{dtor}) {
370             my $dtor = $t{type}->{dtor};
371             $dtor =~ s/\$L/L/;
372             $dtor =~ s/\$\$/\&$var/;
373             print "    $dtor;\n";
374         }
375         print "    $var = $p->{init};\n"
376     }
377     print "};\n";
378 };
379
380 #}}}
381
382 #{{{ dump package
383
384 sub dump_package_full($$) {
385     my ($pkg, $static) = @_;
386
387     if ($static) {
388         dump_struct_static($pkg);
389     } else {
390         dump_struct_full($pkg);
391     }
392     map { print "\n"; dump_fun($pkg->{name}, $pkg->{members}{$_}); } @{$pkg->{meths}};
393     print <<EOF;
394
395 static const luaL_reg luaM_$pkg->{name}_methods[] = {
396 EOF
397     map { print "    { \"$_\", luaM_$pkg->{name}_$_ },\n"; } @{$pkg->{meths}};
398     print <<EOF;
399     { NULL, NULL }
400 };
401
402 static int luaM_$pkg->{name}_index(lua_State *L)
403 {
404     const char *idx = luaL_checkstring(L, 2);
405
406     switch (mlua_which_token(idx, -1)) {
407 EOF
408
409     foreach (@{$pkg->{props}}) {
410         my $p = $pkg->{members}{$_};
411         my %t = find_type($p, $p->{type});
412         my $call = $t{type}->{push} || '$$';
413
414         $call =~ s/\$L/L/;
415         $call =~ s/\$\$/$pkg->{cname}.$p->{name}/;
416
417         my $tok = $p->{name};
418         $tok =~ tr/a-z/A-Z/;
419
420         if ($t{type}->{kind} eq 'b') {
421             $call = "lua_pushboolean(L, $call)";
422         }
423
424         if ($t{type}->{kind} eq 'i') {
425             $call = "lua_pushinteger(L, $call)";
426         }
427
428         if ($t{type}->{kind} eq 's') {
429             $call = "lua_pushstring(L, $call)";
430         }
431
432         put_line($p, 0);
433         print  "      case LTK_$tok:\n";
434         printf "        $call;\n";
435         print  "        return 1;\n";
436     }
437
438 print <<EOF;
439       default:
440         lua_rawget(L, lua_upvalueindex(2));       /* try methods       */
441         return 1;
442     }
443 }
444
445 static int luaM_$pkg->{name}_newindex(lua_State *L)
446 {
447     const char *idx = luaL_checkstring(L, 2);
448
449     switch (mlua_which_token(idx, -1)) {
450 EOF
451
452     foreach (@{$pkg->{props}}) {
453         my $p = $pkg->{members}{$_};
454         my %t = find_type($p, $p->{type});
455
456         next if ($t{const});
457
458         my $tok = $p->{name};
459         my $var = $pkg->{cname}.".".$p->{name};
460         my $check = $t{type}->{check};
461         $tok   =~ tr/a-z/A-Z/;
462         $check =~ s/\$L/L/;
463         $check =~ s/\$\$/3/;
464
465         put_line($p, 0);
466         print  "      case LTK_$tok: \n";
467         if ($t{type}->{dtor}) {
468             my $dtor = $t{type}->{dtor};
469             $dtor =~ s/\$L/L/;
470             $dtor =~ s/\$\$/\&$var/;
471             print "        $dtor;\n";
472         }
473         if ($t{type}->{ctor}) {
474             my $ctor = $t{type}->{ctor};
475             $ctor =~ s/\$L/L/;
476             $ctor =~ s/\$\$/$check/;
477             $check = $ctor;
478         }
479         print "        $var = $check;\n";
480         print "        return 1;\n";
481     }
482
483 print <<EOF;
484       default:
485         return 1;
486     }
487 }
488
489 int luaopen_$pkg->{name}(lua_State *L)
490 {
491     int mt, methods;
492
493     $pkg->{cname}_init();
494
495     /* create methods table, add it the the table of globals */
496     luaL_openlib(L, "$pkg->{name}", luaM_$pkg->{name}_methods, 0);
497     methods = lua_gettop(L);
498
499     /* create metatable for $pkg->{name}, add it to the registry */
500     luaL_newmetatable(L, "$pkg->{name}");
501     mt = lua_gettop(L);
502
503     lua_pushliteral(L, "__index");
504     lua_pushvalue(L, mt);                       /* upvalue 1         */
505     lua_pushvalue(L, methods);                  /* upvalue 2         */
506     lua_pushcclosure(L, &luaM_$pkg->{name}_index, 2);
507     lua_rawset(L, mt);                          /* set mt.__index    */
508
509     lua_pushliteral(L, "__newindex");
510     lua_newtable(L);                            /* for new members   */
511     lua_pushcclosure(L, &luaM_$pkg->{name}_newindex, 1);
512     lua_rawset(L, mt);                          /* set mt.__newindex */
513
514     lua_pushliteral(L, "__metatable");
515     lua_pushvalue(L, methods);                  /* dup methods table */
516     lua_rawset(L, mt);                          /* hide metatable    */
517
518     lua_setmetatable(L, methods);
519
520     lua_pop(L, 1);                              /* drop mt           */
521     return 1;                                   /* return methods    */
522 }
523
524 EOF
525 }
526
527 sub dump_package_short($) {
528     my $pkg = shift;
529     my $upp = $pkg->{name};
530     $upp =~ tr/a-z/A-Z/;
531
532     print <<EOF;
533 #ifndef MUTT_LUA_${upp}_H
534 #define MUTT_LUA_${upp}_H
535
536 EOF
537     dump_struct_short($pkg);
538     print <<EOF
539
540 int luaopen_$pkg->{name}(lua_State *L);
541
542 #endif /* MUTT_LUA_${upp}_H */
543 EOF
544 }
545
546 #}}}
547
548 sub do_c($) {
549     my %src = stream_open(shift);
550     my $resync = 1;
551
552     while (stream_getline(\%src)) {
553         if (/^\s*\@type\s+([a-zA-Z]\w*)\s*=\s*{\s*$/) {
554             parse_type(\%src, $1);
555             $resync = 1;
556         } elsif (/^\s*static\s+\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
557             dump_package_full(parse_package(\%src, $1), 1);
558             $resync = 1;
559         } elsif (/^\s*\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
560             dump_package_full(parse_package(\%src, $1), 0);
561             $resync = 1;
562         } elsif (/^\s*(\@\w*)/) {
563             fatal(\%src, "syntax error: unknown directive `$1'");
564         } else {
565             next if ($resync && /^\s+$/);
566             if ($resync) {
567                 put_line(\%src, 0);
568                 $resync = 0;
569             }
570             print;
571         }
572     }
573
574     stream_close(\%src);
575 }
576
577 sub do_h($) {
578     my %src = stream_open(shift);
579     my $resync = 1;
580
581     while (stream_getline(\%src)) {
582         if (/^\s*\@type\s+([a-zA-Z]\w*)\s*=\s*{\s*$/) {
583             parse_type(\%src, $1);
584         } elsif (/^\s*\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
585             dump_package_short(parse_package(\%src, $1));
586         } elsif (/^\s*(\@\w*)/) {
587             fatal(\%src, "syntax error: unknown directive `$1'");
588         }
589     }
590
591     stream_close(\%src);
592 }
593
594 if ($#ARGV < 1 || ($ARGV[0] ne "-h" && $ARGV[0] ne "-c")) {
595     print <<EOF;
596 usage: mluapkg (-h | -c) file.pkg
597 EOF
598     exit 1;
599 }
600
601 print <<EOF;
602 /*****     THIS FILE IS AUTOGENERATED DO NOT MODIFY DIRECTLY !    *****/
603 EOF
604
605 map { do_h($ARGV[$_]); } (1 .. $#ARGV) if ($ARGV[0] eq '-h');
606 map { do_c($ARGV[$_]); } (1 .. $#ARGV) if ($ARGV[0] eq '-c');