Hi, I've created a C to Delphi converter.

Hi, I've created a C to Delphi converter.

Because it's been so useful to me that I've decided to share it.

It converts C code as-you-type, and it keeps track of where the original and converted code is. For fun, you can even run the result (using DWS).

Disclaimer: this is not intended to be a school example on how to build a parser or anything. But it will convert almost anything that you throw at it.

You can download an executable from GitHub (under the releases-tab).
https://github.com/WouterVanNifterick/C-To-Delphi
https://github.com/WouterVanNifterick/C-To-Delphi

Comments

  1. Are there any restrictions in the input or will this accept any valid C code. It looks like it uses regex instead of the traditional method of parsing.

    ReplyDelete
  2. If it handles Duff's device I'll be migthy impressed :)

    ReplyDelete
  3. David Heffernan it uses a mix of regex and char-by-char parsing. It doesn't need to be valid c code. I don't think that a more common lexer/parser would be the right tool for this job. I'd have to deal with all dialects, and fully interpret everything, where i now get away with treating parts of the code as strings without fully understanding what it's purpose is. C and pascal are remarkably similar is what I found when working on this.

    Where a normal parser chokes at the first error that it encounters, this converter probably doesn't care much, and proceeds with parts that it does recognize.

    Just paste some c code and see what it does.

    You'll find that it converts 95% of c code to usable Delphi code, which is enough for my purposes. I've converted some complex algorithms with it, and things like a shapefile library that I've always had as a separate dll. Right now it's just Delphi code, and it's easier to integrate and deploy. I've even used this to convert GLSL shaders to a full Delphi variant. That's only loosely based on C :) (github.com - WouterVanNifterick/delphi-shader)

    ReplyDelete
  4. This looks interesting, how good is it at handling dynamic C arrays created via malloc, 1 and 2D?

    ReplyDelete
  5. Herbert Sauro I've converted many one and two dimensional arrays with it.

    Just paste your c code or drag a .c file to the app, and you'll immediately see the result. There's an executable under 'releases' on github. No need to "install" it.

    It counts the number of items in arrays, so even when an array has no size defined, it'll generate something that's valid in Delphi.


    If you have code that's not properly converted, let me know what the problem is, so i can fix it.

    ReplyDelete
  6. Attila Kovacs you can't install tbceditor? Are you using an old Delphi version maybe?

    ReplyDelete
  7. Attila Kovacs mm.. I'm using Berlin 10.1 on this machine, and the latest TBCEditor works fine here. Maybe you have an old version of the same library somewhere in your library search path?

    ReplyDelete
  8. It's very crude isn't it. Doesn't understand operator precedence for instance. Doesn't understand C return. Could be useful as a first pass, leading to subsequent human work. Neat though.

    ReplyDelete
  9. David Heffernan what do you mean with c return? It converts return XY; to exit(XY); unless it's the last line in a function. Then it turns into Result := XY;

    If you found a situation where that somehow doesn't work, let me know.

    Converting operator precedence is not a trivial task, and I doubt that it's reasonably possible in every case. It means that certain expressions or subexpressions need to be split up into multiple statements or be shuffled around. Even if I'd get the precedence right, the evaluation order would probably not be the same as in C.

    So far i never ran into real trouble because of differences in evaluation order and I've converted many C libraries to native Delphi using this conversion code.

    Relying too much on specific operator precedence is bad practice imho. It might be deterministic and correct, but it's easy to create bugs that are easily overseen, and you don't know if the next person who touches your code will correctly interpret the code like the compiler would. It's better to add some extra braces or split into multiple statements. Makes code easier to port too. ;-)

    ReplyDelete
  10. I saw some code that converted return to return. It was something like return (...), that is the subject of the return statement was surrounded by braces.

    It is certainly possible to handle operator precedence, but it won't be possible with your simple pattern matching approach.

    Your tool would be useful for a first pass of a literal translation of some simple C libraries. It might have saved me a little time when I converted some CSparse routines to Pascal. What I did there was put the C code in my Delphi editor, function by function, and fix it there. Lots of the routine stuff (variable declaration, braces etc.) could have been done by your pattern matching tool. The more complex would still be done manually.

    ReplyDelete
  11. Won't be using it as I'm a C++ programmer (I would use the opposite) but nice idea, I'm sure this will be a time saver.

    ReplyDelete
  12. I downloaded the binary...very cool. Tried to clone and build the source, but ran into many issues, the least of which is that it appears to be using a version of the BCEditor code which is not the head of https://github.com/bonecode/BCEditor.
    github.com - bonecode/BCEditor

    ReplyDelete
  13. Larry Hengen Yeah, it used the original branch of bceditor. I’ve tried to update it to the latest version, but there were many breaking changes and it became extremely unstable. It caused so many fatal and unexplainable crashes that i’ve switched to SynEdit. :-(

    I should probably push to the github repo too :).. the latest version is better at converting and it does conversions in a background thread while you type.

    ReplyDelete

Post a Comment