Hacker News new | ask | show | jobs
by theunamedguy 4071 days ago
Great tool, but too bad it can't handle code very well.

An example (converting C++ to C):

    Input:
    void FileStream::write(char* , int);
    void VirtualMachine::cycle(int);

    Example:
    void FileStream_write(char* , int);

    Output:
    void FileStream_write(char* , int);
    void VirtualMachine_cycle(int* , );
Also, is it open-source?
1 comments

TXR script:

  @(repeat)
  @type @class::@ident(@params);
  @  (output)
  @type @{class}_@ident(@params);
  @  (end)
  @(end)
Do this kind of thing regularly over C code, when it's too much for Vim macros.

I do it out of Vim. That is: first, select a range of text, then pipe it out:

  !txr some_transform_script.txr -
Done. For example, when adding functions to TXR Lisp's library, I start with a declaration like:

  static val foo(val x, val y);
I pipe this through a script which will produce this:

  reg_fun(intern(lit("foo"), user_package), func_n2(foo));
("Intern a symbol called "foo" in the user_package, and register a two-argument function object with this symbol, hosted from the C function foo.")

It's a little complicated:

  @(deffilter sym ("_" "-"))
  @(collect)
  @  (cases)
  @/(static )?/val @fun(void);@(bind arg nil)
  @  (or)
  @/(static )?/val @fun(@(coll)@{type /[^ ,]+/} @{arg /[^ ,)]+/}@(until))@(end));
  @  (end)
  @(output)
    reg_fun(intern(lit("@{fun :filter sym}"), user_package), func_n@(length arg)(@fun));
  @(end)
  @(end)
I filter underscores to dashes, because I want a C function like foo_bar to look like foo-bar in the Lisp dialect. The (void) argument list is handled as a special case.

I need to parse the arguments because the output part needs to know how many there are. Note how "func_n2" is generated, where the 2 comes from the argument count.