I have written a first cut at an app to convert Objective-C syntax into
Perl, as desired by CamelBones.  I'm sure it's not complete and I suspect
that I may have gotten a few things wrong.  So, I'm handing out the model
code and a command-line test harness for review; please let me know what
needs fixing, and how...

If anyone _really_ wants to have the complete Cocoa app, please send me a
note.  I'll warn you, however, that (a) I'm pretty raw at Cocoa and (b) I
don't yet know how to package up Cocoa apps for distribution.

-r

=======
oc2p.pm
=======
#!/usr/bin/env perl
#
# oc2p - simple-minded converter from Objective-C to Perl.
#
# Written by Rich Morin <[EMAIL PROTECTED]>, CFCL, 2002.

sub converter {

# Input (invocation)                   Output
# ----------------------------------   ---------------------------------
#            [rec s1];                           $rec->s1();
#      var = [rec s1];                    $var = $rec->s1();
# vt * var = [rec s1];                 $vt_var = $rec->s1();
#
#            [rec s1:a1];                        $rec->s1($a1);
#      var = [rec s1:a1];                 $var = $rec->s1($a1);
# vt * var = [rec s1:a1];              $vt_var = $rec->s1($a1);
#
#            [rec s1:a1 s2:a2];                  $rec->s1_s2($a1, $a2);
#      var = [rec s1:a1 s2:a2];           $var = $rec->s1_s2($a1, $a2);
# vt * var = [rec s1:a1 s2:a2];        $vt_var = $rec->s1_s2($a1, $a2);
#
#
# Input (declaration)                  Output
# ----------------------------------   ---------------------------------
#
# - (void)s1                                     $rec->s1();
# - (vt *)s1                           $vt_var = $rec->s1();
#
# - (void)s1:(t1 *)a1                            $rec->s1($t1_a1);
# - (vt *)s1:(t1 *)a1                  $vt_var = $rec->s1($t1_a1);
#
# - (void)s1:(t1 *)a1 s2:(t2 *)a2                $rec->s1_s2($t1_a1, $t2_a2);
# - (vt *)s1:(t1 *)a1 s2:(t2 *)a2      $vt_var = $rec->s1_s2($t1_a1, $t2_a2);

   my ($str) = @_;

   my @out;
   $str =~ s|^\s*(.+?)\s*;?\s*$|$1|;

   if ($str =~ m|^-\s*\(([^\)]+)\)\s*(.+)$|) {  # declaration
     $str1 = $1;
     $str2 = $2;

     if ($str1 =~ m|\s*void\s*$|) {
       $out = '$rec->';
     } else {
       $str1 =~ s|\s.*||;
       $out  =  "\$${str1}_var = \$rec->";
     }

     $str2 =~ s|\s+\*||g;
     @term = split(' ', $str2);
     $ndx  = 0;
     for $term (@term) {
       ++$ndx;
       if ($term =~ m|^([^:]+)$|) {
         $out .= $1;
       } elsif ($term =~ m|^([^:]+):\(([^\)]+)\)(.+)$|) {
         $out .= '_' if ($ndx != 1);
         $out .= $1;
         push(@out, "\$$2_$3");
       } else {
         $out = sprintf("Warning: term (%s) not recognized", $term);
         return($out);
       }
     }
     $out .= '(' . join(', ', @out) . ');';
     $out =~ s|\@"([^"]*")|"$1"|g;
     return($out);
   }

   if ($str =~ m|^([^\]]*)\[\s*(.+)\s*\]$|) {     # invocation
     $str1 = $1;
     $str2 = $2;

     if ($str1 =~ m|^([^=]*?)\s*=\s*$|) {
       $out =  $1;
       $out =~ s|\s*\*\s*|_|g;
       $out =  "\$$out = ";
     } elsif ($str1 eq '') {
       $out = '';
     } else {
       $out = sprintf("Warning: term (%s) not recognized", $str1);
       return($out);
     }

     @term =  split(' ', $str2);
     $out  .= '$' . shift(@term);
     $ndx  =  0;
     for $term (@term) {
       $out .= (++$ndx == 1) ? '->' : '_';
       if ($term =~ m|^([^:]+)$|) {
         $out .= $1;
       } elsif ($term =~ m|^([^:]+):(.+)$|) {
         $out .= $1;
         push(@out, "\$$2");
       } else {
         $out = sprintf("Warning: term \"%s\" not recognized\n\n", $term);
         return($out);
       }
     }
     $out .= '(' . join(', ', @out) . ');';
     $out =~ s|\@"([^"]*")|"$1"|g;
     return($out);

   }

   $out = "Warning: input syntax not recognized";
   return($out);
}


1;


=========
test_oc2p
=========
#!/usr/bin/env perl
#
# test_oc2p - test harness for oc2p logic.

require 'oc2p.pm';

{
   while (1) {
     print 'ObjC: ';

     $str = <>;
     chomp($str);
     printf("Perl: %s\n\n", converter($str));
   }
}

-- 
email: [EMAIL PROTECTED]; phone: +1 650-873-7841
http://www.cfcl.com/rdm    - my home page, resume, etc.
http://www.cfcl.com/Meta   - The FreeBSD Browser, Meta Project, etc.
http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series
http://www.ptf.com/tdc     - Prime Time Freeware's Darwin Collection

Reply via email to