NO

Author Topic: Fix brackets, punctuation. Python-like pseudocode-to-C compiler  (Read 7015 times)

hellork

  • Guest
Fix brackets, punctuation. Python-like pseudocode-to-C compiler
« on: November 21, 2010, 02:39:33 am »
Greetings   :)

I am making a free tool and releasing it under the GPL. I hope you like it.

http://freshmeat.net/projects/anchor

This adds missing brackets and semicolons (and remove extra ones.) It does not know anything about languages or tokens, so it ought to work with other languages like C++, D, PHP, Java, too, without much trouble.
http://freshmeat.net/projects/anchor

Code must be indented for the bracket adder to work. If lots of brackets and punctuation are left out it seems like a pseudo code compiler.

It is sort of its own language, almost. For example, this will compile with it:
Code: [Select]
#include <stdio.h>
/* Print command line arguments and exit. */
int main  int c, char **v
    while  c--
        printf  "Argument %i is \"%s\"\n",c,v[c]
    return 0

It fixes markup to standard C (or other "curly bracket" languages). But it does not (yet) do any fancy code conversion. It only looks at indentation levels and adds missing "{}" and ";" characters. It has one macro to put parenthesis around things: The two spaces after main and after printf expands to (parenthesis until the end of the line). I'm not sure of any other way to tell it where parenthesis go in a language-independent way. I suppose other clever macros could be added.

This is the output so you can see how the parenthesis expand. Comments and string literals are ignored.
Code: [Select]
#include <stdio.h>

/* Print command line arguments and exit. */
int main (int c, char **v){
    while (c--){
        printf ("Argument %i is \"%s\"\n",c,v[c]);
    } return 0;
}

Because of the "two spaces" macro, the code has to not have extra spaces in it. Also whitespace at the end of a line makes semicolons not get repaired on that line. On the other hand, this is good, because you can prevent an extra semicolon from being automatically removed by putting a space after it. There are a couple places in C code where an extra semicolon is needed, such as after anonymous structs.

Anyway, I hope you find this useful and fun. You can help me test the windows version. I have not tested it much on large projects and it will truncate comment blocks over 8K in length (and emit a warning).
« Last Edit: November 21, 2010, 02:41:58 am by hellork »

hellork

  • Guest
Re: Fix brackets, punctuation. Python-like pseudocode-to-C compiler
« Reply #1 on: November 21, 2010, 09:46:14 am »
Tested Anchor on Windows with Pelles C today.

Anchor was originally written on Linux. The exe does work from the command line with Pelles, but it could be easier. Maybe this could be made into an extension of the IDE. I will look a the SDK.

My standard C tests work, but the non-standard #!/usr... line needs to be stripped off using tail from win bash.

It fixed up all the missing brackets and stuff in "t.a.c", stored it in "t.c" and after setting PATH it compiled.
Code: [Select]
anchor tests/t.a.c|tail -n +2 > t.c

cc t.c

t.exe
Yay, works, compiling programs with no warnings that would otherwise be rife with errors.  ;D

The windows-style program was a bit more difficult to compile with Pelles C from the command line but this could be scripted fairly easily.
Code: [Select]
anchor tests/windows.a.c > w.c
cc /Ze w.c  -subsystem:windows -machi ne:x86  kernel32.lib user32.lib gdi32.lib comctl32.lib comdlg32.lib advapi32.lib  delayimp.lib -subsystem:windows -machine:x86  kernel32.lib user32.lib gdi32.lib  comctl32.lib comdlg32.lib advapi32.lib delayimp.lib

w.exe
« Last Edit: November 21, 2010, 09:53:59 am by hellork »

Offline Vortex

  • Member
  • *
  • Posts: 529
    • http://www.vortex.masmcode.com
Re: Fix brackets, punctuation. Python-like pseudocode-to-C compiler
« Reply #2 on: November 21, 2010, 10:23:36 am »
Hi hellork,

Thanks for the new tool anchor.exe

Processing the source code below :

Code: [Select]
#include <stdio.h>

int main
printf "this is a test"
return 0

the tool outputs :

Code: [Select]
#include <stdio.h>

int main{
 printf "this is a test";
 return 0;
}

The printf functions seems to miss the two parentheses symbols here. Could you have a look at it? Tested with anchor.exe extracted from anchor-r29.zip
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 529
    • http://www.vortex.masmcode.com
Re: Fix brackets, punctuation. Python-like pseudocode-to-C compiler
« Reply #3 on: November 21, 2010, 10:28:15 am »
A small note : In the command line prompt, running anchor.exe without any parameters does not display a copyright or a help message. No any problem with the usage of the tool but it would be nice to see a message if the tool is invoked without a command line parameter.
Code it... That's all...

hellork

  • Guest
Re: Fix brackets, punctuation. Python-like pseudocode-to-C compiler
« Reply #4 on: November 21, 2010, 11:20:22 am »
The parenthesis are done with a macro, that is, just a trick for convenience. I put that in the wiki. Puting two spaces instead of one between printf and the opening quotes causes it to insert a left paren and a matching one at the end of the line.

Any intervening parenthesis will have to remain. The problem is it is near impossible to determine where parenthesis go once they're gone, so we have to leave some in. I tried to eliminate them though  :'(

The only way to really fix parenthesis is to write a full-blown parser or preprocessor for each language, which has been done before, in different, incompatible ways... I would do it though if I had corporate sponsorship. :D

There is some help with the -h option. Thanks for the suggestion. Because of your input the next version will show better help messages.
« Last Edit: November 21, 2010, 12:35:57 pm by hellork »

Offline Vortex

  • Member
  • *
  • Posts: 529
    • http://www.vortex.masmcode.com
Re: Fix brackets, punctuation. Python-like pseudocode-to-C compiler
« Reply #5 on: November 21, 2010, 01:22:36 pm »
The parenthesis are done with a macro, that is, just a trick for convenience. I put that in the wiki. Puting two spaces instead of one between printf and the opening quotes causes it to insert a left paren and a matching one at the end of the line.

Thanks for the help. Adding the second space character gives the correct result.

Also, thanks for taking in account my suggestion.
Code it... That's all...