Lua improvements:
[apps/madmutt.git] / lib-lua / luapkg2c.pl
index 2d152cb..72d84c7 100755 (executable)
@@ -86,10 +86,10 @@ sub parse_type($$) {
         if (/^\s*\.(push|kind|ctype|check)\s*=\s*(.*?)\s*;\s*$/) {
             $t{$1} = $2;
             if ($1 eq "kind") {
-                if ($2 =~ /^'([si])'$/) {
+                if ($2 =~ /^'([bis])'$/) {
                     $t{kind} = $1;
                 } else {
-                    fatal($src, "error: .kind should be 'i' or 's' got $2)");
+                    fatal($src, "error: .kind should be [ibs] got $2)");
                 }
             }
         } elsif (!/^\s*$/) {
@@ -101,15 +101,22 @@ sub parse_type($$) {
         fatal($src, "incomplete type $type: missing .$i") unless defined $t{$i};
     }
     unless (defined $t{check}) {
-        if ($t{kind} eq 's') {
-            $t{check} = 'luaL_checkstring($L, $$)';
+        if ($t{kind} eq 'b') {
+            $t{check} = '(luaL_checkint($L, $$) == 0)';
         }
 
         if ($t{kind} eq 'i') {
-            $t{check} = 'luaL_checkint($l, $$)';
+            $t{check} = 'luaL_checkint($L, $$)';
+        }
+
+        if ($t{kind} eq 's') {
+            $t{check} = 'luaL_checkstring($L, $$)';
         }
     }
 
+    $t{ctypefmt} .= ' %s' unless (($t{ctypefmt} = $t{ctype}) =~ s/:/ \%s :/);
+    $t{ctype} =~ s/:.*//;
+
     return $types{$type} = \%t;
 }
 
@@ -212,6 +219,16 @@ sub dump_fun($$) {
         $call =~ s/\$\$/$t{type}->{push}/;
     }
 
+    if ($t{type}->{kind} eq 'b') {
+        put_line($f, 0);
+        print "    lua_pushboolean(L, $call);\n";
+    }
+
+    if ($t{type}->{kind} eq 'i') {
+        put_line($f, 0);
+        print "    lua_pushint(L, $call);\n";
+    }
+
     if ($t{type}->{kind} eq 's') {
         if ($t{const}) {
             put_line($f, 0);
@@ -226,11 +243,6 @@ sub dump_fun($$) {
         }
     }
 
-    if ($t{type}->{kind} eq 'i') {
-        put_line($f, 0);
-        print "    lua_pushint(L, $call);\n";
-    }
-
     printf "    return %d;\n", ($f->{type} ne "void");
     print "}\n";
 }
@@ -253,6 +265,7 @@ sub dump_struct_full($) {
             put_line($p, 0);
             print "    $p->{init},\n";
         } else {
+            print "    0,\n"    if ($t{type}->{kind} eq 'b');
             print "    0,\n"    if ($t{type}->{kind} eq 'i');
             print "    NULL,\n" if ($t{type}->{kind} eq 's');
         }
@@ -273,8 +286,9 @@ EOF
         next if $t{const};
 
         put_line($p, 0);
-        print "        m_strreplace(&$var, $p->{init});\n" if ($t{type}->{kind} eq 's');
+        print "        $var = $p->{init};\n"               if ($t{type}->{kind} eq 'b');
         print "        $var = $p->{init};\n"               if ($t{type}->{kind} eq 'i');
+        print "        m_strreplace(&$var, $p->{init});\n" if ($t{type}->{kind} eq 's');
     }
     print "};\n";
 };
@@ -286,7 +300,7 @@ sub dump_struct_short($) {
     foreach (@{$pkg->{props}}) {
         my $p = $pkg->{members}{$_};
         my %t = find_type($p, $p->{type});
-        print "    $t{type}->{ctype} $p->{name};\n";
+        printf "    $t{type}->{ctypefmt};\n", $p->{name};
     }
     put_line($pkg, 0);
     print <<EOF;
@@ -329,14 +343,18 @@ EOF
         my $tok = $p->{name};
         $tok =~ tr/a-z/A-Z/;
 
-        if ($t{type}->{kind} eq 's') {
-            $call = "lua_pushstring(L, $call)";
+        if ($t{type}->{kind} eq 'b') {
+            $call = "lua_pushboolean(L, $call)";
         }
 
         if ($t{type}->{kind} eq 'i') {
             $call = "lua_pushinteger(L, $call)";
         }
 
+        if ($t{type}->{kind} eq 's') {
+            $call = "lua_pushstring(L, $call)";
+        }
+
         if (defined $t{type}->{push}) {
             $call =~ s/\$\$/$t{type}->{push}/;
         }
@@ -376,8 +394,9 @@ EOF
 
         put_line($p, 0);
         print  "      case LTK_$tok: \n";
-        print "        m_strreplace(&$var, $check)" if ($t{type}->{kind} eq 's');
+        print "        $var = $check"               if ($t{type}->{kind} eq 'b');
         print "        $var = $check"               if ($t{type}->{kind} eq 'i');
+        print "        m_strreplace(&$var, $check)" if ($t{type}->{kind} eq 's');
 
         print  ";\n        return 1;\n";
     }
@@ -469,7 +488,6 @@ sub do_c($) {
             print;
         }
     }
-    print "/* vi"."m:set ft=c: */\n";
 
     stream_close(\%src);
 }