I've been offline for a few days and haven't caught up on email yet
(nor, most likely, will I ever), so I hope no one else has already
done this, but....

Attached is a file, msv.tar.gz which contains a simple script and .pm
file (*) for editing the string vtable.  It asks you for a bunch of
typedef/function_name pairs and then rewrites string.h, string.c, and
all of the encoding code files (strnative.c, strforeign.c, strutf*.c),
incorporating what you gave it.

PROS:
- you can give it as many typedef/function pairs as you want at once

- it adds your new typedef only if it isn't already there

- before adding your new function, it checks the vtable in string.h to
make sure that there isn't already an identical function, or one that
differs only by return type

- it understands that (e.g.) 'two_strings_to_iv_t' should expand to:
    IV (*two_strings_to_iv_t)(STRING *, STRING *);
(Actually, it understands numbers up to nine, but I hope you never go
that high.)

- it sets up stubs in string.c and all the encoding files, and adds
the appropriate entry to each file's vtable

- if your encoding files don't exist, it will generate them for you

CONS:

- your typedefs must match:  <type>+_to_<type>_t  where <type> is
composed only of word characters.  The arguments (the part before the
'to') must be separated by underscores.  So, these are legal:
  string_to_string_t
  iv_nv_to_string_t
  three_strings_nv_to_nv_t  OR  three_string_nv_to_nv_t   (same thing)
these are not legal:
  &(*&_t              (not a valid symbol)
  string_string_t     (doesn't specify a return type)
worst of all, this doesn't work:
  substr_t            (the patch to change it to an accepted version
                         is below) 

- any comments that you put in the vtable or the typedef section will
be stomped the next time you run the program.  If people actually use
this and find it useful, I will fix this misbehaviour.

NOTES:

- the stubs that are put into string.c and the encoding tables don't
have names for their parameters, since the program has no way of
knowing what meaningful names would be.

- the bodies of the stubs consist almost solely of 'FINISH ME', not in
comments (so that it won't compile).  There is probably more that I
could make it do for you here, but I wanted to get a working version
out. 

- the program uses various parts, including comments, of the string.h
file as delimiters, so if you start making regular manual changes it
might stop working or misbehave.




A sample session (actually a record of the last session I ran before
writing this email) is below.  I've put my inputs on the line
following the prompt and indented them to make them stand out.

------------CUT HERE--------------
Enter a function name (e.g. chopn), or <RET> to quit: 
    reverse
Enter a typedef name (e.g. iv_to_iv_t) to associate with reverse: 
    string_to_string_t
Enter a function name (e.g. chopn), or <RET> to quit: 

About to rewrite string.h...
Successfully wrote revised text to 'string.h.working'

About to rewrite string.c...
Successfully wrote revised text to 'string.c.working'

About to rewrite encoding files...
Successfully wrote revised text to 'strforeign.c.working'
Successfully wrote revised text to 'strnative.c.working'
Successfully wrote revised text to 'strutf16.c.working'
Successfully wrote revised text to 'strutf32.c.working'
Successfully wrote revised text to 'strutf8.c.working'

About to move working copies to primary versions...
Successfully moved working file 'strforeign.c.working' to 'strforeign.c'
Successfully moved working file 'strutf32.c.working' to 'strutf32.c'
Successfully moved working file 'strutf16.c.working' to 'strutf16.c'
Successfully moved working file 'strutf8.c.working' to 'strutf8.c'
Successfully moved working file 'strnative.c.working' to 'strnative.c'
Successfully moved working file 'string.h.working' to 'string.h'
Successfully moved working file 'string.c.working' to 'string.c'

./msv.pl completed successfully.

$ [edit string.c and strnative.c]
$ [compile and test]
$ [celebrate by submitting]


Here is the patch to change substr_t into something this program can  
accept:

[dstorrs@localhost parrot]$ cvsp -q diff -c string.h
--------------  DIFF OUTPUT STARTS ON NEXT LINE -------------------
Index: string.h
===================================================================
RCS file: /home/perlcvs/parrot/string.h,v
retrieving revision 1.5
diff -u -d -c -r1.5 string.h
cvs server: conflicting specifications of output style
*** string.h    2001/09/14 14:08:00     1.5
--- string.h    2001/09/16 03:54:37
***************
*** 25,31 ****
  typedef IV (*string_to_iv_t)(STRING *);
  typedef STRING* (*string_iv_to_string_t)(STRING *, IV);
  typedef STRING* (*two_strings_iv_to_string_t)(STRING *, STRING *, IV);
! typedef STRING* (*substr_t)(STRING*, IV, IV, STRING*);
  typedef IV (*iv_to_iv_t)(IV);
  
  struct string_vtable {
--- 25,31 ----
  typedef IV (*string_to_iv_t)(STRING *);
  typedef STRING* (*string_iv_to_string_t)(STRING *, IV);
  typedef STRING* (*two_strings_iv_to_string_t)(STRING *, STRING *, IV);
! typedef STRING* (*string_two_ivs_string_to_string_t)(STRING*, IV, IV, STRING*);
  typedef IV (*iv_to_iv_t)(IV);
  
  struct string_vtable {
***************
*** 34,40 ****
      iv_to_iv_t max_bytes;               /* I have n characters - how many bytes 
should I allocate? */
      two_strings_iv_to_string_t concat;  /* Append string b to the end of string a */
      string_iv_to_string_t chopn;        /* Remove n characters from the end of a 
string */
!     substr_t substr;                    /* Substring operation */
  };
  
  struct parrot_string {
--- 34,40 ----
      iv_to_iv_t max_bytes;               /* I have n characters - how many bytes 
should I allocate? */
      two_strings_iv_to_string_t concat;  /* Append string b to the end of string a */
      string_iv_to_string_t chopn;        /* Remove n characters from the end of a 
string */
!     string_two_ivs_string_to_string_t substr;                      /* Substring 
operation */
  };
  
  struct parrot_string {
--------------  DIFF ENDS ON PREVIOUS LINE --------------------------

I hope people find this useful.

Dave

* Note:  I separated the script file from the .pm file because I had this
thought that maybe we could generate a whole raft of Parrot::Make::*  
modules that would roll various part of the system for us, and that I 
would establish the namespace.  If people think this is a good idea, I
could try to break some of these functions out into a proto Parrot/Make.pm
module.

msv.tar.gz

Reply via email to